Skip to content

Commit

Permalink
fix: replace key event
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-hearts committed Jun 16, 2024
1 parent 99c134c commit 73404c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
32 changes: 28 additions & 4 deletions components/input/OTP/OTPInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default defineComponent({
value: { type: String, default: undefined },
mask: { type: [Boolean, String], default: false },
onChange: { type: Function as PropType<(index: number, value: string) => void> },
onActiveChange: Function as PropType<(nextIndex: number) => void>,
},
setup(props, { attrs, expose }) {
const inputRef = shallowRef();
Expand All @@ -27,8 +28,7 @@ export default defineComponent({
inputRef.value.select();
});
};
const handleSyncMouseDown = e => {
e.preventDefault();
const handleSyncMouseDown = () => {
syncSelection();
};
// ======================= Event handlers =================
Expand All @@ -41,6 +41,30 @@ export default defineComponent({
syncSelection();
};

const activeSelection = () => {
if (document.activeElement === inputRef.value.input.input) {
syncSelection();
}
};

const onInternalKeydown = ({ key }) => {
if (key === 'ArrowLeft') {
props.onActiveChange(props.index - 1);
} else if (key === 'ArrowRight') {
props.onActiveChange(props.index + 1);
}

activeSelection();
};

const onInternalKeyUp = ({ key }) => {
if (key === 'Backspace' && !props.value) {
props.onActiveChange(props.index - 1);
}

activeSelection();
};

expose({
focus,
});
Expand All @@ -55,8 +79,8 @@ export default defineComponent({
onInput={onInternalChange}
onMousedown={handleSyncMouseDown}
onMouseUp={handleSyncMouseDown}
onKeydown={syncSelection}
onKeyup={syncSelection}
onKeydown={onInternalKeydown}
onKeyup={onInternalKeyUp}
/>
);
},
Expand Down
5 changes: 5 additions & 0 deletions components/input/OTP/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export default defineComponent({
};

// ======================= Change handlers =================
const onInputActiveChange = (nextIndex: number) => {
refs.value[nextIndex]?.focus();
};

const onInputChange = (index: number, value: string) => {
const nextValueCells = patchValue(index, value);
const nextIndex = Math.min(index + value.length, props.length);
Expand Down Expand Up @@ -111,6 +115,7 @@ export default defineComponent({
value={singleValue}
htmlSize={1}
onChange={onInputChange}
onActiveChange={onInputActiveChange}
autofocus={index === 0 && autofocus}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion components/vc-input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default defineComponent({
expose({
focus,
blur,
input: computed(() => (inputRef.value.input as any)?.input),
input: computed(() => inputRef.value.input),
stateValue,
setSelectionRange,
select,
Expand Down

0 comments on commit 73404c9

Please sign in to comment.