Skip to content

Commit 98d81f8

Browse files
authored
fix: in strictMode render correct defaultValue (#452)
1 parent 9e3a271 commit 98d81f8

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dependencies": {
4141
"@babel/runtime": "^7.10.1",
4242
"classnames": "^2.2.5",
43-
"rc-util": "^5.9.8"
43+
"rc-util": "^5.23.0"
4444
},
4545
"devDependencies": {
4646
"@testing-library/jest-dom": "^5.16.4",

src/InputNumber.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
33
import KeyCode from 'rc-util/lib/KeyCode';
4+
import { useLayoutUpdateEffect } from "rc-util/lib/hooks/useLayoutEffect"
45
import { composeRef } from 'rc-util/lib/ref';
56
import getMiniDecimal, {
67
DecimalClass,
@@ -18,7 +19,7 @@ import {
1819
validateNumber,
1920
} from './utils/numberUtil';
2021
import useCursor from './hooks/useCursor';
21-
import useUpdateEffect from './hooks/useUpdateEffect';
22+
2223
import useFrame from './hooks/useFrame';
2324

2425
/**
@@ -515,14 +516,14 @@ const InputNumber = React.forwardRef(
515516

516517
// ========================== Controlled ==========================
517518
// Input by precision
518-
useUpdateEffect(() => {
519+
useLayoutUpdateEffect(() => {
519520
if (!decimalValue.isInvalidate()) {
520521
setInputValue(decimalValue, false);
521522
}
522523
}, [precision]);
523524

524525
// Input by value
525-
useUpdateEffect(() => {
526+
useLayoutUpdateEffect(() => {
526527
const newValue = getMiniDecimal(value);
527528
setDecimalValue(newValue);
528529

@@ -537,7 +538,7 @@ const InputNumber = React.forwardRef(
537538
}, [value]);
538539

539540
// ============================ Cursor ============================
540-
useUpdateEffect(() => {
541+
useLayoutUpdateEffect(() => {
541542
if (formatter) {
542543
restoreCursor();
543544
}

src/hooks/useLayoutEffect.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/hooks/useUpdateEffect.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/formatter.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,21 @@ describe('InputNumber.Formatter', () => {
135135
expect(input.value).toEqual('0');
136136
});
137137
});
138+
139+
it('in strictMode render correct defaultValue ', () => {
140+
const Demo = () => {
141+
return (
142+
<React.StrictMode>
143+
<div>
144+
<InputNumber defaultValue={5} formatter={num => `$ ${num}`} />
145+
</div>
146+
</React.StrictMode>
147+
);
148+
};
149+
const { container } = render(<Demo />);
150+
const input = container.querySelector('input');
151+
expect(input.value).toEqual('$ 5');
152+
153+
fireEvent.change(input, { target: { value: 3 } });
154+
expect(input.value).toEqual('$ 3')
155+
});

0 commit comments

Comments
 (0)