2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-02-17 11:59:30 +08:00
|
|
|
import RcSlider from 'rc-slider/lib/Slider';
|
|
|
|
import RcRange from 'rc-slider/lib/Range';
|
|
|
|
import RcHandle from 'rc-slider/lib/Handle';
|
2019-05-26 13:11:13 +08:00
|
|
|
import Tooltip, { TooltipPlacement } from '../tooltip';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } 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
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export type SliderValue = number | [number, number];
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
interface HandleGeneratorInfo {
|
|
|
|
value: number;
|
|
|
|
dragging: boolean;
|
|
|
|
index: number;
|
|
|
|
rest: any[];
|
2018-12-07 20:02:01 +08:00
|
|
|
}
|
|
|
|
export type HandleGeneratorFn = (
|
|
|
|
tooltipPrefixCls: string,
|
|
|
|
info: HandleGeneratorInfo,
|
|
|
|
) => React.ReactElement<any>;
|
2017-11-21 14:53:25 +08:00
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export interface SliderProps {
|
2017-02-17 11:59:30 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
tooltipPrefixCls?: string;
|
2016-08-10 09:46:56 +08:00
|
|
|
range?: boolean;
|
|
|
|
min?: number;
|
|
|
|
max?: number;
|
2017-11-03 09:40:56 +08:00
|
|
|
step?: number | null;
|
2016-08-10 09:46:56 +08:00
|
|
|
marks?: SliderMarks;
|
|
|
|
dots?: boolean;
|
|
|
|
value?: SliderValue;
|
|
|
|
defaultValue?: SliderValue;
|
|
|
|
included?: boolean;
|
|
|
|
disabled?: boolean;
|
2017-01-05 12:05:24 +08:00
|
|
|
vertical?: boolean;
|
2016-09-09 13:55:46 +08:00
|
|
|
onChange?: (value: SliderValue) => void;
|
|
|
|
onAfterChange?: (value: SliderValue) => void;
|
2017-11-03 09:40:56 +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;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
export interface SliderState {
|
|
|
|
visibles: { [index: number]: boolean };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Slider extends React.Component<SliderProps, SliderState> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
2017-11-21 14:53:25 +08:00
|
|
|
tipFormatter(value: number) {
|
2017-02-17 11:59:30 +08:00
|
|
|
return value.toString();
|
|
|
|
},
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2019-05-26 13:13:12 +08:00
|
|
|
rcSlider: typeof RcSlider;
|
2017-12-01 18:01:14 +08:00
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
constructor(props: SliderProps) {
|
2017-02-17 11:59:30 +08:00
|
|
|
super(props);
|
2017-02-23 17:03:18 +08:00
|
|
|
this.state = {
|
|
|
|
visibles: {},
|
|
|
|
};
|
2017-02-17 11:59:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
toggleTooltipVisible = (index: number, visible: boolean) => {
|
2017-02-23 17:03:18 +08:00
|
|
|
this.setState(({ visibles }) => ({
|
2017-02-17 11:59:30 +08:00
|
|
|
visibles: {
|
2017-02-23 17:03:18 +08:00
|
|
|
...visibles,
|
2017-02-17 11:59:30 +08:00
|
|
|
[index]: visible,
|
|
|
|
},
|
2017-02-23 17:03:18 +08:00
|
|
|
}));
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
|
|
|
handleWithTooltip: HandleGeneratorFn = (
|
|
|
|
tooltipPrefixCls: string,
|
|
|
|
{ value, dragging, index, ...restProps },
|
|
|
|
) => {
|
2019-05-26 13:18:51 +08:00
|
|
|
const { tipFormatter, tooltipVisible, tooltipPlacement, getTooltipPopupContainer } = this.props;
|
2017-02-23 17:03:18 +08:00
|
|
|
const { visibles } = this.state;
|
2018-12-07 20:02:01 +08:00
|
|
|
const isTipFormatter = tipFormatter ? visibles[index] || dragging : false;
|
2018-12-12 03:50:09 +08:00
|
|
|
const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter);
|
2017-02-17 11:59:30 +08:00
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
prefixCls={tooltipPrefixCls}
|
|
|
|
title={tipFormatter ? tipFormatter(value) : ''}
|
2017-11-21 14:53:25 +08:00
|
|
|
visible={visible}
|
2019-05-26 13:11:13 +08:00
|
|
|
placement={tooltipPlacement || 'top'}
|
2017-02-17 11:59:30 +08:00
|
|
|
transitionName="zoom-down"
|
|
|
|
key={index}
|
2019-05-26 13:13:12 +08:00
|
|
|
getPopupContainer={
|
|
|
|
getTooltipPopupContainer ? getTooltipPopupContainer : () => document.body
|
|
|
|
}
|
2017-02-17 11:59:30 +08:00
|
|
|
>
|
2017-02-23 17:03:18 +08:00
|
|
|
<RcHandle
|
|
|
|
{...restProps}
|
2017-06-01 12:08:39 +08:00
|
|
|
value={value}
|
2017-02-23 17:03:18 +08:00
|
|
|
onMouseEnter={() => this.toggleTooltipVisible(index, true)}
|
|
|
|
onMouseLeave={() => this.toggleTooltipVisible(index, false)}
|
|
|
|
/>
|
2017-02-17 11:59:30 +08:00
|
|
|
</Tooltip>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2017-12-01 18:01:14 +08:00
|
|
|
focus() {
|
|
|
|
this.rcSlider.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
2018-12-06 21:36:43 +08:00
|
|
|
this.rcSlider.blur();
|
2017-12-01 18:01:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
saveSlider = (node: any) => {
|
|
|
|
this.rcSlider = node;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-12-01 18:01:14 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderSlider = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const {
|
2018-12-07 20:02:01 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
tooltipPrefixCls: customizeTooltipPrefixCls,
|
|
|
|
range,
|
|
|
|
...restProps
|
2018-12-05 19:12:18 +08:00
|
|
|
} = this.props;
|
|
|
|
const prefixCls = getPrefixCls('slider', customizePrefixCls);
|
|
|
|
const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
|
2017-02-17 11:59:30 +08:00
|
|
|
if (range) {
|
2018-12-05 19:12:18 +08:00
|
|
|
return (
|
|
|
|
<RcRange
|
|
|
|
{...restProps}
|
|
|
|
ref={this.saveSlider}
|
2018-12-07 20:02:01 +08:00
|
|
|
handle={(info: HandleGeneratorInfo) => this.handleWithTooltip(tooltipPrefixCls, info)}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
tooltipPrefixCls={tooltipPrefixCls}
|
|
|
|
/>
|
|
|
|
);
|
2017-02-17 11:59:30 +08:00
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
return (
|
|
|
|
<RcSlider
|
|
|
|
{...restProps}
|
|
|
|
ref={this.saveSlider}
|
2018-12-07 20:02:01 +08:00
|
|
|
handle={(info: HandleGeneratorInfo) => this.handleWithTooltip(tooltipPrefixCls, info)}
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
tooltipPrefixCls={tooltipPrefixCls}
|
|
|
|
/>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderSlider}</ConfigConsumer>;
|
2015-07-13 08:00:31 +08:00
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|