Skip to content

Commit

Permalink
Update required when Select gets new props
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hite committed Mar 15, 2016
1 parent 9be7919 commit 46ca406
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ const Select = React.createClass({
}
},

componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value && nextProps.required) {
this.setState({
required: this.handleRequired(nextProps.value, nextProps.multi),
});
}
},

componentWillUpdate (nextProps, nextState) {
if (nextState.isOpen !== this.state.isOpen) {
const handler = nextState.isOpen ? nextProps.onOpen : nextProps.onClose;
Expand Down
12 changes: 12 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,18 @@ describe('Select', () => {
expect(instance.state.required, 'to be true');
});

it('input should not have required attribute after updating the component with a value', () => {
wrapper = createControlWithWrapper({
options: defaultOptions,
value: '',
required: true
});

expect(instance.state.required, 'to be true');
wrapper.setPropsForChild({ value: 'one' });
expect(instance.state.required, 'to be false');
});

});

describe('required with multi=true', () => {
Expand Down

0 comments on commit 46ca406

Please sign in to comment.