|
| 1 | +<template> |
| 2 | + <div :class="n()"> |
| 3 | + <div :class="n('container')"> |
| 4 | + <slot /> |
| 5 | + </div> |
| 6 | + |
| 7 | + <var-form-details :error-message="errorMessage" @mousedown.stop> |
| 8 | + <template v-if="$slots['extra-message']" #extra-message> |
| 9 | + <slot name="extra-message" /> |
| 10 | + </template> |
| 11 | + </var-form-details> |
| 12 | + </div> |
| 13 | +</template> |
| 14 | + |
| 15 | +<script lang="ts"> |
| 16 | +import { computed, defineComponent, nextTick, ref } from 'vue' |
| 17 | +import { call } from '@varlet/shared' |
| 18 | +import { useForm } from '../form/provide' |
| 19 | +import { createNamespace, useValidation } from '../utils/components' |
| 20 | +import { props, type OptInputValidateTrigger } from './props' |
| 21 | +import { useInputOtpItems, type InputOtpProvider } from './provide' |
| 22 | +
|
| 23 | +const { name, n } = createNamespace('input-otp') |
| 24 | +
|
| 25 | +export default defineComponent({ |
| 26 | + name, |
| 27 | + props, |
| 28 | + setup(props, { emit }) { |
| 29 | + const { length, inputOtpItems, bindInputOtpItem } = useInputOtpItems() |
| 30 | + const activeInput = ref() |
| 31 | +
|
| 32 | + const model = computed({ |
| 33 | + get: () => props.modelValue, |
| 34 | + set: (value) => { |
| 35 | + call(props.onChange, value) |
| 36 | + call(props['onUpdate:modelValue'], value) |
| 37 | + validateWithTrigger('onChange') |
| 38 | + }, |
| 39 | + }) |
| 40 | +
|
| 41 | + const { errorMessage, validateWithTrigger: vt, validate: v, resetValidation } = useValidation() |
| 42 | +
|
| 43 | + const inputOtpProvider: InputOtpProvider = { |
| 44 | + parentModel: model, |
| 45 | + activeInput, |
| 46 | + length, |
| 47 | + disabled: ref(props.disabled), |
| 48 | + readonly: ref(props.readonly), |
| 49 | + variant: ref(props.variant), |
| 50 | + size: ref(props.size), |
| 51 | + textColor: ref(props.textColor), |
| 52 | + focusColor: ref(props.focusColor), |
| 53 | + blurColor: ref(props.blurColor), |
| 54 | + autofocus: ref(props.autofocus), |
| 55 | + onItemChange, |
| 56 | + onItemFocus, |
| 57 | + onItemBlur, |
| 58 | + reset, |
| 59 | + validate, |
| 60 | + resetValidation, |
| 61 | + } |
| 62 | +
|
| 63 | + bindInputOtpItem(inputOtpProvider) |
| 64 | +
|
| 65 | + const { bindForm } = useForm() |
| 66 | + call(bindForm, inputOtpProvider) |
| 67 | +
|
| 68 | + function validateWithTrigger(trigger: OptInputValidateTrigger) { |
| 69 | + nextTick(() => { |
| 70 | + const { validateTrigger, rules, modelValue } = props |
| 71 | + vt(validateTrigger, trigger, rules, modelValue) |
| 72 | + }) |
| 73 | + } |
| 74 | +
|
| 75 | + function onItemChange(index: number, value?: string) { |
| 76 | + if (value == null) { |
| 77 | + activeInput.value = index |
| 78 | + } else { |
| 79 | + const currentValue = model.value || '' |
| 80 | + if (index < length.value) { |
| 81 | + activeInput.value = value ? index + 1 : index - 1 |
| 82 | + emit('update:modelValue', currentValue.slice(0, index) + value + currentValue.slice(index + 1)) |
| 83 | + } else { |
| 84 | + emit('update:modelValue', currentValue + value) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + function onItemFocus(index: number) { |
| 90 | + call(props.onFocus, index) |
| 91 | + } |
| 92 | +
|
| 93 | + function onItemBlur(index: number) { |
| 94 | + call(props.onBlur, index) |
| 95 | + } |
| 96 | +
|
| 97 | + // expose |
| 98 | + function reset() { |
| 99 | + call(props['onUpdate:modelValue'], '') |
| 100 | + resetValidation() |
| 101 | + } |
| 102 | +
|
| 103 | + // expose |
| 104 | + function validate() { |
| 105 | + return v(props.rules, props.modelValue) |
| 106 | + } |
| 107 | +
|
| 108 | + return { |
| 109 | + length, |
| 110 | + inputOtpItems, |
| 111 | + errorMessage, |
| 112 | + n, |
| 113 | + activeInput, |
| 114 | + reset, |
| 115 | + validate, |
| 116 | + } |
| 117 | + }, |
| 118 | +}) |
| 119 | +</script> |
| 120 | + |
| 121 | +<style lang="less"> |
| 122 | +@import '../styles/common'; |
| 123 | +@import '../form-details/formDetails'; |
| 124 | +@import './inputOtp'; |
| 125 | +</style> |
0 commit comments