-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathInputOtp.vue
More file actions
125 lines (107 loc) · 3.21 KB
/
InputOtp.vue
File metadata and controls
125 lines (107 loc) · 3.21 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<template>
<div :class="n()">
<div :class="n('container')">
<slot />
</div>
<var-form-details :error-message="errorMessage" @mousedown.stop>
<template v-if="$slots['extra-message']" #extra-message>
<slot name="extra-message" />
</template>
</var-form-details>
</div>
</template>
<script lang="ts">
import { computed, defineComponent, nextTick, ref } from 'vue'
import { call } from '@varlet/shared'
import { useForm } from '../form/provide'
import { createNamespace, useValidation } from '../utils/components'
import { props, type OptInputValidateTrigger } from './props'
import { useInputOtpItems, type InputOtpProvider } from './provide'
const { name, n } = createNamespace('input-otp')
export default defineComponent({
name,
props,
setup(props, { emit }) {
const { length, bindInputOtpItem } = useInputOtpItems()
const activeInput = ref<number>(-1)
const model = computed({
get: () => props.modelValue,
set: (value: string) => {
call(props.onChange, value)
call(props['onUpdate:modelValue'], value)
validateWithTrigger('onChange')
},
})
const { errorMessage, validateWithTrigger: vt, validate: v, resetValidation } = useValidation()
const inputOtpProvider: InputOtpProvider = {
parentModel: model,
activeInput,
length,
disabled: computed(() => props.disabled),
readonly: computed(() => props.readonly),
variant: computed(() => props.variant),
size: computed(() => props.size),
textColor: computed(() => props.textColor),
focusColor: computed(() => props.focusColor),
blurColor: computed(() => props.blurColor),
autofocus: computed(() => props.autofocus),
onItemChange,
onItemFocus,
onItemBlur,
reset,
validate,
resetValidation,
}
bindInputOtpItem(inputOtpProvider)
const { bindForm } = useForm()
call(bindForm, inputOtpProvider)
function validateWithTrigger(trigger: OptInputValidateTrigger) {
nextTick(() => {
const { validateTrigger, rules, modelValue } = props
vt(validateTrigger, trigger, rules, modelValue)
})
}
function onItemChange(index: number, value?: string) {
if (value == null) {
activeInput.value = index
return
}
const currentValue = model.value || ''
if (index < length.value) {
activeInput.value = value ? index + 1 : index - 1
emit('update:modelValue', currentValue.slice(0, index) + value + currentValue.slice(index + 1))
} else {
emit('update:modelValue', currentValue + value)
}
}
function onItemFocus(index: number) {
call(props.onFocus, index)
}
function onItemBlur(index: number) {
call(props.onBlur, index)
}
// expose
function reset() {
call(props['onUpdate:modelValue'], '')
resetValidation()
}
// expose
function validate() {
return v(props.rules, props.modelValue)
}
return {
length,
activeInput,
errorMessage,
n,
reset,
validate,
}
},
})
</script>
<style lang="less">
@import '../styles/common';
@import '../form-details/formDetails';
@import './inputOtp';
</style>