Skip to content

Commit a90d42c

Browse files
authored
chore: blur with empty string will trigger null as change event (#296)
* chore: onChange can trigger null when empty * chore: empty should trigger as null
1 parent 18f6b01 commit a90d42c

File tree

5 files changed

+14
-2107
lines changed

5 files changed

+14
-2107
lines changed

docs/examples/simple.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ export default () => {
1111
const [value, setValue] = React.useState<string | number>(5);
1212

1313
const onChange = (val: number) => {
14-
console.warn('onChange:', val);
14+
console.warn('onChange:', val, typeof val);
1515
setValue(val);
1616
};
1717

1818
return (
1919
<div style={{ margin: 10 }}>
20+
<h3>Controlled</h3>
2021
<InputNumber
2122
aria-label="Simple number input example"
2223
min={-8}
@@ -43,6 +44,10 @@ export default () => {
4344
toggle stringMode ({String(stringMode)})
4445
</button>
4546
</p>
47+
48+
<hr />
49+
<h3>Uncontrolled</h3>
50+
{/* <InputNumber style={{ width: 100 }} onChange={onChange} defaultValue={33} /> */}
4651
</div>
4752
);
4853
};

src/InputNumber.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ const InputNumber = React.forwardRef(
285285
// Trigger event
286286
if (!updateValue.equals(decimalValue)) {
287287
setUncontrolledDecimalValue(updateValue);
288-
onChange?.(getDecimalValue(stringMode, updateValue));
288+
onChange?.(updateValue.isEmpty() ? null : getDecimalValue(stringMode, updateValue));
289289

290290
// Reformat input if value is not controlled
291291
if (value === undefined) {
@@ -381,11 +381,9 @@ const InputNumber = React.forwardRef(
381381
let formatValue: DecimalClass = parsedValue;
382382

383383
if (!parsedValue.isNaN()) {
384-
// Only validate value can be re-fill to inputValue
385-
if (!formatValue.isEmpty()) {
386-
// Reassign the formatValue within ranged of trigger control
387-
formatValue = triggerValueUpdate(parsedValue, true);
388-
}
384+
// Only validate value or empty value can be re-fill to inputValue
385+
// Reassign the formatValue within ranged of trigger control
386+
formatValue = triggerValueUpdate(parsedValue, true);
389387
} else {
390388
formatValue = decimalValue;
391389
}

src/hooks/useUpdateEffect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as React from 'react';
66
export default function useUpdateEffect(callback: () => void | (() => void), condition: any[]) {
77
const initRef = React.useRef(false);
88

9-
React.useEffect(() => {
9+
React.useLayoutEffect(() => {
1010
if (!initRef.current) {
1111
initRef.current = true;
1212
return undefined;

tests/github.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,13 @@ describe('InputNumber.Github', () => {
211211
expect(onInput).toHaveBeenCalledWith('');
212212

213213
wrapper.blurInput();
214-
expect(onChange).toHaveBeenCalledTimes(1);
214+
expect(onChange).toHaveBeenCalledTimes(2);
215215
expect(onInput).toHaveBeenCalledTimes(2);
216+
expect(onChange).toHaveBeenLastCalledWith(null);
216217

217218
wrapper.focusInput();
218219
wrapper.blurInput();
219-
expect(onChange).toHaveBeenCalledTimes(1);
220+
expect(onChange).toHaveBeenCalledTimes(2);
220221
expect(onInput).toHaveBeenCalledTimes(2);
221222
});
222223

0 commit comments

Comments
 (0)