@nanoporetech-digital/components 8.7.1 → 8.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/{fade-NeUskjyB.js → fade-DBKqk3wh.js} +1 -1
- package/dist/cjs/{fullscreen-D5cgtwoy.js → fullscreen-lcdCtrG6.js} +1 -1
- package/dist/cjs/index-DGttnXif.js +4 -0
- package/dist/cjs/{lazyload-Dm1NcTJ1.js → lazyload-BjSSEx9f.js} +1 -1
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/nano-avatar_5.cjs.entry.js +7 -7
- package/dist/cjs/nano-collapsible-comparison.cjs.entry.js +1 -1
- package/dist/cjs/nano-components.cjs.js +1 -1
- package/dist/cjs/nano-icon-button_2.cjs.entry.js +2 -2
- package/dist/cjs/nano-input-otp.cjs.entry.js +710 -0
- package/dist/cjs/nano-intersection-observe.cjs.entry.js +1 -1
- package/dist/cjs/nano-masked-overflow.cjs.entry.js +3 -3
- package/dist/cjs/nano-menu-drawer.cjs.entry.js +2 -2
- package/dist/cjs/nano-more-less.cjs.entry.js +2 -2
- package/dist/cjs/nano-rating.cjs.entry.js +4 -4
- package/dist/cjs/nano-resize-observe.cjs.entry.js +2 -2
- package/dist/cjs/nano-slide.cjs.entry.js +1 -1
- package/dist/cjs/{nano-slides-ycRcUf0g.js → nano-slides-CryCLfsK.js} +7 -7
- package/dist/cjs/nano-slides.cjs.entry.js +1 -1
- package/dist/cjs/nano-sortable.cjs.entry.js +1 -1
- package/dist/cjs/nano-step-accordion.cjs.entry.js +1 -1
- package/dist/cjs/nano-step-breadcrumb.cjs.entry.js +2 -2
- package/dist/cjs/nano-sticker.cjs.entry.js +2 -2
- package/dist/cjs/nano-tab-content.cjs.entry.js +2 -2
- package/dist/cjs/nano-tab.cjs.entry.js +2 -2
- package/dist/cjs/nano-table.cjs.entry.js +2 -2
- package/dist/cjs/{page-dots--38yVfTZ.js → page-dots-DR1fLjRi.js} +1 -1
- package/dist/collection/collection-manifest.json +1 -0
- package/dist/collection/components/breadcrumb/breadcrumb.js +1 -1
- package/dist/collection/components/collapsible-comparison/collapsible-comparison.js +1 -1
- package/dist/collection/components/cta/cta.js +3 -3
- package/dist/collection/components/input-otp/input-otp-interface.js +1 -0
- package/dist/collection/components/input-otp/input-otp.css +184 -0
- package/dist/collection/components/input-otp/input-otp.js +1044 -0
- package/dist/collection/components/intersection-observe/intersection-observe.js +1 -1
- package/dist/collection/components/masked-overflow/masked-overflow.js +3 -3
- package/dist/collection/components/menu-drawer/menu-drawer.js +2 -2
- package/dist/collection/components/more-less/more-less.js +2 -2
- package/dist/collection/components/rating/rating.js +4 -4
- package/dist/collection/components/resize-observe/resize-observe.js +2 -2
- package/dist/collection/components/select/select.js +7 -7
- package/dist/collection/components/slides/slide.js +1 -1
- package/dist/collection/components/slides/slides.js +3 -3
- package/dist/collection/components/sortable/sortable.js +1 -1
- package/dist/collection/components/step-accordion/step-accordion.js +1 -1
- package/dist/collection/components/step-breadcrumb/step-breadcrumb.js +2 -2
- package/dist/collection/components/sticker/sticker.js +2 -2
- package/dist/collection/components/table/table.js +2 -2
- package/dist/collection/components/tabs/tab-content.js +2 -2
- package/dist/collection/components/tabs/tab.js +2 -2
- package/dist/collection/components/tooltip/tooltip.js +2 -2
- package/dist/collection/types/shared.js +0 -1
- package/dist/components/cta.js +1 -1
- package/dist/components/masked-overflow.js +3 -3
- package/dist/components/nano-collapsible-comparison.js +1 -1
- package/dist/components/nano-input-otp.d.ts +11 -0
- package/dist/components/nano-input-otp.js +749 -0
- package/dist/components/nano-intersection-observe.js +1 -1
- package/dist/components/nano-menu-drawer.js +2 -2
- package/dist/components/nano-more-less.js +2 -2
- package/dist/components/nano-rating.js +4 -4
- package/dist/components/nano-slide.js +1 -1
- package/dist/components/nano-sortable.js +1 -1
- package/dist/components/nano-step-accordion.js +1 -1
- package/dist/components/nano-step-breadcrumb.js +2 -2
- package/dist/components/nano-tab-content.js +2 -2
- package/dist/components/nano-tab.js +2 -2
- package/dist/components/nano-table.js +2 -2
- package/dist/components/resize-observe.js +2 -2
- package/dist/components/select.js +7 -7
- package/dist/components/slides.js +3 -3
- package/dist/components/sticker.js +2 -2
- package/dist/components/tooltip.js +2 -2
- package/dist/esm/{fade-BA6b-Fk9.js → fade-D980mOvm.js} +1 -1
- package/dist/esm/{fullscreen-De_yN4cg.js → fullscreen-CjInlYb9.js} +1 -1
- package/dist/esm/index-BM3Om9WE.js +4 -0
- package/dist/esm/{lazyload-Cl8k3tPs.js → lazyload-B9B4e3ZI.js} +1 -1
- package/dist/esm/loader.js +1 -1
- package/dist/esm/nano-avatar_5.entry.js +7 -7
- package/dist/esm/nano-collapsible-comparison.entry.js +1 -1
- package/dist/esm/nano-components.js +1 -1
- package/dist/esm/nano-icon-button_2.entry.js +2 -2
- package/dist/esm/nano-input-otp.entry.js +708 -0
- package/dist/esm/nano-intersection-observe.entry.js +1 -1
- package/dist/esm/nano-masked-overflow.entry.js +3 -3
- package/dist/esm/nano-menu-drawer.entry.js +2 -2
- package/dist/esm/nano-more-less.entry.js +2 -2
- package/dist/esm/nano-rating.entry.js +4 -4
- package/dist/esm/nano-resize-observe.entry.js +2 -2
- package/dist/esm/nano-slide.entry.js +1 -1
- package/dist/esm/{nano-slides-GrbOydfa.js → nano-slides-xbY6lXX9.js} +7 -7
- package/dist/esm/nano-slides.entry.js +1 -1
- package/dist/esm/nano-sortable.entry.js +1 -1
- package/dist/esm/nano-step-accordion.entry.js +1 -1
- package/dist/esm/nano-step-breadcrumb.entry.js +2 -2
- package/dist/esm/nano-sticker.entry.js +2 -2
- package/dist/esm/nano-tab-content.entry.js +2 -2
- package/dist/esm/nano-tab.entry.js +2 -2
- package/dist/esm/nano-table.entry.js +2 -2
- package/dist/esm/{page-dots-CSf81OuJ.js → page-dots-CCfERISy.js} +1 -1
- package/dist/nano-components/{fade-BA6b-Fk9.js → fade-D980mOvm.js} +1 -1
- package/dist/nano-components/{fullscreen-De_yN4cg.js → fullscreen-CjInlYb9.js} +1 -1
- package/dist/nano-components/lazyload-B9B4e3ZI.js +4 -0
- package/dist/nano-components/nano-avatar_5.entry.js +1 -1
- package/dist/nano-components/nano-collapsible-comparison.entry.js +1 -1
- package/dist/nano-components/nano-components.esm.js +1 -1
- package/dist/nano-components/nano-icon-button_2.entry.js +1 -1
- package/dist/nano-components/nano-input-otp.entry.js +4 -0
- package/dist/nano-components/nano-intersection-observe.entry.js +1 -1
- package/dist/nano-components/nano-masked-overflow.entry.js +1 -1
- package/dist/nano-components/nano-menu-drawer.entry.js +1 -1
- package/dist/nano-components/nano-more-less.entry.js +1 -1
- package/dist/nano-components/nano-rating.entry.js +1 -1
- package/dist/nano-components/nano-resize-observe.entry.js +1 -1
- package/dist/nano-components/nano-slide.entry.js +1 -1
- package/dist/nano-components/{nano-slides-GrbOydfa.js → nano-slides-xbY6lXX9.js} +3 -3
- package/dist/nano-components/nano-slides.entry.js +1 -1
- package/dist/nano-components/nano-sortable.entry.js +1 -1
- package/dist/nano-components/nano-step-accordion.entry.js +1 -1
- package/dist/nano-components/nano-step-breadcrumb.entry.js +1 -1
- package/dist/nano-components/nano-sticker.entry.js +1 -1
- package/dist/nano-components/nano-tab-content.entry.js +1 -1
- package/dist/nano-components/nano-tab.entry.js +1 -1
- package/dist/nano-components/nano-table.entry.js +1 -1
- package/dist/nano-components/{page-dots-CSf81OuJ.js → page-dots-CCfERISy.js} +1 -1
- package/dist/types/components/cta/cta.d.ts +1 -1
- package/dist/types/components/input-otp/input-otp-interface.d.ts +24 -0
- package/dist/types/components/input-otp/input-otp.d.ts +281 -0
- package/dist/types/components.d.ts +181 -2
- package/docs-json.json +618 -11
- package/docs-vscode.json +80 -9
- package/hydrate/index.js +781 -45
- package/hydrate/index.mjs +781 -45
- package/package.json +2 -2
- package/dist/nano-components/lazyload-Cl8k3tPs.js +0 -4
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/generate-vue-component.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/index.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/output-vue.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/plugin.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/types.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/utils.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/stencil.config.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/stencil.config.prod.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/testing/mocks/intersection-observer.d.ts +0 -0
- /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/wdio.conf.d.ts +0 -0
@@ -0,0 +1,749 @@
|
|
1
|
+
/*!
|
2
|
+
* Custom elements for Nanopore-Digital Web applications
|
3
|
+
*/
|
4
|
+
import { proxyCustomElement, HTMLElement, createEvent, h, Host, Fragment } from '@stencil/core/internal/client';
|
5
|
+
import { d as defineCustomElement$2 } from './icon.js';
|
6
|
+
|
7
|
+
const inputOtpCss = ".sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{box-sizing:border-box}[hidden].sc-nano-input-otp{display:none !important}@media (prefers-reduced-motion: reduce){.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{animation-duration:0.01ms !important;animation-iteration-count:1 !important;transition-duration:0.01ms !important;scroll-behavior:auto !important}}.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{box-sizing:border-box}[hidden].sc-nano-input-otp{display:none !important}@media (prefers-reduced-motion: reduce){.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{animation-duration:0.01ms !important;animation-iteration-count:1 !important;transition-duration:0.01ms !important;scroll-behavior:auto !important}}.sc-nano-input-otp-h{--margin-top:0;--margin-end:0;--margin-bottom:0;--margin-start:0;--padding-top:var(--nano-spacing-md);--padding-end:0;--padding-bottom:var(--nano-spacing-xl);--padding-start:0;--color:initial;--min-width:40px;--width:40px;--height:50px;--gap:8px;--separator-width:7px;--separator-height:1px;--separator-border-radius:0;--separator-color:var(--nano-color-primary-1000);--highlight-color-valid:var(--nano-color-success-1100);--highlight-color-invalid:var(--nano-color-warning-1100);--border-radius:8px;--border-color:var(--nano-color-neutral-700);--border-width:1px;display:block;position:relative}.input-otp-group.sc-nano-input-otp{margin:var(--margin-top) var(--margin-end) var(--margin-bottom) var(--margin-start);padding:var(--padding-top) var(--padding-end) var(--padding-bottom) var(--padding-start);display:flex;align-items:center;justify-content:center;gap:var(--gap)}.native-wrapper.sc-nano-input-otp{display:flex;align-items:center;justify-content:center;min-inline-size:var(--min-width)}.native-input.sc-nano-input-otp{border-radius:var(--border-radius);inline-size:var(--width);min-inline-size:inherit;block-size:var(--height);border-width:var(--border-width);border-style:solid;border-color:var(--border-color);background:var(--background);color:var(--color);font-size:inherit;text-align:center;appearance:none}.input-otp-description.sc-nano-input-otp{color:currentcolor;font-size:var(--nano-font-size-xs);line-height:var(--nano-line-height-normal);text-align:center;position:relative;inset-inline-start:-24px}.input-otp-description-hidden.sc-nano-input-otp{display:none}.input-otp-separator.sc-nano-input-otp{border-radius:var(--separator-border-radius);flex-shrink:0;inline-size:var(--separator-width);block-size:var(--separator-height);background:var(--separator-color)}.input-otp-disabled.sc-nano-input-otp-h{--color:var(--nano-color-neutral-700);opacity:0.5}.input-otp-disabled.sc-nano-input-otp-h,.input-otp-disabled.sc-nano-input-otp-h .native-input.sc-nano-input-otp:disabled{cursor:not-allowed}.has-focus.sc-nano-input-otp-h .native-input.sc-nano-input-otp:focus{outline:var(--nano-focus-ring)}.nano-invalid.sc-nano-input-otp-h .native-input.sc-nano-input-otp{border-color:var(--highlight-color-invalid)}.nano-valid.sc-nano-input-otp-h .native-input.sc-nano-input-otp{border-color:var(--highlight-color-valid)}.input-otp-icon.sc-nano-input-otp{inline-size:24px;block-size:24px;display:flex;align-items:center}.input-otp-icon--valid.sc-nano-input-otp{color:var(--nano-color-success-1100)}.input-otp-icon--invalid.sc-nano-input-otp{color:var(--nano-color-warning-1100)}";
|
8
|
+
|
9
|
+
const InputOTP = /*@__PURE__*/ proxyCustomElement(class InputOTP extends HTMLElement {
|
10
|
+
constructor() {
|
11
|
+
super();
|
12
|
+
this.__registerHost();
|
13
|
+
this.nanoInput = createEvent(this, "nanoInput", 7);
|
14
|
+
this.nanoChange = createEvent(this, "nanoChange", 7);
|
15
|
+
this.nanoComplete = createEvent(this, "nanoComplete", 7);
|
16
|
+
this.nanoBlur = createEvent(this, "nanoBlur", 7);
|
17
|
+
this.nanoFocus = createEvent(this, "nanoFocus", 7);
|
18
|
+
}
|
19
|
+
inputRefs = [];
|
20
|
+
inputId = `nano-input-otp-${inputIds++}`;
|
21
|
+
/**
|
22
|
+
* Stores the initial value of the input when it receives focus.
|
23
|
+
* Used to determine if the value changed during the focus session
|
24
|
+
* to avoid emitting unnecessary change events on blur.
|
25
|
+
*/
|
26
|
+
focusedValue;
|
27
|
+
/**
|
28
|
+
* Tracks whether the user is navigating through input boxes using keyboard navigation
|
29
|
+
* (arrow keys, tab) versus mouse clicks. This is used to determine the appropriate
|
30
|
+
* focus behavior when an input box is focused.
|
31
|
+
*/
|
32
|
+
isKeyboardNavigation = false;
|
33
|
+
get el() { return this; }
|
34
|
+
inputValues = [];
|
35
|
+
hasFocus = false;
|
36
|
+
previousInputValues = [];
|
37
|
+
invalid = false;
|
38
|
+
valid = false;
|
39
|
+
/**
|
40
|
+
* If `true`, the user cannot interact with the input.
|
41
|
+
*/
|
42
|
+
disabled = false;
|
43
|
+
/**
|
44
|
+
* A hint to the browser for which keyboard to display.
|
45
|
+
* Possible values: `"none"`, `"text"`, `"tel"`, `"url"`,
|
46
|
+
* `"email"`, `"numeric"`, `"decimal"`, and `"search"`.
|
47
|
+
*
|
48
|
+
* For numbers (type="number"): "numeric"
|
49
|
+
* For text (type="text"): "text"
|
50
|
+
*/
|
51
|
+
inputmode;
|
52
|
+
/**
|
53
|
+
* The number of input boxes to display.
|
54
|
+
*/
|
55
|
+
length = 6;
|
56
|
+
/**
|
57
|
+
* A regex pattern string for allowed characters. Defaults based on type.
|
58
|
+
*
|
59
|
+
* For numbers (`type="number"`): `"[\p{N}]"`
|
60
|
+
* For text (`type="text"`): `"[\p{L}\p{N}]"`
|
61
|
+
*/
|
62
|
+
pattern;
|
63
|
+
/**
|
64
|
+
* If `true`, the user cannot modify the value.
|
65
|
+
*/
|
66
|
+
readonly = false;
|
67
|
+
/**
|
68
|
+
* The type of input allowed in the input boxes.
|
69
|
+
*/
|
70
|
+
type = 'number';
|
71
|
+
/**
|
72
|
+
* The value of the input group.
|
73
|
+
*/
|
74
|
+
value = '';
|
75
|
+
/**
|
76
|
+
* If `true`, allows the input to be completely filled with invalid values.
|
77
|
+
* By default, if any input box contains an invalid value, the entire input group
|
78
|
+
* is marked as invalid.
|
79
|
+
* Default is `false`.
|
80
|
+
*/
|
81
|
+
allowInvalid = false;
|
82
|
+
/**
|
83
|
+
* If `true`, shows a validity icon when all input boxes are filled.
|
84
|
+
* A check icon is shown if all values are valid, and an X icon is shown if any value is invalid.
|
85
|
+
* This does not affect the `invalid` or `valid` states of the input group.
|
86
|
+
* Default is `false`.
|
87
|
+
* */
|
88
|
+
showValidity = false;
|
89
|
+
/**
|
90
|
+
* The `nanoInput` event is fired each time the user modifies the input's value.
|
91
|
+
* Unlike the `nanoChange` event, the `nanoInput` event is fired for each alteration
|
92
|
+
* to the input's value. This typically happens for each keystroke as the user types.
|
93
|
+
*
|
94
|
+
* For elements that accept text input (`type=text`, `type=tel`, etc.), the interface
|
95
|
+
* is [`InputEvent`](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent); for others,
|
96
|
+
* the interface is [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event). If
|
97
|
+
* the input is cleared on edit, the type is `null`.
|
98
|
+
*/
|
99
|
+
nanoInput;
|
100
|
+
/**
|
101
|
+
* The `nanoChange` event is fired when the user modifies the input's value.
|
102
|
+
* Unlike the `nanoInput` event, the `nanoChange` event is only fired when changes
|
103
|
+
* are committed, not as the user types.
|
104
|
+
*
|
105
|
+
* The `nanoChange` event fires when the `<nano-input-otp>` component loses
|
106
|
+
* focus after its value has changed.
|
107
|
+
*
|
108
|
+
* This event will not emit when programmatically setting the `value` property.
|
109
|
+
*/
|
110
|
+
nanoChange;
|
111
|
+
/**
|
112
|
+
* Emitted when all input boxes have been filled with valid values.
|
113
|
+
*/
|
114
|
+
nanoComplete;
|
115
|
+
/**
|
116
|
+
* Emitted when the input group loses focus.
|
117
|
+
*/
|
118
|
+
nanoBlur;
|
119
|
+
/**
|
120
|
+
* Emitted when the input group has focus.
|
121
|
+
*/
|
122
|
+
nanoFocus;
|
123
|
+
/**
|
124
|
+
* Sets focus to an input box.
|
125
|
+
* @param index - The index of the input box to focus (0-based).
|
126
|
+
* If provided and the input box has a value, the input box at that index will be focused.
|
127
|
+
* Otherwise, the first empty input box or the last input if all are filled will be focused.
|
128
|
+
*/
|
129
|
+
async setFocus(index) {
|
130
|
+
if (typeof index === 'number') {
|
131
|
+
const validIndex = Math.max(0, Math.min(index, this.length - 1));
|
132
|
+
this.inputRefs[validIndex]?.focus();
|
133
|
+
}
|
134
|
+
else {
|
135
|
+
const tabbableIndex = this.getTabbableIndex();
|
136
|
+
this.inputRefs[tabbableIndex]?.focus();
|
137
|
+
}
|
138
|
+
}
|
139
|
+
valueChanged() {
|
140
|
+
this.initializeValues();
|
141
|
+
this.updateTabIndexes();
|
142
|
+
this.computeValidity();
|
143
|
+
}
|
144
|
+
componentWillLoad() {
|
145
|
+
this.initializeValues();
|
146
|
+
this.computeValidity();
|
147
|
+
}
|
148
|
+
componentDidLoad() {
|
149
|
+
this.updateTabIndexes();
|
150
|
+
}
|
151
|
+
/**
|
152
|
+
* Get the regex pattern for allowed characters.
|
153
|
+
* If a pattern is provided, use it to create a regex pattern
|
154
|
+
* Otherwise, use the default regex pattern based on type
|
155
|
+
*/
|
156
|
+
get validKeyPattern() {
|
157
|
+
return new RegExp(`^${this.getPattern()}$`, 'u');
|
158
|
+
}
|
159
|
+
get specialCharsPattern() {
|
160
|
+
return new RegExp(`^[\\p{S}\\p{P}]$`, 'u');
|
161
|
+
}
|
162
|
+
/**
|
163
|
+
* Gets the string pattern to pass to the input element
|
164
|
+
* and use in the regex for allowed characters.
|
165
|
+
* @returns {string} The regex pattern string for allowed characters.
|
166
|
+
*/
|
167
|
+
getPattern() {
|
168
|
+
const { pattern, type } = this;
|
169
|
+
if (pattern) {
|
170
|
+
return pattern;
|
171
|
+
}
|
172
|
+
return type === 'number' ? '[\\p{N}]' : '[\\p{L}\\p{N}]';
|
173
|
+
}
|
174
|
+
/**
|
175
|
+
* Get the default value for inputmode.
|
176
|
+
* If inputmode is provided, use it.
|
177
|
+
* Otherwise, use the default inputmode based on type
|
178
|
+
* @returns {string} The inputmode to use for the input boxes.
|
179
|
+
*/
|
180
|
+
getInputmode() {
|
181
|
+
const { inputmode } = this;
|
182
|
+
if (inputmode) {
|
183
|
+
return inputmode;
|
184
|
+
}
|
185
|
+
if (this.type == 'number') {
|
186
|
+
return 'numeric';
|
187
|
+
}
|
188
|
+
else {
|
189
|
+
return 'text';
|
190
|
+
}
|
191
|
+
}
|
192
|
+
/**
|
193
|
+
* Initializes the input values array based on the current value prop.
|
194
|
+
* This splits the value into individual characters and validates them against
|
195
|
+
* the allowed pattern. The values are then used as the values in the native
|
196
|
+
* input boxes and the value of the input group is updated.
|
197
|
+
*/
|
198
|
+
initializeValues() {
|
199
|
+
// Clear all input values
|
200
|
+
this.inputValues = Array(this.length).fill('');
|
201
|
+
// If the value is null, undefined, or an empty string, return
|
202
|
+
if (this.value == null || String(this.value).length === 0) {
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
// Split the value into individual characters and validate
|
206
|
+
// them against the allowed pattern if invalid characters are allowed, else just put them in
|
207
|
+
const chars = String(this.value).split('').slice(0, this.length);
|
208
|
+
chars.forEach((char, index) => {
|
209
|
+
if (this.allowInvalid && !this.specialCharsPattern.test(char)) {
|
210
|
+
this.inputValues[index] = char;
|
211
|
+
}
|
212
|
+
else if (this.validKeyPattern.test(char)) {
|
213
|
+
this.inputValues[index] = char;
|
214
|
+
}
|
215
|
+
});
|
216
|
+
// Update the value without emitting events
|
217
|
+
this.value = this.inputValues.join('');
|
218
|
+
this.previousInputValues = [...this.inputValues];
|
219
|
+
}
|
220
|
+
/**
|
221
|
+
* Returns `true` if the document or host element
|
222
|
+
* has a `dir` set to `rtl`. The host value will always
|
223
|
+
* take priority over the root document value.
|
224
|
+
* @returns {boolean} True if RTL direction is set, false otherwise.
|
225
|
+
*/
|
226
|
+
isRTL = (hostEl) => {
|
227
|
+
if (hostEl) {
|
228
|
+
if (hostEl.dir !== '') {
|
229
|
+
return hostEl.dir.toLowerCase() === 'rtl';
|
230
|
+
}
|
231
|
+
}
|
232
|
+
return document?.dir.toLowerCase() === 'rtl';
|
233
|
+
};
|
234
|
+
/**
|
235
|
+
* Updates the value of the input group.
|
236
|
+
* This updates the value of the input group and emits an `nanoChange` event.
|
237
|
+
* If all of the input boxes are filled, it emits an `nanoComplete` event.
|
238
|
+
*/
|
239
|
+
updateValue(event) {
|
240
|
+
const { inputValues, length } = this;
|
241
|
+
const newValue = this.getValidChars(inputValues.join('')).join('');
|
242
|
+
this.value = newValue;
|
243
|
+
this.computeValidity();
|
244
|
+
this.emitNanoInput(event);
|
245
|
+
if (newValue.length === length) {
|
246
|
+
this.nanoComplete.emit({ value: newValue, valid: this.valid });
|
247
|
+
}
|
248
|
+
}
|
249
|
+
/**
|
250
|
+
* Emits an `nanoChange` event.
|
251
|
+
* This API should be called for user committed changes.
|
252
|
+
* This API should not be used for external value changes.
|
253
|
+
*/
|
254
|
+
emitNanoChange(event) {
|
255
|
+
const { value } = this;
|
256
|
+
// Checks for both null and undefined values
|
257
|
+
const newValue = value == null ? value : value.toString();
|
258
|
+
this.computeValidity();
|
259
|
+
this.nanoChange.emit({
|
260
|
+
value: newValue?.toString(),
|
261
|
+
event,
|
262
|
+
valid: this.valid,
|
263
|
+
});
|
264
|
+
}
|
265
|
+
/**
|
266
|
+
* Emits an `nanoInput` event.
|
267
|
+
* This is used to emit the input value when the user types,
|
268
|
+
* backspaces, or pastes.
|
269
|
+
*/
|
270
|
+
emitNanoInput(event) {
|
271
|
+
const { value } = this;
|
272
|
+
// Checks for both null and undefined values
|
273
|
+
const newValue = value == null ? value : value.toString();
|
274
|
+
this.computeValidity();
|
275
|
+
this.nanoInput.emit({
|
276
|
+
value: newValue?.toString(),
|
277
|
+
event,
|
278
|
+
valid: this.valid,
|
279
|
+
});
|
280
|
+
}
|
281
|
+
/**
|
282
|
+
* Handles the focus behavior for the input OTP component.
|
283
|
+
*
|
284
|
+
* Focus behavior:
|
285
|
+
* 1. Keyboard navigation: Allow normal focus movement
|
286
|
+
* 2. Mouse click:
|
287
|
+
* - If clicked box has value: Focus that box
|
288
|
+
* - If clicked box is empty: Focus first empty box
|
289
|
+
*
|
290
|
+
* Emits the `nanoFocus` event when the input group gains focus.
|
291
|
+
* @returns {(event: FocusEvent) => void} Event handler for focus events.
|
292
|
+
*/
|
293
|
+
onFocus = (index) => (event) => {
|
294
|
+
const { inputRefs } = this;
|
295
|
+
// Only emit nanoFocus and set the focusedValue when the
|
296
|
+
// component first gains focus
|
297
|
+
if (!this.hasFocus) {
|
298
|
+
this.nanoFocus.emit(event);
|
299
|
+
this.focusedValue = this.value;
|
300
|
+
}
|
301
|
+
this.hasFocus = true;
|
302
|
+
let finalIndex = index;
|
303
|
+
if (!this.isKeyboardNavigation) {
|
304
|
+
// If the clicked box has a value, focus it
|
305
|
+
// Otherwise focus the first empty box
|
306
|
+
const targetIndex = this.inputValues[index]
|
307
|
+
? index
|
308
|
+
: this.getFirstEmptyIndex();
|
309
|
+
finalIndex = targetIndex === -1 ? this.length - 1 : targetIndex;
|
310
|
+
// Focus the target box
|
311
|
+
this.inputRefs[finalIndex]?.focus();
|
312
|
+
}
|
313
|
+
// Update tabIndexes to match the focused box
|
314
|
+
inputRefs.forEach((input, i) => {
|
315
|
+
input.tabIndex = i === finalIndex ? 0 : -1;
|
316
|
+
});
|
317
|
+
// Reset the keyboard navigation flag
|
318
|
+
this.isKeyboardNavigation = false;
|
319
|
+
};
|
320
|
+
/**
|
321
|
+
* Handles the blur behavior for the input OTP component.
|
322
|
+
* Emits the `nanoBlur` event when the input group loses focus.
|
323
|
+
*/
|
324
|
+
onBlur = (event) => {
|
325
|
+
const { inputRefs } = this;
|
326
|
+
const relatedTarget = event.relatedTarget;
|
327
|
+
// Do not emit blur if we're moving to another input box in the same component
|
328
|
+
const isInternalFocus = relatedTarget != null &&
|
329
|
+
inputRefs.includes(relatedTarget);
|
330
|
+
if (!isInternalFocus) {
|
331
|
+
this.hasFocus = false;
|
332
|
+
// Reset tabIndexes when focus leaves the component
|
333
|
+
this.updateTabIndexes();
|
334
|
+
// Always emit nanoBlur when focus leaves the component
|
335
|
+
this.nanoBlur.emit(event);
|
336
|
+
// Only emit nanoChange if the value has actually changed
|
337
|
+
if (this.focusedValue !== this.value) {
|
338
|
+
this.emitNanoChange(event);
|
339
|
+
}
|
340
|
+
}
|
341
|
+
};
|
342
|
+
/**
|
343
|
+
* Focuses the next input box.
|
344
|
+
*/
|
345
|
+
focusNext(currentIndex) {
|
346
|
+
const { inputRefs, length } = this;
|
347
|
+
if (currentIndex < length - 1) {
|
348
|
+
inputRefs[currentIndex + 1]?.focus();
|
349
|
+
}
|
350
|
+
}
|
351
|
+
/**
|
352
|
+
* Focuses the previous input box.
|
353
|
+
*/
|
354
|
+
focusPrevious(currentIndex) {
|
355
|
+
const { inputRefs } = this;
|
356
|
+
if (currentIndex > 0) {
|
357
|
+
inputRefs[currentIndex - 1]?.focus();
|
358
|
+
}
|
359
|
+
}
|
360
|
+
/**
|
361
|
+
* Searches through the input values and returns the index
|
362
|
+
* of the first empty input.
|
363
|
+
* Returns -1 if all inputs are filled.
|
364
|
+
* @returns {number} The index of the first empty input, or -1 if all are filled.
|
365
|
+
*/
|
366
|
+
getFirstEmptyIndex() {
|
367
|
+
const { inputValues, length } = this;
|
368
|
+
// Create an array of the same length as the input OTP
|
369
|
+
// and fill it with the input values
|
370
|
+
const values = Array.from({ length }, (_, i) => inputValues[i] || '');
|
371
|
+
return values.findIndex((value) => !value || value === '') ?? -1;
|
372
|
+
}
|
373
|
+
/**
|
374
|
+
* Returns the index of the input that should be tabbed to.
|
375
|
+
* If all inputs are filled, returns the last input's index.
|
376
|
+
* Otherwise, returns the index of the first empty input.
|
377
|
+
* @returns {number} The index to tab to.
|
378
|
+
*/
|
379
|
+
getTabbableIndex() {
|
380
|
+
const { length } = this;
|
381
|
+
const firstEmptyIndex = this.getFirstEmptyIndex();
|
382
|
+
return firstEmptyIndex === -1 ? length - 1 : firstEmptyIndex;
|
383
|
+
}
|
384
|
+
/**
|
385
|
+
* Updates the tabIndexes for the input boxes.
|
386
|
+
* This is used to ensure that the correct input is
|
387
|
+
* focused when the user navigates using the tab key.
|
388
|
+
*/
|
389
|
+
updateTabIndexes() {
|
390
|
+
const { inputRefs, inputValues, length } = this;
|
391
|
+
// Find first empty index after any filled boxes
|
392
|
+
let firstEmptyIndex = -1;
|
393
|
+
for (let i = 0; i < length; i++) {
|
394
|
+
if (!inputValues[i] || inputValues[i] === '') {
|
395
|
+
firstEmptyIndex = i;
|
396
|
+
break;
|
397
|
+
}
|
398
|
+
}
|
399
|
+
// Update tabIndex and aria-hidden for all inputs
|
400
|
+
inputRefs.forEach((input, index) => {
|
401
|
+
const shouldBeTabbable = firstEmptyIndex === -1
|
402
|
+
? index === length - 1
|
403
|
+
: firstEmptyIndex === index;
|
404
|
+
input.tabIndex = shouldBeTabbable ? 0 : -1;
|
405
|
+
// If the input is empty and not the first empty input,
|
406
|
+
// it should be hidden from screen readers.
|
407
|
+
const isEmpty = !inputValues[index] || inputValues[index] === '';
|
408
|
+
input.setAttribute('aria-hidden', isEmpty && !shouldBeTabbable ? 'true' : 'false');
|
409
|
+
});
|
410
|
+
}
|
411
|
+
/**
|
412
|
+
* Handles keyboard navigation for the OTP component.
|
413
|
+
*
|
414
|
+
* Navigation:
|
415
|
+
* - Backspace: Clears current input and moves to previous box if empty
|
416
|
+
* - Arrow Left/Right: Moves focus between input boxes
|
417
|
+
* - Tab: Allows normal tab navigation between components
|
418
|
+
* @returns {(event: KeyboardEvent) => void} Event handler for keydown events.
|
419
|
+
*/
|
420
|
+
onKeyDown = (index) => (event) => {
|
421
|
+
const { length } = this;
|
422
|
+
const rtl = this.isRTL(this.el);
|
423
|
+
const input = event.target;
|
424
|
+
// Meta shortcuts are used to copy, paste, and select text
|
425
|
+
// We don't want to handle these keys here
|
426
|
+
const metaShortcuts = ['a', 'c', 'v', 'x', 'r', 'z', 'y'];
|
427
|
+
const isTextSelection = input.selectionStart !== input.selectionEnd;
|
428
|
+
// Return if the key is a meta shortcut or the input value
|
429
|
+
// text is selected and let the onPaste / onInput handler manage it
|
430
|
+
if (isTextSelection ||
|
431
|
+
((event.metaKey || event.ctrlKey) &&
|
432
|
+
metaShortcuts.includes(event.key.toLowerCase()))) {
|
433
|
+
return;
|
434
|
+
}
|
435
|
+
if (event.key === 'Backspace') {
|
436
|
+
if (this.inputValues[index]) {
|
437
|
+
// Shift all values to the right of the current index left by one
|
438
|
+
for (let i = index; i < length - 1; i++) {
|
439
|
+
this.inputValues[i] = this.inputValues[i + 1];
|
440
|
+
}
|
441
|
+
// Clear the last box
|
442
|
+
this.inputValues[length - 1] = '';
|
443
|
+
// Update all inputRefs to match inputValues
|
444
|
+
for (let i = 0; i < length; i++) {
|
445
|
+
this.inputRefs[i].value = this.inputValues[i] || '';
|
446
|
+
}
|
447
|
+
this.updateValue(event);
|
448
|
+
event.preventDefault();
|
449
|
+
}
|
450
|
+
else if (!this.inputValues[index] && index > 0) {
|
451
|
+
// If current input is empty, move to previous input
|
452
|
+
this.focusPrevious(index);
|
453
|
+
}
|
454
|
+
}
|
455
|
+
else if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
456
|
+
this.isKeyboardNavigation = true;
|
457
|
+
event.preventDefault();
|
458
|
+
const isLeft = event.key === 'ArrowLeft';
|
459
|
+
const shouldMoveNext = (isLeft && rtl) || (!isLeft && !rtl);
|
460
|
+
// Only allow moving to the next input if the current has a value
|
461
|
+
if (shouldMoveNext) {
|
462
|
+
if (this.inputValues[index] && index < length - 1) {
|
463
|
+
this.focusNext(index);
|
464
|
+
}
|
465
|
+
}
|
466
|
+
else {
|
467
|
+
this.focusPrevious(index);
|
468
|
+
}
|
469
|
+
}
|
470
|
+
else if (event.key === 'Tab') {
|
471
|
+
this.isKeyboardNavigation = true;
|
472
|
+
// Let all tab events proceed normally
|
473
|
+
return;
|
474
|
+
}
|
475
|
+
};
|
476
|
+
/**
|
477
|
+
* Processes all input scenarios for each input box.
|
478
|
+
*
|
479
|
+
* This function manages:
|
480
|
+
* 1. Autofill handling
|
481
|
+
* 2. Input validation
|
482
|
+
* 3. Full selection replacement or typing in an empty box
|
483
|
+
* 4. Inserting in the middle with available space (shifting)
|
484
|
+
* 5. Single character replacement
|
485
|
+
* @returns {(event: InputEvent) => void} Event handler for input events.
|
486
|
+
*/
|
487
|
+
onInput = (index) => (event) => {
|
488
|
+
const { length, validKeyPattern, allowInvalid } = this;
|
489
|
+
const input = event.target;
|
490
|
+
const value = input.value;
|
491
|
+
const previousValue = this.previousInputValues[index] || '';
|
492
|
+
// 1. Autofill handling
|
493
|
+
// If the length of the value increases by more than 1 from the previous
|
494
|
+
// value, treat this as autofill. This is to prevent the case where the
|
495
|
+
// user is typing a single character into an input box containing a value
|
496
|
+
// as that will trigger this function with a value length of 2 characters.
|
497
|
+
const isAutofill = value.length - previousValue.length > 1;
|
498
|
+
if (isAutofill) {
|
499
|
+
// Distribute valid characters across input boxes
|
500
|
+
const validChars = this.getValidChars(value);
|
501
|
+
// If there are no valid characters coming from the
|
502
|
+
// autofill, all input refs have to be cleared after the
|
503
|
+
// browser has finished the autofill behavior
|
504
|
+
if (validChars.length === 0) {
|
505
|
+
requestAnimationFrame(() => {
|
506
|
+
this.inputRefs.forEach((input) => {
|
507
|
+
input.value = '';
|
508
|
+
});
|
509
|
+
});
|
510
|
+
}
|
511
|
+
for (let i = 0; i < length; i++) {
|
512
|
+
this.inputValues[i] = validChars[i] || '';
|
513
|
+
this.inputRefs[i].value = validChars[i] || '';
|
514
|
+
}
|
515
|
+
this.updateValue(event);
|
516
|
+
// Focus the first empty input box or the last input box if all boxes
|
517
|
+
// are filled after a small delay to ensure the input boxes have been
|
518
|
+
// updated before moving the focus
|
519
|
+
setTimeout(() => {
|
520
|
+
const nextIndex = validChars.length < length ? validChars.length : length - 1;
|
521
|
+
this.inputRefs[nextIndex]?.focus();
|
522
|
+
}, 20);
|
523
|
+
this.previousInputValues = [...this.inputValues];
|
524
|
+
return;
|
525
|
+
}
|
526
|
+
// 2. Input validation
|
527
|
+
// If the character entered is invalid (does not match the pattern),
|
528
|
+
// restore the previous value and exit
|
529
|
+
if ((!allowInvalid &&
|
530
|
+
value.length > 0 &&
|
531
|
+
!validKeyPattern.test(value[value.length - 1])) ||
|
532
|
+
(allowInvalid && this.specialCharsPattern.test(value[value.length - 1]))) {
|
533
|
+
input.value = this.inputValues[index] || '';
|
534
|
+
this.previousInputValues = [...this.inputValues];
|
535
|
+
return;
|
536
|
+
}
|
537
|
+
// 3. Full selection replacement or typing in an empty box
|
538
|
+
// If the user selects all text in the input box and types, or if the
|
539
|
+
// input box is empty, replace only this input box. If the box is empty,
|
540
|
+
// move to the next box, otherwise stay focused on this box.
|
541
|
+
const isAllSelected = input.selectionStart === 0 && input.selectionEnd === value.length;
|
542
|
+
const isEmpty = !this.inputValues[index];
|
543
|
+
if (isAllSelected || isEmpty) {
|
544
|
+
this.inputValues[index] = value;
|
545
|
+
input.value = value;
|
546
|
+
this.updateValue(event);
|
547
|
+
this.focusNext(index);
|
548
|
+
this.previousInputValues = [...this.inputValues];
|
549
|
+
return;
|
550
|
+
}
|
551
|
+
// 4. Inserting in the middle with available space (shifting)
|
552
|
+
// If typing in a filled input box and there are empty boxes at the end,
|
553
|
+
// shift all values starting at the current box to the right, and insert
|
554
|
+
// the new character at the current box.
|
555
|
+
const hasAvailableBoxAtEnd = this.inputValues[this.inputValues.length - 1] === '';
|
556
|
+
if (this.inputValues[index] && hasAvailableBoxAtEnd && value.length === 2) {
|
557
|
+
// Get the inserted character (from event or by diffing value/previousValue)
|
558
|
+
let newChar = event.data;
|
559
|
+
if (!newChar) {
|
560
|
+
newChar =
|
561
|
+
value.split('').find((c, i) => c !== previousValue[i]) ||
|
562
|
+
value[value.length - 1];
|
563
|
+
}
|
564
|
+
// Validate the new character before shifting
|
565
|
+
if (!allowInvalid && !validKeyPattern.test(newChar)) {
|
566
|
+
input.value = this.inputValues[index] || '';
|
567
|
+
this.previousInputValues = [...this.inputValues];
|
568
|
+
return;
|
569
|
+
}
|
570
|
+
// Shift values right from the end to the insertion point
|
571
|
+
for (let i = this.inputValues.length - 1; i > index; i--) {
|
572
|
+
this.inputValues[i] = this.inputValues[i - 1];
|
573
|
+
this.inputRefs[i].value = this.inputValues[i] || '';
|
574
|
+
}
|
575
|
+
this.inputValues[index] = newChar;
|
576
|
+
this.inputRefs[index].value = newChar;
|
577
|
+
this.updateValue(event);
|
578
|
+
this.previousInputValues = [...this.inputValues];
|
579
|
+
return;
|
580
|
+
}
|
581
|
+
// 5. Single character replacement
|
582
|
+
// Handles replacing a single character in a box containing a value based
|
583
|
+
// on the cursor position. We need the cursor position to determine which
|
584
|
+
// character was the last character typed. For example, if the user types "2"
|
585
|
+
// in an input box with the cursor at the beginning of the value of "6",
|
586
|
+
// the value will be "26", but we want to grab the "2" as the last character
|
587
|
+
// typed.
|
588
|
+
const cursorPos = input.selectionStart ?? value.length;
|
589
|
+
const newCharIndex = cursorPos - 1;
|
590
|
+
const newChar = value[newCharIndex] ?? value[0];
|
591
|
+
// Check if the new character is valid before updating the value
|
592
|
+
if (!allowInvalid && !validKeyPattern.test(newChar)) {
|
593
|
+
input.value = this.inputValues[index] || '';
|
594
|
+
this.previousInputValues = [...this.inputValues];
|
595
|
+
return;
|
596
|
+
}
|
597
|
+
this.inputValues[index] = newChar;
|
598
|
+
input.value = newChar;
|
599
|
+
this.updateValue(event);
|
600
|
+
this.previousInputValues = [...this.inputValues];
|
601
|
+
};
|
602
|
+
/**
|
603
|
+
* Handles pasting text into the input OTP component.
|
604
|
+
* This function prevents the default paste behavior and
|
605
|
+
* validates the pasted text against the allowed pattern.
|
606
|
+
* It then updates the value of the input group and focuses
|
607
|
+
* the next empty input after pasting.
|
608
|
+
*/
|
609
|
+
onPaste = (event) => {
|
610
|
+
const { inputRefs, length } = this;
|
611
|
+
event.preventDefault();
|
612
|
+
const pastedText = event.clipboardData?.getData('text');
|
613
|
+
// If there is no pasted text, still emit the input change event
|
614
|
+
// because this is how the native input element behaves
|
615
|
+
// but return early because there is nothing to paste.
|
616
|
+
if (!pastedText) {
|
617
|
+
this.emitNanoInput(event);
|
618
|
+
return;
|
619
|
+
}
|
620
|
+
const validChars = this.getValidChars(pastedText);
|
621
|
+
// Always paste starting at the first box
|
622
|
+
validChars.forEach((char, index) => {
|
623
|
+
if (index < length) {
|
624
|
+
this.inputRefs[index].value = char;
|
625
|
+
this.inputValues[index] = char;
|
626
|
+
}
|
627
|
+
});
|
628
|
+
// Update the value so that all input boxes are updated
|
629
|
+
this.value = validChars.join('');
|
630
|
+
this.updateValue(event);
|
631
|
+
// Focus the next empty input after pasting
|
632
|
+
// If all boxes are filled, focus the last input
|
633
|
+
const nextEmptyIndex = validChars.length < length ? validChars.length : length - 1;
|
634
|
+
inputRefs[nextEmptyIndex]?.focus();
|
635
|
+
};
|
636
|
+
/**
|
637
|
+
* Determines if a separator should be shown for a given index by:
|
638
|
+
* - Checking if the total length is even (no separator for odd lengths)
|
639
|
+
* - If even, showing a separator after the middle index
|
640
|
+
*
|
641
|
+
* @param index - The index to check for a separator.
|
642
|
+
* @returns {boolean} True if a separator should be shown after the given index, false otherwise.
|
643
|
+
*/
|
644
|
+
showSeparator(index) {
|
645
|
+
const { length } = this;
|
646
|
+
if (length < 3 || length % 2 !== 0)
|
647
|
+
return false;
|
648
|
+
return index === length / 2 - 1;
|
649
|
+
}
|
650
|
+
/**
|
651
|
+
* Filters the input string to only include valid characters based on the
|
652
|
+
* `validKeyPattern` regex. If `allowInvalid` is true, it will also
|
653
|
+
* exclude special characters. The resulting array is sliced to the
|
654
|
+
* maximum length of the input OTP.
|
655
|
+
* @param str - The input string to filter.
|
656
|
+
* @returns {string[]} An array of valid characters.
|
657
|
+
*/
|
658
|
+
getValidChars(str) {
|
659
|
+
const { allowInvalid, validKeyPattern, length } = this;
|
660
|
+
const allChars = str.split('');
|
661
|
+
return (allowInvalid
|
662
|
+
? allChars.filter((char) => char.trim() !== '' && !this.specialCharsPattern.test(char))
|
663
|
+
: allChars.filter((char) => char.trim() !== '' && validKeyPattern.test(char))).slice(0, length);
|
664
|
+
}
|
665
|
+
/**
|
666
|
+
* Computes the validity of the input group based on the current input values.
|
667
|
+
* Sets the following `invalid` and `valid` states:
|
668
|
+
* - `invalid` is true if `allowInvalid` is true and any input value is invalid.
|
669
|
+
* - `valid` is true if all input boxes are filled with valid values.
|
670
|
+
*/
|
671
|
+
computeValidity() {
|
672
|
+
const { inputValues } = this;
|
673
|
+
this.invalid =
|
674
|
+
this.allowInvalid &&
|
675
|
+
this.inputValues.some((val) => val !== '' && !this.validKeyPattern.test(val));
|
676
|
+
this.valid =
|
677
|
+
this.inputValues.length === this.length &&
|
678
|
+
this.inputValues.every((val) => val !== '' && this.validKeyPattern.test(val));
|
679
|
+
inputValues.some((v) => v !== '' && !this.validKeyPattern.test(v));
|
680
|
+
}
|
681
|
+
render() {
|
682
|
+
const { disabled, el, hasFocus, inputId, inputRefs, inputValues, length, readonly, invalid, valid, } = this;
|
683
|
+
const inputmode = this.getInputmode();
|
684
|
+
const tabbableIndex = this.getTabbableIndex();
|
685
|
+
const pattern = this.getPattern();
|
686
|
+
const hasDescription = el.querySelector('.input-otp-description')?.textContent?.trim() !== '';
|
687
|
+
return (h(Host, { key: 'f397a0775d2d961ccc9013206fae72a8d0ad90ae', class: {
|
688
|
+
'has-focus': hasFocus,
|
689
|
+
'input-otp-disabled': disabled,
|
690
|
+
'input-otp-readonly': readonly,
|
691
|
+
'nano-invalid': invalid,
|
692
|
+
'nano-valid': valid,
|
693
|
+
} }, h("div", { key: 'add5fe905af87f4c9b87d29c66134ede52ff2b6b', role: "group", "aria-label": "One-time password input", class: "input-otp-group" }, Array.from({ length }).map((_, index) => (h(Fragment, null, h("div", { class: "native-wrapper" }, h("input", { class: "native-input", id: `${inputId}-${index}`, "aria-label": `Input ${index + 1} of ${length}`, type: "text", inputmode: inputmode, pattern: pattern, disabled: disabled, readOnly: readonly, tabIndex: index === tabbableIndex ? 0 : -1, value: inputValues[index] || '', autocomplete: "one-time-code", ref: (el) => (inputRefs[index] = el), onInput: this.onInput(index), onBlur: this.onBlur, onFocus: this.onFocus(index), onKeyDown: this.onKeyDown(index), onPaste: this.onPaste })), this.showSeparator(index) && h("div", { class: "input-otp-separator" })))), this.showValidity && (h("div", { key: '71d38d8f343be559d187f1c5710a64733cf7ceaa', class: {
|
694
|
+
'input-otp-icon': true,
|
695
|
+
'input-otp-icon--valid': valid,
|
696
|
+
'input-otp-icon--invalid': invalid,
|
697
|
+
} }, h("slot", { key: '14529ecc57a6268932bc1485f0a425bb6cb45c51', name: "validity-icon" }, h("nano-icon", { key: '65669a6b4ca76011b721e76fb68b7f9bfeb87092', hidden: !valid, name: "light/circle-check", class: "input-otp-valid-icon", "aria-label": "Valid input" }))))), h("div", { key: '3e677ab8517765759a0cc4a0aa6f0fec58767c90', class: {
|
698
|
+
'input-otp-description': true,
|
699
|
+
'input-otp-description-hidden': !hasDescription,
|
700
|
+
'input-otp-description-inset': this.showValidity,
|
701
|
+
} }, h("slot", { key: 'e875d5c5d7ed8f804b9866618b7ed5e5211dd7b1' }))));
|
702
|
+
}
|
703
|
+
static get watchers() { return {
|
704
|
+
"value": ["valueChanged"]
|
705
|
+
}; }
|
706
|
+
static get style() { return inputOtpCss; }
|
707
|
+
}, [262, "nano-input-otp", {
|
708
|
+
"disabled": [516],
|
709
|
+
"inputmode": [1],
|
710
|
+
"length": [2],
|
711
|
+
"pattern": [1],
|
712
|
+
"readonly": [516],
|
713
|
+
"type": [1],
|
714
|
+
"value": [1032],
|
715
|
+
"allowInvalid": [516, "allow-invalid"],
|
716
|
+
"showValidity": [516, "show-validity"],
|
717
|
+
"inputValues": [32],
|
718
|
+
"hasFocus": [32],
|
719
|
+
"previousInputValues": [32],
|
720
|
+
"invalid": [32],
|
721
|
+
"valid": [32],
|
722
|
+
"setFocus": [64]
|
723
|
+
}, undefined, {
|
724
|
+
"value": ["valueChanged"]
|
725
|
+
}]);
|
726
|
+
let inputIds = 0;
|
727
|
+
function defineCustomElement$1() {
|
728
|
+
if (typeof customElements === "undefined") {
|
729
|
+
return;
|
730
|
+
}
|
731
|
+
const components = ["nano-input-otp", "nano-icon"];
|
732
|
+
components.forEach(tagName => { switch (tagName) {
|
733
|
+
case "nano-input-otp":
|
734
|
+
if (!customElements.get(tagName)) {
|
735
|
+
customElements.define(tagName, InputOTP);
|
736
|
+
}
|
737
|
+
break;
|
738
|
+
case "nano-icon":
|
739
|
+
if (!customElements.get(tagName)) {
|
740
|
+
defineCustomElement$2();
|
741
|
+
}
|
742
|
+
break;
|
743
|
+
} });
|
744
|
+
}
|
745
|
+
|
746
|
+
const NanoInputOtp = InputOTP;
|
747
|
+
const defineCustomElement = defineCustomElement$1;
|
748
|
+
|
749
|
+
export { NanoInputOtp, defineCustomElement };
|