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(slider): slider will rerender when every mouseEvent emitted #2658

Closed
wants to merge 1 commit into from
Closed
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
fix(slider): slider will rerender when every mouseEvent emitted
We only should setState when the position changes
  • Loading branch information
NWYLZW committed Dec 11, 2023
commit 17778c6eba4f9b644fa439d92542b991dd26b873
7 changes: 7 additions & 0 deletions src/slider/Slider.tsx
Original file line number Diff line number Diff line change
@@ -93,6 +93,8 @@ const Slider = React.forwardRef<HTMLDivElement, SliderProps>((originalProps, ref
const sizeKey = isVertical ? 'height' : 'width';
const renderDots = isVertical ? dots.map((item) => ({ ...item, position: 1 - item.position })) : dots;

const positionsRef = useRef([0]);

const handleInputChange = (newValue: number, nodeIndex: SliderHandleNode) => {
const safeValue = Number(newValue.toFixed(32));
let resultValue = Math.max(Math.min(max, safeValue), min);
@@ -160,8 +162,13 @@ const Slider = React.forwardRef<HTMLDivElement, SliderProps>((originalProps, ref
const clientKey = isVertical ? 'clientY' : 'clientX';
const sliderPositionInfo = sliderRef.current.getBoundingClientRect();
const sliderOffset = sliderPositionInfo[startDirection];
const oldPosition = positionsRef.current[nodeIndex || 0];
const position = ((event[clientKey] - sliderOffset) / sliderPositionInfo[sizeKey]) * (isVertical ? -1 : 1);
if (oldPosition === position) {
return;
}
setPosition(position, nodeIndex);
positionsRef.current[nodeIndex || 0] = position;
};

const handleClickMarks = (event: React.MouseEvent, value: number) => {
Loading