Skip to content

Commit dd637f0

Browse files
committed
feat: new component input-otp
1 parent f86175c commit dd637f0

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

packages/varlet-ui/src/input-otp/InputOtp.vue

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export default defineComponent({
2727
props,
2828
setup(props, { emit }) {
2929
const { length, bindInputOtpItem } = useInputOtpItems()
30-
const activeInput = ref()
30+
const activeInput = ref<number>(-1)
3131
3232
const model = computed({
3333
get: () => props.modelValue,
34-
set: (value) => {
34+
set: (value: string) => {
3535
call(props.onChange, value)
3636
call(props['onUpdate:modelValue'], value)
3737
validateWithTrigger('onChange')
@@ -44,14 +44,14 @@ export default defineComponent({
4444
parentModel: model,
4545
activeInput,
4646
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),
47+
disabled: computed(() => props.disabled),
48+
readonly: computed(() => props.readonly),
49+
variant: computed(() => props.variant),
50+
size: computed(() => props.size),
51+
textColor: computed(() => props.textColor),
52+
focusColor: computed(() => props.focusColor),
53+
blurColor: computed(() => props.blurColor),
54+
autofocus: computed(() => props.autofocus),
5555
onItemChange,
5656
onItemFocus,
5757
onItemBlur,
@@ -75,14 +75,15 @@ export default defineComponent({
7575
function onItemChange(index: number, value?: string) {
7676
if (value == null) {
7777
activeInput.value = index
78+
return
79+
}
80+
81+
const currentValue = model.value || ''
82+
if (index < length.value) {
83+
activeInput.value = value ? index + 1 : index - 1
84+
emit('update:modelValue', currentValue.slice(0, index) + value + currentValue.slice(index + 1))
7885
} 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+
emit('update:modelValue', currentValue + value)
8687
}
8788
}
8889

packages/varlet-ui/varlet.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ export default defineConfig({
263263
},
264264
type: 1,
265265
},
266-
267266
{
268267
text: {
269268
'zh-CN': 'Skeleton 骨架屏',
@@ -668,6 +667,14 @@ export default defineConfig({
668667
doc: 'input',
669668
type: 2,
670669
},
670+
{
671+
text: {
672+
'zh-CN': 'InputOtp 一次性密码输入框',
673+
'en-US': 'InputOtp',
674+
},
675+
doc: 'input-otp',
676+
type: 2,
677+
},
671678
{
672679
text: {
673680
'zh-CN': 'AutoComplete 自动补全',

0 commit comments

Comments
 (0)