Skip to content

Commit 115f686

Browse files
committed
feat: add a new parameter for onStep & update api-docs
1 parent d9662d5 commit 115f686

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

docs/api.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ nav:
8080
<td>readOnly</td>
8181
<td>Boolean</td>
8282
<td>false</td>
83-
<td>Specifies that an InputNumber is read only </td>
83+
<td>Specifies that an InputNumber is read only</td>
84+
</tr>
85+
<tr>
86+
<td>changeOnWheel</td>
87+
<td>Boolean</td>
88+
<td>false</td>
89+
<td>Specifies that the value is set using the mouse wheel</td>
8490
</tr>
8591
<tr>
8692
<td>controls</td>
@@ -136,6 +142,12 @@ nav:
136142
<td></td>
137143
<td>Called when an element gets focus</td>
138144
</tr>
145+
<tr>
146+
<td>onStep</td>
147+
<td>(value: T, info: { offset: ValueType; type: 'up' | 'down', emitter: 'handler' | 'keydown' | 'wheel' }) => void</td>
148+
<td></td>
149+
<td>Called when the user clicks the arrows on the keyboard or interface and when the mouse wheel is spun.</td>
150+
</tr>
139151
<tr>
140152
<td>style</td>
141153
<td>Object</td>

docs/demo/on-step.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* eslint no-console:0 */
2+
import InputNumber from '@rc-component/input-number';
3+
import React, { useState } from 'react';
4+
import '../../assets/index.less';
5+
6+
export default () => {
7+
const [emitter, setEmitter] = useState('interface buttons (up)');
8+
const [value, setValue] = React.useState<string | number>(0);
9+
10+
const onChange = (val: number) => {
11+
console.warn('onChange:', val, typeof val);
12+
setValue(val);
13+
};
14+
15+
const onStep = (_: number, info: { offset: number; type: 'up' | 'down', emitter: 'handler' | 'keyboard' | 'wheel' }) => {
16+
if (info.emitter === 'handler') {
17+
setEmitter(`interface buttons (${info.type})`);
18+
}
19+
20+
if (info.emitter === 'keyboard') {
21+
setEmitter(`keyboard (${info.type})`);
22+
}
23+
24+
if (info.emitter === 'wheel') {
25+
setEmitter(`mouse wheel (${info.type})`);
26+
}
27+
};
28+
29+
return (
30+
<div style={{ margin: 10 }}>
31+
<h3>onStep callback</h3>
32+
<InputNumber
33+
aria-label="onStep callback example"
34+
min={0}
35+
max={10}
36+
style={{ width: 100 }}
37+
value={value}
38+
changeOnWheel
39+
onChange={onChange}
40+
onStep={onStep}
41+
/>
42+
43+
<div style={{ marginTop: 10 }}>Triggered by: {emitter}</div>
44+
</div>
45+
);
46+
};

docs/example.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ nav:
4141

4242
<code src="./demo/small-step.tsx"></code>
4343

44+
## on-step
45+
46+
<code src="./demo/on-step.tsx"></code>
47+
4448
## wheel
4549

4650
<code src="./demo/wheel.tsx"></code>

src/InputNumber.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export interface InputNumberProps<T extends ValueType = ValueType>
9999
onChange?: (value: T | null) => void;
100100
onPressEnter?: React.KeyboardEventHandler<HTMLInputElement>;
101101

102-
onStep?: (value: T, info: { offset: ValueType; type: 'up' | 'down' }) => void;
102+
onStep?: (value: T, info: { offset: ValueType; type: 'up' | 'down', emitter: 'handler' | 'keyboard' | 'wheel' }) => void;
103103

