11<template >
22 <div class =" hl-input-container m-auto text-center" :class =" wrapperClassName" >
3- <input
4- v-for =" (input, index) in length"
5- :element-num =" input"
6- :id =" generateInputId(index)"
7- :ref =" (el) => generateRef(index, el)"
8- :key =" index"
9- v-model =" inputValues[index]"
10- :type =" type"
11- maxlength =" 1"
12- :style =" {
13- borderBottom: hlBorderColor(index),
14- color: fontColor,
15- ...conditionClass(index)
16- }"
17- :class =" inputClassName"
18- :disabled =" disabled"
19- :readonly =" readonly"
20- contenteditable =" true"
21- @keydown =" (e: KeyboardEvent) => handleKeydown(e)"
22- @keyup =" handleInputFocus(index)"
23- @paste.prevent =" handlePastedValues"
24- @input =" returnFullString()"
25- @focus =" handleFocus(index)"
26- />
3+ <template v-for =" (input , index ) in length " :key =" index " >
4+ <input
5+ :element-num =" input"
6+ :id =" generateInputId(index)"
7+ :ref =" (el) => generateRef(index, el)"
8+ v-model =" inputValues[index]"
9+ :type =" type"
10+ maxlength =" 1"
11+ :style =" {
12+ borderBottom: hlBorderColor(index),
13+ color: fontColor,
14+ ...conditionClass(index)
15+ }"
16+ :class =" inputClassName"
17+ :disabled =" disabled"
18+ :readonly =" readonly"
19+ contenteditable =" true"
20+ @keydown =" (e: KeyboardEvent) => handleKeydown(e)"
21+ @keyup =" handleInputFocus(index)"
22+ @paste.prevent =" handlePastedValues"
23+ @input =" returnFullString()"
24+ @focus =" handleFocus(index)"
25+ :autocomplete =" autocomplete"
26+ />
27+ <div v-if =" separator && checkSeparatorType(index)" class =" hl-separator" ><span >{{ separator }}</span ></div >
28+ </template >
2729 </div >
2830</template >
2931
@@ -93,6 +95,20 @@ const props = defineProps({
9395 inputClassName: {
9496 type: String ,
9597 default: " "
98+ },
99+ autocomplete: {
100+ type: [String ],
101+ default: ' off'
102+ },
103+ separator: {
104+ type: String ,
105+ default: ' '
106+ },
107+ separatorType: {
108+ type: String as PropType <" middle" | " all" >,
109+ default: " middle" ,
110+ validator : (value : string ) =>
111+ [" middle" , " all" ].includes (value ),
96112 }
97113})
98114
@@ -105,6 +121,14 @@ watch(
105121 }
106122)
107123
124+ const checkSeparatorType = (index : number ) => {
125+ if (props .separatorType === ' middle' ) {
126+ return index === props .length / 2 - 1
127+ } else {
128+ return index < props .length - 1
129+ }
130+ }
131+
108132const inputRefs: any = {}
109133const inputValues = ref ([])
110134const currentActiveIndex = ref (- 1 )
@@ -116,10 +140,6 @@ const clear = () => {
116140 currentActiveIndex .value = - 1
117141}
118142
119- defineExpose ({
120- clear
121- });
122-
123143const handleKeydown = (event : KeyboardEvent ) => {
124144 if (! props .onlyNumber ) {
125145 return
@@ -205,6 +225,18 @@ const returnFullString = () => {
205225 }
206226}
207227
228+ const genResultWithSeparator = () => {
229+ const data = inputValues .value .join (' ' )
230+ if (! props .separator ) return data
231+ if (props .separatorType === ' middle' ) {
232+ const newArr = data .split (' ' )
233+ newArr .splice (props .length / 2 , 0 , props .separator )
234+ return newArr .join (' ' )
235+ } else {
236+ return data .split (' ' ).join (props .separator )
237+ }
238+ }
239+
208240onMounted (() => {
209241 if (props .autoFocus ) {
210242 inputRefs && inputRefs [0 ].focus ()
@@ -231,4 +263,9 @@ const outlineFocusClass = (isFocus: boolean) => {
231263 }
232264 return ' none'
233265}
266+
267+ defineExpose ({
268+ clear ,
269+ genResultWithSeparator
270+ });
234271 </script >
0 commit comments