Skip to content

Commit 73404c9

Browse files
committed
fix: replace key event
1 parent 99c134c commit 73404c9

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

components/input/OTP/OTPInput.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineComponent({
1414
value: { type: String, default: undefined },
1515
mask: { type: [Boolean, String], default: false },
1616
onChange: { type: Function as PropType<(index: number, value: string) => void> },
17+
onActiveChange: Function as PropType<(nextIndex: number) => void>,
1718
},
1819
setup(props, { attrs, expose }) {
1920
const inputRef = shallowRef();
@@ -27,8 +28,7 @@ export default defineComponent({
2728
inputRef.value.select();
2829
});
2930
};
30-
const handleSyncMouseDown = e => {
31-
e.preventDefault();
31+
const handleSyncMouseDown = () => {
3232
syncSelection();
3333
};
3434
// ======================= Event handlers =================
@@ -41,6 +41,30 @@ export default defineComponent({
4141
syncSelection();
4242
};
4343

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+
4468
expose({
4569
focus,
4670
});
@@ -55,8 +79,8 @@ export default defineComponent({
5579
onInput={onInternalChange}
5680
onMousedown={handleSyncMouseDown}
5781
onMouseUp={handleSyncMouseDown}
58-
onKeydown={syncSelection}
59-
onKeyup={syncSelection}
82+
onKeydown={onInternalKeydown}
83+
onKeyup={onInternalKeyUp}
6084
/>
6185
);
6286
},

components/input/OTP/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export default defineComponent({
6767
};
6868

6969
// ======================= Change handlers =================
70+
const onInputActiveChange = (nextIndex: number) => {
71+
refs.value[nextIndex]?.focus();
72+
};
73+
7074
const onInputChange = (index: number, value: string) => {
7175
const nextValueCells = patchValue(index, value);
7276
const nextIndex = Math.min(index + value.length, props.length);
@@ -111,6 +115,7 @@ export default defineComponent({
111115
value={singleValue}
112116
htmlSize={1}
113117
onChange={onInputChange}
118+
onActiveChange={onInputActiveChange}
114119
autofocus={index === 0 && autofocus}
115120
/>
116121
);

components/vc-input/Input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default defineComponent({
6565
expose({
6666
focus,
6767
blur,
68-
input: computed(() => (inputRef.value.input as any)?.input),
68+
input: computed(() => inputRef.value.input),
6969
stateValue,
7070
setSelectionRange,
7171
select,

0 commit comments

Comments
 (0)