104104
/**
105105
* Trigger change onBlur event.
@@ -432,7 +432,7 @@ const InternalInputNumber = React.forwardRef(
432432
};
433433

434434
// ============================= Step =============================
435-
const onInternalStep = (up: boolean) => {
435+
const onInternalStep = (up: boolean, emitter: 'handler' | 'keyboard' | 'wheel') => {
436436
// Ignore step since out of range
437437
if ((up && upDisabled) || (!up && downDisabled)) {
438438
return;
@@ -454,6 +454,7 @@ const InternalInputNumber = React.forwardRef(
454454
onStep?.(getDecimalValue(stringMode, updatedValue), {
455455
offset: shiftKeyRef.current ? getDecupleSteps(step) : step,
456456
type: up ? 'up' : 'down',
457+
emitter,
457458
});
458459

459460
inputRef.current?.focus();
@@ -511,7 +512,7 @@ const InternalInputNumber = React.forwardRef(
511512

512513
// Do step
513514
if (!compositionRef.current && ['Up', 'ArrowUp', 'Down', 'ArrowDown'].includes(key)) {
514-
onInternalStep(key === 'Up' || key === 'ArrowUp');
515+
onInternalStep(key === 'Up' || key === 'ArrowUp', 'keyboard');
515516
event.preventDefault();
516517
}
517518
};
@@ -526,7 +527,7 @@ const InternalInputNumber = React.forwardRef(
526527
const onWheel = (event) => {
527528
// moving mouse wheel rises wheel event with deltaY < 0
528529
// scroll value grows from top to bottom, as screen Y coordinate
529-
onInternalStep(event.deltaY < 0);
530+
onInternalStep(event.deltaY < 0, 'wheel');
530531
event.preventDefault();
531532
};
532533
const input = inputRef.current;

src/StepHandler.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface StepHandlerProps {
2020
downNode?: React.ReactNode;
2121
upDisabled?: boolean;
2222
downDisabled?: boolean;
23-
onStep: (up: boolean) => void;
23+
onStep: (up: boolean, emitter: 'handler' | 'keyboard' | 'wheel') => void;
2424
}
2525

2626
export default function StepHandler({
@@ -48,11 +48,11 @@ export default function StepHandler({
4848
e.preventDefault();
4949
onStopStep();
5050

51-
onStepRef.current(up);
51+
onStepRef.current(up, 'handler');
5252

5353
// Loop step for interval
5454
function loopStep() {
55-
onStepRef.current(up);
55+
onStepRef.current(up, 'handler');
5656

5757
stepTimeoutRef.current = setTimeout(loopStep, STEP_INTERVAL);
5858
}

tests/click.test.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('InputNumber.Click', () => {
2323
selector: string,
2424
changedValue: string | number,
2525
stepType: 'up' | 'down',
26+
emitter: 'handler' | 'keyboard' | 'wheel',
2627
) {
2728
it(name, () => {
2829
const onChange = jest.fn();
@@ -36,27 +37,27 @@ describe('InputNumber.Click', () => {
3637
fireEvent.click(container.querySelector(selector));
3738
expect(onChange).toHaveBeenCalledTimes(1);
3839
expect(onChange).toHaveBeenCalledWith(changedValue);
39-
expect(onStep).toHaveBeenCalledWith(changedValue, { offset: 1, type: stepType });
40+
expect(onStep).toHaveBeenCalledWith(changedValue, { offset: 1, type: stepType, emitter });
4041
unmount();
4142
});
4243
}
4344

4445
describe('basic work', () => {
45-
testInputNumber('up button', { defaultValue: 10 }, '.rc-input-number-handler-up', 11, 'up');
46+
testInputNumber('up button', { defaultValue: 10 }, '.rc-input-number-handler-up', 11, 'up', 'handler');
4647

47-
testInputNumber('down button', { value: 10 }, '.rc-input-number-handler-down', 9, 'down');
48+
testInputNumber('down button', { value: 10 }, '.rc-input-number-handler-down', 9, 'down', 'handler');
4849
});
4950

5051
describe('empty input', () => {
51-
testInputNumber('up button', {}, '.rc-input-number-handler-up', 1, 'up');
52+
testInputNumber('up button', {}, '.rc-input-number-handler-up', 1, 'up', 'handler');
5253

53-
testInputNumber('down button', {}, '.rc-input-number-handler-down', -1, 'down');
54+
testInputNumber('down button', {}, '.rc-input-number-handler-down', -1, 'down', 'handler');
5455
});
5556

5657
describe('empty with min & max', () => {
57-
testInputNumber('up button', { min: 6, max: 10 }, '.rc-input-number-handler-up', 6, 'up');
58+
testInputNumber('up button', { min: 6, max: 10 }, '.rc-input-number-handler-up', 6, 'up', 'handler');
5859

59-
testInputNumber('down button', { min: 6, max: 10 }, '.rc-input-number-handler-down', 6, 'down');
60+
testInputNumber('down button', { min: 6, max: 10 }, '.rc-input-number-handler-down', 6, 'down', 'handler');
6061
});
6162

6263
describe('null with min & max', () => {
@@ -66,6 +67,7 @@ describe('InputNumber.Click', () => {
6667
'.rc-input-number-handler-up',
6768
6,
6869
'up',
70+
'handler',
6971
);
7072

7173
testInputNumber(
@@ -74,6 +76,7 @@ describe('InputNumber.Click', () => {
7476
'.rc-input-number-handler-down',
7577
6,
7678
'down',
79+
'handler',
7780
);
7881
});
7982

@@ -183,7 +186,7 @@ describe('InputNumber.Click', () => {
183186
});
184187

185188
expect(onChange).toHaveBeenCalledWith(1.1);
186-
expect(onStep).toHaveBeenCalledWith(1.1, { offset: '0.1', type: 'down' });
189+
expect(onStep).toHaveBeenCalledWith(1.1, { offset: '0.1', type: 'down', emitter: 'keyboard' });
187190
});
188191

189192
it('click up button with pressing shift key', () => {
@@ -201,6 +204,6 @@ describe('InputNumber.Click', () => {
201204
keyCode: KeyCode.UP,
202205
});
203206
expect(onChange).toHaveBeenCalledWith(1.3);
204-
expect(onStep).toHaveBeenCalledWith(1.3, { offset: '0.1', type: 'up' });
207+
expect(onStep).toHaveBeenCalledWith(1.3, { offset: '0.1', type: 'up', emitter: 'keyboard' });
205208
});
206209
});

0 commit comments

Comments
 (0)