ant-design/components/slider/index.tsx

269 lines
7.9 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classNames from 'classnames';
import type { SliderProps as RcSliderProps } from 'rc-slider';
import RcSlider from 'rc-slider';
import type { SliderRef } from 'rc-slider/lib/Slider';
import { devUseWarning } from '../_util/warning';
import { ConfigContext } from '../config-provider';
import DisabledContext from '../config-provider/DisabledContext';
import type { AbstractTooltipProps, TooltipPlacement } from '../tooltip';
import SliderTooltip from './SliderTooltip';
import useStyle from './style';
2016-08-10 09:46:56 +08:00
export type SliderMarks = RcSliderProps['marks'];
2016-08-10 09:46:56 +08:00
interface HandleGeneratorInfo {
2020-06-15 17:50:36 +08:00
value?: number;
dragging?: boolean;
index: number;
2018-12-07 20:02:01 +08:00
}
export type HandleGeneratorFn = (config: {
tooltipPrefixCls?: string;
prefixCls?: string;
info: HandleGeneratorInfo;
}) => React.ReactElement;
export type Formatter = ((value?: number) => React.ReactNode) | null;
export interface SliderTooltipProps extends AbstractTooltipProps {
prefixCls?: string;
open?: boolean;
placement?: TooltipPlacement;
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
formatter?: Formatter;
autoAdjustOverflow?: boolean;
}
export interface SliderBaseProps {
2017-02-17 11:59:30 +08:00
prefixCls?: string;
reverse?: boolean;
2016-08-10 09:46:56 +08:00
min?: number;
max?: number;
step?: null | number;
2016-08-10 09:46:56 +08:00
marks?: SliderMarks;
dots?: boolean;
included?: boolean;
disabled?: boolean;
keyboard?: boolean;
vertical?: boolean;
2017-09-10 02:32:00 +08:00
className?: string;
rootClassName?: string;
2017-09-10 02:32:00 +08:00
id?: string;
2018-11-13 22:55:17 +08:00
style?: React.CSSProperties;
tooltip?: SliderTooltipProps;
autoFocus?: boolean;
styles?: RcSliderProps['styles'];
classNames?: RcSliderProps['classNames'];
// Deprecated
/** @deprecated `tooltipPrefixCls` is deprecated. Please use `tooltip.prefixCls` instead. */
tooltipPrefixCls?: string;
/** @deprecated `tipFormatter` is deprecated. Please use `tooltip.formatter` instead. */
tipFormatter?: Formatter;
/** @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
}
export interface SliderSingleProps extends SliderBaseProps {
range?: false;
value?: number;
defaultValue?: number;
onChange?: (value: number) => void;
onAfterChange?: (value: number) => void;
/** @deprecated Please use `styles.handle` instead */
handleStyle?: React.CSSProperties;
/** @deprecated Please use `styles.track` instead */
trackStyle?: React.CSSProperties;
/** @deprecated Please use `styles.rail` instead */
railStyle?: React.CSSProperties;
}
export interface SliderRangeProps extends SliderBaseProps {
range: true | SliderRange;
value?: number[];
defaultValue?: number[];
onChange?: (value: number[]) => void;
onAfterChange?: (value: number[]) => void;
/** @deprecated Please use `styles.handle` instead */
handleStyle?: React.CSSProperties[];
/** @deprecated Please use `styles.track` instead */
trackStyle?: React.CSSProperties[];
/** @deprecated Please use `styles.rail` instead */
railStyle?: React.CSSProperties;
}
interface SliderRange {
draggableTrack?: boolean;
}
export type Opens = { [index: number]: boolean };
function getTipFormatter(tipFormatter?: Formatter, legacyTipFormatter?: Formatter) {
if (tipFormatter || tipFormatter === null) {
return tipFormatter;
}
if (legacyTipFormatter || legacyTipFormatter === null) {
return legacyTipFormatter;
}
return (val?: number) => (typeof val === 'number' ? val.toString() : '');
}
const Slider = React.forwardRef<SliderRef, SliderSingleProps | SliderRangeProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
range,
className,
rootClassName,
style,
disabled,
// Deprecated Props
tooltipPrefixCls: legacyTooltipPrefixCls,
tipFormatter: legacyTipFormatter,
tooltipVisible: legacyTooltipVisible,
getTooltipPopupContainer: legacyGetTooltipPopupContainer,
tooltipPlacement: legacyTooltipPlacement,
...restProps
} = props;
const { direction, slider, getPrefixCls, getPopupContainer } = React.useContext(ConfigContext);
const contextDisabled = React.useContext(DisabledContext);
const mergedDisabled = disabled ?? contextDisabled;
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,
slider?.className,
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];
}
return typeof range === 'object' ? [true, range.draggableTrack] : [true, false];
}, [range]);
// Warning for deprecated usage
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Slider');
[
['tooltipPrefixCls', 'prefixCls'],
['getTooltipPopupContainer', 'getPopupContainer'],
['tipFormatter', 'formatter'],
['tooltipPlacement', 'placement'],
['tooltipVisible', 'open'],
].forEach(([deprecatedName, newName]) => {
warning.deprecated(!(deprecatedName in props), deprecatedName, `tooltip.${newName}`);
});
}
const handleRender: RcSliderProps['handleRender'] = (node, info) => {
const { index, dragging } = info;
const { tooltip = {}, vertical } = props;
const tooltipProps: SliderTooltipProps = {
...tooltip,
};
const {
open: tooltipOpen,
placement: tooltipPlacement,
getPopupContainer: getTooltipPopupContainer,
prefixCls: customizeTooltipPrefixCls,
formatter: tipFormatter,
} = tooltipProps;
const mergedTipFormatter = getTipFormatter(tipFormatter, legacyTipFormatter);
const isTipFormatter = mergedTipFormatter ? opens[index] || dragging : false;
const open =
tooltipOpen ?? legacyTooltipVisible ?? (tooltipOpen === undefined && isTipFormatter);
const onOpenChange = (newOpen: boolean) => {
toggleTooltipOpen(index, newOpen);
};
return (
<SliderTooltip
trigger={['hover', 'focus']}
{...tooltipProps}
prefixCls={getPrefixCls('tooltip', customizeTooltipPrefixCls ?? legacyTooltipPrefixCls)}
title={mergedTipFormatter ? mergedTipFormatter(info.value) : ''}
open={open}
onOpenChange={onOpenChange}
placement={getTooltipPlacement(tooltipPlacement ?? legacyTooltipPlacement, vertical)}
key={index}
overlayClassName={`${prefixCls}-tooltip`}
getPopupContainer={
getTooltipPopupContainer || legacyGetTooltipPopupContainer || getPopupContainer
}
>
{node}
</SliderTooltip>
);
};
const mergedStyle: React.CSSProperties = { ...slider?.style, ...style };
return wrapSSR(
<RcSlider
{...restProps}
step={restProps.step}
range={mergedRange}
draggableTrack={draggableTrack}
className={cls}
style={mergedStyle}
disabled={mergedDisabled}
ref={ref}
prefixCls={prefixCls}
handleRender={handleRender}
/>,
);
});
if (process.env.NODE_ENV !== 'production') {
Slider.displayName = 'Slider';
}
export default Slider;