2024-02-21 18:38:16 +00:00
|
|
|
<template>
|
|
|
|
<div :class="cx('root')" v-bind="ptmi('root')">
|
2024-02-22 06:40:19 +00:00
|
|
|
<template v-for="i in length" :key="i">
|
2024-02-22 07:42:48 +00:00
|
|
|
<slot :events="getTemplateEvents(i - 1)" :attrs="getTemplateAttrs(i - 1)" :index="i">
|
2024-02-22 06:40:19 +00:00
|
|
|
<OtpInputText
|
|
|
|
:value="tokens[i - 1]"
|
|
|
|
:type="inputType"
|
|
|
|
:class="cx('input')"
|
|
|
|
:inputmode="inputMode"
|
2024-02-22 07:42:48 +00:00
|
|
|
:variant="variant"
|
|
|
|
:readonly="readonly"
|
|
|
|
:disabled="disabled"
|
|
|
|
:invalid="invalid"
|
|
|
|
:tabindex="tabindex"
|
2024-02-22 08:52:39 +00:00
|
|
|
:unstyled="unstyled"
|
2024-02-22 06:40:19 +00:00
|
|
|
@input="onInput($event, i - 1)"
|
|
|
|
@focus="onFocus($event)"
|
|
|
|
@blur="onBlur($event)"
|
|
|
|
@paste="onPaste($event)"
|
|
|
|
@keydown="onKeyDown($event)"
|
|
|
|
:pt="ptm('input')"
|
|
|
|
/>
|
|
|
|
</slot>
|
|
|
|
</template>
|
2024-02-21 18:38:16 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import InputText from 'primevue/inputtext';
|
|
|
|
import BaseInputOtp from './BaseInputOtp.vue';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'InputOtp',
|
|
|
|
extends: BaseInputOtp,
|
|
|
|
inheritAttrs: false,
|
|
|
|
emits: ['update:modelValue', 'change', 'focus', 'blur'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
tokens: []
|
|
|
|
};
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
modelValue: {
|
|
|
|
immediate: true,
|
|
|
|
handler(newValue) {
|
|
|
|
this.tokens = newValue ? newValue.split('') : new Array(this.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2024-02-22 06:40:19 +00:00
|
|
|
getTemplateAttrs(index) {
|
|
|
|
return {
|
|
|
|
value: this.tokens[index]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getTemplateEvents(index) {
|
|
|
|
return {
|
|
|
|
input: (event) => this.onInput(event, index),
|
|
|
|
keydown: (event) => this.onKeyDown(event),
|
|
|
|
focus: (event) => this.onFocus(event),
|
|
|
|
blur: (event) => this.onBlur(event),
|
|
|
|
paste: (event) => this.onPaste(event)
|
|
|
|
};
|
|
|
|
},
|
2024-02-21 18:38:16 +00:00
|
|
|
getPTOptions(key) {
|
|
|
|
const _ptm = key === 'root' ? this.ptmi : this.ptm;
|
|
|
|
|
|
|
|
return _ptm(key, {
|
|
|
|
context: {
|
|
|
|
checked: this.checked,
|
|
|
|
disabled: this.disabled
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-02-22 06:40:19 +00:00
|
|
|
onInput(event, index) {
|
|
|
|
this.tokens[index] = event.target.value;
|
2024-02-21 18:38:16 +00:00
|
|
|
this.updateModel(event);
|
2024-02-22 06:40:19 +00:00
|
|
|
|
|
|
|
if (event.inputType === 'deleteContentBackward') {
|
|
|
|
this.moveToPrev(event);
|
|
|
|
} else if (event.inputType === 'insertText' || event.inputType === 'deleteContentForward') {
|
|
|
|
this.moveToNext(event);
|
|
|
|
}
|
2024-02-21 18:38:16 +00:00
|
|
|
},
|
|
|
|
updateModel(event) {
|
|
|
|
const newValue = this.tokens.join('');
|
|
|
|
|
|
|
|
this.$emit('update:modelValue', newValue);
|
|
|
|
this.$emit('change', {
|
|
|
|
originalEvent: event,
|
|
|
|
value: newValue
|
|
|
|
});
|
|
|
|
},
|
|
|
|
moveToPrev(event) {
|
2024-02-22 09:19:35 +00:00
|
|
|
let prevInput = this.findPrevInput(event.target);
|
2024-02-21 18:38:16 +00:00
|
|
|
|
2024-02-22 07:42:48 +00:00
|
|
|
if (prevInput) {
|
|
|
|
prevInput.focus();
|
|
|
|
prevInput.select();
|
2024-02-21 18:38:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
moveToNext(event) {
|
2024-02-22 09:19:35 +00:00
|
|
|
let nextInput = this.findNextInput(event.target);
|
2024-02-21 18:38:16 +00:00
|
|
|
|
2024-02-22 07:42:48 +00:00
|
|
|
if (nextInput) {
|
|
|
|
nextInput.focus();
|
|
|
|
nextInput.select();
|
2024-02-21 18:38:16 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-22 07:42:48 +00:00
|
|
|
findNextInput(element) {
|
2024-02-22 09:19:35 +00:00
|
|
|
let nextElement = element.nextElementSibling;
|
|
|
|
|
|
|
|
if (!nextElement) return;
|
2024-02-22 07:42:48 +00:00
|
|
|
|
|
|
|
return nextElement.nodeName === 'INPUT' ? nextElement : this.findNextInput(nextElement);
|
|
|
|
},
|
|
|
|
findPrevInput(element) {
|
2024-02-22 09:19:35 +00:00
|
|
|
let prevElement = element.previousElementSibling;
|
|
|
|
|
|
|
|
if (!prevElement) return;
|
2024-02-22 07:42:48 +00:00
|
|
|
|
|
|
|
return prevElement.nodeName === 'INPUT' ? prevElement : this.findPrevInput(prevElement);
|
|
|
|
},
|
2024-02-21 18:38:16 +00:00
|
|
|
onFocus(event) {
|
|
|
|
event.target.select();
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
|
|
|
onBlur(event) {
|
|
|
|
this.$emit('blur', event);
|
|
|
|
},
|
2024-02-22 06:40:19 +00:00
|
|
|
onKeyDown(event) {
|
2024-02-21 18:38:16 +00:00
|
|
|
const keyCode = event.keyCode;
|
|
|
|
|
|
|
|
switch (keyCode) {
|
|
|
|
case 37:
|
|
|
|
this.moveToPrev(event);
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 38:
|
|
|
|
case 40:
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-02-22 07:17:13 +00:00
|
|
|
case 8:
|
|
|
|
if (event.target.value.length === 0) {
|
|
|
|
this.moveToPrev(event);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-02-21 18:38:16 +00:00
|
|
|
case 39:
|
|
|
|
this.moveToNext(event);
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2024-03-13 13:52:23 +00:00
|
|
|
if ((this.integerOnly && !((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) || (this.tokens.join('').length >= this.length && event.keyCode != 46)) {
|
2024-03-07 05:03:38 +00:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
2024-03-07 05:14:01 +00:00
|
|
|
|
2024-02-21 18:38:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onPaste(event) {
|
|
|
|
let paste = event.clipboardData.getData('text');
|
|
|
|
|
|
|
|
if (paste.length) {
|
|
|
|
let pastedCode = paste.substring(0, this.length + 1);
|
|
|
|
|
2024-03-08 10:19:36 +00:00
|
|
|
if (!this.integerOnly || !isNaN(pastedCode)) {
|
2024-02-21 18:38:16 +00:00
|
|
|
this.tokens = pastedCode.split('');
|
|
|
|
this.updateModel(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
inputMode() {
|
2024-03-07 09:01:21 +00:00
|
|
|
return this.integerOnly ? 'numeric' : 'text';
|
2024-02-21 18:38:16 +00:00
|
|
|
},
|
|
|
|
inputType() {
|
|
|
|
return this.mask ? 'password' : 'text';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
OtpInputText: InputText
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|