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

chore: add internal hooks #1014

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
83 changes: 48 additions & 35 deletions docs/examples/editable.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 (
<div>
<div style={style}>
<Slider
// range
range={{
editable: true,
minCount: 1,
maxCount: 4,
}}
// track={false}
min={0}
max={100}
value={value}
// defaultValue={null}
onChange={(nextValue) => {
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',
},
}}
/>
<UnstableContext.Provider value={{ onDragStart, onDragChange }}>
<Slider
// range
range={{
editable: true,
minCount: 1,
maxCount: 4,
}}
// track={false}
min={0}
max={100}
value={value}
// defaultValue={null}
onChange={(nextValue) => {
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',
},
}}
/>
</UnstableContext.Provider>
</div>

<p>Here is a word that drag should not select it</p>
Expand Down
17 changes: 17 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ const SliderContext = React.createContext<SliderContextProps>({
});

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<UnstableContextProps>({});
21 changes: 21 additions & 0 deletions src/hooks/useDrag.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SliderProps, SliderRef } from './Slider';
import Slider from './Slider';
export { UnstableContext } from './context';

export type { SliderProps, SliderRef };

Expand Down
Loading