From 14200d6268b2216e3a32e64ce80f84df4a0e058b Mon Sep 17 00:00:00 2001 From: Ugo Stephant Date: Thu, 5 Dec 2019 08:51:31 +0100 Subject: [PATCH] fix(select-field): update internal value when options change --- src/SelectField.js | 4 ++++ tests/SelectField.test.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/SelectField.js b/src/SelectField.js index b85adf058..5f7220ddb 100644 --- a/src/SelectField.js +++ b/src/SelectField.js @@ -93,6 +93,10 @@ class SelectField extends React.Component { this.onPropValueChange(); } + if (prevProps.options !== this.props.options) { + this.onPropValueChange(false); + } + if (prevProps.disabled !== this.props.disabled && this.props.disabled) { this.setState({ opened: false, diff --git a/tests/SelectField.test.js b/tests/SelectField.test.js index eb3517170..a3dca6693 100644 --- a/tests/SelectField.test.js +++ b/tests/SelectField.test.js @@ -215,4 +215,26 @@ describe('', () => { component.find(DropdownItem).at(0).find('a').simulate('click'); expect(component.instance().getValue()).toBe('Five'); }); + + it('should update internal value when options change', () => { + const component = mount( + opt.value} + /> + ); + + expect(component.instance().getValue()).not.toBeDefined(); + + component.setProps({ + options: [ + { title: 'Four', value: 4 }, + { title: 'Five', value: 5 }, + { title: 'Six', value: 6 }, + ], + }); + + expect(component.instance().getValue()).toBe(5); + }); });