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

feat(Slider): make SliderButton moves with the mouse when the SliderButton quickly jumps to the mouse click. #2623

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions components/Slider/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import React, {
useEffect,
useRef,
useMemo,
forwardRef,
useImperativeHandle,
} from 'react';
import cs from '../_util/classNames';
import { on, off } from '../_util/dom';
Expand Down Expand Up @@ -36,6 +38,10 @@ interface SliderButtonProps {
onArrowEvent?: (type: 'addition' | 'subtraction') => void;
}

export type SliderButtonHandle = {
mouseDown: () => void;
};

const triggerStyle = { maxWidth: 350 };

const triggerDuration = {
Expand All @@ -50,7 +56,10 @@ const triggerPopupAlign = {
bottom: 12,
};

const SliderButton = function (props: SliderButtonProps) {
const SliderButton = function (
props: SliderButtonProps,
ref: (handle: SliderButtonHandle) => void
) {
// props
const {
style,
Expand Down Expand Up @@ -83,8 +92,8 @@ const SliderButton = function (props: SliderButtonProps) {
const isDragging = useRef(false);
const tooltip = useRef(null);

function handleMouseDown(e) {
e.stopPropagation();
function handleMouseDown(e?: React.MouseEvent) {
e?.stopPropagation();
if (disabled) return;

moveStart(e);
Expand Down Expand Up @@ -118,9 +127,9 @@ const SliderButton = function (props: SliderButtonProps) {
}
}

function moveStart(e) {
function moveStart(e?: React.MouseEvent) {
// 如果不阻止默认行为可能会在拖动时产生鼠标选中状态,所以手动处理元素失焦
e.preventDefault();
e?.preventDefault();
const activeElement = document.activeElement as HTMLElement;
activeElement && activeElement.blur && activeElement.blur();

Expand Down Expand Up @@ -199,6 +208,12 @@ const SliderButton = function (props: SliderButtonProps) {
tooltip && tooltip.current && tooltip.current.updatePopupPosition();
}, [value]);

useImperativeHandle<SliderButtonHandle, SliderButtonHandle>(ref, () => ({
mouseDown: () => {
handleMouseDown();
},
}));

return (
<Trigger
style={triggerStyle}
Expand Down Expand Up @@ -252,4 +267,8 @@ const SliderButton = function (props: SliderButtonProps) {
);
};

export default memo(SliderButton);
const SliderButtonComponent = forwardRef<SliderButtonHandle, SliderButtonProps>(SliderButton);

SliderButtonComponent.displayName = 'Slider';

export default memo(SliderButtonComponent);
10 changes: 9 additions & 1 deletion components/Slider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { forwardRef, memo, useContext, CSSProperties, useRef, useMemo } from 'react';
import NP, { plus, times, divide } from 'number-precision';
import omit from '../_util/omit';
import SliderButton from './button';
import SliderButton, { SliderButtonHandle } from './button';
import Marks from './marks';
import Dots from './dots';
import Input from './input';
Expand Down Expand Up @@ -55,6 +55,8 @@ function Slider(baseProps: SliderProps, ref) {
...rest
} = props;

const refSliderButtons = useRef<{ [key: number]: SliderButtonHandle }>({});

const range = !!propRange;
const rangeConfig = isObject(propRange) ? { ...propRange } : { draggableBar: false };
const isReverse = rtl ? !reverse : reverse;
Expand Down Expand Up @@ -248,6 +250,8 @@ function Slider(baseProps: SliderProps, ref) {
}

function onRoadMouseDown(e) {
// 防止拖动的时候鼠标选中
e.preventDefault();
getPosition();
const val = getValueByCoords(e.clientX, e.clientY);
if (rangeConfig.draggableBar && inRange(val)) {
Expand All @@ -273,9 +277,12 @@ function Slider(baseProps: SliderProps, ref) {
if (range && nearEndValue - value > value - nearBeginVal) {
copyVal[beforeIndex] = value;
onChange(copyVal, 'jumpToClick');
// 触发鼠标点击的事件,SliderButton 在此时可以一起被拖动
refSliderButtons.current[beforeIndex]?.mouseDown?.();
} else {
copyVal[nextIndex] = value;
onChange(copyVal, 'jumpToClick');
refSliderButtons.current[nextIndex]?.mouseDown?.();
}
onMouseUp();
}
Expand Down Expand Up @@ -402,6 +409,7 @@ function Slider(baseProps: SliderProps, ref) {
return (
<SliderButton
key={index}
ref={(ref) => (refSliderButtons.current[index] = ref)}
style={getBtnStyle(getIntervalOffset(val, intervalConfigs))}
disabled={disabled}
prefixCls={prefixCls}
Expand Down