Skip to content

Commit

Permalink
fix: onChangeComplete should be triggered when click marks (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Jul 25, 2024
1 parent 9c055f5 commit ce5d307
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/examples/marks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default () => (
defaultValue={[-10, 0]}
allowCross={false}
pushable
draggableTrack
onChangeComplete={(v) => console.log('AfterChange:', v)}
/>
</div>

Expand Down
12 changes: 11 additions & 1 deletion src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,22 @@ const Slider = React.forwardRef<SliderRef, SliderProps<number | number[]>>((prop
cloneNextValues.push(newValue);
}

onBeforeChange?.(getTriggerValue(cloneNextValues));
const nextValue = getTriggerValue(cloneNextValues);
onBeforeChange?.(nextValue);
triggerChange(cloneNextValues);

if (e) {
(document.activeElement as HTMLElement)?.blur?.();
handlesRef.current.focus(focusIndex);
onStartDrag(e, focusIndex, cloneNextValues);
} else {
// https://github.com/ant-design/ant-design/issues/49997
onAfterChange?.(nextValue);
warning(
!onAfterChange,
'[rc-slider] `onAfterChange` is deprecated. Please use `onChangeComplete` instead.',
);
onChangeComplete?.(nextValue);
}
}
};
Expand Down
9 changes: 7 additions & 2 deletions tests/marks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ describe('marks', () => {

it('should select correct value while click on marks', () => {
const marks = { 0: '0', 30: '30', 100: '100' };

const { container } = render(<Slider marks={marks} />);
const onChange = jest.fn();
const onChangeComplete = jest.fn();
const { container } = render(<Slider marks={marks} onChange={onChange} onChangeComplete={onChangeComplete} />);
fireEvent.click(container.getElementsByClassName('rc-slider-mark-text')[1]);
expect(container.getElementsByClassName('rc-slider-handle')[0]).toHaveAttribute(
'aria-valuenow',
'30',
);
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(30);
expect(onChangeComplete).toHaveBeenCalledTimes(1);
expect(onChangeComplete).toHaveBeenCalledWith(30);
});

// TODO: not implement yet
Expand Down

0 comments on commit ce5d307

Please sign in to comment.