-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcomposable.js
68 lines (59 loc) · 2.24 KB
/
composable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { NumberInput } from './numberInput'
import { computed, getCurrentInstance, isVue3, onMounted, onUnmounted, ref } from 'vue-demi'
const findInput = (el) => el.tagName.toLowerCase() === 'input' ? el : el.querySelector('input')
export default (options) => {
let numberInput, input
const inputRef = ref(null)
const formattedValue = ref(null)
const instance = getCurrentInstance()
const emit = (event, value) => isVue3 ? instance.emit(event, value) : instance.proxy.$emit(event, value)
const lazyModel = isVue3 && (instance.attrs.modelModifiers || {}).lazy
const numberValue = computed(() => isVue3 ? instance.props.modelValue : instance.props.value)
const inputEvent = isVue3 ? 'update:modelValue' : 'input'
const changeEvent = isVue3 && lazyModel ? 'update:modelValue' : 'change'
const hasInputEventListener = isVue3 ? (!!instance.attrs['onUpdate:modelValue'] && !lazyModel) : !!instance.proxy.$listeners[inputEvent]
const hasChangeEventListener = isVue3 ? (lazyModel || !!instance.attrs.onChange) : !!instance.proxy.$listeners[changeEvent]
const onInput = (e) => {
if (e.detail) {
if (numberValue.value !== e.detail.number) {
emit(inputEvent, e.detail.number)
}
formattedValue.value = e.detail.formatted
}
}
const onChange = (e) => {
if (e.detail) {
emit(changeEvent, e.detail.number)
formattedValue.value = e.detail.formatted
}
}
onMounted(() => {
input = '$el' in inputRef.value ? findInput(inputRef.value.$el) : inputRef.value
if (input == null) {
throw new Error('No input element found')
} else {
numberInput = new NumberInput(input, options)
if (hasInputEventListener) {
input.addEventListener('input', onInput)
}
if (hasChangeEventListener) {
input.addEventListener('change', onChange)
}
numberInput.setValue(numberValue.value)
}
})
onUnmounted(() => {
if (hasInputEventListener) {
input.removeEventListener('input', onInput)
}
if (hasChangeEventListener) {
input.removeEventListener('change', onChange)
}
})
return {
inputRef,
formattedValue,
setValue: (value) => numberInput.setValue(value),
setOptions: (options) => numberInput.setOptions(options)
}
}