Skip to content

Commit

Permalink
fix(NumberInput): avoid validation for space (carbon-design-system#2280)
Browse files Browse the repository at this point in the history
  • Loading branch information
asudoh committed May 1, 2019
1 parent 66c35f9 commit 290fab3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/components/NumberInput/NumberInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,39 @@ describe('NumberInput', () => {
expect(wrapper.find('NumberInput').instance().state.value).toEqual(2);
});

it('should cap the number given to value prop', () => {
// Enzyme doesn't seem to allow setState() in a forwardRef-wrapped class component
wrapper
.find('NumberInput')
.instance()
.setState({ value: 0 });
wrapper.update();
wrapper.setProps({ value: 5, min: 10, max: 20 });
// Enzyme doesn't seem to allow state() in a forwardRef-wrapped class component
expect(wrapper.find('NumberInput').instance().state.value).toEqual(
10
);
wrapper.setProps({ value: 25, min: 10, max: 20 });
// Enzyme doesn't seem to allow state() in a forwardRef-wrapped class component
expect(wrapper.find('NumberInput').instance().state.value).toEqual(
20
);
});

it('should avoid capping when non-number prop is given to value prop', () => {
// Enzyme doesn't seem to allow setState() in a forwardRef-wrapped class component
wrapper
.find('NumberInput')
.instance()
.setState({ value: 2 });
wrapper.update();
wrapper.setProps({ value: '', min: 1, max: 3 });
// Enzyme doesn't seem to allow state() in a forwardRef-wrapped class component
expect(wrapper.find('NumberInput').instance().state.value).toEqual(
''
);
});

it('should avoid change the value upon setting props, unless there the value actually changes', () => {
wrapper.setProps({ value: 1 });
// Enzyme doesn't seem to allow setState() in a forwardRef-wrapped class component
Expand Down
13 changes: 11 additions & 2 deletions src/components/NumberInput/NumberInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const defaultTranslations = {
[translationIds['decrement.number']]: 'Decrement number',
};

const capMin = (min, value) =>
isNaN(min) || (!min && min !== 0) || isNaN(value) || (!value && value !== 0)
? value
: Math.max(min, value);
const capMax = (max, value) =>
isNaN(max) || (!max && max !== 0) || isNaN(value) || (!value && value !== 0)
? value
: Math.min(max, value);

class NumberInput extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -148,12 +157,12 @@ class NumberInput extends Component {
*/
_inputRef = null;

static getDerivedStateFromProps({ min, value }, state) {
static getDerivedStateFromProps({ min, max, value }, state) {
const { prevValue } = state;
return prevValue === value
? null
: {
value: isNaN(min) ? value : Math.max(min, value),
value: capMax(max, capMin(min, value)),
prevValue: value,
};
}
Expand Down

0 comments on commit 290fab3

Please sign in to comment.