2023-07-15 12:57:03 +08:00
|
|
|
'use client';
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SliderProps as RcSliderProps } from 'rc-slider';
|
|
|
|
import RcSlider from 'rc-slider';
|
2023-05-17 10:06:44 +08:00
|
|
|
import type { SliderRef } from 'rc-slider/lib/Slider';
|
|
|
|
import React from 'react';
|
|
|
|
import warning from '../_util/warning';
|
2022-06-21 10:24:52 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-06-24 14:20:56 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { TooltipPlacement } from '../tooltip';
|
2020-01-06 20:09:12 +08:00
|
|
|
import SliderTooltip from './SliderTooltip';
|
2022-03-14 19:12:10 +08:00
|
|
|
import useStyle from './style';
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2022-03-01 14:48:41 +08:00
|
|
|
export type SliderMarks = RcSliderProps['marks'];
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
interface HandleGeneratorInfo {
|
2020-06-15 17:50:36 +08:00
|
|
|
value?: number;
|
2020-09-24 18:14:22 +08:00
|
|
|
dragging?: boolean;
|
2018-12-05 19:12:18 +08:00
|
|
|
index: number;
|
2018-12-07 20:02:01 +08:00
|
|
|
}
|
2019-11-30 18:10:48 +08:00
|
|
|
|
|
|
|
export type HandleGeneratorFn = (config: {
|
|
|
|
tooltipPrefixCls?: string;
|
|
|
|
prefixCls?: string;
|
|
|
|
info: HandleGeneratorInfo;
|
2020-09-24 18:14:22 +08:00
|
|
|
}) => React.ReactElement;
|
2017-11-21 14:53:25 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
export type Formatter = (value?: number) => React.ReactNode;
|
2022-11-18 23:34:07 +08:00
|
|
|
const defaultFormatter: Formatter = (val) => (typeof val === 'number' ? val.toString() : '');
|
2022-09-06 21:46:49 +08:00
|
|
|
|
2022-08-17 15:47:22 +08:00
|
|
|
export interface SliderTooltipProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
open?: boolean;
|
|
|
|
placement?: TooltipPlacement;
|
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
2022-09-06 21:46:49 +08:00
|
|
|
formatter?: null | Formatter;
|
2022-08-17 15:47:22 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:29:28 +08:00
|
|
|
export interface SliderBaseProps {
|
2017-02-17 11:59:30 +08:00
|
|
|
prefixCls?: string;
|
2019-11-13 14:34:10 +08:00
|
|
|
reverse?: boolean;
|
2016-08-10 09:46:56 +08:00
|
|
|
min?: number;
|
|
|
|
max?: number;
|
2020-10-02 22:15:12 +08:00
|
|
|
step?: null | number;
|
2016-08-10 09:46:56 +08:00
|
|
|
marks?: SliderMarks;
|
|
|
|
dots?: boolean;
|
|
|
|
included?: boolean;
|
|
|
|
disabled?: boolean;
|
2023-02-03 15:16:27 +08:00
|
|
|
keyboard?: boolean;
|
2017-01-05 12:05:24 +08:00
|
|
|
vertical?: boolean;
|
2017-09-10 02:32:00 +08:00
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2017-09-10 02:32:00 +08:00
|
|
|
id?: string;
|
2018-11-13 22:55:17 +08:00
|
|
|
style?: React.CSSProperties;
|
2022-08-17 15:47:22 +08:00
|
|
|
tooltip?: SliderTooltipProps;
|
2020-10-02 22:16:18 +08:00
|
|
|
autoFocus?: boolean;
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
/** @deprecated `tooltipPrefixCls` is deprecated. Please use `tooltip.prefixCls` instead. */
|
|
|
|
tooltipPrefixCls?: string;
|
|
|
|
/** @deprecated `tipFormatter` is deprecated. Please use `tooltip.formatter` instead. */
|
|
|
|
tipFormatter?: null | ((value?: number) => React.ReactNode);
|
|
|
|
/** @deprecated `tooltipVisible` is deprecated. Please use `tooltip.open` instead. */
|
|
|
|
tooltipVisible?: boolean;
|
|
|
|
/**
|
|
|
|
* @deprecated `getTooltipPopupContainer` is deprecated. Please use `tooltip.getPopupContainer`
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
getTooltipPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
|
|
|
/** @deprecated `tooltipPlacement` is deprecated. Please use `tooltip.placement` instead. */
|
|
|
|
tooltipPlacement?: TooltipPlacement;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:29:28 +08:00
|
|
|
export interface SliderSingleProps extends SliderBaseProps {
|
|
|
|
range?: false;
|
|
|
|
value?: number;
|
|
|
|
defaultValue?: number;
|
|
|
|
onChange?: (value: number) => void;
|
|
|
|
onAfterChange?: (value: number) => void;
|
2020-10-09 09:37:26 +08:00
|
|
|
handleStyle?: React.CSSProperties;
|
|
|
|
trackStyle?: React.CSSProperties;
|
2023-02-07 09:55:20 +08:00
|
|
|
railStyle?: React.CSSProperties;
|
2020-07-01 23:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SliderRangeProps extends SliderBaseProps {
|
2020-12-29 15:59:13 +08:00
|
|
|
range: true | SliderRange;
|
2020-07-01 23:29:28 +08:00
|
|
|
value?: [number, number];
|
|
|
|
defaultValue?: [number, number];
|
|
|
|
onChange?: (value: [number, number]) => void;
|
|
|
|
onAfterChange?: (value: [number, number]) => void;
|
2020-10-09 09:37:26 +08:00
|
|
|
handleStyle?: React.CSSProperties[];
|
|
|
|
trackStyle?: React.CSSProperties[];
|
2023-02-07 09:55:20 +08:00
|
|
|
railStyle?: React.CSSProperties;
|
2020-07-01 23:29:28 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 15:59:13 +08:00
|
|
|
interface SliderRange {
|
|
|
|
draggableTrack?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:47:22 +08:00
|
|
|
export type Opens = { [index: number]: boolean };
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const Slider = React.forwardRef<SliderRef, SliderSingleProps | SliderRangeProps>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
range,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
2023-06-25 17:28:00 +08:00
|
|
|
style,
|
2023-06-24 14:20:56 +08:00
|
|
|
disabled,
|
2023-05-17 10:06:44 +08:00
|
|
|
// Deprecated Props
|
|
|
|
tooltipPrefixCls: legacyTooltipPrefixCls,
|
|
|
|
tipFormatter: legacyTipFormatter,
|
|
|
|
tooltipVisible: legacyTooltipVisible,
|
|
|
|
getTooltipPopupContainer: legacyGetTooltipPopupContainer,
|
|
|
|
tooltipPlacement: legacyTooltipPlacement,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2023-06-25 17:28:00 +08:00
|
|
|
const { direction, slider, getPrefixCls, getPopupContainer } = React.useContext(ConfigContext);
|
2023-06-24 14:20:56 +08:00
|
|
|
const contextDisabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = disabled ?? contextDisabled;
|
2023-05-17 10:06:44 +08:00
|
|
|
const [opens, setOpens] = React.useState<Opens>({});
|
|
|
|
|
|
|
|
const toggleTooltipOpen = (index: number, open: boolean) => {
|
|
|
|
setOpens((prev: Opens) => ({ ...prev, [index]: open }));
|
|
|
|
};
|
|
|
|
|
|
|
|
const getTooltipPlacement = (placement?: TooltipPlacement, vertical?: boolean) => {
|
|
|
|
if (placement) {
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
if (!vertical) {
|
|
|
|
return 'top';
|
|
|
|
}
|
|
|
|
return direction === 'rtl' ? 'left' : 'right';
|
|
|
|
};
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('slider', customizePrefixCls);
|
|
|
|
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
|
|
|
const cls = classNames(
|
|
|
|
className,
|
2023-06-25 17:28:00 +08:00
|
|
|
slider?.className,
|
2023-05-17 10:06:44 +08:00
|
|
|
rootClassName,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
);
|
|
|
|
|
|
|
|
// make reverse default on rtl direction
|
|
|
|
if (direction === 'rtl' && !restProps.vertical) {
|
|
|
|
restProps.reverse = !restProps.reverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range config
|
|
|
|
const [mergedRange, draggableTrack] = React.useMemo(() => {
|
|
|
|
if (!range) {
|
|
|
|
return [false];
|
|
|
|
}
|
2022-03-14 19:12:10 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
return typeof range === 'object' ? [true, range.draggableTrack] : [true, false];
|
|
|
|
}, [range]);
|
|
|
|
|
|
|
|
// Warning for deprecated usage
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
[
|
|
|
|
['tooltipPrefixCls', 'prefixCls'],
|
|
|
|
['getTooltipPopupContainer', 'getPopupContainer'],
|
|
|
|
['tipFormatter', 'formatter'],
|
|
|
|
['tooltipPlacement', 'placement'],
|
|
|
|
['tooltipVisible', 'open'],
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
|
|
|
warning(
|
|
|
|
!(deprecatedName in props),
|
|
|
|
'Slider',
|
|
|
|
`\`${deprecatedName}\` is deprecated, please use \`tooltip.${newName}\` instead.`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2022-03-14 19:12:10 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const handleRender: RcSliderProps['handleRender'] = (node, info) => {
|
|
|
|
const { index, dragging } = info;
|
2020-12-29 15:59:13 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const { tooltip = {}, vertical } = props;
|
2021-01-30 19:39:02 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const tooltipProps: SliderTooltipProps = {
|
|
|
|
...tooltip,
|
|
|
|
};
|
|
|
|
const {
|
|
|
|
open: tooltipOpen,
|
|
|
|
placement: tooltipPlacement,
|
|
|
|
getPopupContainer: getTooltipPopupContainer,
|
|
|
|
prefixCls: customizeTooltipPrefixCls,
|
|
|
|
formatter: tipFormatter,
|
|
|
|
} = tooltipProps;
|
|
|
|
|
|
|
|
let mergedTipFormatter;
|
|
|
|
if (tipFormatter || tipFormatter === null) {
|
|
|
|
mergedTipFormatter = tipFormatter;
|
|
|
|
} else if (legacyTipFormatter || legacyTipFormatter === null) {
|
|
|
|
mergedTipFormatter = legacyTipFormatter;
|
|
|
|
} else {
|
|
|
|
mergedTipFormatter = defaultFormatter;
|
2022-08-17 15:47:22 +08:00
|
|
|
}
|
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const isTipFormatter = mergedTipFormatter ? opens[index] || dragging : false;
|
|
|
|
const open =
|
|
|
|
tooltipOpen ?? legacyTooltipVisible ?? (tooltipOpen === undefined && isTipFormatter);
|
2022-08-17 15:47:22 +08:00
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const passedProps = {
|
|
|
|
...node.props,
|
|
|
|
onMouseEnter: () => toggleTooltipOpen(index, true),
|
|
|
|
onMouseLeave: () => toggleTooltipOpen(index, false),
|
2022-03-01 14:48:41 +08:00
|
|
|
};
|
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
const tooltipPrefixCls = getPrefixCls(
|
|
|
|
'tooltip',
|
|
|
|
customizeTooltipPrefixCls ?? legacyTooltipPrefixCls,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SliderTooltip
|
|
|
|
prefixCls={tooltipPrefixCls}
|
|
|
|
title={mergedTipFormatter ? mergedTipFormatter(info.value) : ''}
|
|
|
|
open={open}
|
|
|
|
placement={getTooltipPlacement(tooltipPlacement ?? legacyTooltipPlacement, vertical)}
|
|
|
|
key={index}
|
|
|
|
overlayClassName={`${prefixCls}-tooltip`}
|
|
|
|
getPopupContainer={
|
|
|
|
getTooltipPopupContainer || legacyGetTooltipPopupContainer || getPopupContainer
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{React.cloneElement(node, passedProps)}
|
|
|
|
</SliderTooltip>
|
2018-12-05 19:12:18 +08:00
|
|
|
);
|
2023-05-17 10:06:44 +08:00
|
|
|
};
|
|
|
|
|
2023-06-25 17:28:00 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...slider?.style, ...style };
|
|
|
|
|
2023-05-17 10:06:44 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<RcSlider
|
|
|
|
{...restProps}
|
|
|
|
step={restProps.step}
|
|
|
|
range={mergedRange}
|
|
|
|
draggableTrack={draggableTrack}
|
|
|
|
className={cls}
|
2023-06-25 17:28:00 +08:00
|
|
|
style={mergedStyle}
|
2023-06-24 14:20:56 +08:00
|
|
|
disabled={mergedDisabled}
|
2023-05-17 10:06:44 +08:00
|
|
|
ref={ref}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
handleRender={handleRender}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
});
|
2020-04-22 15:58:57 +08:00
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Slider.displayName = 'Slider';
|
|
|
|
}
|
2020-04-25 22:46:21 +08:00
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
export default Slider;
|