Skip to content

Commit 7ca4201

Browse files
authored
Merge pull request #61 from raminjafary/fix/mobile-autofill
fix: paste otp codes from mobile autofill
2 parents 5a64d98 + 1fc1649 commit 7ca4201

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/components/single-otp-input.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const props = withDefaults(defineProps<Props>(), {
3535
isLastChild: false,
3636
placeholder: "",
3737
isDisabled: false,
38+
value: "",
3839
});
3940
4041
const emit = defineEmits<{
@@ -48,12 +49,19 @@ const emit = defineEmits<{
4849
const model = ref(props.value || "");
4950
const input = ref<HTMLInputElement | null>(null) as Ref<HTMLInputElement>;
5051
51-
const handleOnChange = () => {
52-
model.value = model.value;
53-
if (model.value.length > 1) {
54-
model.value = model.value.slice(0, 1);
52+
const handleOnChange = (e: Event) => {
53+
const value = (e.target as HTMLInputElement).value;
54+
if (value && value.trim().length > 1) {
55+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
56+
// @ts-ignore
57+
// This is a workaround for dealing IOS does not fire onPaste event from sms auto-populate.
58+
// The `:maxlength="isLastChild ? 1 : undefined"` on the input is also needed for this workaround.
59+
e.clipboardData = {
60+
getData: () => value.trim(),
61+
};
62+
return emit("on-paste", e as ClipboardEvent);
5563
}
56-
return emit("on-change", model.value.toString());
64+
return emit("on-change", value);
5765
};
5866
5967
const isCodeLetter = (charCode: number) => charCode >= 65 && charCode <= 90;
@@ -134,9 +142,9 @@ onMounted(() => {
134142
ref="input"
135143
min="0"
136144
max="9"
137-
:maxlength="1"
145+
:maxlength="isLastChild ? 1 : undefined"
138146
pattern="[0-9]"
139-
v-model="model"
147+
:value="model"
140148
:class="[inputClasses, conditionalClass, { 'is-complete': model }]"
141149
@input="handleOnChange"
142150
@keydown="handleOnKeyDown"

0 commit comments

Comments
 (0)