From 4909d579bbf3a7396ba3fa3d37b958ec4eb951af Mon Sep 17 00:00:00 2001 From: emyarod <afw5059@gmail.com> Date: Wed, 18 Nov 2020 09:35:49 -0600 Subject: [PATCH 1/4] test(NumberInput): add uncontrolled input defaultValue test --- .../components/NumberInput/NumberInput-test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/react/src/components/NumberInput/NumberInput-test.js b/packages/react/src/components/NumberInput/NumberInput-test.js index 527445e48fbf..a752f7918a42 100644 --- a/packages/react/src/components/NumberInput/NumberInput-test.js +++ b/packages/react/src/components/NumberInput/NumberInput-test.js @@ -155,6 +155,24 @@ describe('NumberInput', () => { ); const getNumberInput = (wrapper) => wrapper.find('input'); + it('should correctly set defaultValue on uncontrolled input', () => { + const wrapper = mount( + <NumberInput + min={-1} + max={100} + defaultValue={10} + id="test" + label="Number Input" + className="extra-class" + /> + ); + const numberInput = getNumberInput(wrapper); + expect(wrapper.find('NumberInput').instance().state.value).toEqual( + 10 + ); + expect(numberInput.prop('value')).toEqual(10); + }); + it('should set value as expected when value > min', () => { const wrapper = getWrapper(-1, 100, 0); const numberInput = getNumberInput(wrapper); From 3ed8d11010baf6856d3034c40654e002ba33bb08 Mon Sep 17 00:00:00 2001 From: emyarod <afw5059@gmail.com> Date: Wed, 18 Nov 2020 09:36:29 -0600 Subject: [PATCH 2/4] fix(NumberInput): avoid spreading defaultValue into input --- packages/react/src/components/NumberInput/NumberInput.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react/src/components/NumberInput/NumberInput.js b/packages/react/src/components/NumberInput/NumberInput.js index 89240af1eddd..0a35fdabedf4 100644 --- a/packages/react/src/components/NumberInput/NumberInput.js +++ b/packages/react/src/components/NumberInput/NumberInput.js @@ -300,6 +300,7 @@ class NumberInput extends Component { translateWithId: t, isMobile, size, + defaultValue, // eslint-disable-line ...other } = this.props; From 096c54759170c337558763edc835c743646074ca Mon Sep 17 00:00:00 2001 From: emyarod <afw5059@gmail.com> Date: Wed, 18 Nov 2020 09:37:20 -0600 Subject: [PATCH 3/4] fix(NumberInput): add defaultValue typecheck --- packages/react/src/components/NumberInput/NumberInput.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react/src/components/NumberInput/NumberInput.js b/packages/react/src/components/NumberInput/NumberInput.js index 0a35fdabedf4..ee0f27d5d530 100644 --- a/packages/react/src/components/NumberInput/NumberInput.js +++ b/packages/react/src/components/NumberInput/NumberInput.js @@ -204,7 +204,10 @@ class NumberInput extends Component { this.state = {}; return; } - let value = useControlledStateWithValue ? props.defaultValue : props.value; + let value = + useControlledStateWithValue || typeof props.defaultValue !== 'undefined' + ? props.defaultValue + : props.value; value = value === undefined ? 0 : value; if (props.min || props.min === 0) { value = Math.max(props.min, value); From 6797fffb097fb4fc636c582435f5e601554a76bc Mon Sep 17 00:00:00 2001 From: emyarod <afw5059@gmail.com> Date: Wed, 18 Nov 2020 09:38:10 -0600 Subject: [PATCH 4/4] fix(NumberInput): remove default internal state value in gDSFP --- packages/react/src/components/NumberInput/NumberInput.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/components/NumberInput/NumberInput.js b/packages/react/src/components/NumberInput/NumberInput.js index ee0f27d5d530..1e336d5cb115 100644 --- a/packages/react/src/components/NumberInput/NumberInput.js +++ b/packages/react/src/components/NumberInput/NumberInput.js @@ -170,7 +170,7 @@ class NumberInput extends Component { translateWithId: (id) => defaultTranslations[id], }; - static getDerivedStateFromProps({ min, max, value = 0 }, state) { + static getDerivedStateFromProps({ min, max, value }, state) { const { prevValue } = state; if (useControlledStateWithValue && value === '' && prevValue !== '') {