@zag-js/pin-input 0.1.17 → 0.2.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/index.d.ts +9 -0
- package/dist/index.js +98 -41
- package/dist/index.mjs +98 -41
- package/package.json +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -81,6 +81,10 @@ declare type PublicContext = DirectionProperty & CommonProperties & {
|
|
|
81
81
|
* Whether to blur the input when the value is complete
|
|
82
82
|
*/
|
|
83
83
|
blurOnComplete?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Whether to select input value when input is focused
|
|
86
|
+
*/
|
|
87
|
+
selectOnFocus?: boolean;
|
|
84
88
|
/**
|
|
85
89
|
* Specifies the localized strings that identifies the accessibility elements and their states
|
|
86
90
|
*/
|
|
@@ -108,6 +112,11 @@ declare type ComputedContext = Readonly<{
|
|
|
108
112
|
* The string representation of the input values
|
|
109
113
|
*/
|
|
110
114
|
valueAsString: string;
|
|
115
|
+
/**
|
|
116
|
+
* @computed
|
|
117
|
+
* The value at focused index
|
|
118
|
+
*/
|
|
119
|
+
focusedValue: string;
|
|
111
120
|
}>;
|
|
112
121
|
declare type PrivateContext = Context<{}>;
|
|
113
122
|
declare type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
package/dist/index.js
CHANGED
|
@@ -39,22 +39,44 @@ function isWindow(value) {
|
|
|
39
39
|
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
40
40
|
}
|
|
41
41
|
function getDocument(el) {
|
|
42
|
+
var _a;
|
|
42
43
|
if (isWindow(el))
|
|
43
44
|
return el.document;
|
|
44
45
|
if (isDocument(el))
|
|
45
46
|
return el;
|
|
46
|
-
return (el == null ? void 0 : el.ownerDocument)
|
|
47
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
47
48
|
}
|
|
48
49
|
function defineDomHelpers(helpers) {
|
|
49
50
|
const dom2 = {
|
|
50
51
|
getRootNode: (ctx) => {
|
|
51
|
-
var _a;
|
|
52
|
-
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx))
|
|
52
|
+
var _a, _b;
|
|
53
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
53
54
|
},
|
|
54
55
|
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
55
|
-
getWin: (ctx) =>
|
|
56
|
+
getWin: (ctx) => {
|
|
57
|
+
var _a;
|
|
58
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
59
|
+
},
|
|
56
60
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
57
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
61
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
62
|
+
createEmitter: (ctx, target) => {
|
|
63
|
+
const win = dom2.getWin(ctx);
|
|
64
|
+
return function emit(evt, detail, options) {
|
|
65
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
66
|
+
const eventName = `zag:${evt}`;
|
|
67
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
68
|
+
const event = new win.CustomEvent(eventName, init);
|
|
69
|
+
target.dispatchEvent(event);
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
createListener: (target) => {
|
|
73
|
+
return function listen(evt, handler) {
|
|
74
|
+
const eventName = `zag:${evt}`;
|
|
75
|
+
const listener = (e) => handler(e);
|
|
76
|
+
target.addEventListener(eventName, listener);
|
|
77
|
+
return () => target.removeEventListener(eventName, listener);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
58
80
|
};
|
|
59
81
|
return {
|
|
60
82
|
...dom2,
|
|
@@ -62,7 +84,8 @@ function defineDomHelpers(helpers) {
|
|
|
62
84
|
};
|
|
63
85
|
}
|
|
64
86
|
function getNativeEvent(e) {
|
|
65
|
-
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = e.nativeEvent) != null ? _a : e;
|
|
66
89
|
}
|
|
67
90
|
var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
|
|
68
91
|
var rtlKeyMap = {
|
|
@@ -79,9 +102,10 @@ var sameKeyMap = {
|
|
|
79
102
|
Right: "ArrowRight"
|
|
80
103
|
};
|
|
81
104
|
function getEventKey(event, options = {}) {
|
|
105
|
+
var _a;
|
|
82
106
|
const { dir = "ltr", orientation = "horizontal" } = options;
|
|
83
107
|
let { key } = event;
|
|
84
|
-
key = sameKeyMap[key]
|
|
108
|
+
key = (_a = sameKeyMap[key]) != null ? _a : key;
|
|
85
109
|
const isRtl = dir === "rtl" && orientation === "horizontal";
|
|
86
110
|
if (isRtl && key in rtlKeyMap) {
|
|
87
111
|
key = rtlKeyMap[key];
|
|
@@ -95,7 +119,8 @@ function raf(fn) {
|
|
|
95
119
|
};
|
|
96
120
|
}
|
|
97
121
|
function queryAll(root, selector) {
|
|
98
|
-
|
|
122
|
+
var _a;
|
|
123
|
+
return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
|
|
99
124
|
}
|
|
100
125
|
var visuallyHiddenStyle = {
|
|
101
126
|
border: "0",
|
|
@@ -122,20 +147,20 @@ function invariant(...a) {
|
|
|
122
147
|
// src/pin-input.dom.ts
|
|
123
148
|
var dom = defineDomHelpers({
|
|
124
149
|
getRootId: (ctx) => {
|
|
125
|
-
var _a;
|
|
126
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.root)
|
|
150
|
+
var _a, _b;
|
|
151
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.id}`;
|
|
127
152
|
},
|
|
128
153
|
getInputId: (ctx, id) => {
|
|
129
|
-
var _a, _b;
|
|
130
|
-
return ((_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id))
|
|
154
|
+
var _a, _b, _c;
|
|
155
|
+
return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.id}:${id}`;
|
|
131
156
|
},
|
|
132
157
|
getHiddenInputId: (ctx) => {
|
|
133
|
-
var _a;
|
|
134
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.hiddenInput)
|
|
158
|
+
var _a, _b;
|
|
159
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.hiddenInput) != null ? _b : `pin-input:${ctx.id}:hidden`;
|
|
135
160
|
},
|
|
136
161
|
getLabelId: (ctx) => {
|
|
137
|
-
var _a;
|
|
138
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.label)
|
|
162
|
+
var _a, _b;
|
|
163
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `pin-input:${ctx.id}:label`;
|
|
139
164
|
},
|
|
140
165
|
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
141
166
|
getElements: (ctx) => {
|
|
@@ -219,7 +244,7 @@ function connect(state, send, normalize) {
|
|
|
219
244
|
"aria-invalid": ariaAttr(isInvalid),
|
|
220
245
|
"data-invalid": dataAttr(isInvalid),
|
|
221
246
|
type: state.context.mask ? "password" : inputType,
|
|
222
|
-
|
|
247
|
+
defaultValue: state.context.value[index] || "",
|
|
223
248
|
autoCapitalize: "none",
|
|
224
249
|
autoComplete: state.context.otp ? "one-time-code" : "off",
|
|
225
250
|
placeholder: focusedIndex === index ? "" : state.context.placeholder,
|
|
@@ -285,12 +310,14 @@ var import_core = require("@zag-js/core");
|
|
|
285
310
|
|
|
286
311
|
// ../../utilities/form-utils/dist/index.mjs
|
|
287
312
|
function getWindow(el) {
|
|
288
|
-
|
|
313
|
+
var _a;
|
|
314
|
+
return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
289
315
|
}
|
|
290
316
|
function getDescriptor(el, options) {
|
|
317
|
+
var _a;
|
|
291
318
|
const { type, property } = options;
|
|
292
319
|
const proto = getWindow(el)[type].prototype;
|
|
293
|
-
return Object.getOwnPropertyDescriptor(proto, property)
|
|
320
|
+
return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
|
|
294
321
|
}
|
|
295
322
|
function dispatchInputValueEvent(el, value) {
|
|
296
323
|
var _a;
|
|
@@ -328,11 +355,12 @@ function machine(ctx) {
|
|
|
328
355
|
valueLength: (ctx2) => ctx2.value.length,
|
|
329
356
|
filledValueLength: (ctx2) => ctx2.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
|
|
330
357
|
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
331
|
-
valueAsString: (ctx2) => ctx2.value.join("")
|
|
358
|
+
valueAsString: (ctx2) => ctx2.value.join(""),
|
|
359
|
+
focusedValue: (ctx2) => ctx2.value[ctx2.focusedIndex]
|
|
332
360
|
},
|
|
333
361
|
watch: {
|
|
334
|
-
focusedIndex: "focusInput",
|
|
335
|
-
value: "invokeOnChange",
|
|
362
|
+
focusedIndex: ["focusInput", "setInputSelection"],
|
|
363
|
+
value: ["invokeOnChange", "dispatchInputEvent"],
|
|
336
364
|
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
337
365
|
},
|
|
338
366
|
on: {
|
|
@@ -382,17 +410,20 @@ function machine(ctx) {
|
|
|
382
410
|
INPUT: [
|
|
383
411
|
{
|
|
384
412
|
guard: and("isFinalValue", "isValidValue"),
|
|
385
|
-
actions: "setFocusedValue"
|
|
413
|
+
actions: ["setFocusedValue", "dispatchInputEventIfNeeded"]
|
|
386
414
|
},
|
|
387
415
|
{
|
|
388
416
|
guard: "isValidValue",
|
|
389
|
-
actions: ["setFocusedValue", "setNextFocusedIndex"]
|
|
417
|
+
actions: ["setFocusedValue", "setNextFocusedIndex", "dispatchInputEventIfNeeded"]
|
|
390
418
|
}
|
|
391
419
|
],
|
|
392
|
-
PASTE:
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
420
|
+
PASTE: [
|
|
421
|
+
{
|
|
422
|
+
guard: "isValidValue",
|
|
423
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
424
|
+
},
|
|
425
|
+
{ actions: "resetFocusedValue" }
|
|
426
|
+
],
|
|
396
427
|
BLUR: {
|
|
397
428
|
target: "idle",
|
|
398
429
|
actions: "clearFocusedIndex"
|
|
@@ -461,6 +492,16 @@ function machine(ctx) {
|
|
|
461
492
|
(_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.focus();
|
|
462
493
|
});
|
|
463
494
|
},
|
|
495
|
+
setInputSelection: (ctx2) => {
|
|
496
|
+
raf(() => {
|
|
497
|
+
if (ctx2.focusedIndex === -1)
|
|
498
|
+
return;
|
|
499
|
+
const input = dom.getFocusedEl(ctx2);
|
|
500
|
+
const length = input.value.length;
|
|
501
|
+
input.selectionStart = ctx2.selectOnFocus ? 0 : length;
|
|
502
|
+
input.selectionEnd = length;
|
|
503
|
+
});
|
|
504
|
+
},
|
|
464
505
|
invokeOnComplete: (ctx2) => {
|
|
465
506
|
var _a;
|
|
466
507
|
if (!ctx2.isValueComplete)
|
|
@@ -472,7 +513,16 @@ function machine(ctx) {
|
|
|
472
513
|
if (evt.type === "SETUP")
|
|
473
514
|
return;
|
|
474
515
|
(_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value) });
|
|
516
|
+
},
|
|
517
|
+
dispatchInputEvent: (ctx2, evt) => {
|
|
518
|
+
if (evt.type === "SETUP")
|
|
519
|
+
return;
|
|
475
520
|
dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
|
|
521
|
+
const inputs = dom.getElements(ctx2);
|
|
522
|
+
ctx2.value.forEach((val, index) => {
|
|
523
|
+
const input = inputs[index];
|
|
524
|
+
input.value = val || "";
|
|
525
|
+
});
|
|
476
526
|
},
|
|
477
527
|
invokeOnInvalid: (ctx2, evt) => {
|
|
478
528
|
var _a;
|
|
@@ -490,21 +540,34 @@ function machine(ctx) {
|
|
|
490
540
|
setFocusedValue: (ctx2, evt) => {
|
|
491
541
|
ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
|
|
492
542
|
},
|
|
543
|
+
dispatchInputEventIfNeeded: (ctx2, evt) => {
|
|
544
|
+
const valueIsChanged = lastChar(evt.value) !== ctx2.focusedValue;
|
|
545
|
+
if (evt.value.length <= 1 || valueIsChanged)
|
|
546
|
+
return;
|
|
547
|
+
const inputs = dom.getElements(ctx2);
|
|
548
|
+
const input = inputs[ctx2.focusedIndex];
|
|
549
|
+
input.value = lastChar(evt.value);
|
|
550
|
+
},
|
|
493
551
|
setPastedValue(ctx2, evt) {
|
|
494
552
|
raf(() => {
|
|
495
|
-
const
|
|
496
|
-
|
|
553
|
+
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
554
|
+
const value = evt.value.substring(startIndex, ctx2.valueLength);
|
|
555
|
+
assign(ctx2, value);
|
|
497
556
|
});
|
|
498
557
|
},
|
|
499
558
|
setValueAtIndex: (ctx2, evt) => {
|
|
500
559
|
ctx2.value[evt.index] = lastChar(evt.value);
|
|
501
560
|
},
|
|
502
561
|
clearValue: (ctx2) => {
|
|
503
|
-
|
|
562
|
+
ctx2.value = ctx2.value.map(() => "");
|
|
504
563
|
},
|
|
505
564
|
clearFocusedValue: (ctx2) => {
|
|
506
565
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
507
566
|
},
|
|
567
|
+
resetFocusedValue: (ctx2) => {
|
|
568
|
+
const input = dom.getFocusedEl(ctx2);
|
|
569
|
+
input.value = ctx2.focusedValue;
|
|
570
|
+
},
|
|
508
571
|
setFocusIndexToFirst: (ctx2) => {
|
|
509
572
|
ctx2.focusedIndex = 0;
|
|
510
573
|
},
|
|
@@ -553,16 +616,10 @@ function isValidType(value, type) {
|
|
|
553
616
|
return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
|
|
554
617
|
}
|
|
555
618
|
function assign(ctx, value) {
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
continue;
|
|
561
|
-
ctx.value[i] = value[i];
|
|
562
|
-
} else {
|
|
563
|
-
ctx.value[i] = value;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
619
|
+
const valueArr = value.split("").filter(Boolean);
|
|
620
|
+
const valueObj = Object.assign({}, ctx.value, valueArr);
|
|
621
|
+
console.log("valueObj :>> ", valueObj);
|
|
622
|
+
ctx.value = Object.values(valueObj);
|
|
566
623
|
}
|
|
567
624
|
function lastChar(value) {
|
|
568
625
|
return value.charAt(value.length - 1);
|
package/dist/index.mjs
CHANGED
|
@@ -12,22 +12,44 @@ function isWindow(value) {
|
|
|
12
12
|
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
13
13
|
}
|
|
14
14
|
function getDocument(el) {
|
|
15
|
+
var _a;
|
|
15
16
|
if (isWindow(el))
|
|
16
17
|
return el.document;
|
|
17
18
|
if (isDocument(el))
|
|
18
19
|
return el;
|
|
19
|
-
return (el == null ? void 0 : el.ownerDocument)
|
|
20
|
+
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
|
|
20
21
|
}
|
|
21
22
|
function defineDomHelpers(helpers) {
|
|
22
23
|
const dom2 = {
|
|
23
24
|
getRootNode: (ctx) => {
|
|
24
|
-
var _a;
|
|
25
|
-
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx))
|
|
25
|
+
var _a, _b;
|
|
26
|
+
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
26
27
|
},
|
|
27
28
|
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
28
|
-
getWin: (ctx) =>
|
|
29
|
+
getWin: (ctx) => {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
32
|
+
},
|
|
29
33
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
30
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
34
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
|
|
35
|
+
createEmitter: (ctx, target) => {
|
|
36
|
+
const win = dom2.getWin(ctx);
|
|
37
|
+
return function emit(evt, detail, options) {
|
|
38
|
+
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
39
|
+
const eventName = `zag:${evt}`;
|
|
40
|
+
const init = { bubbles, cancelable, composed, detail };
|
|
41
|
+
const event = new win.CustomEvent(eventName, init);
|
|
42
|
+
target.dispatchEvent(event);
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
createListener: (target) => {
|
|
46
|
+
return function listen(evt, handler) {
|
|
47
|
+
const eventName = `zag:${evt}`;
|
|
48
|
+
const listener = (e) => handler(e);
|
|
49
|
+
target.addEventListener(eventName, listener);
|
|
50
|
+
return () => target.removeEventListener(eventName, listener);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
31
53
|
};
|
|
32
54
|
return {
|
|
33
55
|
...dom2,
|
|
@@ -35,7 +57,8 @@ function defineDomHelpers(helpers) {
|
|
|
35
57
|
};
|
|
36
58
|
}
|
|
37
59
|
function getNativeEvent(e) {
|
|
38
|
-
|
|
60
|
+
var _a;
|
|
61
|
+
return (_a = e.nativeEvent) != null ? _a : e;
|
|
39
62
|
}
|
|
40
63
|
var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
|
|
41
64
|
var rtlKeyMap = {
|
|
@@ -52,9 +75,10 @@ var sameKeyMap = {
|
|
|
52
75
|
Right: "ArrowRight"
|
|
53
76
|
};
|
|
54
77
|
function getEventKey(event, options = {}) {
|
|
78
|
+
var _a;
|
|
55
79
|
const { dir = "ltr", orientation = "horizontal" } = options;
|
|
56
80
|
let { key } = event;
|
|
57
|
-
key = sameKeyMap[key]
|
|
81
|
+
key = (_a = sameKeyMap[key]) != null ? _a : key;
|
|
58
82
|
const isRtl = dir === "rtl" && orientation === "horizontal";
|
|
59
83
|
if (isRtl && key in rtlKeyMap) {
|
|
60
84
|
key = rtlKeyMap[key];
|
|
@@ -68,7 +92,8 @@ function raf(fn) {
|
|
|
68
92
|
};
|
|
69
93
|
}
|
|
70
94
|
function queryAll(root, selector) {
|
|
71
|
-
|
|
95
|
+
var _a;
|
|
96
|
+
return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
|
|
72
97
|
}
|
|
73
98
|
var visuallyHiddenStyle = {
|
|
74
99
|
border: "0",
|
|
@@ -95,20 +120,20 @@ function invariant(...a) {
|
|
|
95
120
|
// src/pin-input.dom.ts
|
|
96
121
|
var dom = defineDomHelpers({
|
|
97
122
|
getRootId: (ctx) => {
|
|
98
|
-
var _a;
|
|
99
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.root)
|
|
123
|
+
var _a, _b;
|
|
124
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.id}`;
|
|
100
125
|
},
|
|
101
126
|
getInputId: (ctx, id) => {
|
|
102
|
-
var _a, _b;
|
|
103
|
-
return ((_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id))
|
|
127
|
+
var _a, _b, _c;
|
|
128
|
+
return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.id}:${id}`;
|
|
104
129
|
},
|
|
105
130
|
getHiddenInputId: (ctx) => {
|
|
106
|
-
var _a;
|
|
107
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.hiddenInput)
|
|
131
|
+
var _a, _b;
|
|
132
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.hiddenInput) != null ? _b : `pin-input:${ctx.id}:hidden`;
|
|
108
133
|
},
|
|
109
134
|
getLabelId: (ctx) => {
|
|
110
|
-
var _a;
|
|
111
|
-
return ((_a = ctx.ids) == null ? void 0 : _a.label)
|
|
135
|
+
var _a, _b;
|
|
136
|
+
return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `pin-input:${ctx.id}:label`;
|
|
112
137
|
},
|
|
113
138
|
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
114
139
|
getElements: (ctx) => {
|
|
@@ -192,7 +217,7 @@ function connect(state, send, normalize) {
|
|
|
192
217
|
"aria-invalid": ariaAttr(isInvalid),
|
|
193
218
|
"data-invalid": dataAttr(isInvalid),
|
|
194
219
|
type: state.context.mask ? "password" : inputType,
|
|
195
|
-
|
|
220
|
+
defaultValue: state.context.value[index] || "",
|
|
196
221
|
autoCapitalize: "none",
|
|
197
222
|
autoComplete: state.context.otp ? "one-time-code" : "off",
|
|
198
223
|
placeholder: focusedIndex === index ? "" : state.context.placeholder,
|
|
@@ -258,12 +283,14 @@ import { createMachine, guards } from "@zag-js/core";
|
|
|
258
283
|
|
|
259
284
|
// ../../utilities/form-utils/dist/index.mjs
|
|
260
285
|
function getWindow(el) {
|
|
261
|
-
|
|
286
|
+
var _a;
|
|
287
|
+
return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
262
288
|
}
|
|
263
289
|
function getDescriptor(el, options) {
|
|
290
|
+
var _a;
|
|
264
291
|
const { type, property } = options;
|
|
265
292
|
const proto = getWindow(el)[type].prototype;
|
|
266
|
-
return Object.getOwnPropertyDescriptor(proto, property)
|
|
293
|
+
return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
|
|
267
294
|
}
|
|
268
295
|
function dispatchInputValueEvent(el, value) {
|
|
269
296
|
var _a;
|
|
@@ -301,11 +328,12 @@ function machine(ctx) {
|
|
|
301
328
|
valueLength: (ctx2) => ctx2.value.length,
|
|
302
329
|
filledValueLength: (ctx2) => ctx2.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
|
|
303
330
|
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
304
|
-
valueAsString: (ctx2) => ctx2.value.join("")
|
|
331
|
+
valueAsString: (ctx2) => ctx2.value.join(""),
|
|
332
|
+
focusedValue: (ctx2) => ctx2.value[ctx2.focusedIndex]
|
|
305
333
|
},
|
|
306
334
|
watch: {
|
|
307
|
-
focusedIndex: "focusInput",
|
|
308
|
-
value: "invokeOnChange",
|
|
335
|
+
focusedIndex: ["focusInput", "setInputSelection"],
|
|
336
|
+
value: ["invokeOnChange", "dispatchInputEvent"],
|
|
309
337
|
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
310
338
|
},
|
|
311
339
|
on: {
|
|
@@ -355,17 +383,20 @@ function machine(ctx) {
|
|
|
355
383
|
INPUT: [
|
|
356
384
|
{
|
|
357
385
|
guard: and("isFinalValue", "isValidValue"),
|
|
358
|
-
actions: "setFocusedValue"
|
|
386
|
+
actions: ["setFocusedValue", "dispatchInputEventIfNeeded"]
|
|
359
387
|
},
|
|
360
388
|
{
|
|
361
389
|
guard: "isValidValue",
|
|
362
|
-
actions: ["setFocusedValue", "setNextFocusedIndex"]
|
|
390
|
+
actions: ["setFocusedValue", "setNextFocusedIndex", "dispatchInputEventIfNeeded"]
|
|
363
391
|
}
|
|
364
392
|
],
|
|
365
|
-
PASTE:
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
393
|
+
PASTE: [
|
|
394
|
+
{
|
|
395
|
+
guard: "isValidValue",
|
|
396
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
397
|
+
},
|
|
398
|
+
{ actions: "resetFocusedValue" }
|
|
399
|
+
],
|
|
369
400
|
BLUR: {
|
|
370
401
|
target: "idle",
|
|
371
402
|
actions: "clearFocusedIndex"
|
|
@@ -434,6 +465,16 @@ function machine(ctx) {
|
|
|
434
465
|
(_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.focus();
|
|
435
466
|
});
|
|
436
467
|
},
|
|
468
|
+
setInputSelection: (ctx2) => {
|
|
469
|
+
raf(() => {
|
|
470
|
+
if (ctx2.focusedIndex === -1)
|
|
471
|
+
return;
|
|
472
|
+
const input = dom.getFocusedEl(ctx2);
|
|
473
|
+
const length = input.value.length;
|
|
474
|
+
input.selectionStart = ctx2.selectOnFocus ? 0 : length;
|
|
475
|
+
input.selectionEnd = length;
|
|
476
|
+
});
|
|
477
|
+
},
|
|
437
478
|
invokeOnComplete: (ctx2) => {
|
|
438
479
|
var _a;
|
|
439
480
|
if (!ctx2.isValueComplete)
|
|
@@ -445,7 +486,16 @@ function machine(ctx) {
|
|
|
445
486
|
if (evt.type === "SETUP")
|
|
446
487
|
return;
|
|
447
488
|
(_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value) });
|
|
489
|
+
},
|
|
490
|
+
dispatchInputEvent: (ctx2, evt) => {
|
|
491
|
+
if (evt.type === "SETUP")
|
|
492
|
+
return;
|
|
448
493
|
dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
|
|
494
|
+
const inputs = dom.getElements(ctx2);
|
|
495
|
+
ctx2.value.forEach((val, index) => {
|
|
496
|
+
const input = inputs[index];
|
|
497
|
+
input.value = val || "";
|
|
498
|
+
});
|
|
449
499
|
},
|
|
450
500
|
invokeOnInvalid: (ctx2, evt) => {
|
|
451
501
|
var _a;
|
|
@@ -463,21 +513,34 @@ function machine(ctx) {
|
|
|
463
513
|
setFocusedValue: (ctx2, evt) => {
|
|
464
514
|
ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
|
|
465
515
|
},
|
|
516
|
+
dispatchInputEventIfNeeded: (ctx2, evt) => {
|
|
517
|
+
const valueIsChanged = lastChar(evt.value) !== ctx2.focusedValue;
|
|
518
|
+
if (evt.value.length <= 1 || valueIsChanged)
|
|
519
|
+
return;
|
|
520
|
+
const inputs = dom.getElements(ctx2);
|
|
521
|
+
const input = inputs[ctx2.focusedIndex];
|
|
522
|
+
input.value = lastChar(evt.value);
|
|
523
|
+
},
|
|
466
524
|
setPastedValue(ctx2, evt) {
|
|
467
525
|
raf(() => {
|
|
468
|
-
const
|
|
469
|
-
|
|
526
|
+
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
527
|
+
const value = evt.value.substring(startIndex, ctx2.valueLength);
|
|
528
|
+
assign(ctx2, value);
|
|
470
529
|
});
|
|
471
530
|
},
|
|
472
531
|
setValueAtIndex: (ctx2, evt) => {
|
|
473
532
|
ctx2.value[evt.index] = lastChar(evt.value);
|
|
474
533
|
},
|
|
475
534
|
clearValue: (ctx2) => {
|
|
476
|
-
|
|
535
|
+
ctx2.value = ctx2.value.map(() => "");
|
|
477
536
|
},
|
|
478
537
|
clearFocusedValue: (ctx2) => {
|
|
479
538
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
480
539
|
},
|
|
540
|
+
resetFocusedValue: (ctx2) => {
|
|
541
|
+
const input = dom.getFocusedEl(ctx2);
|
|
542
|
+
input.value = ctx2.focusedValue;
|
|
543
|
+
},
|
|
481
544
|
setFocusIndexToFirst: (ctx2) => {
|
|
482
545
|
ctx2.focusedIndex = 0;
|
|
483
546
|
},
|
|
@@ -526,16 +589,10 @@ function isValidType(value, type) {
|
|
|
526
589
|
return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
|
|
527
590
|
}
|
|
528
591
|
function assign(ctx, value) {
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
continue;
|
|
534
|
-
ctx.value[i] = value[i];
|
|
535
|
-
} else {
|
|
536
|
-
ctx.value[i] = value;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
592
|
+
const valueArr = value.split("").filter(Boolean);
|
|
593
|
+
const valueObj = Object.assign({}, ctx.value, valueArr);
|
|
594
|
+
console.log("valueObj :>> ", valueObj);
|
|
595
|
+
ctx.value = Object.values(valueObj);
|
|
539
596
|
}
|
|
540
597
|
function lastChar(value) {
|
|
541
598
|
return value.charAt(value.length - 1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core logic for the pin-input widget implemented as a state machine",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@zag-js/core": "0.
|
|
33
|
-
"@zag-js/types": "0.
|
|
32
|
+
"@zag-js/core": "0.2.0",
|
|
33
|
+
"@zag-js/types": "0.3.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@zag-js/dom-utils": "0.
|
|
37
|
-
"@zag-js/form-utils": "0.
|
|
38
|
-
"@zag-js/utils": "0.
|
|
36
|
+
"@zag-js/dom-utils": "0.2.0",
|
|
37
|
+
"@zag-js/form-utils": "0.2.0",
|
|
38
|
+
"@zag-js/utils": "0.2.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build-fast": "tsup src/index.ts --format=esm,cjs",
|