ant-design-vue/components/vc-slider/demo/handle.jsx

117 lines
2.7 KiB
React
Raw Normal View History

2018-03-26 02:08:36 +00:00
import Slider from '../index'
import Tooltip from '../../vc-tooltip'
import '../assets/index.less'
import '../../vc-tooltip/assets/bootstrap.less'
2018-03-28 03:51:27 +00:00
const { Handle, Range } = Slider
2018-03-26 02:08:36 +00:00
export default {
data () {
return {
2018-03-28 03:51:27 +00:00
visibles: [],
2018-03-26 02:08:36 +00:00
}
},
2018-03-28 03:51:27 +00:00
methods: {
handleTooltipVisibleChange (index, visible) {
this.visibles[index] = visible
this.visibles = { ...this.visibles }
},
},
2018-03-26 02:08:36 +00:00
render () {
2018-03-28 03:51:27 +00:00
const handle = (h, props) => {
2018-03-29 06:32:43 +00:00
const { value, dragging, index, refStr, style, ...restProps } = props
2018-03-26 02:08:36 +00:00
const handleProps = {
props: {
...restProps,
value,
},
2018-03-26 04:18:06 +00:00
attrs: {
refStr,
},
2018-03-26 02:08:36 +00:00
key: index,
2018-03-29 06:32:43 +00:00
style,
2018-03-26 02:08:36 +00:00
}
return (
<Tooltip
prefixCls='rc-slider-tooltip'
overlay={value}
visible={dragging}
placement='top'
key={index}
>
2018-03-28 03:51:27 +00:00
2018-03-26 02:08:36 +00:00
<Handle {...handleProps} />
</Tooltip>
)
}
2018-03-28 03:51:27 +00:00
2018-03-29 06:32:43 +00:00
const handleRange = (h, { value, dragging, index, disabled, style, ...restProps }) => {
2018-03-28 03:51:27 +00:00
const tipFormatter = value => `${value}%`
const tipProps = {}
const {
prefixCls = 'rc-slider-tooltip',
overlay = tipFormatter(value),
placement = 'top',
visible = visible || false,
...restTooltipProps } = tipProps
let handleStyleWithIndex
2018-03-29 06:32:43 +00:00
if (Array.isArray(style)) {
handleStyleWithIndex = style[index] || style[0]
2018-03-28 03:51:27 +00:00
} else {
2018-03-29 06:32:43 +00:00
handleStyleWithIndex = style
2018-03-28 03:51:27 +00:00
}
const tooltipProps = {
props: {
prefixCls,
overlay,
placement,
visible: (!disabled && (this.visibles[index] || dragging)) || visible,
...restTooltipProps,
},
key: index,
}
const handleProps = {
props: {
value,
...restProps,
},
on: {
mouseenter: () => this.handleTooltipVisibleChange(index, true),
mouseleave: () => this.handleTooltipVisibleChange(index, false),
},
style: {
...handleStyleWithIndex,
},
}
return (
<Tooltip
{...tooltipProps}
>
<Handle
{...handleProps}
/>
</Tooltip>
)
}
2018-03-26 02:08:36 +00:00
const wrapperStyle = 'width: 400px; margin: 50px'
return (
<div>
<div style={wrapperStyle}>
<p>Slider with custom handle</p>
<Slider min={0} max={20} defaultValue={3} handle={handle} />
</div>
2018-03-28 03:51:27 +00:00
<div style={wrapperStyle}>
2018-03-26 02:08:36 +00:00
<p>Range with custom handle</p>
2018-03-28 03:51:27 +00:00
<Range min={0} max={20} defaultValue={[3, 10]} handle={handleRange} />
</div>
2018-03-26 02:08:36 +00:00
</div>
)
},
}