Skip to content

Commit

Permalink
fix: handler do not move when click track and move without mouseup (#…
Browse files Browse the repository at this point in the history
…948)

* fix: handle do not move when click track and move

* chore: ugprade devDeps

* chore: fix lint

* test: add drag test case
  • Loading branch information
afc163 authored Nov 3, 2023
1 parent 2b92eb4 commit 9a12e4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"less": "^3.10.3",
"np": "^7.0.0",
"rc-test": "^7.0.15",
"rc-tooltip": "^5.0.1",
"rc-trigger": "^5.0.4",
"rc-tooltip": "^6.1.2",
"rc-trigger": "^5.3.4",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"regenerator-runtime": "^0.13.9",
Expand Down
39 changes: 21 additions & 18 deletions src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,23 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
setValue(cloneNextValues);
};

const changeToCloseValue = (newValue: number) => {
const finishChange = () => {
onAfterChange?.(getTriggerValue(rawValuesRef.current));
};

const [draggingIndex, draggingValue, cacheValues, onStartDrag] = useDrag(
containerRef,
direction,
rawValues,
mergedMin,
mergedMax,
formatValue,
triggerChange,
finishChange,
offsetValues,
);

const changeToCloseValue = (newValue: number, e?: React.MouseEvent) => {
if (!disabled) {
let valueIndex = 0;
let valueDist = mergedMax - mergedMin;
Expand All @@ -314,6 +330,9 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
onBeforeChange?.(getTriggerValue(cloneNextValues));
triggerChange(cloneNextValues);
onAfterChange?.(getTriggerValue(cloneNextValues));
if (e) {
onStartDrag(e, valueIndex, cloneNextValues);
}
}
};

Expand Down Expand Up @@ -344,7 +363,7 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
}

const nextValue = mergedMin + percent * (mergedMax - mergedMin);
changeToCloseValue(formatValue(nextValue));
changeToCloseValue(formatValue(nextValue), e);
};

// =========================== Keyboard ===========================
Expand Down Expand Up @@ -384,22 +403,6 @@ const Slider = React.forwardRef((props: SliderProps, ref: React.Ref<SliderRef>)
return draggableTrack;
}, [draggableTrack, mergedStep]);

const finishChange = () => {
onAfterChange?.(getTriggerValue(rawValuesRef.current));
};

const [draggingIndex, draggingValue, cacheValues, onStartDrag] = useDrag(
containerRef,
direction,
rawValues,
mergedMin,
mergedMax,
formatValue,
triggerChange,
finishChange,
offsetValues,
);

const onStartMove: OnStartMove = (e, valueIndex) => {
onStartDrag(e, valueIndex);

Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ export default function useDrag(
const updateCacheValueRef = React.useRef(updateCacheValue);
updateCacheValueRef.current = updateCacheValue;

const onStartMove: OnStartMove = (e, valueIndex) => {
const onStartMove: OnStartMove = (e, valueIndex, startValues?: number[]) => {
e.stopPropagation();

const originValue = rawValues[valueIndex];
// 如果是点击 track 触发的,需要传入变化后的初始值,而不能直接用 rawValues
const initialValues = startValues || rawValues;
const originValue = initialValues[valueIndex];

setDraggingIndex(valueIndex);
setDraggingValue(originValue);
setOriginValues(rawValues);
setOriginValues(initialValues);

const { pageX: startX, pageY: startY } = getPosition(e);

Expand Down
2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type React from 'react';

export type Direction = 'rtl' | 'ltr' | 'ttb' | 'btt';

export type OnStartMove = (e: React.MouseEvent | React.TouchEvent, valueIndex: number) => void;
export type OnStartMove = (e: React.MouseEvent | React.TouchEvent, valueIndex: number, startValues?: number[]) => void;

export type AriaValueFormat = (value: number) => string;

Expand Down
18 changes: 17 additions & 1 deletion tests/Slider.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import classNames from 'classnames';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, createEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import keyCode from 'rc-util/lib/KeyCode';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
Expand Down Expand Up @@ -609,4 +609,20 @@ describe('Slider', () => {
});
expect(onAfterChange).toHaveBeenCalledWith(20);
});

// https://github.com/react-component/slider/pull/948
it('could drag handler after click tracker', () => {
const onChange = jest.fn();
const { container } = render(<Slider onChange={onChange} />);
fireEvent.mouseDown(container.querySelector('.rc-slider'), {
clientX: 20,
});
expect(onChange).toHaveBeenLastCalledWith(20);

// Drag
const mouseMove = createEvent.mouseMove(document);
mouseMove.pageX = 100;
fireEvent(document, mouseMove);
expect(onChange).toHaveBeenLastCalledWith(100);
});
});

1 comment on commit 9a12e4b

@vercel
Copy link

@vercel vercel bot commented on 9a12e4b Nov 3, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.