Skip to content

Commit 89bb9c0

Browse files
committed
Merge pull request JedWatson#834 from RealHQ/master
Update required when Select gets new props
2 parents 9be7919 + 46ca406 commit 89bb9c0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Select.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ const Select = React.createClass({
138138
}
139139
},
140140

141+
componentWillReceiveProps(nextProps) {
142+
if (this.props.value !== nextProps.value && nextProps.required) {
143+
this.setState({
144+
required: this.handleRequired(nextProps.value, nextProps.multi),
145+
});
146+
}
147+
},
148+
141149
componentWillUpdate (nextProps, nextState) {
142150
if (nextState.isOpen !== this.state.isOpen) {
143151
const handler = nextState.isOpen ? nextProps.onOpen : nextProps.onClose;

test/Select-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,18 @@ describe('Select', () => {
28162816
expect(instance.state.required, 'to be true');
28172817
});
28182818

2819+
it('input should not have required attribute after updating the component with a value', () => {
2820+
wrapper = createControlWithWrapper({
2821+
options: defaultOptions,
2822+
value: '',
2823+
required: true
2824+
});
2825+
2826+
expect(instance.state.required, 'to be true');
2827+
wrapper.setPropsForChild({ value: 'one' });
2828+
expect(instance.state.required, 'to be false');
2829+
});
2830+
28192831
});
28202832

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

0 commit comments

Comments
 (0)