Skip to content

Commit 99316a2

Browse files
authored
fix: onAfterChange should not trigger when click on track (#952)
* fix: onAfterChange should not trigger when click on track * fix: fix keyboard onAfterChange * chore: doc * chore: test case
1 parent fa77b3e commit 99316a2

File tree

8 files changed

+66
-7
lines changed

8 files changed

+66
-7
lines changed

docs/examples/debug.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export default () => {
5252
console.log('Change:', nextValues);
5353
// setValue(nextValues as any);
5454
}}
55+
onAfterChange={(v) => {
56+
console.log('AfterChange:', v);
57+
}}
5558
// value={value}
5659

5760
min={0}

docs/examples/marks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default () => (
2525
<div>
2626
<div style={style}>
2727
<p>Slider with marks, `step=null`</p>
28-
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} />
28+
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} onAfterChange={(v) => console.log('AfterChange:', v)} />
2929
</div>
3030

3131
<div style={style}>

src/Handles/Handle.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface HandleProps {
2323
onFocus?: (e: React.FocusEvent<HTMLDivElement>) => void;
2424
onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
2525
render?: (origin: React.ReactElement<HandleProps>, props: RenderProps) => React.ReactElement;
26+
onChangeComplete?: () => void;
2627
}
2728

2829
const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivElement>) => {
@@ -35,6 +36,7 @@ const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivEleme
3536
render,
3637
dragging,
3738
onOffsetChange,
39+
onChangeComplete,
3840
...restProps
3941
} = props;
4042
const {
@@ -109,6 +111,21 @@ const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivEleme
109111
}
110112
};
111113

114+
const handleKeyUp = (e: React.KeyboardEvent<HTMLDivElement>) => {
115+
switch (e.which || e.keyCode) {
116+
case KeyCode.LEFT:
117+
case KeyCode.RIGHT:
118+
case KeyCode.UP:
119+
case KeyCode.DOWN:
120+
case KeyCode.HOME:
121+
case KeyCode.END:
122+
case KeyCode.PAGE_UP:
123+
case KeyCode.PAGE_DOWN:
124+
onChangeComplete?.();
125+
break;
126+
}
127+
}
128+
112129
// ============================ Offset ============================
113130
const positionStyle = getDirectionStyle(direction, value, min, max);
114131

@@ -132,6 +149,7 @@ const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivEleme
132149
onMouseDown={onInternalStartMove}
133150
onTouchStart={onInternalStartMove}
134151
onKeyDown={onKeyDown}
152+
onKeyUp={handleKeyUp}
135153
tabIndex={disabled ? null : getIndex(tabIndex, valueIndex)}
136154
role="slider"
137155
aria-valuemin={min}

src/Handles/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface HandlesProps {
1414
onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
1515
handleRender?: HandleProps['render'];
1616
draggingIndex: number;
17+
onChangeComplete?: () => void;
1718
}
1819

1920
export interface HandlesRef {

src/Slider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
329329

330330
onBeforeChange?.(getTriggerValue(cloneNextValues));
331331
triggerChange(cloneNextValues);
332-
onAfterChange?.(getTriggerValue(cloneNextValues));
333332
if (e) {
334333
onStartDrag(e, valueIndex, cloneNextValues);
335334
}
@@ -375,7 +374,6 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
375374

376375
onBeforeChange?.(getTriggerValue(rawValues));
377376
triggerChange(next.values);
378-
onAfterChange?.(getTriggerValue(next.values));
379377

380378
setKeyboardValue(next.value);
381379
}
@@ -543,6 +541,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
543541
onFocus={onFocus}
544542
onBlur={onBlur}
545543
handleRender={handleRender}
544+
onChangeComplete={finishChange}
546545
/>
547546

548547
<Marks prefixCls={prefixCls} marks={markList} onClick={changeToCloseValue} />

tests/Range.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,13 @@ describe('Range', () => {
116116
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[1], {
117117
keyCode: keyCode.RIGHT,
118118
});
119+
expect(onAfterChange).not.toHaveBeenCalled();
119120

120-
expect(onAfterChange).toBeCalled();
121+
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[1], {
122+
keyCode: keyCode.RIGHT,
123+
});
124+
125+
expect(onAfterChange).toHaveBeenCalled();
121126
});
122127

