mirror of
https://github.com/primefaces/primevue.git
synced 2025-05-09 00:42:36 +00:00
Initiated InputOtp
This commit is contained in:
parent
c555e258c0
commit
067da063a8
102 changed files with 2228 additions and 1 deletions
53
components/lib/inputotp/BaseInputOtp.vue
Normal file
53
components/lib/inputotp/BaseInputOtp.vue
Normal file
|
@ -0,0 +1,53 @@
|
|||
<script>
|
||||
import BaseComponent from 'primevue/basecomponent';
|
||||
import InputOtpStyle from 'primevue/inputotp/style';
|
||||
|
||||
export default {
|
||||
name: 'BaseInputOtp',
|
||||
extends: BaseComponent,
|
||||
props: {
|
||||
modelValue: {
|
||||
type: null,
|
||||
default: false
|
||||
},
|
||||
invalid: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
readonly: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
variant: {
|
||||
type: String,
|
||||
default: null
|
||||
},
|
||||
tabindex: {
|
||||
type: Number,
|
||||
default: null
|
||||
},
|
||||
length: {
|
||||
type: Number,
|
||||
default: 4
|
||||
},
|
||||
mask: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
integerOnly: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
style: InputOtpStyle,
|
||||
provide() {
|
||||
return {
|
||||
$parentInstance: this
|
||||
};
|
||||
}
|
||||
};
|
||||
</script>
|
227
components/lib/inputotp/InputOtp.d.ts
vendored
Executable file
227
components/lib/inputotp/InputOtp.d.ts
vendored
Executable file
|
@ -0,0 +1,227 @@
|
|||
/**
|
||||
*
|
||||
* InputSwitch is used to select a boolean value.
|
||||
*
|
||||
* [Live Demo](https://www.primevue.org/inputswitch/)
|
||||
*
|
||||
* @module inputotp
|
||||
*
|
||||
*/
|
||||
import { ComponentHooks } from '../basecomponent/BaseComponent';
|
||||
import { PassThroughOptions } from '../passthrough';
|
||||
import { ClassComponent, GlobalComponentConstructor, PassThrough } from '../ts-helpers';
|
||||
|
||||
export declare type InputSwitchPassThroughOptionType = InputSwitchPassThroughAttributes | ((options: InputSwitchPassThroughMethodOptions) => InputSwitchPassThroughAttributes | string) | string | null | undefined;
|
||||
|
||||
/**
|
||||
* Custom passthrough(pt) option method.
|
||||
*/
|
||||
export interface InputSwitchPassThroughMethodOptions {
|
||||
/**
|
||||
* Defines instance.
|
||||
*/
|
||||
instance: any;
|
||||
/**
|
||||
* Defines valid properties.
|
||||
*/
|
||||
props: InputSwitchProps;
|
||||
/**
|
||||
* Defines current inline state.
|
||||
*/
|
||||
state: InputSwitchState;
|
||||
/**
|
||||
* Defines current options.
|
||||
*/
|
||||
context: InputSwitchContext;
|
||||
/**
|
||||
* Defines valid attributes.
|
||||
*/
|
||||
attrs: any;
|
||||
/**
|
||||
* Defines parent options.
|
||||
*/
|
||||
parent: any;
|
||||
/**
|
||||
* Defines passthrough(pt) options in global config.
|
||||
*/
|
||||
global: object | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom passthrough(pt) options.
|
||||
* @see {@link InputSwitchProps.pt}
|
||||
*/
|
||||
export interface InputSwitchPassThroughOptions {
|
||||
/**
|
||||
* Used to pass attributes to the root's DOM element.
|
||||
*/
|
||||
root?: InputSwitchPassThroughOptionType;
|
||||
/**
|
||||
* Used to pass attributes to the input's DOM element.
|
||||
*/
|
||||
input?: InputSwitchPassThroughOptionType;
|
||||
/**
|
||||
* Used to pass attributes to the slider's DOM element.
|
||||
*/
|
||||
slider?: InputSwitchPassThroughOptionType;
|
||||
/**
|
||||
* Used to manage all lifecycle hooks.
|
||||
* @see {@link BaseComponent.ComponentHooks}
|
||||
*/
|
||||
hooks?: ComponentHooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom passthrough attributes for each DOM elements
|
||||
*/
|
||||
export interface InputSwitchPassThroughAttributes {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines current inline state in InputSwitch component.
|
||||
*/
|
||||
export interface InputSwitchState {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines valid properties in InputSwitch component.
|
||||
*/
|
||||
export interface InputSwitchProps {
|
||||
/**
|
||||
* Specifies whether a inputswitch should be checked or not.
|
||||
* @defaultValue false
|
||||
*/
|
||||
modelValue?: boolean | string | undefined;
|
||||
/**
|
||||
* Value in checked state.
|
||||
* @defaultValue true
|
||||
*/
|
||||
trueValue?: any;
|
||||
/**
|
||||
* Value in unchecked state.
|
||||
* @defaultValue false
|
||||
*/
|
||||
falseValue?: any;
|
||||
/**
|
||||
* When present, it specifies that the component should have invalid state style.
|
||||
* @defaultValue false
|
||||
*/
|
||||
invalid?: boolean | undefined;
|
||||
/**
|
||||
* When present, it specifies that the component should be disabled.
|
||||
* @defaultValue false
|
||||
*/
|
||||
disabled?: boolean | undefined;
|
||||
/**
|
||||
* When present, it specifies that an input field is read-only.
|
||||
* @default false
|
||||
*/
|
||||
readonly?: boolean | undefined;
|
||||
/**
|
||||
* Index of the element in tabbing order.
|
||||
*/
|
||||
tabindex?: number | undefined;
|
||||
/**
|
||||
* Identifier of the underlying input element.
|
||||
*/
|
||||
inputId?: string | undefined;
|
||||
/**
|
||||
* Style class of the input field.
|
||||
*/
|
||||
inputClass?: string | object | undefined;
|
||||
/**
|
||||
* Inline style of the input field.
|
||||
*/
|
||||
inputStyle?: object | undefined;
|
||||
/**
|
||||
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
|
||||
*/
|
||||
ariaLabelledby?: string | undefined;
|
||||
/**
|
||||
* Establishes a string value that labels the component.
|
||||
*/
|
||||
ariaLabel?: string | undefined;
|
||||
/**
|
||||
* Used to pass attributes to DOM elements inside the component.
|
||||
* @type {InputSwitchPassThroughOptions}
|
||||
*/
|
||||
pt?: PassThrough<InputSwitchPassThroughOptions>;
|
||||
/**
|
||||
* Used to configure passthrough(pt) options of the component.
|
||||
* @type {PassThroughOptions}
|
||||
*/
|
||||
ptOptions?: PassThroughOptions;
|
||||
/**
|
||||
* When enabled, it removes component related styles in the core.
|
||||
* @defaultValue false
|
||||
*/
|
||||
unstyled?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines current options in InputSwitch component.
|
||||
*/
|
||||
export interface InputSwitchContext {
|
||||
/**
|
||||
* Current checked state of the item as a boolean.
|
||||
* @defaultValue false
|
||||
*/
|
||||
checked: boolean;
|
||||
/**
|
||||
* Current disabled state of the item as a boolean.
|
||||
* @defaultValue false
|
||||
*/
|
||||
disabled: boolean;
|
||||
}
|
||||
|
||||
export interface InputSwitchSlots {}
|
||||
|
||||
/**
|
||||
* Defines valid emits in InputSwitch component.
|
||||
*/
|
||||
export interface InputSwitchEmits {
|
||||
/**
|
||||
* Emitted when the value changes.
|
||||
* @param {boolean} value - New value.
|
||||
*/
|
||||
'update:modelValue'(value: boolean): void;
|
||||
/**
|
||||
* Callback to invoke on value change.
|
||||
* @param {Event} event - Browser event.
|
||||
*/
|
||||
change(event: Event): void;
|
||||
/**
|
||||
* Callback to invoke when the component receives focus.
|
||||
* @param {Event} event - Browser event.
|
||||
*/
|
||||
focus(event: Event): void;
|
||||
/**
|
||||
* Callback to invoke when the component loses focus.
|
||||
* @param {Event} event - Browser event.
|
||||
*/
|
||||
blur(event: Event): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* **PrimeVue - InputSwitch**
|
||||
*
|
||||
* _InputSwitch is used to select a boolean value._
|
||||
*
|
||||
* [Live Demo](https://www.primevue.org/inputswitch/)
|
||||
* --- ---
|
||||
* 
|
||||
*
|
||||
* @group Component
|
||||
*
|
||||
*/
|
||||
declare class InputSwitch extends ClassComponent<InputSwitchProps, InputSwitchSlots, InputSwitchEmits> {}
|
||||
|
||||
declare module '@vue/runtime-core' {
|
||||
interface GlobalComponents {
|
||||
InputSwitch: GlobalComponentConstructor<InputSwitch>;
|
||||
}
|
||||
}
|
||||
|
||||
export default InputSwitch;
|
20
components/lib/inputotp/InputOtp.spec.js
Normal file
20
components/lib/inputotp/InputOtp.spec.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { mount } from '@vue/test-utils';
|
||||
import InputSwitch from './InputSwitch.vue';
|
||||
|
||||
describe('InputSwitch.vue', () => {
|
||||
it('should exist', async () => {
|
||||
const wrapper = mount(InputSwitch);
|
||||
|
||||
expect(wrapper.find('.p-inputswitch.p-component').exists()).toBe(true);
|
||||
expect(wrapper.find('.p-inputswitch-slider').exists()).toBe(true);
|
||||
|
||||
await wrapper.vm.onChange({});
|
||||
|
||||
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([true]);
|
||||
|
||||
await wrapper.setProps({ modelValue: true });
|
||||
|
||||
expect(wrapper.vm.checked).toBe(true);
|
||||
expect(wrapper.find('.p-inputswitch').classes()).toContain('p-highlight');
|
||||
});
|
||||
});
|
155
components/lib/inputotp/InputOtp.vue
Executable file
155
components/lib/inputotp/InputOtp.vue
Executable file
|
@ -0,0 +1,155 @@
|
|||
<template>
|
||||
<div :class="cx('root')" v-bind="ptmi('root')">
|
||||
<OtpInputText
|
||||
v-for="i in length"
|
||||
:key="i"
|
||||
v-model="tokens[i - 1]"
|
||||
:type="inputType"
|
||||
:class="cx('input')"
|
||||
maxlength="1"
|
||||
:inputmode="inputMode"
|
||||
@input="onInput"
|
||||
@focus="onFocus"
|
||||
@blur="onBlur"
|
||||
@paste="onPaste"
|
||||
@keydown="onKeyDown($event, i - 1)"
|
||||
:pt="ptm('input')"
|
||||
/>
|
||||
</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: {
|
||||
getPTOptions(key) {
|
||||
const _ptm = key === 'root' ? this.ptmi : this.ptm;
|
||||
|
||||
return _ptm(key, {
|
||||
context: {
|
||||
checked: this.checked,
|
||||
disabled: this.disabled
|
||||
}
|
||||
});
|
||||
},
|
||||
onInput(event) {
|
||||
this.moveToNext(event);
|
||||
this.updateModel(event);
|
||||
},
|
||||
updateModel(event) {
|
||||
const newValue = this.tokens.join('');
|
||||
|
||||
this.$emit('update:modelValue', newValue);
|
||||
this.$emit('change', {
|
||||
originalEvent: event,
|
||||
value: newValue
|
||||
});
|
||||
},
|
||||
moveToPrev(event) {
|
||||
var prevElement = event.target.previousElementSibling;
|
||||
|
||||
if (prevElement) {
|
||||
prevElement.focus();
|
||||
prevElement.select();
|
||||
}
|
||||
},
|
||||
moveToNext(event) {
|
||||
var nextElement = event.target.nextElementSibling;
|
||||
|
||||
if (nextElement) {
|
||||
nextElement.focus();
|
||||
nextElement.select();
|
||||
}
|
||||
},
|
||||
onFocus(event) {
|
||||
event.target.select();
|
||||
this.$emit('focus', event);
|
||||
},
|
||||
onBlur(event) {
|
||||
this.$emit('blur', event);
|
||||
},
|
||||
onKeyDown(event, index) {
|
||||
const keyCode = event.keyCode;
|
||||
|
||||
switch (keyCode) {
|
||||
case 37:
|
||||
this.moveToPrev(event);
|
||||
event.preventDefault();
|
||||
|
||||
break;
|
||||
|
||||
case 38:
|
||||
case 40:
|
||||
event.preventDefault();
|
||||
|
||||
break;
|
||||
|
||||
case 39:
|
||||
this.moveToNext(event);
|
||||
event.preventDefault();
|
||||
|
||||
break;
|
||||
|
||||
case 8:
|
||||
event.preventDefault();
|
||||
this.tokens[index] = '';
|
||||
this.moveToPrev(event);
|
||||
this.updateModel(event);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if (this.integerOnly && !(event.keyCode >= 48 && event.keyCode <= 57)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
},
|
||||
onPaste(event) {
|
||||
let paste = event.clipboardData.getData('text');
|
||||
|
||||
if (paste.length) {
|
||||
let pastedCode = paste.substring(0, this.length + 1);
|
||||
|
||||
if (!this.isIntegerOnly || !isNaN(pastedCode)) {
|
||||
this.tokens = pastedCode.split('');
|
||||
this.updateModel(event);
|
||||
}
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
inputMode() {
|
||||
return this.integerOnly ? 'number' : 'text';
|
||||
},
|
||||
inputType() {
|
||||
return this.mask ? 'password' : 'text';
|
||||
}
|
||||
},
|
||||
components: {
|
||||
OtpInputText: InputText
|
||||
}
|
||||
};
|
||||
</script>
|
9
components/lib/inputotp/package.json
Normal file
9
components/lib/inputotp/package.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"main": "./inputotp.cjs.js",
|
||||
"module": "./inputotp.esm.js",
|
||||
"unpkg": "./inputotp.min.js",
|
||||
"types": "./inputotp.d.ts",
|
||||
"browser": {
|
||||
"./sfc": "./inputotp.vue"
|
||||
}
|
||||
}
|
3
components/lib/inputotp/style/InputOtpStyle.d.ts
vendored
Normal file
3
components/lib/inputotp/style/InputOtpStyle.d.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { BaseStyle } from '../../base/style/BaseStyle';
|
||||
|
||||
export interface InputOtpStyle extends BaseStyle {}
|
11
components/lib/inputotp/style/InputOtpStyle.js
Normal file
11
components/lib/inputotp/style/InputOtpStyle.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import BaseStyle from 'primevue/base/style';
|
||||
|
||||
const classes = {
|
||||
root: ({ props }) => ['p-inputotp p-component'],
|
||||
input: 'p-inputotp-input'
|
||||
};
|
||||
|
||||
export default BaseStyle.extend({
|
||||
name: 'inputotp',
|
||||
classes
|
||||
});
|
6
components/lib/inputotp/style/package.json
Normal file
6
components/lib/inputotp/style/package.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"main": "./inputotpstyle.cjs.js",
|
||||
"module": "./inputotpstyle.esm.js",
|
||||
"unpkg": "./inputotpstyle.min.js",
|
||||
"types": "./inputotpstyle.d.ts"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue