Skip to content

Commit 1155356

Browse files
authored
feat: support changeOnBlur (#596)
1 parent bb48d80 commit 1155356

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

docs/demo/simple.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint no-console:0 */
2-
import React from 'react';
32
import InputNumber from 'rc-input-number';
3+
import React from 'react';
44
import '../../assets/index.less';
55

66
export default () => {
@@ -54,6 +54,17 @@ export default () => {
5454
max={99}
5555
defaultValue={33}
5656
/>
57+
58+
<hr />
59+
<h3>!changeOnBlur</h3>
60+
<InputNumber
61+
style={{ width: 100 }}
62+
min={-9}
63+
max={9}
64+
defaultValue={10}
65+
onChange={onChange}
66+
changeOnBlur={false}
67+
/>
5768
</div>
5869
);
5970
};

src/InputNumber.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ export interface InputNumberProps<T extends ValueType = ValueType>
102102
// focusOnUpDown: boolean;
103103
// useTouch: boolean;
104104

105-
// size?: ISize;
105+
/**
106+
* Trigger change onBlur event.
107+
* If disabled, user must press enter or click handler to confirm the value update
108+
*/
109+
changeOnBlur?: boolean;
106110
}
107111

108112
type InternalInputNumberProps = Omit<InputNumberProps, 'prefix' | 'suffix'>;
@@ -138,6 +142,8 @@ const InternalInputNumber = React.forwardRef(
138142
onPressEnter,
139143
onStep,
140144

145+
changeOnBlur = true,
146+
141147
...inputProps
142148
} = props;
143149

@@ -513,7 +519,9 @@ const InternalInputNumber = React.forwardRef(
513519

514520
// >>> Focus & Blur
515521
const onBlur = () => {
516-
flushInputValue(false);
522+
if (changeOnBlur) {
523+
flushInputValue(false);
524+
}
517525

518526
setFocus(false);
519527

tests/input.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,15 @@ describe('InputNumber.Input', () => {
212212
expect(onChange).toHaveBeenLastCalledWith(null);
213213
});
214214
});
215+
216+
it('!changeOnBlur', () => {
217+
const onChange = jest.fn();
218+
219+
const { container } = render(
220+
<InputNumber min={0} max={9} defaultValue={10} changeOnBlur={false} onChange={onChange} />,
221+
);
222+
223+
fireEvent.blur(container.querySelector('input'));
224+
expect(onChange).not.toHaveBeenCalled();
225+
});
215226
});

0 commit comments

Comments
 (0)