@w2p-ui/kit 0.1.1 → 0.1.4
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/assets/fonts/manrope/Manrope-Bold.ttf +0 -0
- package/assets/fonts/manrope/Manrope-ExtraBold.ttf +0 -0
- package/assets/fonts/manrope/Manrope-ExtraLight.ttf +0 -0
- package/assets/fonts/manrope/Manrope-Light.ttf +0 -0
- package/assets/fonts/manrope/Manrope-Medium.ttf +0 -0
- package/assets/fonts/manrope/Manrope-Regular.ttf +0 -0
- package/assets/fonts/manrope/Manrope-SemiBold.ttf +0 -0
- package/assets/fonts/sf-pro/SF-Pro-Display-Bold.otf +0 -0
- package/assets/fonts/sf-pro/SF-Pro-Display-Light.otf +0 -0
- package/assets/fonts/sf-pro/SF-Pro-Display-Medium.otf +0 -0
- package/assets/fonts/sf-pro/SF-Pro-Display-Regular.otf +0 -0
- package/assets/fonts/sf-pro/SF-Pro-Display-Semibold.otf +0 -0
- package/fesm2022/w2p-ui-kit-components-action-button.mjs +2 -2
- package/fesm2022/w2p-ui-kit-components-action-button.mjs.map +1 -1
- package/fesm2022/w2p-ui-kit-components-button.mjs +2 -2
- package/fesm2022/w2p-ui-kit-components-button.mjs.map +1 -1
- package/fesm2022/w2p-ui-kit-components-color-alert.mjs +24 -0
- package/fesm2022/w2p-ui-kit-components-color-alert.mjs.map +1 -0
- package/fesm2022/w2p-ui-kit-components-input.mjs +91 -0
- package/fesm2022/w2p-ui-kit-components-input.mjs.map +1 -0
- package/fesm2022/w2p-ui-kit-components-label.mjs +18 -0
- package/fesm2022/w2p-ui-kit-components-label.mjs.map +1 -0
- package/fesm2022/w2p-ui-kit-components-loader.mjs +19 -0
- package/fesm2022/w2p-ui-kit-components-loader.mjs.map +1 -0
- package/fesm2022/w2p-ui-kit-components-otp-input.mjs +154 -0
- package/fesm2022/w2p-ui-kit-components-otp-input.mjs.map +1 -0
- package/fesm2022/w2p-ui-kit-components.mjs +264 -7
- package/fesm2022/w2p-ui-kit-components.mjs.map +1 -1
- package/fesm2022/w2p-ui-kit.mjs +264 -7
- package/fesm2022/w2p-ui-kit.mjs.map +1 -1
- package/package.json +21 -1
- package/styles/fonts.scss +62 -0
- package/styles/index.scss +1 -0
- package/types/w2p-ui-kit-components-color-alert.d.ts +13 -0
- package/types/w2p-ui-kit-components-input.d.ts +31 -0
- package/types/w2p-ui-kit-components-label.d.ts +8 -0
- package/types/w2p-ui-kit-components-loader.d.ts +8 -0
- package/types/w2p-ui-kit-components-otp-input.d.ts +34 -0
- package/types/w2p-ui-kit-components.d.ts +79 -1
- package/types/w2p-ui-kit.d.ts +79 -1
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { input, output, viewChildren, signal, inject, effect, afterNextRender, Component } from '@angular/core';
|
|
3
|
+
import { NgControl } from '@angular/forms';
|
|
4
|
+
import { toSignal } from '@angular/core/rxjs-interop';
|
|
5
|
+
|
|
6
|
+
class W2pOtpInput {
|
|
7
|
+
// сколько ячеек (длина кода)
|
|
8
|
+
length = input(4, ...(ngDevMode ? [{ debugName: "length" }] : /* istanbul ignore next */ []));
|
|
9
|
+
disabledInput = input(false, ...(ngDevMode ? [{ debugName: "disabledInput" }] : /* istanbul ignore next */ []));
|
|
10
|
+
invalidInput = input(false, ...(ngDevMode ? [{ debugName: "invalidInput" }] : /* istanbul ignore next */ []));
|
|
11
|
+
autofocus = input(false, ...(ngDevMode ? [{ debugName: "autofocus" }] : /* istanbul ignore next */ []));
|
|
12
|
+
// эмитим, когда код полностью введён
|
|
13
|
+
complete = output();
|
|
14
|
+
cells = viewChildren('cellRef', ...(ngDevMode ? [{ debugName: "cells" }] : /* istanbul ignore next */ []));
|
|
15
|
+
// массив введённых символов по ячейкам
|
|
16
|
+
digits = signal([], ...(ngDevMode ? [{ debugName: "digits" }] : /* istanbul ignore next */ []));
|
|
17
|
+
disabled = signal(false, ...(ngDevMode ? [{ debugName: "disabled" }] : /* istanbul ignore next */ []));
|
|
18
|
+
invalid = signal(false, ...(ngDevMode ? [{ debugName: "invalid" }] : /* istanbul ignore next */ []));
|
|
19
|
+
ngControl = inject(NgControl, { optional: true, self: true });
|
|
20
|
+
controlStatus = this.ngControl?.statusChanges
|
|
21
|
+
? toSignal(this.ngControl.statusChanges, { initialValue: this.ngControl.status })
|
|
22
|
+
: signal(null);
|
|
23
|
+
onChange = () => { };
|
|
24
|
+
onTouched = () => { };
|
|
25
|
+
constructor() {
|
|
26
|
+
if (this.ngControl) {
|
|
27
|
+
this.ngControl.valueAccessor = this;
|
|
28
|
+
}
|
|
29
|
+
// пересоздаём массив ячеек при изменении length
|
|
30
|
+
effect(() => {
|
|
31
|
+
const len = this.length();
|
|
32
|
+
const current = this.digits();
|
|
33
|
+
if (current.length !== len) {
|
|
34
|
+
const next = Array.from({ length: len }, (_, i) => current[i] ?? '');
|
|
35
|
+
this.digits.set(next);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
effect(() => {
|
|
39
|
+
this.disabled.set(this.disabledInput());
|
|
40
|
+
});
|
|
41
|
+
effect(() => {
|
|
42
|
+
this.controlStatus();
|
|
43
|
+
const manualInvalid = this.invalidInput();
|
|
44
|
+
const controlInvalid = !!(this.ngControl?.invalid &&
|
|
45
|
+
(this.ngControl?.touched || this.ngControl?.dirty));
|
|
46
|
+
this.invalid.set(manualInvalid || controlInvalid);
|
|
47
|
+
});
|
|
48
|
+
afterNextRender(() => {
|
|
49
|
+
if (this.autofocus()) {
|
|
50
|
+
this.cells()[0]?.nativeElement.focus();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
registerOnChange(fn) {
|
|
55
|
+
this.onChange = fn;
|
|
56
|
+
}
|
|
57
|
+
registerOnTouched(fn) {
|
|
58
|
+
this.onTouched = fn;
|
|
59
|
+
}
|
|
60
|
+
setDisabledState(isDisabled) {
|
|
61
|
+
this.disabled.set(isDisabled);
|
|
62
|
+
}
|
|
63
|
+
setInvalid(isInvalid) {
|
|
64
|
+
this.invalid.set(isInvalid);
|
|
65
|
+
}
|
|
66
|
+
writeValue(obj) {
|
|
67
|
+
const value = obj ?? '';
|
|
68
|
+
const len = this.length();
|
|
69
|
+
this.digits.set(Array.from({ length: len }, (_, i) => value[i] ?? ''));
|
|
70
|
+
}
|
|
71
|
+
onInput(event, index) {
|
|
72
|
+
const input = event.target;
|
|
73
|
+
// берём только последний введённый символ, только цифра
|
|
74
|
+
const raw = input.value.replace(/\D/g, '');
|
|
75
|
+
const char = raw.slice(-1);
|
|
76
|
+
const next = [...this.digits()];
|
|
77
|
+
next[index] = char;
|
|
78
|
+
this.digits.set(next);
|
|
79
|
+
input.value = char;
|
|
80
|
+
if (char && index < this.length() - 1) {
|
|
81
|
+
this.focusCell(index + 1);
|
|
82
|
+
}
|
|
83
|
+
this.emitValue();
|
|
84
|
+
}
|
|
85
|
+
onKeydown(event, index) {
|
|
86
|
+
const input = event.target;
|
|
87
|
+
if (event.key === 'Backspace') {
|
|
88
|
+
if (!input.value && index > 0) {
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
const next = [...this.digits()];
|
|
91
|
+
next[index - 1] = '';
|
|
92
|
+
this.digits.set(next);
|
|
93
|
+
this.focusCell(index - 1);
|
|
94
|
+
this.emitValue();
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (event.key === 'ArrowLeft' && index > 0) {
|
|
99
|
+
event.preventDefault();
|
|
100
|
+
this.focusCell(index - 1);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (event.key === 'ArrowRight' && index < this.length() - 1) {
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
this.focusCell(index + 1);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
onPaste(event, index) {
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
const pasted = event.clipboardData?.getData('text').replace(/\D/g, '') ?? '';
|
|
112
|
+
if (!pasted)
|
|
113
|
+
return;
|
|
114
|
+
const next = [...this.digits()];
|
|
115
|
+
let cursor = index;
|
|
116
|
+
for (const char of pasted) {
|
|
117
|
+
if (cursor >= this.length())
|
|
118
|
+
break;
|
|
119
|
+
next[cursor] = char;
|
|
120
|
+
cursor++;
|
|
121
|
+
}
|
|
122
|
+
this.digits.set(next);
|
|
123
|
+
this.emitValue();
|
|
124
|
+
const focusIndex = Math.min(cursor, this.length() - 1);
|
|
125
|
+
this.focusCell(focusIndex);
|
|
126
|
+
}
|
|
127
|
+
onBlur() {
|
|
128
|
+
this.onTouched();
|
|
129
|
+
}
|
|
130
|
+
focusCell(index) {
|
|
131
|
+
// ячейки перерисуются после сигнала, откладываем фокус на следующий тик
|
|
132
|
+
queueMicrotask(() => this.cells()[index]?.nativeElement.focus());
|
|
133
|
+
}
|
|
134
|
+
emitValue() {
|
|
135
|
+
const value = this.digits().join('');
|
|
136
|
+
this.onChange(value);
|
|
137
|
+
if (value.length === this.length() && !value.includes('')) {
|
|
138
|
+
this.complete.emit(value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.18", ngImport: i0, type: W2pOtpInput, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
142
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.18", type: W2pOtpInput, isStandalone: true, selector: "w2p-otp-input", inputs: { length: { classPropertyName: "length", publicName: "length", isSignal: true, isRequired: false, transformFunction: null }, disabledInput: { classPropertyName: "disabledInput", publicName: "disabledInput", isSignal: true, isRequired: false, transformFunction: null }, invalidInput: { classPropertyName: "invalidInput", publicName: "invalidInput", isSignal: true, isRequired: false, transformFunction: null }, autofocus: { classPropertyName: "autofocus", publicName: "autofocus", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { complete: "complete" }, viewQueries: [{ propertyName: "cells", predicate: ["cellRef"], descendants: true, isSignal: true }], ngImport: i0, template: "<div class=\"otp-wrapper\" [class.invalid]=\"invalid()\">\n @for (digit of digits(); track $index) {\n <input\n #cellRef\n class=\"otp-cell\"\n type=\"text\"\n inputmode=\"numeric\"\n maxlength=\"1\"\n autocomplete=\"one-time-code\"\n [value]=\"digit\"\n [disabled]=\"disabled()\"\n [class.filled]=\"!!digit\"\n [class.invalid]=\"invalid()\"\n (input)=\"onInput($event, $index)\"\n (keydown)=\"onKeydown($event, $index)\"\n (paste)=\"onPaste($event, $index)\"\n (blur)=\"onBlur()\"\n />\n }\n</div>\n", styles: ["@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Regular.ttf) format(\"truetype\");font-weight:400;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Medium.ttf) format(\"truetype\");font-weight:500;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-SemiBold.ttf) format(\"truetype\");font-weight:600;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Bold.ttf) format(\"truetype\");font-weight:700;font-style:normal}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:100;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Light.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:400;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Regular.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:500;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Medium.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:600;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Semibold.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:700;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Bold.otf) format(\"opentype\")}.otp-wrapper{display:flex;gap:.44rem;width:min-content;--w2p-ui-otp-input-cell-width: 3rem;--w2p-ui-otp-input-cell-height: 4rem;--w2p-ui-otp-input-cell-padding: .75rem;--w2p-ui-otp-input-cell-border-radius: var(--Border-radius-3, .75rem);--w2p-ui-otp-input-cell-border: 1px solid var(--Base-Inverse-20, rgba(255, 255, 255, .2));--w2p-ui-otp-input-cell-background: linear-gradient( 0deg, var(--Base-Base-30, rgba(11, 11, 11, .3)) 0%, var(--Base-Base-30, rgba(11, 11, 11, .3)) 100% ), var(--Base-Modal-Default, #1a1a1a);--w2p-ui-otp-input-cell-focus-border-color: var(--Brand-Primary-Default, #f3400d);--w2p-ui-otp-input-cell-filled-border-color: var(--Base-Inverse-50, rgba(255, 255, 255, .5));--w2p-ui-otp-input-cell-caret-color: var(--Brand-Primary-Default, #f3400d);--w2p-ui-otp-input-cell-text-color: var(--Base-Inverse-Default, #fff);--w2p-ui-otp-input-cell-invalid-border-color: var(--System-Danger-70, rgba(247, 36, 48, .7));--w2p-ui-otp-input-cell-invalid-text-color: var(--Base-Inverse-70, rgba(255, 255, 255, .7))}.otp-cell{outline:none;display:flex;width:var(--w2p-ui-otp-input-cell-width);height:var(--w2p-ui-otp-input-cell-height);padding:var(--w2p-ui-otp-input-cell-padding);justify-content:center;align-items:center;border-radius:var(--w2p-ui-otp-input-cell-border-radius);border:var(--w2p-ui-otp-input-cell-border);background:var(--w2p-ui-otp-input-cell-background);caret-color:var(--w2p-ui-otp-input-cell-caret-color);text-align:center;color:var(--w2p-ui-otp-input-cell-text-color);font-family:Manrope;font-size:2.25rem;font-style:normal;font-weight:600;line-height:normal}.otp-cell:focus{border-color:var(--w2p-ui-otp-input-cell-focus-border-color)}.otp-cell.filled{border-color:var(--w2p-ui-otp-input-cell-filled-border-color)}.otp-cell:disabled{--w2p-ui-otp-input-cell-text-color: var(--Base-Inverse-10, rgba(255, 255, 255, .1));border-color:var(--Base-Inverse-10, rgba(255, 255, 255, .1));--w2p-ui-otp-input-cell-background: linear-gradient( 0deg, var(--Base-Base-30, rgba(11, 11, 11, .3)) 0%, var(--Base-Base-30, rgba(11, 11, 11, .3)) 100% ), var(--Base-Modal-Default, #1a1a1a)}.otp-cell.invalid{border-color:var(--w2p-ui-otp-input-cell-invalid-border-color);color:var(--w2p-ui-otp-input-cell-invalid-text-color)}\n"] });
|
|
143
|
+
}
|
|
144
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.18", ngImport: i0, type: W2pOtpInput, decorators: [{
|
|
145
|
+
type: Component,
|
|
146
|
+
args: [{ selector: 'w2p-otp-input', imports: [], template: "<div class=\"otp-wrapper\" [class.invalid]=\"invalid()\">\n @for (digit of digits(); track $index) {\n <input\n #cellRef\n class=\"otp-cell\"\n type=\"text\"\n inputmode=\"numeric\"\n maxlength=\"1\"\n autocomplete=\"one-time-code\"\n [value]=\"digit\"\n [disabled]=\"disabled()\"\n [class.filled]=\"!!digit\"\n [class.invalid]=\"invalid()\"\n (input)=\"onInput($event, $index)\"\n (keydown)=\"onKeydown($event, $index)\"\n (paste)=\"onPaste($event, $index)\"\n (blur)=\"onBlur()\"\n />\n }\n</div>\n", styles: ["@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Regular.ttf) format(\"truetype\");font-weight:400;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Medium.ttf) format(\"truetype\");font-weight:500;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-SemiBold.ttf) format(\"truetype\");font-weight:600;font-style:normal}@font-face{font-family:Manrope;src:url(/assets/fonts/manrope/Manrope-Bold.ttf) format(\"truetype\");font-weight:700;font-style:normal}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:100;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Light.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:400;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Regular.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:500;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Medium.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:600;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Semibold.otf) format(\"opentype\")}@font-face{font-family:SF Pro Display;font-style:normal;font-weight:700;src:url(/assets/fonts/sf-pro/SF-Pro-Display-Bold.otf) format(\"opentype\")}.otp-wrapper{display:flex;gap:.44rem;width:min-content;--w2p-ui-otp-input-cell-width: 3rem;--w2p-ui-otp-input-cell-height: 4rem;--w2p-ui-otp-input-cell-padding: .75rem;--w2p-ui-otp-input-cell-border-radius: var(--Border-radius-3, .75rem);--w2p-ui-otp-input-cell-border: 1px solid var(--Base-Inverse-20, rgba(255, 255, 255, .2));--w2p-ui-otp-input-cell-background: linear-gradient( 0deg, var(--Base-Base-30, rgba(11, 11, 11, .3)) 0%, var(--Base-Base-30, rgba(11, 11, 11, .3)) 100% ), var(--Base-Modal-Default, #1a1a1a);--w2p-ui-otp-input-cell-focus-border-color: var(--Brand-Primary-Default, #f3400d);--w2p-ui-otp-input-cell-filled-border-color: var(--Base-Inverse-50, rgba(255, 255, 255, .5));--w2p-ui-otp-input-cell-caret-color: var(--Brand-Primary-Default, #f3400d);--w2p-ui-otp-input-cell-text-color: var(--Base-Inverse-Default, #fff);--w2p-ui-otp-input-cell-invalid-border-color: var(--System-Danger-70, rgba(247, 36, 48, .7));--w2p-ui-otp-input-cell-invalid-text-color: var(--Base-Inverse-70, rgba(255, 255, 255, .7))}.otp-cell{outline:none;display:flex;width:var(--w2p-ui-otp-input-cell-width);height:var(--w2p-ui-otp-input-cell-height);padding:var(--w2p-ui-otp-input-cell-padding);justify-content:center;align-items:center;border-radius:var(--w2p-ui-otp-input-cell-border-radius);border:var(--w2p-ui-otp-input-cell-border);background:var(--w2p-ui-otp-input-cell-background);caret-color:var(--w2p-ui-otp-input-cell-caret-color);text-align:center;color:var(--w2p-ui-otp-input-cell-text-color);font-family:Manrope;font-size:2.25rem;font-style:normal;font-weight:600;line-height:normal}.otp-cell:focus{border-color:var(--w2p-ui-otp-input-cell-focus-border-color)}.otp-cell.filled{border-color:var(--w2p-ui-otp-input-cell-filled-border-color)}.otp-cell:disabled{--w2p-ui-otp-input-cell-text-color: var(--Base-Inverse-10, rgba(255, 255, 255, .1));border-color:var(--Base-Inverse-10, rgba(255, 255, 255, .1));--w2p-ui-otp-input-cell-background: linear-gradient( 0deg, var(--Base-Base-30, rgba(11, 11, 11, .3)) 0%, var(--Base-Base-30, rgba(11, 11, 11, .3)) 100% ), var(--Base-Modal-Default, #1a1a1a)}.otp-cell.invalid{border-color:var(--w2p-ui-otp-input-cell-invalid-border-color);color:var(--w2p-ui-otp-input-cell-invalid-text-color)}\n"] }]
|
|
147
|
+
}], ctorParameters: () => [], propDecorators: { length: [{ type: i0.Input, args: [{ isSignal: true, alias: "length", required: false }] }], disabledInput: [{ type: i0.Input, args: [{ isSignal: true, alias: "disabledInput", required: false }] }], invalidInput: [{ type: i0.Input, args: [{ isSignal: true, alias: "invalidInput", required: false }] }], autofocus: [{ type: i0.Input, args: [{ isSignal: true, alias: "autofocus", required: false }] }], complete: [{ type: i0.Output, args: ["complete"] }], cells: [{ type: i0.ViewChildren, args: ['cellRef', { isSignal: true }] }] } });
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Generated bundle index. Do not edit.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
export { W2pOtpInput };
|
|
154
|
+
//# sourceMappingURL=w2p-ui-kit-components-otp-input.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"w2p-ui-kit-components-otp-input.mjs","sources":["../../../projects/kit/components/otp-input/otp-input.ts","../../../projects/kit/components/otp-input/otp-input.html","../../../projects/kit/components/otp-input/w2p-ui-kit-components-otp-input.ts"],"sourcesContent":["import {\n afterNextRender,\n Component,\n effect,\n ElementRef,\n inject,\n input,\n output,\n signal,\n viewChildren,\n} from '@angular/core';\nimport { NgControl } from '@angular/forms';\nimport { toSignal } from '@angular/core/rxjs-interop';\n\n@Component({\n selector: 'w2p-otp-input',\n imports: [],\n templateUrl: './otp-input.html',\n styleUrls: ['./otp-input.scss'],\n})\nexport class W2pOtpInput {\n // сколько ячеек (длина кода)\n public readonly length = input<number>(4);\n public readonly disabledInput = input<boolean>(false);\n public readonly invalidInput = input<boolean>(false);\n public readonly autofocus = input<boolean>(false);\n\n // эмитим, когда код полностью введён\n public readonly complete = output<string>();\n\n public readonly cells = viewChildren<ElementRef<HTMLInputElement>>('cellRef');\n\n // массив введённых символов по ячейкам\n public readonly digits = signal<string[]>([]);\n public readonly disabled = signal<boolean>(false);\n public readonly invalid = signal<boolean>(false);\n\n private readonly ngControl = inject(NgControl, { optional: true, self: true });\n\n private readonly controlStatus = this.ngControl?.statusChanges\n ? toSignal(this.ngControl.statusChanges, { initialValue: this.ngControl.status })\n : signal(null);\n\n private onChange: (value: string) => void = () => {};\n private onTouched: () => void = () => {};\n\n constructor() {\n if (this.ngControl) {\n this.ngControl.valueAccessor = this;\n }\n\n // пересоздаём массив ячеек при изменении length\n effect(() => {\n const len = this.length();\n const current = this.digits();\n if (current.length !== len) {\n const next = Array.from({ length: len }, (_, i) => current[i] ?? '');\n this.digits.set(next);\n }\n });\n\n effect(() => {\n this.disabled.set(this.disabledInput());\n });\n\n effect(() => {\n this.controlStatus();\n const manualInvalid = this.invalidInput();\n const controlInvalid = !!(\n this.ngControl?.invalid &&\n (this.ngControl?.touched || this.ngControl?.dirty)\n );\n this.invalid.set(manualInvalid || controlInvalid);\n });\n\n afterNextRender(() => {\n if (this.autofocus()) {\n this.cells()[0]?.nativeElement.focus();\n }\n });\n }\n\n registerOnChange(fn: any): void {\n this.onChange = fn;\n }\n\n registerOnTouched(fn: any): void {\n this.onTouched = fn;\n }\n\n setDisabledState(isDisabled: boolean): void {\n this.disabled.set(isDisabled);\n }\n\n public setInvalid(isInvalid: boolean): void {\n this.invalid.set(isInvalid);\n }\n\n writeValue(obj: string): void {\n const value = obj ?? '';\n const len = this.length();\n this.digits.set(Array.from({ length: len }, (_, i) => value[i] ?? ''));\n }\n\n onInput(event: Event, index: number): void {\n const input = event.target as HTMLInputElement;\n // берём только последний введённый символ, только цифра\n const raw = input.value.replace(/\\D/g, '');\n const char = raw.slice(-1);\n\n const next = [...this.digits()];\n next[index] = char;\n this.digits.set(next);\n input.value = char;\n\n if (char && index < this.length() - 1) {\n this.focusCell(index + 1);\n }\n\n this.emitValue();\n }\n\n onKeydown(event: KeyboardEvent, index: number): void {\n const input = event.target as HTMLInputElement;\n\n if (event.key === 'Backspace') {\n if (!input.value && index > 0) {\n event.preventDefault();\n const next = [...this.digits()];\n next[index - 1] = '';\n this.digits.set(next);\n this.focusCell(index - 1);\n this.emitValue();\n }\n return;\n }\n\n if (event.key === 'ArrowLeft' && index > 0) {\n event.preventDefault();\n this.focusCell(index - 1);\n return;\n }\n\n if (event.key === 'ArrowRight' && index < this.length() - 1) {\n event.preventDefault();\n this.focusCell(index + 1);\n return;\n }\n }\n\n onPaste(event: ClipboardEvent, index: number): void {\n event.preventDefault();\n const pasted = event.clipboardData?.getData('text').replace(/\\D/g, '') ?? '';\n if (!pasted) return;\n\n const next = [...this.digits()];\n let cursor = index;\n for (const char of pasted) {\n if (cursor >= this.length()) break;\n next[cursor] = char;\n cursor++;\n }\n this.digits.set(next);\n this.emitValue();\n\n const focusIndex = Math.min(cursor, this.length() - 1);\n this.focusCell(focusIndex);\n }\n\n onBlur(): void {\n this.onTouched();\n }\n\n private focusCell(index: number): void {\n // ячейки перерисуются после сигнала, откладываем фокус на следующий тик\n queueMicrotask(() => this.cells()[index]?.nativeElement.focus());\n }\n\n private emitValue(): void {\n const value = this.digits().join('');\n this.onChange(value);\n if (value.length === this.length() && !value.includes('')) {\n this.complete.emit(value);\n }\n }\n}\n","<div class=\"otp-wrapper\" [class.invalid]=\"invalid()\">\n @for (digit of digits(); track $index) {\n <input\n #cellRef\n class=\"otp-cell\"\n type=\"text\"\n inputmode=\"numeric\"\n maxlength=\"1\"\n autocomplete=\"one-time-code\"\n [value]=\"digit\"\n [disabled]=\"disabled()\"\n [class.filled]=\"!!digit\"\n [class.invalid]=\"invalid()\"\n (input)=\"onInput($event, $index)\"\n (keydown)=\"onKeydown($event, $index)\"\n (paste)=\"onPaste($event, $index)\"\n (blur)=\"onBlur()\"\n />\n }\n</div>\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;MAoBa,WAAW,CAAA;;AAEN,IAAA,MAAM,GAAG,KAAK,CAAS,CAAC,6EAAC;AACzB,IAAA,aAAa,GAAG,KAAK,CAAU,KAAK,oFAAC;AACrC,IAAA,YAAY,GAAG,KAAK,CAAU,KAAK,mFAAC;AACpC,IAAA,SAAS,GAAG,KAAK,CAAU,KAAK,gFAAC;;IAGjC,QAAQ,GAAG,MAAM,EAAU;AAE3B,IAAA,KAAK,GAAG,YAAY,CAA+B,SAAS,4EAAC;;AAG7D,IAAA,MAAM,GAAG,MAAM,CAAW,EAAE,6EAAC;AAC7B,IAAA,QAAQ,GAAG,MAAM,CAAU,KAAK,+EAAC;AACjC,IAAA,OAAO,GAAG,MAAM,CAAU,KAAK,8EAAC;AAE/B,IAAA,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AAE7D,IAAA,aAAa,GAAG,IAAI,CAAC,SAAS,EAAE;AAC/C,UAAE,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,aAAa,EAAE,EAAE,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;AAChF,UAAE,MAAM,CAAC,IAAI,CAAC;AAER,IAAA,QAAQ,GAA4B,MAAK,EAAE,CAAC;AAC5C,IAAA,SAAS,GAAe,MAAK,EAAE,CAAC;AAExC,IAAA,WAAA,GAAA;AACE,QAAA,IAAI,IAAI,CAAC,SAAS,EAAE;AAClB,YAAA,IAAI,CAAC,SAAS,CAAC,aAAa,GAAG,IAAI;QACrC;;QAGA,MAAM,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