feat: slider add tooltipVisible and optimize experience

This commit is contained in:
tangjinzhou 2019-01-05 11:23:34 +08:00
parent 032655f097
commit 439d9cfa5a
6 changed files with 48 additions and 51 deletions

View File

@ -1,3 +1,7 @@
// base rc-slider 8.6.4 // base rc-slider 8.6.4
import Vue from 'vue'
import ref from 'vue-ref'
import Slider from './src/' import Slider from './src/'
Vue.use(ref, { name: 'ant-ref' })
export default Slider export default Slider

View File

@ -26,11 +26,9 @@ export default {
} }
}, },
mounted () { mounted () {
this.$nextTick(() => { // mouseup won't trigger if mouse moved out of handle
// mouseup won't trigger if mouse moved out of handle // so we listen on document here.
// so we listen on document here. this.onMouseUpListener = addEventListener(document, 'mouseup', this.handleMouseUp)
this.onMouseUpListener = addEventListener(document, 'mouseup', this.handleMouseUp)
})
}, },
beforeDestroy () { beforeDestroy () {
if (this.onMouseUpListener) { if (this.onMouseUpListener) {
@ -46,8 +44,9 @@ export default {
this.setClickFocus(true) this.setClickFocus(true)
} }
}, },
handleBlur () { handleBlur (e) {
this.setClickFocus(false) this.setClickFocus(false)
this.__emit('blur', e)
}, },
handleKeyDown () { handleKeyDown () {
this.setClickFocus(false) this.setClickFocus(false)
@ -62,12 +61,16 @@ export default {
blur () { blur () {
this.$refs.handle.blur() this.$refs.handle.blur()
}, },
// when click can not focus in vue, use mousedown trigger focus
handleMousedown (e) {
this.focus()
this.__emit('mousedown', e)
},
}, },
render () { render () {
const { const {
prefixCls, vertical, offset, disabled, min, max, value, tabIndex, prefixCls, vertical, offset, disabled, min, max, value, tabIndex,
} = getOptionProps(this) } = getOptionProps(this)
const className = classNames( const className = classNames(
this.$props.className, this.$props.className,
{ {
@ -76,9 +79,7 @@ export default {
) )
const postionStyle = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` } const postionStyle = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` }
const elStyle = {
...postionStyle,
}
const ariaProps = { const ariaProps = {
'aria-valuemin': min, 'aria-valuemin': min,
'aria-valuemax': max, 'aria-valuemax': max,
@ -93,14 +94,17 @@ export default {
...ariaProps, ...ariaProps,
}, },
class: className, class: className,
on: this.$listeners, on: {
...this.$listeners,
blur: this.handleBlur,
keydown: this.handleKeyDown,
mousedown: this.handleMousedown,
},
ref: 'handle', ref: 'handle',
style: elStyle, style: postionStyle,
} }
return ( return (
<div <div {...handleProps} />
{...handleProps}
/>
) )
}, },
} }

View File

@ -362,9 +362,14 @@ const Range = {
max, max,
disabled, disabled,
style: handleStyle[i], style: handleStyle[i],
ref: 'handleRefs_' + i, directives: [{
handleFocus: this.onFocus, name: 'ant-ref',
handleBlur: this.onBlur, value: h => this.saveHandle(i, h),
}],
on: {
focus: this.onFocus,
blur: this.onBlur,
},
})) }))
return { return {

View File

@ -189,9 +189,14 @@ const Slider = {
index: 0, index: 0,
tabIndex, tabIndex,
style: handleStyle[0] || handleStyle, style: handleStyle[0] || handleStyle,
ref: 'handleRefs_0', directives: [{
handleFocus: this.onFocus, name: 'ant-ref',
handleBlur: this.onBlur, value: h => this.saveHandle(0, h),
}],
on: {
focus: this.onFocus,
blur: this.onBlur,
},
}) })
const _trackStyle = trackStyle[0] || trackStyle const _trackStyle = trackStyle[0] || trackStyle

View File

@ -79,6 +79,7 @@ export default function createSlider (Component) {
step step
) )
} }
this.handlesRefs = {}
return {} return {}
}, },
mounted () { mounted () {
@ -94,20 +95,8 @@ export default function createSlider (Component) {
this.removeDocumentEvents() this.removeDocumentEvents()
}) })
}, },
computed: {
handlesRefs () {
const handlesRefs = []
for (const [k, v] of Object.entries(this.$refs)) {
const matchs = k.match(/handleRefs_(\d+$)/)
if (matchs) {
handlesRefs[+matchs[1]] = v
}
}
return handlesRefs
},
},
methods: { methods: {
defaultHandle ({ index, ref, className, style, ...restProps }) { defaultHandle ({ index, directives, className, style, on, ...restProps }) {
delete restProps.dragging delete restProps.dragging
if (restProps.value === null) { if (restProps.value === null) {
return null return null
@ -119,7 +108,8 @@ export default function createSlider (Component) {
class: className, class: className,
style, style,
key: index, key: index,
ref, directives,
on,
} }
return <Handle {...handleProps} /> return <Handle {...handleProps} />
}, },
@ -264,6 +254,9 @@ export default function createSlider (Component) {
const ratio = (value - min) / (max - min) const ratio = (value - min) / (max - min)
return ratio * 100 return ratio * 100
}, },
saveHandle (index, handle) {
this.handlesRefs[index] = handle
},
}, },
render (h) { render (h) {
const { const {
@ -306,6 +299,7 @@ export default function createSlider (Component) {
return ( return (
<div <div
ref='sliderRef' ref='sliderRef'
tabIndex='-1'
class={sliderClassName} class={sliderClassName}
onTouchstart={disabled ? noop : this.onTouchStart} onTouchstart={disabled ? noop : this.onTouchStart}
onMousedown={disabled ? noop : this.onMouseDown} onMousedown={disabled ? noop : this.onMouseDown}

View File

@ -7,7 +7,7 @@ export function isDev () {
export function isEventFromHandle (e, handles) { export function isEventFromHandle (e, handles) {
try { try {
return Object.keys(handles) return Object.keys(handles)
.some(key => e.target === handles[key].$el) .some(key => e.target === handles[key].$el || e.target === handles[key])
} catch (error) { } catch (error) {
return false return false
} }
@ -122,18 +122,3 @@ export function getKeyboardValueMutator (e) {
default: return undefined default: return undefined
} }
} }
export function getComponentProps (obj, prop) {
if (obj[prop]) {
return obj
} else if (obj.$children.length) {
const len = obj.$children.length
for (let i = 0; i < len; i++) {
if (obj.$children[i][prop]) {
return obj.$children[i]
} else if (obj.$children[i].$children.length) {
return getComponentProps(obj.$children[i], prop)
}
}
}
}