2121 :aria-label =" `Code digit ${index}`"
2222 class =" h-12 w-11 appearance-none rounded-xl border-none bg-surface-4 p-1 text-center text-base font-medium text-primary focus:text-primary focus:ring-4 focus:ring-brand-shadow disabled:cursor-not-allowed"
2323 :class =" [inputClass, 'outline-none']"
24+ @focus =" handleFocus($event)"
2425 @input =" handleInput($event, index - 1)"
2526 @keydown =" handleKeydown($event, index - 1)"
2627 @paste.prevent =" handlePaste"
@@ -84,7 +85,7 @@ function setCodeInput(element: Element | ComponentPublicInstance | null, index:
8485function focusInput(index : number ) {
8586 const input = codeInputs .value [index ]
8687 input ?.focus ()
87- input ?. setSelectionRange (input . value . length , input . value . length )
88+ selectInputValue (input )
8889}
8990
9091function focusFirstUnfilledCodeInput() {
@@ -94,14 +95,29 @@ function focusFirstUnfilledCodeInput() {
9495}
9596
9697function handlePointerDown(event : PointerEvent ) {
97- if (disabledOrReadonly ()) {
98+ if (props .disabled ) {
99+ return
100+ }
101+
102+ if (event .target instanceof HTMLInputElement && event .target .value ) {
103+ event .preventDefault ()
104+ event .target .focus ()
105+ selectInputValue (event .target )
98106 return
99107 }
100108
101109 event .preventDefault ()
102110 focusFirstUnfilledCodeInput ()
103111}
104112
113+ function handleFocus(event : FocusEvent ) {
114+ if (props .disabled ) {
115+ return
116+ }
117+
118+ selectInputValue (event .target as HTMLInputElement )
119+ }
120+
105121function handleInput(event : Event , index : number ) {
106122 if (disabledOrReadonly ()) {
107123 return
@@ -174,6 +190,18 @@ function disabledOrReadonly() {
174190 return props .disabled || props .readonly
175191}
176192
193+ function selectInputValue(input ? : HTMLInputElement ) {
194+ if (! input ) {
195+ return
196+ }
197+
198+ if (input .value ) {
199+ input .select ()
200+ } else {
201+ input .setSelectionRange (input .value .length , input .value .length )
202+ }
203+ }
204+
177205function clear() {
178206 digits .value = Array .from ({ length: codeLength }, () => ' ' )
179207 updateModel ()
0 commit comments