123128
it('should not change value from keyboard events when disabled', () => {

tests/Slider.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ describe('Slider', () => {
144144
keyCode: keyCode.RIGHT,
145145
});
146146

147-
expect(onAfterChange).toBeCalled();
147+
expect(onAfterChange).not.toHaveBeenCalled();
148+
149+
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
150+
keyCode: keyCode.RIGHT,
151+
});
152+
153+
expect(onAfterChange).toHaveBeenCalled();
148154
});
149155

150156
it('decreases the value for reverse-horizontal when key "right" is pressed', () => {
@@ -182,13 +188,21 @@ describe('Slider', () => {
182188

183189
it('decreases the value when key "left" is pressed', () => {
184190
const onChange = jest.fn();
185-
const { container } = render(<Slider defaultValue={50} onChange={onChange} />);
191+
const onChangeComplete = jest.fn();
192+
const { container } = render(<Slider defaultValue={50} onChange={onChange} onAfterChange={onChangeComplete} />);
186193

187194
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[0], {
188195
keyCode: keyCode.LEFT,
189196
});
190197

191198
expect(onChange).toHaveBeenCalledWith(49);
199+
expect(onChangeComplete).not.toHaveBeenCalled();
200+
201+
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
202+
keyCode: keyCode.LEFT,
203+
});
204+
205+
expect(onChangeComplete).toHaveBeenCalled();
192206
});
193207

194208
it('it should work fine when arrow key is pressed', () => {
@@ -555,6 +569,10 @@ describe('Slider', () => {
555569

556570
expect(onBeforeChange).toHaveBeenCalledWith(20);
557571
expect(onChange).toHaveBeenCalledWith(20);
572+
expect(onAfterChange).not.toHaveBeenCalled();
573+
fireEvent.mouseUp(container.querySelector('.rc-slider'), {
574+
clientX: 20,
575+
});
558576
expect(onAfterChange).toHaveBeenCalledWith(20);
559577
});
560578
});
@@ -607,6 +625,10 @@ describe('Slider', () => {
607625
fireEvent.mouseDown(container.querySelector('.rc-slider'), {
608626
clientX: 20,
609627
});
628+
expect(onAfterChange).not.toHaveBeenCalled();
629+
fireEvent.mouseUp(container.querySelector('.rc-slider'), {
630+
clientX: 20,
631+
});
610632
expect(onAfterChange).toHaveBeenCalledWith(20);
611633
});
612634

tests/common.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ describe('Common', () => {
301301
);
302302
const sliderHandleWrapper = container.querySelector(`#${labelId}`);
303303
fireEvent.mouseDown(sliderHandleWrapper);
304-
fireEvent.mouseUp(sliderHandleWrapper);
304+
// Simulate propagation
305+
fireEvent.mouseDown(container.querySelector('.rc-slider'));
306+
fireEvent.mouseUp(container.querySelector('.rc-slider'));
307+
305308
fireEvent.click(sliderHandleWrapper);
306309
expect(sliderOnChange).toHaveBeenCalled();
307310
expect(sliderOnAfterChange).toHaveBeenCalled();
@@ -312,6 +315,9 @@ describe('Common', () => {
312315
);
313316
const rangeHandleWrapper = container2.querySelector(`#${labelId}`);
314317
fireEvent.click(rangeHandleWrapper);
318+
// Simulate propagation
319+
fireEvent.mouseDown(container2.querySelector('.rc-slider'));
320+
fireEvent.mouseUp(container2.querySelector('.rc-slider'));
315321
expect(rangeOnAfterChange).toHaveBeenCalled();
316322
});
317323

@@ -327,6 +333,11 @@ describe('Common', () => {
327333
});
328334

329335
expect(sliderOnChange).toHaveBeenCalled();
336+
expect(sliderOnAfterChange).not.toHaveBeenCalled();
337+
338+
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
339+
keyCode: KeyCode.UP,
340+
});
330341
expect(sliderOnAfterChange).toHaveBeenCalled();
331342
expect(sliderOnAfterChange).toHaveBeenCalledTimes(1);
332343
});

0 commit comments

Comments
 (0)