Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: onAfterChange should not trigger when click on track #952

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/examples/debug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default () => {
console.log('Change:', nextValues);
// setValue(nextValues as any);
}}
onAfterChange={(v) => {
console.log('AfterChange:', v);
}}
// value={value}

min={0}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/marks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default () => (
<div>
<div style={style}>
<p>Slider with marks, `step=null`</p>
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} />
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} onAfterChange={(v) => console.log('AfterChange:', v)} />
</div>

<div style={style}>
Expand Down
18 changes: 18 additions & 0 deletions src/Handles/Handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
onFocus?: (e: React.FocusEvent<HTMLDivElement>) => void;
onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
render?: (origin: React.ReactElement<HandleProps>, props: RenderProps) => React.ReactElement;
onChangeComplete?: () => void;
}

const Handle = React.forwardRef((props: HandleProps, ref: React.Ref<HTMLDivElement>) => {
Expand All @@ -35,6 +36,7 @@
render,
dragging,
onOffsetChange,
onChangeComplete,
...restProps
} = props;
const {
Expand Down Expand Up @@ -109,6 +111,21 @@
}
};

const handleKeyUp = (e: React.KeyboardEvent<HTMLDivElement>) => {
switch (e.which || e.keyCode) {
case KeyCode.LEFT:
case KeyCode.RIGHT:
case KeyCode.UP:
case KeyCode.DOWN:
case KeyCode.HOME:
case KeyCode.END:
case KeyCode.PAGE_UP:
case KeyCode.PAGE_DOWN:
onChangeComplete?.();
break;
}
}

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (90% of all statements in
the enclosing function
have an explicit semicolon).

// ============================ Offset ============================
const positionStyle = getDirectionStyle(direction, value, min, max);

Expand All @@ -132,6 +149,7 @@
onMouseDown={onInternalStartMove}
onTouchStart={onInternalStartMove}
onKeyDown={onKeyDown}
onKeyUp={handleKeyUp}
tabIndex={disabled ? null : getIndex(tabIndex, valueIndex)}
role="slider"
aria-valuemin={min}
Expand Down
1 change: 1 addition & 0 deletions src/Handles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface HandlesProps {
onBlur?: (e: React.FocusEvent<HTMLDivElement>) => void;
handleRender?: HandleProps['render'];
draggingIndex: number;
onChangeComplete?: () => void;
}

export interface HandlesRef {
Expand Down
3 changes: 1 addition & 2 deletions src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)

onBeforeChange?.(getTriggerValue(cloneNextValues));
triggerChange(cloneNextValues);
onAfterChange?.(getTriggerValue(cloneNextValues));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥 onBeforeChange 可以有, onAfterChange不能?

Copy link
Member Author

@MadCcc MadCcc Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onAfterChange 必须对应到 mouseup 事件上,现在允许拖拽之后 change 并不代表一定会触发 afterChange

if (e) {
onStartDrag(e, valueIndex, cloneNextValues);
}
Expand Down Expand Up @@ -375,7 +374,6 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)

onBeforeChange?.(getTriggerValue(rawValues));
triggerChange(next.values);
onAfterChange?.(getTriggerValue(next.values));

setKeyboardValue(next.value);
}
Expand Down Expand Up @@ -543,6 +541,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
onFocus={onFocus}
onBlur={onBlur}
handleRender={handleRender}
onChangeComplete={finishChange}
/>

<Marks prefixCls={prefixCls} marks={markList} onClick={changeToCloseValue} />
Expand Down
7 changes: 6 additions & 1 deletion tests/Range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ describe('Range', () => {
fireEvent.keyDown(container.getElementsByClassName('rc-slider-handle')[1], {
keyCode: keyCode.RIGHT,
});
expect(onAfterChange).not.toHaveBeenCalled();

expect(onAfterChange).toBeCalled();
fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[1], {
keyCode: keyCode.RIGHT,
});

expect(onAfterChange).toHaveBeenCalled();
});

it('should not change value from keyboard events when disabled', () => {
Expand Down
26 changes: 24 additions & 2 deletions tests/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ describe('Slider', () => {
keyCode: keyCode.RIGHT,
});

expect(onAfterChange).toBeCalled();
expect(onAfterChange).not.toHaveBeenCalled();

fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
keyCode: keyCode.RIGHT,
});

expect(onAfterChange).toHaveBeenCalled();
});

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

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

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

expect(onChange).toHaveBeenCalledWith(49);
expect(onChangeComplete).not.toHaveBeenCalled();

fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
keyCode: keyCode.LEFT,
});

expect(onChangeComplete).toHaveBeenCalled();
});

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

expect(onBeforeChange).toHaveBeenCalledWith(20);
expect(onChange).toHaveBeenCalledWith(20);
expect(onAfterChange).not.toHaveBeenCalled();
fireEvent.mouseUp(container.querySelector('.rc-slider'), {
clientX: 20,
});
expect(onAfterChange).toHaveBeenCalledWith(20);
});
});
Expand Down Expand Up @@ -607,6 +625,10 @@ describe('Slider', () => {
fireEvent.mouseDown(container.querySelector('.rc-slider'), {
clientX: 20,
});
expect(onAfterChange).not.toHaveBeenCalled();
fireEvent.mouseUp(container.querySelector('.rc-slider'), {
clientX: 20,
});
expect(onAfterChange).toHaveBeenCalledWith(20);
});

Expand Down
13 changes: 12 additions & 1 deletion tests/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ describe('Common', () => {
);
const sliderHandleWrapper = container.querySelector(`#${labelId}`);
fireEvent.mouseDown(sliderHandleWrapper);
fireEvent.mouseUp(sliderHandleWrapper);
// Simulate propagation
fireEvent.mouseDown(container.querySelector('.rc-slider'));
fireEvent.mouseUp(container.querySelector('.rc-slider'));

fireEvent.click(sliderHandleWrapper);
expect(sliderOnChange).toHaveBeenCalled();
expect(sliderOnAfterChange).toHaveBeenCalled();
Expand All @@ -312,6 +315,9 @@ describe('Common', () => {
);
const rangeHandleWrapper = container2.querySelector(`#${labelId}`);
fireEvent.click(rangeHandleWrapper);
// Simulate propagation
fireEvent.mouseDown(container2.querySelector('.rc-slider'));
fireEvent.mouseUp(container2.querySelector('.rc-slider'));
expect(rangeOnAfterChange).toHaveBeenCalled();
});

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

expect(sliderOnChange).toHaveBeenCalled();
expect(sliderOnAfterChange).not.toHaveBeenCalled();

fireEvent.keyUp(container.getElementsByClassName('rc-slider-handle')[0], {
keyCode: KeyCode.UP,
});
expect(sliderOnAfterChange).toHaveBeenCalled();
expect(sliderOnAfterChange).toHaveBeenCalledTimes(1);
});
Expand Down
Loading