2016-09-21 11:54:53 +08:00
|
|
|
import 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';
|
|
|
|
import Tooltip from '../tooltip';
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface SliderMarks {
|
2016-08-10 09:46:56 +08:00
|
|
|
[key: number]: React.ReactNode | {
|
|
|
|
style: React.CSSProperties,
|
|
|
|
label: React.ReactNode,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export type SliderValue = number | [number, number];
|
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;
|
|
|
|
step?: number | void;
|
|
|
|
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;
|
2016-08-10 09:46:56 +08:00
|
|
|
tipFormatter?: void | ((value: number) => React.ReactNode);
|
2017-09-10 02:32:00 +08:00
|
|
|
className?: string;
|
|
|
|
id?: string;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Slider extends React.Component<SliderProps, any> {
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-slider',
|
2016-09-18 10:12:21 +08:00
|
|
|
tooltipPrefixCls: 'ant-tooltip',
|
2017-02-17 11:59:30 +08:00
|
|
|
tipFormatter(value) {
|
|
|
|
return value.toString();
|
|
|
|
},
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-02-17 11:59:30 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-02-23 17:03:18 +08:00
|
|
|
this.state = {
|
|
|
|
visibles: {},
|
|
|
|
};
|
2017-02-17 11:59:30 +08:00
|
|
|
}
|
|
|
|
|
2017-02-23 17:03:18 +08:00
|
|
|
toggleTooltipVisible = (index, visible) => {
|
|
|
|
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
|
|
|
}));
|
2017-02-17 11:59:30 +08:00
|
|
|
}
|
|
|
|
handleWithTooltip = ({ value, dragging, index, ...restProps }) => {
|
|
|
|
const { tooltipPrefixCls, tipFormatter } = this.props;
|
2017-02-23 17:03:18 +08:00
|
|
|
const { visibles } = this.state;
|
2017-02-17 11:59:30 +08:00
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
prefixCls={tooltipPrefixCls}
|
|
|
|
title={tipFormatter ? tipFormatter(value) : ''}
|
2017-02-23 17:03:18 +08:00
|
|
|
visible={tipFormatter && (visibles[index] || dragging)}
|
2017-02-17 11:59:30 +08:00
|
|
|
placement="top"
|
|
|
|
transitionName="zoom-down"
|
|
|
|
key={index}
|
|
|
|
>
|
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>
|
|
|
|
);
|
|
|
|
}
|
2016-08-10 09:46:56 +08:00
|
|
|
|
2015-07-13 08:00:31 +08:00
|
|
|
render() {
|
2017-02-17 11:59:30 +08:00
|
|
|
const { range, ...restProps } = this.props;
|
|
|
|
if (range) {
|
|
|
|
return <RcRange {...restProps} handle={this.handleWithTooltip} />;
|
|
|
|
}
|
|
|
|
return <RcSlider {...restProps} handle={this.handleWithTooltip} />;
|
2015-07-13 08:00:31 +08:00
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|