Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
#1 Implement the MDC Select.
Browse files Browse the repository at this point in the history
Also add self compiled babel 7.0.0 beta.
  • Loading branch information
enbock committed Oct 16, 2017
1 parent 6053fc2 commit 239d42b
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# production
/build
/packages

# misc
.DS_Store
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$",
"packages\\babel"
],
"moduleNameMapper": {
"^react-native$": "react-native-web"
Expand Down
58 changes: 31 additions & 27 deletions public/Template/Settings.html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
<div class="mdc-list-item__text">Sprache</div>
</div>
<div class="settings-content mdc-list-item">
<div class="mdc-select language" role="listbox" tabIndex="0">
<span class="mdc-select__selected-text">Deutsch</span>
<div class="mdc-simple-menu mdc-select__menu">
<ul class="mdc-list mdc-simple-menu__items">
<li class="mdc-list-item" role="option" id="german" tabIndex="0">
Deutsch
</li>
<li class="mdc-list-item" role="option" id="english" tabIndex="0">
Englisch
</li>
</ul>
<MDC.Select onChange={this.onSelectionChange.bind(this)} name="language">
<div class="mdc-select" role="listbox" tabIndex="0">
<span class="mdc-select__selected-text">Deutsch</span>
<div class="mdc-simple-menu mdc-select__menu">
<ul class="mdc-list mdc-simple-menu__items">
<li class="mdc-list-item" role="option" id="german" tabIndex="0">
Deutsch
</li>
<li class="mdc-list-item" role="option" id="english" tabIndex="0">
Englisch
</li>
</ul>
</div>
</div>
</div>
</MDC.Select>
</div>
</div>
<div class="settings-block">
Expand All @@ -32,22 +34,24 @@
Farbschema
</div>
<div class="settings-content mdc-list-item">
<div class="mdc-select color" role="listbox" tabIndex="0">
<span class="mdc-select__selected-text">Standard</span>
<div class="mdc-simple-menu mdc-select__menu">
<ul class="mdc-list mdc-simple-menu__items">
<li class="mdc-list-item" role="option" id="german" tabIndex="0">
Standard
</li>
<li class="mdc-list-item" role="option" id="english" tabIndex="0">
Codefrog
</li>
<li class="mdc-list-item" role="option" id="english" tabIndex="0">
Dunkel
</li>
</ul>
<MDC.Select onChange={this.onSelectionChange.bind(this)} name="color">
<div class="mdc-select" role="listbox" tabIndex="0">
<span class="mdc-select__selected-text">Standard</span>
<div class="mdc-simple-menu mdc-select__menu">
<ul class="mdc-list mdc-simple-menu__items">
<li class="mdc-list-item" role="option" id="default" tabIndex="0">
Standard
</li>
<li class="mdc-list-item" role="option" id="codefrog" tabIndex="0">
Codefrog
</li>
<li class="mdc-list-item" role="option" id="dark" tabIndex="0">
Dunkel
</li>
</ul>
</div>
</div>
</div>
</MDC.Select>
</div>
</div>
</div>
Expand Down
26 changes: 1 addition & 25 deletions public/lib/babel.min.js

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions src/Settings/Settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import Component from '../Shared/LiveJSX';
import {MDCSelect} from '@material/select';

/**
* Option screen primary component.
Expand All @@ -15,17 +14,14 @@ class Settings extends Component {
return '/Template/Settings.html.tpl';
}

onTemplateMounted(domNode) {
const select = new MDCSelect(domNode.querySelector('.mdc-select.language'));
select.listen('MDCSelect:change', () => {
console.log(`Selected "${select.selectedOptions[0].textContent}" at index ${select.selectedIndex} ` +
`with value "${select.value}"`);
});
const select2 = new MDCSelect(domNode.querySelector('.mdc-select.color'));
select2.listen('MDCSelect:change', () => {
console.log(`Selected "${select2.selectedOptions[0].textContent}" at index ${select2.selectedIndex} ` +
`with value "${select2.value}"`);
});
/**
* On Selection change.
*
* @param {Object} event
*
* @see ../Shared/Select.onChange
*/
onSelectionChange(event) {
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/Settings/Settings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {mockAxiosAction} from 'axios';
import React from 'react';
import {shallow} from 'enzyme';
import Settings from './Settings';

jest.mock('react-dom');


/**
* Test Settings Container.
*/
describe('Settings Page', function testSettings() {

/**
* Test change language.
*/
it('Change language', function () {
let bound = null;
const promise = {
then: function onThen(callback) {
bound = callback;
return promise;
},
catch: function onCatch() {
return promise;
}
};

let templateLoaded = false;

mockAxiosAction(
'get',
function onRequest(url) {
expect(url).toBe('/Template/Settings.html.tpl');
templateLoaded = true;

return promise;
}
);

const wrapper = shallow(<Settings/>);
let instance = wrapper.instance();
bound({data: 'TEMPLATE'});
wrapper.setProps({});

instance.onSelectionChange(
{
name: 'language',
selectedOptions: ['german'],
selectedIndex: 1,
value: 'german'
}
);

wrapper.unmount();
});
});
70 changes: 70 additions & 0 deletions src/Shared/MDC/Select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import {MDCSelect} from '@material/select';

/**
* MDC Select implementation.
*/
class Select extends React.Component {

/**
* Supported property types.
*/
static get propTypes() {
return {
onChange: PropTypes.func.isRequired,
name: PropTypes.string.isRequired
};
}

/**
* Apply change information.
*/
onChange() {
let selectedOptions = [], index;

// Convert options to array
const options = this.select.selectedOptions;
for (index = 0; index < options.length; index++) {
selectedOptions[index] = options[index].id;
}

const event = {
name: this.props.name,
selectedOptions: selectedOptions,
selectedIndex: this.select.selectedIndex,
value: this.select.value
};
this.props.onChange(event);
}

/**
* Connect MDC after mount.
*/
componentDidMount() {
this.domNode = ReactDOM.findDOMNode(this);
this.boundChnage = this.onChange.bind(this);
this.select = new MDCSelect(this.domNode);
this.select.listen('MDCSelect:change', this.boundChnage);
}

/**
* Disconnect from MDC.
*/
componentWillUnmount() {
this.select.unlisten('MDCSelect:change', this.boundChnage);
this.select.destroy();
this.select = undefined;
}

/**
* Generate the output.
*/
render() {
// No special adding into dom tree, passing children as own level.
return this.props.children;
}
}

export default Select;
78 changes: 78 additions & 0 deletions src/Shared/MDC/Select.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import {shallow} from 'enzyme';
import Select from './Select';
import ReactDOM from 'react-dom';
import * as MDC from '@material/select';


jest.mock('@material/select', () => {
const mockInstance = {
destroy: jest.fn(),
listen: jest.fn(),
unlisten: jest.fn(),
selectedOptions: ["option1"],
selectedIndex: 1,
value: "option"
};
const mock = jest.fn().mockImplementation(
function onCreate() {
return mockInstance;
}
);
return {
mock: mockInstance,
MDCSelect: mock,
};
});

jest.mock('react-dom', () => {
const mock = {
findDOMNode: jest.fn()
};
return {
findDOMNode: mock.findDOMNode,
getMock: function () {
return mock;
}
};
});


/**
* Test select interaction.
*/
describe('MDC: Select', function testMDCDrawer() {
it("Emit event on change", function() {
let wasChanged = false;

const domNode = {
addEventListener: jest.fn()
};

const domMock = ReactDOM.getMock().findDOMNode;
domMock.mockClear();
domMock.mockReturnValue(domNode);

MDC.mock.destroy.mockClear();

/**
* Callback helper.
* @param {Object} event
*/
function checkEvent(event)
{
wasChanged = true;
}

let wrapper = shallow(
<Select name="selectMenu" onChange={checkEvent}/>
);
expect(MDC.mock.listen).toHaveBeenCalled();
MDC.mock.listen.mock.calls[0][1](); // emulate emit


wrapper.unmount();
expect(MDC.mock.destroy).toHaveBeenCalled();
expect(MDC.mock.unlisten).toHaveBeenCalled();
});
});
4 changes: 3 additions & 1 deletion src/Shared/MDC/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Drawer from './Drawer';
import Select from './Select';

export default {
Drawer: Drawer
Drawer: Drawer,
Select: Select
};

0 comments on commit 239d42b

Please sign in to comment.