Skip to content

Commit 439d9cf

Browse files
committed
feat: slider add tooltipVisible and optimize experience
1 parent 032655f commit 439d9cf

File tree

6 files changed

+48
-51
lines changed

6 files changed

+48
-51
lines changed

components/vc-slider/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// base rc-slider 8.6.4
2+
import Vue from 'vue'
3+
import ref from 'vue-ref'
24
import Slider from './src/'
5+
6+
Vue.use(ref, { name: 'ant-ref' })
37
export default Slider

components/vc-slider/src/Handle.jsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ export default {
2626
}
2727
},
2828
mounted () {
29-
this.$nextTick(() => {
30-
// mouseup won't trigger if mouse moved out of handle
31-
// so we listen on document here.
32-
this.onMouseUpListener = addEventListener(document, 'mouseup', this.handleMouseUp)
33-
})
29+
// mouseup won't trigger if mouse moved out of handle
30+
// so we listen on document here.
31+
this.onMouseUpListener = addEventListener(document, 'mouseup', this.handleMouseUp)
3432
},
3533
beforeDestroy () {
3634
if (this.onMouseUpListener) {
@@ -46,8 +44,9 @@ export default {
4644
this.setClickFocus(true)
4745
}
4846
},
49-
handleBlur () {
47+
handleBlur (e) {
5048
this.setClickFocus(false)
49+
this.__emit('blur', e)
5150
},
5251
handleKeyDown () {
5352
this.setClickFocus(false)
@@ -62,12 +61,16 @@ export default {
6261
blur () {
6362
this.$refs.handle.blur()
6463
},
64+
// when click can not focus in vue, use mousedown trigger focus
65+
handleMousedown (e) {
66+
this.focus()
67+
this.__emit('mousedown', e)
68+
},
6569
},
6670
render () {
6771
const {
6872
prefixCls, vertical, offset, disabled, min, max, value, tabIndex,
6973
} = getOptionProps(this)
70-
7174
const className = classNames(
7275
this.$props.className,
7376
{
@@ -76,9 +79,7 @@ export default {
7679
)
7780

7881
const postionStyle = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` }
79-
const elStyle = {
80-
...postionStyle,
81-
}
82+
8283
const ariaProps = {
8384
'aria-valuemin': min,
8485
'aria-valuemax': max,
@@ -93,14 +94,17 @@ export default {
9394
...ariaProps,
9495
},
9596
class: className,
96-
on: this.$listeners,
97+
on: {
98+
...this.$listeners,
99+
blur: this.handleBlur,
100+
keydown: this.handleKeyDown,
101+
mousedown: this.handleMousedown,
102+
},
97103
ref: 'handle',
98-
style: elStyle,
104+
style: postionStyle,
99105
}
100106
return (
101-
<div
102-
{...handleProps}
103-
/>
107+
<div {...handleProps} />
104108
)
105109
},
106110
}

components/vc-slider/src/Range.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,14 @@ const Range = {
362362
max,
363363
disabled,
364364
style: handleStyle[i],
365-
ref: 'handleRefs_' + i,
366-
handleFocus: this.onFocus,
367-
handleBlur: this.onBlur,
365+
directives: [{
366+
name: 'ant-ref',
367+
value: h => this.saveHandle(i, h),
368+
}],
369+
on: {
370+
focus: this.onFocus,
371+
blur: this.onBlur,
372+
},
368373
}))
369374

370375
return {

components/vc-slider/src/Slider.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,14 @@ const Slider = {
189189
index: 0,
190190
tabIndex,
191191
style: handleStyle[0] || handleStyle,
192-
ref: 'handleRefs_0',
193-
handleFocus: this.onFocus,
194-
handleBlur: this.onBlur,
192+
directives: [{
193+
name: 'ant-ref',
194+
value: h => this.saveHandle(0, h),
195+
}],
196+
on: {
197+
focus: this.onFocus,
198+
blur: this.onBlur,
199+
},
195200
})
196201

197202
const _trackStyle = trackStyle[0] || trackStyle

components/vc-slider/src/common/createSlider.jsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export default function createSlider (Component) {
7979
step
8080
)
8181
}
82+
this.handlesRefs = {}
8283
return {}
8384
},
8485
mounted () {
@@ -94,20 +95,8 @@ export default function createSlider (Component) {
9495
this.removeDocumentEvents()
9596
})
9697
},
97-
computed: {
98-
handlesRefs () {
99-
const handlesRefs = []
100-
for (const [k, v] of Object.entries(this.$refs)) {
101-
const matchs = k.match(/handleRefs_(\d+$)/)
102-
if (matchs) {
103-
handlesRefs[+matchs[1]] = v
104-
}
105-
}
106-
return handlesRefs
107-
},
108-
},
10998
methods: {
110-
defaultHandle ({ index, ref, className, style, ...restProps }) {
99+
defaultHandle ({ index, directives, className, style, on, ...restProps }) {
111100
delete restProps.dragging
112101
if (restProps.value === null) {
113102
return null
@@ -119,7 +108,8 @@ export default function createSlider (Component) {
119108
class: className,
120109
style,
121110
key: index,
122-
ref,
111+
directives,
112+
on,
123113
}
124114
return <Handle {...handleProps} />
125115
},
@@ -264,6 +254,9 @@ export default function createSlider (Component) {
264254
const ratio = (value - min) / (max - min)
265255
return ratio * 100
266256
},
257+
saveHandle (index, handle) {
258+
this.handlesRefs[index] = handle
259+
},
267260
},
268261
render (h) {
269262
const {
@@ -306,6 +299,7 @@ export default function createSlider (Component) {
306299
return (
307300
<div
308301
ref='sliderRef'
302+
tabIndex='-1'
309303
class={sliderClassName}
310304
onTouchstart={disabled ? noop : this.onTouchStart}
311305
onMousedown={disabled ? noop : this.onMouseDown}

components/vc-slider/src/utils.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function isDev () {
77
export function isEventFromHandle (e, handles) {
88
try {
99
return Object.keys(handles)
10-
.some(key => e.target === handles[key].$el)
10+
.some(key => e.target === handles[key].$el || e.target === handles[key])
1111
} catch (error) {
1212
return false
1313
}
@@ -122,18 +122,3 @@ export function getKeyboardValueMutator (e) {
122122
default: return undefined
123123
}
124124
}
125-
126-
export function getComponentProps (obj, prop) {
127-
if (obj[prop]) {
128-
return obj
129-
} else if (obj.$children.length) {
130-
const len = obj.$children.length
131-
for (let i = 0; i < len; i++) {
132-
if (obj.$children[i][prop]) {
133-
return obj.$children[i]
134-
} else if (obj.$children[i].$children.length) {
135-
return getComponentProps(obj.$children[i], prop)
136-
}
137-
}
138-
}
139-
}

0 commit comments

Comments
 (0)