From b18b6533b9a1f2406a719f8e2ade0df80e6a2ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Wed, 24 Jul 2024 19:26:48 +0800 Subject: [PATCH] chore: add internal hooks (#1014) * chore: add internal hooks * chore: fix lint --- docs/examples/editable.tsx | 83 ++++++++++++++++++++++---------------- src/context.ts | 17 ++++++++ src/hooks/useDrag.ts | 21 ++++++++++ src/index.tsx | 1 + 4 files changed, 87 insertions(+), 35 deletions(-) diff --git a/docs/examples/editable.tsx b/docs/examples/editable.tsx index ba03673c4..54e561b7b 100644 --- a/docs/examples/editable.tsx +++ b/docs/examples/editable.tsx @@ -1,7 +1,8 @@ /* eslint react/no-multi-comp: 0, no-console: 0 */ -import Slider from 'rc-slider'; +import Slider, { UnstableContext } from 'rc-slider'; import React from 'react'; import '../../assets/index.less'; +import type { UnstableContextProps } from '../../src/context'; const style: React.CSSProperties = { width: 400, @@ -11,43 +12,55 @@ const style: React.CSSProperties = { export default () => { const [value, setValue] = React.useState([0, 50, 80]); + const onDragStart: UnstableContextProps['onDragStart'] = (info) => { + const { rawValues } = info; + console.log('Start:', rawValues); + }; + + const onDragChange: UnstableContextProps['onDragChange'] = (info) => { + const { rawValues } = info; + console.log('Move:', rawValues); + }; + return (
- { - console.error('Change:', nextValue); - setValue(nextValue as any); - }} - onChangeComplete={(nextValue) => { - console.log('Complete', nextValue); - }} - // handleRender={(ori, handleProps) => { - // if (handleProps.index === 0) { - // console.log('handleRender', ori, handleProps); - // } - // return ori; - // }} - styles={{ - rail: { - background: `linear-gradient(to right, blue, red)`, - }, - track: { - background: 'orange', - }, - }} - /> + + { + console.error('Change:', nextValue); + setValue(nextValue as any); + }} + onChangeComplete={(nextValue) => { + console.log('Complete', nextValue); + }} + // handleRender={(ori, handleProps) => { + // if (handleProps.index === 0) { + // console.log('handleRender', ori, handleProps); + // } + // return ori; + // }} + styles={{ + rail: { + background: `linear-gradient(to right, blue, red)`, + }, + track: { + background: 'orange', + }, + }} + /> +

Here is a word that drag should not select it

diff --git a/src/context.ts b/src/context.ts index 99b398ca5..0e3269861 100644 --- a/src/context.ts +++ b/src/context.ts @@ -34,3 +34,20 @@ const SliderContext = React.createContext({ }); export default SliderContext; + +export interface UnstableContextProps { + onDragStart?: (info: { + rawValues: number[]; + draggingIndex: number; + draggingValue: number; + }) => void; + onDragChange?: (info: { + rawValues: number[]; + deleteIndex: number; + draggingIndex: number; + draggingValue: number; + }) => void; +} + +/** @private NOT PROMISE AVAILABLE. DO NOT USE IN PRODUCTION. */ +export const UnstableContext = React.createContext({}); diff --git a/src/hooks/useDrag.ts b/src/hooks/useDrag.ts index ac57eea4a..d95e39261 100644 --- a/src/hooks/useDrag.ts +++ b/src/hooks/useDrag.ts @@ -1,5 +1,6 @@ import { useEvent } from 'rc-util'; import * as React from 'react'; +import { UnstableContext } from '../context'; import type { Direction, OnStartMove } from '../interface'; import type { OffsetValues } from './useOffset'; @@ -40,6 +41,8 @@ function useDrag( const mouseMoveEventRef = React.useRef<(event: MouseEvent) => void>(null); const mouseUpEventRef = React.useRef<(event: MouseEvent) => void>(null); + const { onDragStart, onDragChange } = React.useContext(UnstableContext); + React.useLayoutEffect(() => { if (draggingIndex === -1) { setCacheValues(rawValues); @@ -69,6 +72,15 @@ function useDrag( changeValues = nextValues.filter((_, i) => i !== draggingIndex); } triggerChange(changeValues); + + if (onDragChange) { + onDragChange({ + rawValues: nextValues, + deleteIndex: deleteMark ? draggingIndex : -1, + draggingIndex, + draggingValue: nextValue, + }); + } }; const updateCacheValue = useEvent( @@ -123,6 +135,15 @@ function useDrag( // We declare it here since closure can't get outer latest value let deleteMark = false; + // Internal trigger event + if (onDragStart) { + onDragStart({ + rawValues: initialValues, + draggingIndex: valueIndex, + draggingValue: originValue, + }); + } + // Moving const onMouseMove = (event: MouseEvent | TouchEvent) => { event.preventDefault(); diff --git a/src/index.tsx b/src/index.tsx index f3077a53c..70dc6bb9a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,6 @@ import type { SliderProps, SliderRef } from './Slider'; import Slider from './Slider'; +export { UnstableContext } from './context'; export type { SliderProps, SliderRef };