ant-design/components/slider/index.tsx

125 lines
3.0 KiB
TypeScript
Raw Normal View History

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';
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 type HandleGeneratorFn = (info: {
value: number,
dragging: boolean,
index: number,
rest: any[],
}) => React.ReactElement<any>;
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 | null;
2016-08-10 09:46:56 +08:00
marks?: SliderMarks;
dots?: boolean;
value?: SliderValue;
defaultValue?: SliderValue;
included?: boolean;
disabled?: boolean;
vertical?: boolean;
onChange?: (value: SliderValue) => void;
onAfterChange?: (value: SliderValue) => void;
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;
tooltipVisible?: boolean;
2016-08-10 09:46:56 +08:00
}
export interface SliderState {
visibles: { [index: number]: boolean };
}
export default class Slider extends React.Component<SliderProps, SliderState> {
static defaultProps = {
prefixCls: 'ant-slider',
tooltipPrefixCls: 'ant-tooltip',
tipFormatter(value: number) {
2017-02-17 11:59:30 +08:00
return value.toString();
},
2016-07-13 11:14:24 +08:00
};
2017-12-01 18:01:14 +08:00
private rcSlider: any;
constructor(props: SliderProps) {
2017-02-17 11:59:30 +08:00
super(props);
this.state = {
visibles: {},
};
2017-02-17 11:59:30 +08:00
}
toggleTooltipVisible = (index: number, visible: boolean) => {
this.setState(({ visibles }) => ({
2017-02-17 11:59:30 +08:00
visibles: {
...visibles,
2017-02-17 11:59:30 +08:00
[index]: visible,
},
}));
2017-02-17 11:59:30 +08:00
}
handleWithTooltip: HandleGeneratorFn = ({ value, dragging, index, ...restProps }) => {
const { tooltipPrefixCls, tipFormatter, tooltipVisible } = this.props;
const { visibles } = this.state;
const isTipFormatter = tipFormatter ? (visibles[index] || dragging) : false;
let visible;
if (tooltipVisible) {
visible = tooltipVisible || isTipFormatter;
} else if (tooltipVisible === undefined) {
visible = isTipFormatter;
}
2017-02-17 11:59:30 +08:00
return (
<Tooltip
prefixCls={tooltipPrefixCls}
title={tipFormatter ? tipFormatter(value) : ''}
visible={visible}
2017-02-17 11:59:30 +08:00
placement="top"
transitionName="zoom-down"
key={index}
>
<RcHandle
{...restProps}
2017-06-01 12:08:39 +08:00
value={value}
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
2017-12-01 18:01:14 +08:00
focus() {
this.rcSlider.focus();
}
blur() {
this.rcSlider.focus();
}
saveSlider = (node: any) => {
this.rcSlider = node;
}
2015-07-13 08:00:31 +08:00
render() {
2017-02-17 11:59:30 +08:00
const { range, ...restProps } = this.props;
if (range) {
2017-12-01 18:01:14 +08:00
return <RcRange {...restProps} ref={this.saveSlider} handle={this.handleWithTooltip} />;
2017-02-17 11:59:30 +08:00
}
2017-12-01 18:01:14 +08:00
return <RcSlider {...restProps} ref={this.saveSlider} handle={this.handleWithTooltip} />;
2015-07-13 08:00:31 +08:00
}
}