Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear input value on receiving props with another value. #2183

Merged
merged 2 commits into from
Dec 13, 2017
Merged
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
4 changes: 4 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class Select extends React.Component {
// Used to be required but it's not any more
this.setState({ required: false });
}

if (this.state.inputValue && this.props.value !== nextProps.value) {
this.setState({ inputValue: this.handleInputValueChange('') });
}
}

componentDidUpdate (prevProps, prevState) {
Expand Down
28 changes: 28 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,34 @@ describe('Select', () => {
});
});

describe('clear the display value on receiving props', () => {
beforeEach(() => {
var wrapper = createControlWithWrapper({
options: defaultOptions,
value: 'one',
});
});
it('should clear display value if display is not empty', () => {
expect(instance.state.inputValue, 'to equal', '');
typeSearchText('and');
expect(instance.state.inputValue, 'to equal', 'and');
wrapper.setPropsForChild({
value: 'newValue',
});
expect(instance.state.inputValue, 'to equal', '');
});

it('should not clear display value if value in next props is equal to previous', () => {
expect(instance.state.inputValue, 'to equal', '');
typeSearchText('and');
expect(instance.state.inputValue, 'to equal', 'and');
wrapper.setPropsForChild({
value: 'one',
});
expect(instance.state.inputValue, 'to equal', 'and');
});
});

describe('clearable=true', () => {

beforeEach(() => {
Expand Down