@@ -14,6 +14,7 @@ export default defineComponent({
14
14
value : { type : String , default : undefined } ,
15
15
mask : { type : [ Boolean , String ] , default : false } ,
16
16
onChange : { type : Function as PropType < ( index : number , value : string ) => void > } ,
17
+ onActiveChange : Function as PropType < ( nextIndex : number ) => void > ,
17
18
} ,
18
19
setup ( props , { attrs, expose } ) {
19
20
const inputRef = shallowRef ( ) ;
@@ -27,8 +28,7 @@ export default defineComponent({
27
28
inputRef . value . select ( ) ;
28
29
} ) ;
29
30
} ;
30
- const handleSyncMouseDown = e => {
31
- e . preventDefault ( ) ;
31
+ const handleSyncMouseDown = ( ) => {
32
32
syncSelection ( ) ;
33
33
} ;
34
34
// ======================= Event handlers =================
@@ -41,6 +41,30 @@ export default defineComponent({
41
41
syncSelection ( ) ;
42
42
} ;
43
43
44
+ const activeSelection = ( ) => {
45
+ if ( document . activeElement === inputRef . value . input . input ) {
46
+ syncSelection ( ) ;
47
+ }
48
+ } ;
49
+
50
+ const onInternalKeydown = ( { key } ) => {
51
+ if ( key === 'ArrowLeft' ) {
52
+ props . onActiveChange ( props . index - 1 ) ;
53
+ } else if ( key === 'ArrowRight' ) {
54
+ props . onActiveChange ( props . index + 1 ) ;
55
+ }
56
+
57
+ activeSelection ( ) ;
58
+ } ;
59
+
60
+ const onInternalKeyUp = ( { key } ) => {
61
+ if ( key === 'Backspace' && ! props . value ) {
62
+ props . onActiveChange ( props . index - 1 ) ;
63
+ }
64
+
65
+ activeSelection ( ) ;
66
+ } ;
67
+
44
68
expose ( {
45
69
focus,
46
70
} ) ;
@@ -55,8 +79,8 @@ export default defineComponent({
55
79
onInput = { onInternalChange }
56
80
onMousedown = { handleSyncMouseDown }
57
81
onMouseUp = { handleSyncMouseDown }
58
- onKeydown = { syncSelection }
59
- onKeyup = { syncSelection }
82
+ onKeydown = { onInternalKeydown }
83
+ onKeyup = { onInternalKeyUp }
60
84
/>
61
85
) ;
62
86
} ,
0 commit comments