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( + + ); + 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); diff --git a/packages/react/src/components/NumberInput/NumberInput.js b/packages/react/src/components/NumberInput/NumberInput.js index def7e6650e34..46a633a9d013 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 !== '') { @@ -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); @@ -300,6 +303,7 @@ class NumberInput extends Component { translateWithId: t, isMobile, size, + defaultValue, // eslint-disable-line ...other } = this.props;