Skip to content

Commit 8d13b5c

Browse files
committed
fix(numberfield): fix wrong value after input and spin button click
1 parent 84a73f6 commit 8d13b5c

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/NumberField/NumberField.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ const NumberField = React.forwardRef(function NumberField(props, ref) {
110110
};
111111

112112
const handleClick = val => {
113+
const stateValue = parseFloat(valueDerived);
113114
const newValue = clamp(
114-
+parseFloat(valueDerived + val).toFixed(2),
115+
+parseFloat(stateValue + val).toFixed(2),
115116
min,
116117
max
117-
);
118+
).toString();
118119

119120
setValueState(newValue);
120121

src/NumberField/NumberField.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('<NumberField />', () => {
1515
const spinButton = getByTestId('increment');
1616
spinButton.click();
1717
expect(handleChange).toHaveBeenCalledTimes(1);
18-
expect(handleChange).toHaveBeenCalledWith(3);
18+
expect(handleChange).toHaveBeenCalledWith('3');
1919
});
2020

2121
it('should call onChange on blur after keyboard input', () => {
@@ -52,6 +52,20 @@ describe('<NumberField />', () => {
5252
expect(handleChange).toHaveBeenCalledTimes(1);
5353
});
5454

55+
it('should give correct result after user changes input value and then clicks increment button', () => {
56+
const handleChange = jest.fn();
57+
const { container, getByTestId } = renderWithTheme(
58+
<NumberField onChange={handleChange} defaultValue={0} />
59+
);
60+
const input = container.querySelector('input');
61+
const incrementButton = getByTestId('increment');
62+
63+
fireEvent.change(input, { target: { value: 2 } });
64+
incrementButton.click();
65+
66+
expect(handleChange).toHaveBeenCalledWith('3');
67+
});
68+
5569
it('should reach max value', () => {
5670
const { getByTestId, container } = renderWithTheme(
5771
<NumberField defaultValue={90} min={0} max={100} step={10} />

src/NumberField/NumberField.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default {
2828

2929
export const Default = () => (
3030
<>
31-
<NumberField defaultValue={1.5} min={0} max={9} width={130} />
31+
<NumberField defaultValue={3} step={1.5} min={1.5} max={9} width={130} />
3232
<br />
3333
<NumberField defaultValue={1995} width={130} />
3434
<br />

0 commit comments

Comments
 (0)