Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/input/autocomplete-select-ref/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const propTypes = {
referenceName: PropTypes.string.isRequired,
keyName: PropTypes.string,
labelName: PropTypes.string,
multiple: PropTypes.bool
multiple: PropTypes.bool,
predicate: PropTypes.func
}

const defaultProps = {
keyName: 'key',
labelName: 'label',
multiple: false
multiple: false,
predicate: (x) => true
}

const AutocompleteReference = createReactClass({
Expand All @@ -33,7 +35,9 @@ const AutocompleteReference = createReactClass({
const { referenceName, labelName, keyName } = this.props;
//We use this normalizedList in order not to normalize the list for every query
if (!this.normalizedList && this.state.reference[referenceName]) {
this.normalizedList = this.state.reference[referenceName].map(x => ({ [keyName]: x[keyName], [labelName]: toNormalizedLowerString(x[labelName]) }));
this.normalizedList = this.state.reference[referenceName]
.filter(predicate)
.map(x => ({ [keyName]: x[keyName], [labelName]: toNormalizedLowerString(x[labelName]) }));
}

let data = (this.normalizedList || []).filter(x => x[labelName].indexOf(normalizedQuery) !== -1);
Expand Down
25 changes: 17 additions & 8 deletions src/components/input/radio/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ReactDOM from 'react-dom';

import Radio from '../';

const { renderIntoDocument, scryRenderedDOMComponentsWithTag, findRenderedDOMComponentWithTag, Simulate } = TestUtils;
const { renderIntoDocument, Simulate } = TestUtils;

describe('Input Radio Component', () => {
describe('Rendering', () => {
Expand All @@ -17,9 +17,6 @@ describe('Input Radio Component', () => {
it.skip('should have its default props', () => { //FIXME problem outside the component, inside render props are good
expect(renderedRadio.props.value).toBe(false);
});
it('should have its default state', () => {
expect(renderedRadio.state.isChecked).toBe(false);
});
describe('The function getValue', () => {
let renderedRadio;
beforeEach(() => {
Expand All @@ -35,12 +32,24 @@ describe('Input Radio Component', () => {
});
describe('When the radio is selected', () => {
let renderedRadio;
let radio;
const onChangeSpy = jest.fn();
beforeEach(() => {
renderedRadio = renderIntoDocument(<Radio label='My value' />);
Simulate.change(renderedRadio.refs.inputRadio);
renderedRadio = renderIntoDocument(
<Radio
label='My value'
onChange={onChangeSpy}
value={true}
/>);
});
it('should change the state', () => {
expect(renderedRadio.state.isChecked).toBe(true);
it('should call the _onChange prop', () => {
radio = ReactDOM.findDOMNode(renderedRadio.refs.inputRadio);
Simulate.change(radio, { target: { checked: true } });
expect(onChangeSpy).toHaveBeenCalledTimes(1);
});

it('the returned value should be equals to this.props.value', () => {
expect(renderedRadio.getValue()).toBe(true);
});
})
});
Expand Down
24 changes: 7 additions & 17 deletions src/components/input/radio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ class Radio extends Component {
onChange: PropTypes.func
};

state = {
isChecked: isUndefined(this.props.value) ? false : this.props.value
};

componentWillReceiveProps(newProps) {
this.setState({ isChecked: newProps.value });
}

componentDidMount() {
this.updateCheckedClass();
}
Expand All @@ -40,7 +32,7 @@ class Radio extends Component {

updateCheckedClass() {
const { inputMdl } = this.refs;
const { isChecked } = this.state;
const isChecked = this.getValue();
if (inputMdl) {
const { classList } = inputMdl;
if (isChecked === true) classList.add('is-checked');
Expand All @@ -53,27 +45,25 @@ class Radio extends Component {
* @param {event} event
*/
_onChange = () => {
this.setState({ isChecked: !this.state.isChecked }, () => {
if (this.props.onChange) {
this.props.onChange(this.state.isChecked);
}
});
if (this.props.onChange) {
this.props.onChange();
}
}

/**
* Get the value from the input in the DOM.
* @returns The DOM node value.
*/
getValue() {
return this.state.isChecked;
return isUndefined(this.props.value) ? false : this.props.value
}

/**
* Render the Checkbox HTML.
* @return {VirtualDOM} - The virtual DOM of the checkbox.
*/
render() {
const { isChecked } = this.state;
const isChecked = this.getValue();
const { label } = this.props;
const validInputProps = filterProps(this.props);

Expand All @@ -92,4 +82,4 @@ class Radio extends Component {

Radio.displayName = 'InputRadio';

export default Radio;
export default Radio;