2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2020-09-24 18:14:22 +08:00
|
|
|
import RcSlider, { Range as RcRange, Handle as RcHandle } from 'rc-slider';
|
2020-01-02 19:10:16 +08:00
|
|
|
import classNames from 'classnames';
|
2020-01-06 20:09:12 +08:00
|
|
|
import { TooltipPlacement } from '../tooltip';
|
|
|
|
import SliderTooltip from './SliderTooltip';
|
2020-04-25 22:46:21 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface SliderMarks {
|
2018-12-07 20:02:01 +08:00
|
|
|
[key: number]:
|
|
|
|
| React.ReactNode
|
|
|
|
| {
|
|
|
|
style: React.CSSProperties;
|
|
|
|
label: React.ReactNode;
|
|
|
|
};
|
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
|
|
|
|
2020-07-01 23:29:28 +08:00
|
|
|
export interface SliderBaseProps {
|
2017-02-17 11:59:30 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
tooltipPrefixCls?: 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;
|
2017-01-05 12:05:24 +08:00
|
|
|
vertical?: boolean;
|
2020-06-15 17:50:36 +08:00
|
|
|
tipFormatter?: null | ((value?: number) => React.ReactNode);
|
2017-09-10 02:32:00 +08:00
|
|
|
className?: string;
|
|
|
|
id?: string;
|
2018-11-13 22:55:17 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-11-01 11:40:47 +08:00
|
|
|
tooltipVisible?: boolean;
|
2019-05-26 13:11:13 +08:00
|
|
|
tooltipPlacement?: TooltipPlacement;
|
2019-05-26 13:13:12 +08:00
|
|
|
getTooltipPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
2020-10-02 22:16:18 +08:00
|
|
|
autoFocus?: boolean;
|
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;
|
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[];
|
2020-07-01 23:29:28 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 15:59:13 +08:00
|
|
|
interface SliderRange {
|
|
|
|
draggableTrack?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
export type Visibles = { [index: number]: boolean };
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
const Slider = React.forwardRef<unknown, SliderSingleProps | SliderRangeProps>(
|
|
|
|
(props, ref: any) => {
|
|
|
|
const { getPrefixCls, direction, getPopupContainer } = React.useContext(ConfigContext);
|
|
|
|
const [visibles, setVisibles] = React.useState<Visibles>({});
|
2017-12-01 18:01:14 +08:00
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
const toggleTooltipVisible = (index: number, visible: boolean) => {
|
2020-12-09 17:12:32 +08:00
|
|
|
setVisibles((prev: Visibles) => ({ ...prev, [index]: visible }));
|
2020-09-24 18:14:22 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
const getTooltipPlacement = (tooltipPlacement?: TooltipPlacement, vertical?: boolean) => {
|
|
|
|
if (tooltipPlacement) {
|
|
|
|
return tooltipPlacement;
|
|
|
|
}
|
|
|
|
if (!vertical) {
|
|
|
|
return 'top';
|
|
|
|
}
|
|
|
|
return direction === 'rtl' ? 'left' : 'right';
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleWithTooltip: HandleGeneratorFn = ({
|
|
|
|
tooltipPrefixCls,
|
|
|
|
prefixCls,
|
|
|
|
info: { value, dragging, index, ...restProps },
|
|
|
|
}) => {
|
|
|
|
const {
|
|
|
|
tipFormatter,
|
|
|
|
tooltipVisible,
|
|
|
|
tooltipPlacement,
|
|
|
|
getTooltipPopupContainer,
|
|
|
|
vertical,
|
|
|
|
} = props;
|
|
|
|
const isTipFormatter = tipFormatter ? visibles[index] || dragging : false;
|
|
|
|
const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
return (
|
|
|
|
<SliderTooltip
|
|
|
|
prefixCls={tooltipPrefixCls}
|
|
|
|
title={tipFormatter ? tipFormatter(value) : ''}
|
|
|
|
visible={visible}
|
|
|
|
placement={getTooltipPlacement(tooltipPlacement, vertical)}
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName={`${rootPrefixCls}-zoom-down`}
|
2020-09-24 18:14:22 +08:00
|
|
|
key={index}
|
|
|
|
overlayClassName={`${prefixCls}-tooltip`}
|
2021-01-15 10:26:20 +08:00
|
|
|
getPopupContainer={getTooltipPopupContainer || getPopupContainer}
|
2020-09-24 18:14:22 +08:00
|
|
|
>
|
|
|
|
<RcHandle
|
|
|
|
{...restProps}
|
|
|
|
value={value}
|
|
|
|
onMouseEnter={() => toggleTooltipVisible(index, true)}
|
|
|
|
onMouseLeave={() => toggleTooltipVisible(index, false)}
|
|
|
|
/>
|
|
|
|
</SliderTooltip>
|
|
|
|
);
|
|
|
|
};
|
2020-07-30 17:04:13 +08:00
|
|
|
|
2020-03-13 13:52:47 +08:00
|
|
|
const {
|
2020-09-24 18:14:22 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
tooltipPrefixCls: customizeTooltipPrefixCls,
|
|
|
|
range,
|
|
|
|
className,
|
|
|
|
...restProps
|
2020-04-22 15:58:57 +08:00
|
|
|
} = props;
|
2020-09-24 18:14:22 +08:00
|
|
|
const prefixCls = getPrefixCls('slider', customizePrefixCls);
|
|
|
|
const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
|
|
|
|
const cls = classNames(className, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2020-12-29 15:59:13 +08:00
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
// make reverse default on rtl direction
|
|
|
|
if (direction === 'rtl' && !restProps.vertical) {
|
|
|
|
restProps.reverse = !restProps.reverse;
|
|
|
|
}
|
2021-01-30 19:39:02 +08:00
|
|
|
|
2020-12-29 15:59:13 +08:00
|
|
|
// extrack draggableTrack from range={{ ... }}
|
|
|
|
let draggableTrack: boolean | undefined;
|
|
|
|
if (typeof range === 'object') {
|
|
|
|
draggableTrack = range.draggableTrack;
|
|
|
|
}
|
2021-01-30 19:39:02 +08:00
|
|
|
|
2020-09-24 18:14:22 +08:00
|
|
|
if (range) {
|
|
|
|
return (
|
|
|
|
<RcRange
|
|
|
|
{...(restProps as SliderRangeProps)}
|
2020-10-02 22:15:12 +08:00
|
|
|
step={restProps.step!}
|
2020-12-29 15:59:13 +08:00
|
|
|
draggableTrack={draggableTrack}
|
2020-09-24 18:14:22 +08:00
|
|
|
className={cls}
|
|
|
|
ref={ref}
|
|
|
|
handle={(info: HandleGeneratorInfo) =>
|
|
|
|
handleWithTooltip({
|
|
|
|
tooltipPrefixCls,
|
|
|
|
prefixCls,
|
|
|
|
info,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
prefixCls={prefixCls}
|
2017-02-23 17:03:18 +08:00
|
|
|
/>
|
2020-09-24 18:14:22 +08:00
|
|
|
);
|
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
return (
|
2020-09-24 18:14:22 +08:00
|
|
|
<RcSlider
|
|
|
|
{...(restProps as SliderSingleProps)}
|
2020-10-02 22:15:12 +08:00
|
|
|
step={restProps.step!}
|
2020-01-02 19:10:16 +08:00
|
|
|
className={cls}
|
2020-04-22 15:58:57 +08:00
|
|
|
ref={ref}
|
2019-11-30 18:10:48 +08:00
|
|
|
handle={(info: HandleGeneratorInfo) =>
|
2020-04-22 15:58:57 +08:00
|
|
|
handleWithTooltip({
|
2019-11-30 18:10:48 +08:00
|
|
|
tooltipPrefixCls,
|
|
|
|
prefixCls,
|
|
|
|
info,
|
|
|
|
})
|
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
/>
|
|
|
|
);
|
2020-09-24 18:14:22 +08:00
|
|
|
},
|
|
|
|
);
|
2020-04-22 15:58:57 +08:00
|
|
|
|
2020-04-25 22:46:21 +08:00
|
|
|
Slider.displayName = 'Slider';
|
|
|
|
|
2020-04-22 15:58:57 +08:00
|
|
|
Slider.defaultProps = {
|
|
|
|
tipFormatter(value: number) {
|
|
|
|
return typeof value === 'number' ? value.toString() : '';
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Slider;
|