@zag-js/pin-input 0.2.7 → 0.2.9
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/{chunk-OI3DK2PT.mjs → chunk-I5GCVNBL.mjs} +1 -19
- package/dist/{chunk-6HKN4SOY.mjs → chunk-IOTDORZW.mjs} +28 -20
- package/dist/{chunk-NT4W6JYX.mjs → chunk-KV727BEF.mjs} +0 -0
- package/dist/{chunk-BJXKBZJG.mjs → chunk-LUBGS2VH.mjs} +0 -0
- package/dist/{chunk-QX3A66VS.mjs → chunk-THBDCFBU.mjs} +3 -3
- package/dist/index.js +28 -38
- package/dist/index.mjs +5 -5
- package/dist/pin-input.anatomy.mjs +1 -1
- package/dist/pin-input.connect.js +2 -20
- package/dist/pin-input.connect.mjs +4 -4
- package/dist/pin-input.dom.d.ts +0 -4
- package/dist/pin-input.dom.js +1 -19
- package/dist/pin-input.dom.mjs +1 -1
- package/dist/pin-input.machine.js +27 -37
- package/dist/pin-input.machine.mjs +3 -3
- package/package.json +4 -4
|
@@ -29,25 +29,7 @@ function defineDomHelpers(helpers) {
|
|
|
29
29
|
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
30
30
|
},
|
|
31
31
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
32
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
33
|
-
createEmitter: (ctx, target) => {
|
|
34
|
-
const win = dom2.getWin(ctx);
|
|
35
|
-
return function emit(evt, detail, options) {
|
|
36
|
-
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
37
|
-
const eventName = `zag:${evt}`;
|
|
38
|
-
const init = { bubbles, cancelable, composed, detail };
|
|
39
|
-
const event = new win.CustomEvent(eventName, init);
|
|
40
|
-
target.dispatchEvent(event);
|
|
41
|
-
};
|
|
42
|
-
},
|
|
43
|
-
createListener: (target) => {
|
|
44
|
-
return function listen(evt, handler) {
|
|
45
|
-
const eventName = `zag:${evt}`;
|
|
46
|
-
const listener = (e) => handler(e);
|
|
47
|
-
target.addEventListener(eventName, listener);
|
|
48
|
-
return () => target.removeEventListener(eventName, listener);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
32
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
51
33
|
};
|
|
52
34
|
return {
|
|
53
35
|
...dom2,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
isObject
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-KV727BEF.mjs";
|
|
4
4
|
import {
|
|
5
5
|
dom,
|
|
6
6
|
getWindow
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-I5GCVNBL.mjs";
|
|
8
8
|
|
|
9
9
|
// src/pin-input.machine.ts
|
|
10
10
|
import { createMachine, guards } from "@zag-js/core";
|
|
@@ -125,11 +125,11 @@ function machine(userContext) {
|
|
|
125
125
|
INPUT: [
|
|
126
126
|
{
|
|
127
127
|
guard: and("isFinalValue", "isValidValue"),
|
|
128
|
-
actions: ["setFocusedValue", "invokeOnChange", "
|
|
128
|
+
actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
|
|
129
129
|
},
|
|
130
130
|
{
|
|
131
131
|
guard: "isValidValue",
|
|
132
|
-
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "
|
|
132
|
+
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
|
|
133
133
|
}
|
|
134
134
|
],
|
|
135
135
|
PASTE: [
|
|
@@ -196,8 +196,8 @@ function machine(userContext) {
|
|
|
196
196
|
actions: {
|
|
197
197
|
setupValue: (ctx2) => {
|
|
198
198
|
const inputs = dom.getElements(ctx2);
|
|
199
|
-
const
|
|
200
|
-
|
|
199
|
+
const emptyValues = Array.from({ length: inputs.length }).fill("");
|
|
200
|
+
assign(ctx2, emptyValues);
|
|
201
201
|
},
|
|
202
202
|
focusInput: (ctx2) => {
|
|
203
203
|
raf(() => {
|
|
@@ -253,28 +253,30 @@ function machine(userContext) {
|
|
|
253
253
|
ctx2.focusedIndex = evt.index;
|
|
254
254
|
},
|
|
255
255
|
setFocusedValue: (ctx2, evt) => {
|
|
256
|
-
ctx2.value[ctx2.focusedIndex] =
|
|
256
|
+
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
257
257
|
},
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
|
|
258
|
+
syncInputValue: (ctx2, evt) => {
|
|
259
|
+
const nextValue = getNextValue(ctx2.focusedValue, evt.value);
|
|
260
|
+
const changed = nextValue !== ctx2.focusedValue;
|
|
261
|
+
if (evt.value.length <= 1 || changed)
|
|
261
262
|
return;
|
|
262
263
|
const inputs = dom.getElements(ctx2);
|
|
263
264
|
const input = inputs[ctx2.focusedIndex];
|
|
264
|
-
input.value =
|
|
265
|
+
input.value = nextValue;
|
|
265
266
|
},
|
|
266
267
|
setPastedValue(ctx2, evt) {
|
|
267
268
|
raf(() => {
|
|
268
269
|
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
269
|
-
const value = evt.value.substring(startIndex, ctx2.valueLength);
|
|
270
|
+
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
270
271
|
assign(ctx2, value);
|
|
271
272
|
});
|
|
272
273
|
},
|
|
273
274
|
setValueAtIndex: (ctx2, evt) => {
|
|
274
|
-
ctx2.value[evt.index] =
|
|
275
|
+
ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
|
|
275
276
|
},
|
|
276
277
|
clearValue: (ctx2) => {
|
|
277
|
-
|
|
278
|
+
const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
|
|
279
|
+
assign(ctx2, nextValue);
|
|
278
280
|
},
|
|
279
281
|
clearFocusedValue: (ctx2) => {
|
|
280
282
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
@@ -310,7 +312,7 @@ function machine(userContext) {
|
|
|
310
312
|
},
|
|
311
313
|
requestFormSubmit(ctx2) {
|
|
312
314
|
var _a;
|
|
313
|
-
if (!ctx2.name)
|
|
315
|
+
if (!ctx2.name || !ctx2.isValueComplete)
|
|
314
316
|
return;
|
|
315
317
|
const input = dom.getHiddenInputEl(ctx2);
|
|
316
318
|
(_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
|
|
@@ -331,12 +333,18 @@ function isValidType(value, type) {
|
|
|
331
333
|
return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
|
|
332
334
|
}
|
|
333
335
|
function assign(ctx, value) {
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
336
|
+
const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
|
|
337
|
+
arr.forEach((value2, index) => {
|
|
338
|
+
ctx.value[index] = value2;
|
|
339
|
+
});
|
|
337
340
|
}
|
|
338
|
-
function
|
|
339
|
-
|
|
341
|
+
function getNextValue(current, next) {
|
|
342
|
+
let nextValue = next;
|
|
343
|
+
if (current[0] === next[0])
|
|
344
|
+
nextValue = next[1];
|
|
345
|
+
else if (current[0] === next[1])
|
|
346
|
+
nextValue = next[0];
|
|
347
|
+
return nextValue;
|
|
340
348
|
}
|
|
341
349
|
|
|
342
350
|
export {
|
|
File without changes
|
|
File without changes
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
parts
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-LUBGS2VH.mjs";
|
|
4
4
|
import {
|
|
5
5
|
dom
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-I5GCVNBL.mjs";
|
|
7
7
|
|
|
8
8
|
// ../../utilities/dom/src/attrs.ts
|
|
9
9
|
var dataAttr = (guard) => {
|
|
@@ -17,7 +17,7 @@ var ariaAttr = (guard) => {
|
|
|
17
17
|
function invariant(...a) {
|
|
18
18
|
const m = a.length === 1 ? a[0] : a[1];
|
|
19
19
|
const c = a.length === 2 ? a[0] : true;
|
|
20
|
-
if (c &&
|
|
20
|
+
if (c && true) {
|
|
21
21
|
throw new Error(m);
|
|
22
22
|
}
|
|
23
23
|
}
|
package/dist/index.js
CHANGED
|
@@ -56,7 +56,7 @@ function compact(obj) {
|
|
|
56
56
|
function invariant(...a) {
|
|
57
57
|
const m = a.length === 1 ? a[0] : a[1];
|
|
58
58
|
const c = a.length === 2 ? a[0] : true;
|
|
59
|
-
if (c &&
|
|
59
|
+
if (c && true) {
|
|
60
60
|
throw new Error(m);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -92,25 +92,7 @@ function defineDomHelpers(helpers) {
|
|
|
92
92
|
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
93
93
|
},
|
|
94
94
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
95
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
96
|
-
createEmitter: (ctx, target) => {
|
|
97
|
-
const win = dom2.getWin(ctx);
|
|
98
|
-
return function emit(evt, detail, options) {
|
|
99
|
-
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
100
|
-
const eventName = `zag:${evt}`;
|
|
101
|
-
const init = { bubbles, cancelable, composed, detail };
|
|
102
|
-
const event = new win.CustomEvent(eventName, init);
|
|
103
|
-
target.dispatchEvent(event);
|
|
104
|
-
};
|
|
105
|
-
},
|
|
106
|
-
createListener: (target) => {
|
|
107
|
-
return function listen(evt, handler) {
|
|
108
|
-
const eventName = `zag:${evt}`;
|
|
109
|
-
const listener = (e) => handler(e);
|
|
110
|
-
target.addEventListener(eventName, listener);
|
|
111
|
-
return () => target.removeEventListener(eventName, listener);
|
|
112
|
-
};
|
|
113
|
-
}
|
|
95
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
114
96
|
};
|
|
115
97
|
return {
|
|
116
98
|
...dom2,
|
|
@@ -452,11 +434,11 @@ function machine(userContext) {
|
|
|
452
434
|
INPUT: [
|
|
453
435
|
{
|
|
454
436
|
guard: and("isFinalValue", "isValidValue"),
|
|
455
|
-
actions: ["setFocusedValue", "invokeOnChange", "
|
|
437
|
+
actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
|
|
456
438
|
},
|
|
457
439
|
{
|
|
458
440
|
guard: "isValidValue",
|
|
459
|
-
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "
|
|
441
|
+
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
|
|
460
442
|
}
|
|
461
443
|
],
|
|
462
444
|
PASTE: [
|
|
@@ -523,8 +505,8 @@ function machine(userContext) {
|
|
|
523
505
|
actions: {
|
|
524
506
|
setupValue: (ctx2) => {
|
|
525
507
|
const inputs = dom.getElements(ctx2);
|
|
526
|
-
const
|
|
527
|
-
|
|
508
|
+
const emptyValues = Array.from({ length: inputs.length }).fill("");
|
|
509
|
+
assign(ctx2, emptyValues);
|
|
528
510
|
},
|
|
529
511
|
focusInput: (ctx2) => {
|
|
530
512
|
raf(() => {
|
|
@@ -580,28 +562,30 @@ function machine(userContext) {
|
|
|
580
562
|
ctx2.focusedIndex = evt.index;
|
|
581
563
|
},
|
|
582
564
|
setFocusedValue: (ctx2, evt) => {
|
|
583
|
-
ctx2.value[ctx2.focusedIndex] =
|
|
565
|
+
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
584
566
|
},
|
|
585
|
-
|
|
586
|
-
const
|
|
587
|
-
|
|
567
|
+
syncInputValue: (ctx2, evt) => {
|
|
568
|
+
const nextValue = getNextValue(ctx2.focusedValue, evt.value);
|
|
569
|
+
const changed = nextValue !== ctx2.focusedValue;
|
|
570
|
+
if (evt.value.length <= 1 || changed)
|
|
588
571
|
return;
|
|
589
572
|
const inputs = dom.getElements(ctx2);
|
|
590
573
|
const input = inputs[ctx2.focusedIndex];
|
|
591
|
-
input.value =
|
|
574
|
+
input.value = nextValue;
|
|
592
575
|
},
|
|
593
576
|
setPastedValue(ctx2, evt) {
|
|
594
577
|
raf(() => {
|
|
595
578
|
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
596
|
-
const value = evt.value.substring(startIndex, ctx2.valueLength);
|
|
579
|
+
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
597
580
|
assign(ctx2, value);
|
|
598
581
|
});
|
|
599
582
|
},
|
|
600
583
|
setValueAtIndex: (ctx2, evt) => {
|
|
601
|
-
ctx2.value[evt.index] =
|
|
584
|
+
ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
|
|
602
585
|
},
|
|
603
586
|
clearValue: (ctx2) => {
|
|
604
|
-
|
|
587
|
+
const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
|
|
588
|
+
assign(ctx2, nextValue);
|
|
605
589
|
},
|
|
606
590
|
clearFocusedValue: (ctx2) => {
|
|
607
591
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
@@ -637,7 +621,7 @@ function machine(userContext) {
|
|
|
637
621
|
},
|
|
638
622
|
requestFormSubmit(ctx2) {
|
|
639
623
|
var _a;
|
|
640
|
-
if (!ctx2.name)
|
|
624
|
+
if (!ctx2.name || !ctx2.isValueComplete)
|
|
641
625
|
return;
|
|
642
626
|
const input = dom.getHiddenInputEl(ctx2);
|
|
643
627
|
(_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
|
|
@@ -658,12 +642,18 @@ function isValidType(value, type) {
|
|
|
658
642
|
return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
|
|
659
643
|
}
|
|
660
644
|
function assign(ctx, value) {
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
645
|
+
const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
|
|
646
|
+
arr.forEach((value2, index) => {
|
|
647
|
+
ctx.value[index] = value2;
|
|
648
|
+
});
|
|
664
649
|
}
|
|
665
|
-
function
|
|
666
|
-
|
|
650
|
+
function getNextValue(current, next) {
|
|
651
|
+
let nextValue = next;
|
|
652
|
+
if (current[0] === next[0])
|
|
653
|
+
nextValue = next[1];
|
|
654
|
+
else if (current[0] === next[1])
|
|
655
|
+
nextValue = next[0];
|
|
656
|
+
return nextValue;
|
|
667
657
|
}
|
|
668
658
|
// Annotate the CommonJS export names for ESM import in node:
|
|
669
659
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
connect
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-THBDCFBU.mjs";
|
|
4
4
|
import {
|
|
5
5
|
anatomy
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-LUBGS2VH.mjs";
|
|
7
7
|
import {
|
|
8
8
|
machine
|
|
9
|
-
} from "./chunk-
|
|
10
|
-
import "./chunk-
|
|
11
|
-
import "./chunk-
|
|
9
|
+
} from "./chunk-IOTDORZW.mjs";
|
|
10
|
+
import "./chunk-KV727BEF.mjs";
|
|
11
|
+
import "./chunk-I5GCVNBL.mjs";
|
|
12
12
|
export {
|
|
13
13
|
anatomy,
|
|
14
14
|
connect,
|
|
@@ -36,7 +36,7 @@ var ariaAttr = (guard) => {
|
|
|
36
36
|
function invariant(...a) {
|
|
37
37
|
const m = a.length === 1 ? a[0] : a[1];
|
|
38
38
|
const c = a.length === 2 ? a[0] : true;
|
|
39
|
-
if (c &&
|
|
39
|
+
if (c && true) {
|
|
40
40
|
throw new Error(m);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -68,25 +68,7 @@ function defineDomHelpers(helpers) {
|
|
|
68
68
|
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
69
69
|
},
|
|
70
70
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
71
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
72
|
-
createEmitter: (ctx, target) => {
|
|
73
|
-
const win = dom2.getWin(ctx);
|
|
74
|
-
return function emit(evt, detail, options) {
|
|
75
|
-
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
76
|
-
const eventName = `zag:${evt}`;
|
|
77
|
-
const init = { bubbles, cancelable, composed, detail };
|
|
78
|
-
const event = new win.CustomEvent(eventName, init);
|
|
79
|
-
target.dispatchEvent(event);
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
|
-
createListener: (target) => {
|
|
83
|
-
return function listen(evt, handler) {
|
|
84
|
-
const eventName = `zag:${evt}`;
|
|
85
|
-
const listener = (e) => handler(e);
|
|
86
|
-
target.addEventListener(eventName, listener);
|
|
87
|
-
return () => target.removeEventListener(eventName, listener);
|
|
88
|
-
};
|
|
89
|
-
}
|
|
71
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
90
72
|
};
|
|
91
73
|
return {
|
|
92
74
|
...dom2,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
connect
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-THBDCFBU.mjs";
|
|
4
|
+
import "./chunk-LUBGS2VH.mjs";
|
|
5
|
+
import "./chunk-KV727BEF.mjs";
|
|
6
|
+
import "./chunk-I5GCVNBL.mjs";
|
|
7
7
|
export {
|
|
8
8
|
connect
|
|
9
9
|
};
|
package/dist/pin-input.dom.d.ts
CHANGED
|
@@ -18,10 +18,6 @@ declare const dom: {
|
|
|
18
18
|
getById: <T = HTMLElement>(ctx: {
|
|
19
19
|
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
20
20
|
}, id: string) => T | null;
|
|
21
|
-
createEmitter: (ctx: {
|
|
22
|
-
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
23
|
-
}, target: HTMLElement) => (evt: string, detail: Record<string, any>, options?: EventInit | undefined) => void;
|
|
24
|
-
createListener: (target: HTMLElement) => <T_1 = any>(evt: string, handler: (e: CustomEvent<T_1>) => void) => () => void;
|
|
25
21
|
} & {
|
|
26
22
|
getRootId: (ctx: MachineContext) => string;
|
|
27
23
|
getInputId: (ctx: MachineContext, id: string) => string;
|
package/dist/pin-input.dom.js
CHANGED
|
@@ -51,25 +51,7 @@ function defineDomHelpers(helpers) {
|
|
|
51
51
|
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
52
52
|
},
|
|
53
53
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
54
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
55
|
-
createEmitter: (ctx, target) => {
|
|
56
|
-
const win = dom2.getWin(ctx);
|
|
57
|
-
return function emit(evt, detail, options) {
|
|
58
|
-
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
59
|
-
const eventName = `zag:${evt}`;
|
|
60
|
-
const init = { bubbles, cancelable, composed, detail };
|
|
61
|
-
const event = new win.CustomEvent(eventName, init);
|
|
62
|
-
target.dispatchEvent(event);
|
|
63
|
-
};
|
|
64
|
-
},
|
|
65
|
-
createListener: (target) => {
|
|
66
|
-
return function listen(evt, handler) {
|
|
67
|
-
const eventName = `zag:${evt}`;
|
|
68
|
-
const listener = (e) => handler(e);
|
|
69
|
-
target.addEventListener(eventName, listener);
|
|
70
|
-
return () => target.removeEventListener(eventName, listener);
|
|
71
|
-
};
|
|
72
|
-
}
|
|
54
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
73
55
|
};
|
|
74
56
|
return {
|
|
75
57
|
...dom2,
|
package/dist/pin-input.dom.mjs
CHANGED
|
@@ -69,25 +69,7 @@ function defineDomHelpers(helpers) {
|
|
|
69
69
|
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
|
|
70
70
|
},
|
|
71
71
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
72
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
73
|
-
createEmitter: (ctx, target) => {
|
|
74
|
-
const win = dom2.getWin(ctx);
|
|
75
|
-
return function emit(evt, detail, options) {
|
|
76
|
-
const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
|
|
77
|
-
const eventName = `zag:${evt}`;
|
|
78
|
-
const init = { bubbles, cancelable, composed, detail };
|
|
79
|
-
const event = new win.CustomEvent(eventName, init);
|
|
80
|
-
target.dispatchEvent(event);
|
|
81
|
-
};
|
|
82
|
-
},
|
|
83
|
-
createListener: (target) => {
|
|
84
|
-
return function listen(evt, handler) {
|
|
85
|
-
const eventName = `zag:${evt}`;
|
|
86
|
-
const listener = (e) => handler(e);
|
|
87
|
-
target.addEventListener(eventName, listener);
|
|
88
|
-
return () => target.removeEventListener(eventName, listener);
|
|
89
|
-
};
|
|
90
|
-
}
|
|
72
|
+
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
91
73
|
};
|
|
92
74
|
return {
|
|
93
75
|
...dom2,
|
|
@@ -241,11 +223,11 @@ function machine(userContext) {
|
|
|
241
223
|
INPUT: [
|
|
242
224
|
{
|
|
243
225
|
guard: and("isFinalValue", "isValidValue"),
|
|
244
|
-
actions: ["setFocusedValue", "invokeOnChange", "
|
|
226
|
+
actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
|
|
245
227
|
},
|
|
246
228
|
{
|
|
247
229
|
guard: "isValidValue",
|
|
248
|
-
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "
|
|
230
|
+
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
|
|
249
231
|
}
|
|
250
232
|
],
|
|
251
233
|
PASTE: [
|
|
@@ -312,8 +294,8 @@ function machine(userContext) {
|
|
|
312
294
|
actions: {
|
|
313
295
|
setupValue: (ctx2) => {
|
|
314
296
|
const inputs = dom.getElements(ctx2);
|
|
315
|
-
const
|
|
316
|
-
|
|
297
|
+
const emptyValues = Array.from({ length: inputs.length }).fill("");
|
|
298
|
+
assign(ctx2, emptyValues);
|
|
317
299
|
},
|
|
318
300
|
focusInput: (ctx2) => {
|
|
319
301
|
raf(() => {
|
|
@@ -369,28 +351,30 @@ function machine(userContext) {
|
|
|
369
351
|
ctx2.focusedIndex = evt.index;
|
|
370
352
|
},
|
|
371
353
|
setFocusedValue: (ctx2, evt) => {
|
|
372
|
-
ctx2.value[ctx2.focusedIndex] =
|
|
354
|
+
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
373
355
|
},
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
356
|
+
syncInputValue: (ctx2, evt) => {
|
|
357
|
+
const nextValue = getNextValue(ctx2.focusedValue, evt.value);
|
|
358
|
+
const changed = nextValue !== ctx2.focusedValue;
|
|
359
|
+
if (evt.value.length <= 1 || changed)
|
|
377
360
|
return;
|
|
378
361
|
const inputs = dom.getElements(ctx2);
|
|
379
362
|
const input = inputs[ctx2.focusedIndex];
|
|
380
|
-
input.value =
|
|
363
|
+
input.value = nextValue;
|
|
381
364
|
},
|
|
382
365
|
setPastedValue(ctx2, evt) {
|
|
383
366
|
raf(() => {
|
|
384
367
|
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
385
|
-
const value = evt.value.substring(startIndex, ctx2.valueLength);
|
|
368
|
+
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
386
369
|
assign(ctx2, value);
|
|
387
370
|
});
|
|
388
371
|
},
|
|
389
372
|
setValueAtIndex: (ctx2, evt) => {
|
|
390
|
-
ctx2.value[evt.index] =
|
|
373
|
+
ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
|
|
391
374
|
},
|
|
392
375
|
clearValue: (ctx2) => {
|
|
393
|
-
|
|
376
|
+
const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
|
|
377
|
+
assign(ctx2, nextValue);
|
|
394
378
|
},
|
|
395
379
|
clearFocusedValue: (ctx2) => {
|
|
396
380
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
@@ -426,7 +410,7 @@ function machine(userContext) {
|
|
|
426
410
|
},
|
|
427
411
|
requestFormSubmit(ctx2) {
|
|
428
412
|
var _a;
|
|
429
|
-
if (!ctx2.name)
|
|
413
|
+
if (!ctx2.name || !ctx2.isValueComplete)
|
|
430
414
|
return;
|
|
431
415
|
const input = dom.getHiddenInputEl(ctx2);
|
|
432
416
|
(_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
|
|
@@ -447,12 +431,18 @@ function isValidType(value, type) {
|
|
|
447
431
|
return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
|
|
448
432
|
}
|
|
449
433
|
function assign(ctx, value) {
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
434
|
+
const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
|
|
435
|
+
arr.forEach((value2, index) => {
|
|
436
|
+
ctx.value[index] = value2;
|
|
437
|
+
});
|
|
453
438
|
}
|
|
454
|
-
function
|
|
455
|
-
|
|
439
|
+
function getNextValue(current, next) {
|
|
440
|
+
let nextValue = next;
|
|
441
|
+
if (current[0] === next[0])
|
|
442
|
+
nextValue = next[1];
|
|
443
|
+
else if (current[0] === next[1])
|
|
444
|
+
nextValue = next[0];
|
|
445
|
+
return nextValue;
|
|
456
446
|
}
|
|
457
447
|
// Annotate the CommonJS export names for ESM import in node:
|
|
458
448
|
0 && (module.exports = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "Core logic for the pin-input widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@zag-js/anatomy": "0.1.3",
|
|
30
|
-
"@zag-js/core": "0.2.
|
|
31
|
-
"@zag-js/types": "0.3.
|
|
30
|
+
"@zag-js/core": "0.2.6",
|
|
31
|
+
"@zag-js/types": "0.3.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"clean-package": "2.2.0",
|
|
35
|
-
"@zag-js/dom-utils": "0.2.
|
|
35
|
+
"@zag-js/dom-utils": "0.2.3",
|
|
36
36
|
"@zag-js/form-utils": "0.2.3",
|
|
37
37
|
"@zag-js/utils": "0.3.2"
|
|
38
38
|
},
|