Add template sample to inputotp

pull/5323/head
Cagatay Civici 2024-02-22 09:40:19 +03:00
parent 067da063a8
commit abfd15d075
3 changed files with 169 additions and 26 deletions

View File

@ -1,20 +1,21 @@
<template> <template>
<div :class="cx('root')" v-bind="ptmi('root')"> <div :class="cx('root')" v-bind="ptmi('root')">
<OtpInputText <template v-for="i in length" :key="i">
v-for="i in length" <slot :events="getTemplateEvents(i - 1)" :attrs="getTemplateAttrs(i - 1)">
:key="i" <OtpInputText
v-model="tokens[i - 1]" :value="tokens[i - 1]"
:type="inputType" :type="inputType"
:class="cx('input')" :class="cx('input')"
maxlength="1" :inputmode="inputMode"
:inputmode="inputMode" @input="onInput($event, i - 1)"
@input="onInput" @focus="onFocus($event)"
@focus="onFocus" @blur="onBlur($event)"
@blur="onBlur" @paste="onPaste($event)"
@paste="onPaste" @keydown="onKeyDown($event)"
@keydown="onKeyDown($event, i - 1)" :pt="ptm('input')"
:pt="ptm('input')" />
/> </slot>
</template>
</div> </div>
</template> </template>
@ -41,6 +42,20 @@ export default {
} }
}, },
methods: { methods: {
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)
};
},
getPTOptions(key) { getPTOptions(key) {
const _ptm = key === 'root' ? this.ptmi : this.ptm; const _ptm = key === 'root' ? this.ptmi : this.ptm;
@ -51,9 +66,15 @@ export default {
} }
}); });
}, },
onInput(event) { onInput(event, index) {
this.moveToNext(event); this.tokens[index] = event.target.value;
this.updateModel(event); this.updateModel(event);
if (event.inputType === 'deleteContentBackward') {
this.moveToPrev(event);
} else if (event.inputType === 'insertText' || event.inputType === 'deleteContentForward') {
this.moveToNext(event);
}
}, },
updateModel(event) { updateModel(event) {
const newValue = this.tokens.join(''); const newValue = this.tokens.join('');
@ -87,7 +108,7 @@ export default {
onBlur(event) { onBlur(event) {
this.$emit('blur', event); this.$emit('blur', event);
}, },
onKeyDown(event, index) { onKeyDown(event) {
const keyCode = event.keyCode; const keyCode = event.keyCode;
switch (keyCode) { switch (keyCode) {
@ -109,14 +130,6 @@ export default {
break; break;
case 8:
event.preventDefault();
this.tokens[index] = '';
this.moveToPrev(event);
this.updateModel(event);
break;
default: default:
if (this.integerOnly && !(event.keyCode >= 48 && event.keyCode <= 57)) { if (this.integerOnly && !(event.keyCode >= 48 && event.keyCode <= 57)) {
event.preventDefault(); event.preventDefault();

View File

@ -0,0 +1,124 @@
<template>
<DocSectionText v-bind="$attrs">
<p>Define a template with your own UI elements with bindings to the provided events and attributes to replace the default design.</p>
</DocSectionText>
<div class="card flex justify-content-center">
<InputOtp v-model="value">
<template #default="{ attrs, events }">
<input type="text" v-bind="attrs" v-on="events" class="custom-otp-input" />
</template>
</InputOtp>
</div>
<DocSectionCode :code="code" />
</template>
<script>
export default {
data() {
return {
value: null,
code: {
basic: `
<InputOtp v-model="value">
<template #default="{ attrs, events }">
<input type="text" v-bind="attrs" v-on="events" class="custom-otp-input" />
</template>
</InputOtp>
`,
options: `
<template>
<div class="card flex justify-content-center">
<InputOtp v-model="value">
<template #default="{ attrs, events }">
<input type="text" v-bind="attrs" v-on="events" class="custom-otp-input" />
</template>
</InputOtp>
</div>
</template>
<script>
export default {
data() {
return {
value: null
}
}
};
<\/script>
<style scoped>
.custom-otp-input {
width: 40px;
font-size: 36px;
border: 0 none;
appearance: none;
text-align: center;
transition: all 0.2s;
background: transparent;
border-bottom: 2px solid var(--surface-500);
}
.custom-otp-input:focus {
outline: 0 none;
border-bottom-color: var(--primary-color);
}
<\/style>
`,
composition: `
<template>
<div class="card flex justify-content-center">
<InputOtp v-model="value">
<template #default="{ attrs, events }">
<input type="text" v-bind="attrs" v-on="events" class="custom-otp-input" />
</template>
</InputOtp>
</div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(null);
<\/script>
<style scoped>
.custom-otp-input {
width: 40px;
font-size: 36px;
border: 0 none;
appearance: none;
text-align: center;
transition: all 0.2s;
background: transparent;
border-bottom: 2px solid var(--surface-500);
}
.custom-otp-input:focus {
outline: 0 none;
border-bottom-color: var(--primary-color);
}
<\/style>
`
}
};
}
};
</script>
<style scoped>
.custom-otp-input {
width: 40px;
font-size: 36px;
border: 0 none;
appearance: none;
text-align: center;
transition: all 0.2s;
background: transparent;
border-bottom: 2px solid var(--surface-500);
}
.custom-otp-input:focus {
outline: 0 none;
border-bottom-color: var(--primary-color);
}
</style>

View File

@ -7,6 +7,7 @@ import AccessibilityDoc from '@/doc/inputotp/AccessibilityDoc.vue';
import BasicDoc from '@/doc/inputotp/BasicDoc.vue'; import BasicDoc from '@/doc/inputotp/BasicDoc.vue';
import ImportDoc from '@/doc/inputotp/ImportDoc.vue'; import ImportDoc from '@/doc/inputotp/ImportDoc.vue';
import MaskDoc from '@/doc/inputotp/MaskDoc.vue'; import MaskDoc from '@/doc/inputotp/MaskDoc.vue';
import TemplateDoc from '@/doc/inputotp/TemplateDoc.vue';
import IntegerOnlyDoc from '@/doc/inputotp/integerOnlyDoc.vue'; import IntegerOnlyDoc from '@/doc/inputotp/integerOnlyDoc.vue';
import PTComponent from '@/doc/inputotp/pt/index.vue'; import PTComponent from '@/doc/inputotp/pt/index.vue';
import ThemingDoc from '@/doc/inputotp/theming/index.vue'; import ThemingDoc from '@/doc/inputotp/theming/index.vue';
@ -35,6 +36,11 @@ export default {
label: 'Integer Only', label: 'Integer Only',
component: IntegerOnlyDoc component: IntegerOnlyDoc
}, },
{
id: 'template',
label: 'Template',
component: TemplateDoc
},
{ {
id: 'accessibility', id: 'accessibility',
label: 'Accessibility', label: 'Accessibility',