ant-design-vue/components/slider/index.jsx

154 lines
4.2 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import { getOptionProps } from '../_util/props-util';
import VcSlider from '../vc-slider/src/Slider';
import VcRange from '../vc-slider/src/Range';
import VcHandle from '../vc-slider/src/Handle';
import Tooltip from '../tooltip';
import Base from '../base';
2019-09-18 12:03:13 +00:00
import { ConfigConsumerProps } from '../config-provider';
2018-04-01 11:23:56 +00:00
// export interface SliderMarks {
// [key]: React.ReactNode | {
// style: React.CSSProperties,
// label: React.ReactNode,
// };
// }
// const SliderMarks = PropTypes.shape({
// style: PropTypes.object,
// label: PropTypes.any,
// }).loose
export const SliderProps = () => ({
prefixCls: PropTypes.string,
tooltipPrefixCls: PropTypes.string,
range: PropTypes.bool,
min: PropTypes.number,
max: PropTypes.number,
step: PropTypes.oneOfType([PropTypes.number, PropTypes.any]),
marks: PropTypes.object,
dots: PropTypes.bool,
2019-01-12 03:33:27 +00:00
value: PropTypes.oneOfType([PropTypes.number, PropTypes.arrayOf(PropTypes.number)]),
defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.arrayOf(PropTypes.number)]),
2018-04-01 11:23:56 +00:00
included: PropTypes.bool,
disabled: PropTypes.bool,
vertical: PropTypes.bool,
2019-01-12 03:33:27 +00:00
tipFormatter: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
tooltipVisible: PropTypes.bool,
2019-01-12 03:33:27 +00:00
});
2018-04-01 11:23:56 +00:00
const Slider = {
2018-04-08 13:17:20 +00:00
name: 'ASlider',
2018-04-01 11:23:56 +00:00
model: {
prop: 'value',
event: 'change',
},
mixins: [BaseMixin],
2019-09-18 12:03:13 +00:00
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
2018-04-01 11:23:56 +00:00
props: {
...SliderProps(),
2019-01-12 03:33:27 +00:00
tipFormatter: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).def(value =>
value.toString(),
),
2018-04-01 11:23:56 +00:00
},
2019-01-12 03:33:27 +00:00
data() {
2018-04-01 11:23:56 +00:00
return {
visibles: {},
2019-01-12 03:33:27 +00:00
};
2018-04-01 11:23:56 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
toggleTooltipVisible(index, visible) {
2018-04-01 11:23:56 +00:00
this.setState(({ visibles }) => ({
visibles: {
...visibles,
[index]: visible,
},
2019-01-12 03:33:27 +00:00
}));
2018-04-01 11:23:56 +00:00
},
2019-09-18 12:03:13 +00:00
handleWithTooltip(tooltipPrefixCls, { value, dragging, index, directives, on, ...restProps }) {
const { tipFormatter, tooltipVisible } = this.$props;
2019-01-12 03:33:27 +00:00
const { visibles } = this;
const isTipFormatter = tipFormatter ? visibles[index] || dragging : false;
const visible = tooltipVisible || (tooltipVisible === undefined && isTipFormatter);
2018-04-01 11:23:56 +00:00
const tooltipProps = {
props: {
prefixCls: tooltipPrefixCls,
title: tipFormatter ? tipFormatter(value) : '',
visible,
placement: 'top',
2018-04-14 09:27:50 +00:00
transitionName: 'fade',
2018-04-01 11:23:56 +00:00
},
key: index,
2019-01-12 03:33:27 +00:00
};
2018-04-01 11:23:56 +00:00
const handleProps = {
props: {
value,
...restProps,
},
directives,
2018-04-01 11:23:56 +00:00
on: {
...on,
2018-04-01 11:23:56 +00:00
mouseenter: () => this.toggleTooltipVisible(index, true),
mouseleave: () => this.toggleTooltipVisible(index, false),
},
2019-01-12 03:33:27 +00:00
};
2018-04-01 11:23:56 +00:00
return (
2019-01-12 03:33:27 +00:00
<Tooltip {...tooltipProps}>
<VcHandle {...handleProps} />
2018-04-01 11:23:56 +00:00
</Tooltip>
2019-01-12 03:33:27 +00:00
);
2018-04-01 11:23:56 +00:00
},
2019-01-12 03:33:27 +00:00
focus() {
this.$refs.sliderRef.focus();
2018-04-01 11:23:56 +00:00
},
2019-01-12 03:33:27 +00:00
blur() {
this.$refs.sliderRef.blur();
2018-04-01 11:23:56 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
2019-09-18 12:03:13 +00:00
const {
range,
prefixCls: customizePrefixCls,
tooltipPrefixCls: customizeTooltipPrefixCls,
...restProps
} = getOptionProps(this);
const getPrefixCls = this.configProvider.getPrefixCls;
2019-09-20 11:19:59 +00:00
const prefixCls = getPrefixCls('slider', customizePrefixCls);
const tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);
2018-04-01 11:23:56 +00:00
if (range) {
const vcRangeProps = {
props: {
...restProps,
2019-09-18 12:03:13 +00:00
prefixCls,
tooltipPrefixCls,
handle: info => this.handleWithTooltip(tooltipPrefixCls, info),
2018-04-01 11:23:56 +00:00
},
ref: 'sliderRef',
on: this.$listeners,
2019-01-12 03:33:27 +00:00
};
return <VcRange {...vcRangeProps} />;
2018-04-01 11:23:56 +00:00
}
const vcSliderProps = {
props: {
...restProps,
2019-09-18 12:03:13 +00:00
prefixCls,
tooltipPrefixCls,
handle: info => this.handleWithTooltip(tooltipPrefixCls, info),
2018-04-01 11:23:56 +00:00
},
ref: 'sliderRef',
on: this.$listeners,
2019-01-12 03:33:27 +00:00
};
return <VcSlider {...vcSliderProps} />;
2018-04-01 11:23:56 +00:00
},
2019-01-12 03:33:27 +00:00
};
/* istanbul ignore next */
2019-01-12 03:33:27 +00:00
Slider.install = function(Vue) {
Vue.use(Base);
2019-01-12 03:33:27 +00:00
Vue.component(Slider.name, Slider);
};
2019-01-12 03:33:27 +00:00
export default Slider;