@zag-js/pin-input 0.1.10 → 0.1.13
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/LICENSE +21 -0
- package/dist/index.d.ts +136 -3
- package/dist/index.js +260 -286
- package/dist/index.mjs +255 -289
- package/package.json +13 -11
- package/dist/pin-input.connect.d.ts +0 -16
- package/dist/pin-input.dom.d.ts +0 -27
- package/dist/pin-input.machine.d.ts +0 -2
- package/dist/pin-input.types.d.ts +0 -122
package/dist/index.mjs
CHANGED
|
@@ -1,40 +1,4 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
|
|
21
1
|
// ../../utilities/dom/dist/index.mjs
|
|
22
|
-
var __defProp2 = Object.defineProperty;
|
|
23
|
-
var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
|
|
24
|
-
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
25
|
-
var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
|
|
26
|
-
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
27
|
-
var __spreadValues2 = (a, b) => {
|
|
28
|
-
for (var prop in b || (b = {}))
|
|
29
|
-
if (__hasOwnProp2.call(b, prop))
|
|
30
|
-
__defNormalProp2(a, prop, b[prop]);
|
|
31
|
-
if (__getOwnPropSymbols2)
|
|
32
|
-
for (var prop of __getOwnPropSymbols2(b)) {
|
|
33
|
-
if (__propIsEnum2.call(b, prop))
|
|
34
|
-
__defNormalProp2(a, prop, b[prop]);
|
|
35
|
-
}
|
|
36
|
-
return a;
|
|
37
|
-
};
|
|
38
2
|
var dataAttr = (guard) => {
|
|
39
3
|
return guard ? "" : void 0;
|
|
40
4
|
};
|
|
@@ -48,56 +12,32 @@ function isWindow(value) {
|
|
|
48
12
|
return (value == null ? void 0 : value.toString()) === "[object Window]";
|
|
49
13
|
}
|
|
50
14
|
function getDocument(el) {
|
|
51
|
-
var _a;
|
|
52
15
|
if (isWindow(el))
|
|
53
16
|
return el.document;
|
|
54
17
|
if (isDocument(el))
|
|
55
18
|
return el;
|
|
56
|
-
return (
|
|
57
|
-
}
|
|
58
|
-
function getWindow(el) {
|
|
59
|
-
var _a;
|
|
60
|
-
return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
19
|
+
return (el == null ? void 0 : el.ownerDocument) ?? document;
|
|
61
20
|
}
|
|
62
21
|
function defineDomHelpers(helpers) {
|
|
63
22
|
const dom2 = {
|
|
64
23
|
getRootNode: (ctx) => {
|
|
65
|
-
var _a, _b;
|
|
66
|
-
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
|
|
67
|
-
},
|
|
68
|
-
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
69
|
-
getWin: (ctx) => {
|
|
70
24
|
var _a;
|
|
71
|
-
return (_a =
|
|
25
|
+
return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
|
|
72
26
|
},
|
|
27
|
+
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
28
|
+
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
73
29
|
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
74
30
|
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
75
31
|
};
|
|
76
|
-
return
|
|
32
|
+
return {
|
|
33
|
+
...dom2,
|
|
34
|
+
...helpers
|
|
35
|
+
};
|
|
77
36
|
}
|
|
78
37
|
function getNativeEvent(e) {
|
|
79
|
-
|
|
80
|
-
return (_a = e.nativeEvent) != null ? _a : e;
|
|
38
|
+
return e.nativeEvent ?? e;
|
|
81
39
|
}
|
|
82
40
|
var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
|
|
83
|
-
function getDescriptor(el, options) {
|
|
84
|
-
var _a;
|
|
85
|
-
const { type, property } = options;
|
|
86
|
-
const proto = getWindow(el)[type].prototype;
|
|
87
|
-
return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
|
|
88
|
-
}
|
|
89
|
-
function dispatchInputValueEvent(el, value) {
|
|
90
|
-
var _a;
|
|
91
|
-
if (!el)
|
|
92
|
-
return;
|
|
93
|
-
const win = getWindow(el);
|
|
94
|
-
if (!(el instanceof win.HTMLInputElement))
|
|
95
|
-
return;
|
|
96
|
-
const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
|
|
97
|
-
(_a = desc.set) == null ? void 0 : _a.call(el, value);
|
|
98
|
-
const event = new win.Event("input", { bubbles: true });
|
|
99
|
-
el.dispatchEvent(event);
|
|
100
|
-
}
|
|
101
41
|
var rtlKeyMap = {
|
|
102
42
|
ArrowLeft: "ArrowRight",
|
|
103
43
|
ArrowRight: "ArrowLeft",
|
|
@@ -114,10 +54,9 @@ var sameKeyMap = {
|
|
|
114
54
|
Right: "ArrowRight"
|
|
115
55
|
};
|
|
116
56
|
function getEventKey(event, options = {}) {
|
|
117
|
-
var _a;
|
|
118
57
|
const { dir = "ltr", orientation = "horizontal" } = options;
|
|
119
58
|
let { key } = event;
|
|
120
|
-
key =
|
|
59
|
+
key = sameKeyMap[key] ?? key;
|
|
121
60
|
const isRtl = dir === "rtl" && orientation === "horizontal";
|
|
122
61
|
if (isRtl && key in rtlKeyMap) {
|
|
123
62
|
key = rtlKeyMap[key];
|
|
@@ -131,8 +70,7 @@ function raf(fn) {
|
|
|
131
70
|
};
|
|
132
71
|
}
|
|
133
72
|
function queryAll(root, selector) {
|
|
134
|
-
|
|
135
|
-
return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
|
|
73
|
+
return Array.from((root == null ? void 0 : root.querySelectorAll(selector)) ?? []);
|
|
136
74
|
}
|
|
137
75
|
var visuallyHiddenStyle = {
|
|
138
76
|
border: "0",
|
|
@@ -151,7 +89,7 @@ var visuallyHiddenStyle = {
|
|
|
151
89
|
function invariant(...a) {
|
|
152
90
|
const m = a.length === 1 ? a[0] : a[1];
|
|
153
91
|
const c = a.length === 2 ? a[0] : true;
|
|
154
|
-
if (c &&
|
|
92
|
+
if (c && process.env.NODE_ENV !== "production") {
|
|
155
93
|
throw new Error(m);
|
|
156
94
|
}
|
|
157
95
|
}
|
|
@@ -159,16 +97,16 @@ function invariant(...a) {
|
|
|
159
97
|
// src/pin-input.dom.ts
|
|
160
98
|
var dom = defineDomHelpers({
|
|
161
99
|
getRootId: (ctx) => {
|
|
162
|
-
var _a
|
|
163
|
-
return (
|
|
100
|
+
var _a;
|
|
101
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `pin-input:${ctx.id}`;
|
|
164
102
|
},
|
|
165
103
|
getInputId: (ctx, id) => {
|
|
166
|
-
var _a, _b
|
|
167
|
-
return (
|
|
104
|
+
var _a, _b;
|
|
105
|
+
return ((_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) ?? `pin-input:${ctx.id}:${id}`;
|
|
168
106
|
},
|
|
169
107
|
getHiddenInputId: (ctx) => {
|
|
170
|
-
var _a
|
|
171
|
-
return (
|
|
108
|
+
var _a;
|
|
109
|
+
return ((_a = ctx.ids) == null ? void 0 : _a.hiddenInput) ?? `pin-input:${ctx.id}:hidden`;
|
|
172
110
|
},
|
|
173
111
|
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
174
112
|
getElements: (ctx) => {
|
|
@@ -300,236 +238,264 @@ function connect(state, send, normalize) {
|
|
|
300
238
|
|
|
301
239
|
// src/pin-input.machine.ts
|
|
302
240
|
import { createMachine, guards } from "@zag-js/core";
|
|
241
|
+
|
|
242
|
+
// ../../utilities/form-utils/dist/index.mjs
|
|
243
|
+
function getWindow(el) {
|
|
244
|
+
return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
|
|
245
|
+
}
|
|
246
|
+
function getDescriptor(el, options) {
|
|
247
|
+
const { type, property } = options;
|
|
248
|
+
const proto = getWindow(el)[type].prototype;
|
|
249
|
+
return Object.getOwnPropertyDescriptor(proto, property) ?? {};
|
|
250
|
+
}
|
|
251
|
+
function dispatchInputValueEvent(el, value) {
|
|
252
|
+
var _a;
|
|
253
|
+
if (!el)
|
|
254
|
+
return;
|
|
255
|
+
const win = getWindow(el);
|
|
256
|
+
if (!(el instanceof win.HTMLInputElement))
|
|
257
|
+
return;
|
|
258
|
+
const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
|
|
259
|
+
(_a = desc.set) == null ? void 0 : _a.call(el, value);
|
|
260
|
+
const event = new win.Event("input", { bubbles: true });
|
|
261
|
+
el.dispatchEvent(event);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/pin-input.machine.ts
|
|
303
265
|
var { and, not } = guards;
|
|
304
266
|
function machine(ctx) {
|
|
305
|
-
return createMachine(
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
computed: {
|
|
320
|
-
valueLength: (ctx2) => ctx2.value.length,
|
|
321
|
-
filledValueLength: (ctx2) => ctx2.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
|
|
322
|
-
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
323
|
-
valueAsString: (ctx2) => ctx2.value.join("")
|
|
324
|
-
},
|
|
325
|
-
watch: {
|
|
326
|
-
focusedIndex: "focusInput",
|
|
327
|
-
value: "invokeOnChange",
|
|
328
|
-
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
329
|
-
},
|
|
330
|
-
on: {
|
|
331
|
-
SET_VALUE: [
|
|
332
|
-
{
|
|
333
|
-
guard: "hasIndex",
|
|
334
|
-
actions: "setValueAtIndex"
|
|
335
|
-
},
|
|
336
|
-
{ actions: "setValue" }
|
|
337
|
-
],
|
|
338
|
-
CLEAR_VALUE: [
|
|
339
|
-
{
|
|
340
|
-
guard: "isDisabled",
|
|
341
|
-
actions: "clearValue"
|
|
342
|
-
},
|
|
343
|
-
{
|
|
344
|
-
actions: ["clearValue", "setFocusIndexToFirst"]
|
|
345
|
-
}
|
|
346
|
-
]
|
|
347
|
-
},
|
|
348
|
-
states: {
|
|
349
|
-
unknown: {
|
|
350
|
-
on: {
|
|
351
|
-
SETUP: [
|
|
352
|
-
{
|
|
353
|
-
guard: "autoFocus",
|
|
354
|
-
target: "focused",
|
|
355
|
-
actions: ["setupValue", "setFocusIndexToFirst"]
|
|
356
|
-
},
|
|
357
|
-
{
|
|
358
|
-
target: "idle",
|
|
359
|
-
actions: "setupValue"
|
|
360
|
-
}
|
|
361
|
-
]
|
|
267
|
+
return createMachine(
|
|
268
|
+
{
|
|
269
|
+
id: "pin-input",
|
|
270
|
+
initial: "unknown",
|
|
271
|
+
context: {
|
|
272
|
+
value: [],
|
|
273
|
+
focusedIndex: -1,
|
|
274
|
+
placeholder: "\u25CB",
|
|
275
|
+
otp: false,
|
|
276
|
+
type: "numeric",
|
|
277
|
+
...ctx,
|
|
278
|
+
messages: {
|
|
279
|
+
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
280
|
+
...ctx.messages
|
|
362
281
|
}
|
|
363
282
|
},
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
}
|
|
283
|
+
computed: {
|
|
284
|
+
valueLength: (ctx2) => ctx2.value.length,
|
|
285
|
+
filledValueLength: (ctx2) => ctx2.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
|
|
286
|
+
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
287
|
+
valueAsString: (ctx2) => ctx2.value.join("")
|
|
371
288
|
},
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
],
|
|
384
|
-
PASTE: {
|
|
385
|
-
guard: "isValidValue",
|
|
386
|
-
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
387
|
-
},
|
|
388
|
-
BLUR: {
|
|
389
|
-
target: "idle",
|
|
390
|
-
actions: "clearFocusedIndex"
|
|
391
|
-
},
|
|
392
|
-
DELETE: {
|
|
393
|
-
guard: "hasValue",
|
|
394
|
-
actions: "clearFocusedValue"
|
|
395
|
-
},
|
|
396
|
-
ARROW_LEFT: {
|
|
397
|
-
actions: "setPrevFocusedIndex"
|
|
289
|
+
watch: {
|
|
290
|
+
focusedIndex: "focusInput",
|
|
291
|
+
value: "invokeOnChange",
|
|
292
|
+
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
293
|
+
},
|
|
294
|
+
on: {
|
|
295
|
+
SET_VALUE: [
|
|
296
|
+
{
|
|
297
|
+
guard: "hasIndex",
|
|
298
|
+
actions: "setValueAtIndex"
|
|
398
299
|
},
|
|
399
|
-
|
|
400
|
-
|
|
300
|
+
{ actions: "setValue" }
|
|
301
|
+
],
|
|
302
|
+
CLEAR_VALUE: [
|
|
303
|
+
{
|
|
304
|
+
guard: "isDisabled",
|
|
305
|
+
actions: "clearValue"
|
|
401
306
|
},
|
|
402
|
-
|
|
403
|
-
|
|
307
|
+
{
|
|
308
|
+
actions: ["clearValue", "setFocusIndexToFirst"]
|
|
309
|
+
}
|
|
310
|
+
]
|
|
311
|
+
},
|
|
312
|
+
states: {
|
|
313
|
+
unknown: {
|
|
314
|
+
on: {
|
|
315
|
+
SETUP: [
|
|
316
|
+
{
|
|
317
|
+
guard: "autoFocus",
|
|
318
|
+
target: "focused",
|
|
319
|
+
actions: ["setupValue", "setFocusIndexToFirst"]
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
target: "idle",
|
|
323
|
+
actions: "setupValue"
|
|
324
|
+
}
|
|
325
|
+
]
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
idle: {
|
|
329
|
+
on: {
|
|
330
|
+
FOCUS: {
|
|
331
|
+
target: "focused",
|
|
332
|
+
actions: "setFocusedIndex"
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
focused: {
|
|
337
|
+
on: {
|
|
338
|
+
INPUT: [
|
|
339
|
+
{
|
|
340
|
+
guard: and("isFinalValue", "isValidValue"),
|
|
341
|
+
actions: "setFocusedValue"
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
guard: "isValidValue",
|
|
345
|
+
actions: ["setFocusedValue", "setNextFocusedIndex"]
|
|
346
|
+
}
|
|
347
|
+
],
|
|
348
|
+
PASTE: {
|
|
349
|
+
guard: "isValidValue",
|
|
350
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
351
|
+
},
|
|
352
|
+
BLUR: {
|
|
353
|
+
target: "idle",
|
|
354
|
+
actions: "clearFocusedIndex"
|
|
355
|
+
},
|
|
356
|
+
DELETE: {
|
|
404
357
|
guard: "hasValue",
|
|
405
358
|
actions: "clearFocusedValue"
|
|
406
359
|
},
|
|
407
|
-
{
|
|
408
|
-
actions:
|
|
360
|
+
ARROW_LEFT: {
|
|
361
|
+
actions: "setPrevFocusedIndex"
|
|
362
|
+
},
|
|
363
|
+
ARROW_RIGHT: {
|
|
364
|
+
actions: "setNextFocusedIndex"
|
|
365
|
+
},
|
|
366
|
+
BACKSPACE: [
|
|
367
|
+
{
|
|
368
|
+
guard: "hasValue",
|
|
369
|
+
actions: "clearFocusedValue"
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
actions: ["setPrevFocusedIndex", "clearFocusedValue"]
|
|
373
|
+
}
|
|
374
|
+
],
|
|
375
|
+
ENTER: {
|
|
376
|
+
guard: "isValueComplete",
|
|
377
|
+
actions: "requestFormSubmit"
|
|
378
|
+
},
|
|
379
|
+
KEY_DOWN: {
|
|
380
|
+
guard: not("isValidValue"),
|
|
381
|
+
actions: ["preventDefault", "invokeOnInvalid"]
|
|
409
382
|
}
|
|
410
|
-
],
|
|
411
|
-
ENTER: {
|
|
412
|
-
guard: "isValueComplete",
|
|
413
|
-
actions: "requestFormSubmit"
|
|
414
|
-
},
|
|
415
|
-
KEY_DOWN: {
|
|
416
|
-
guard: not("isValidValue"),
|
|
417
|
-
actions: ["preventDefault", "invokeOnInvalid"]
|
|
418
383
|
}
|
|
419
384
|
}
|
|
420
385
|
}
|
|
421
|
-
}
|
|
422
|
-
}, {
|
|
423
|
-
guards: {
|
|
424
|
-
autoFocus: (ctx2) => !!ctx2.autoFocus,
|
|
425
|
-
isValueEmpty: (_ctx, evt) => evt.value === "",
|
|
426
|
-
hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
|
|
427
|
-
isValueComplete: (ctx2) => ctx2.isValueComplete,
|
|
428
|
-
isValidValue: (ctx2, evt) => {
|
|
429
|
-
if (!ctx2.pattern)
|
|
430
|
-
return isValidType(evt.value, ctx2.type);
|
|
431
|
-
const regex = new RegExp(ctx2.pattern, "g");
|
|
432
|
-
return regex.test(evt.value);
|
|
433
|
-
},
|
|
434
|
-
isFinalValue: (ctx2) => {
|
|
435
|
-
return ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex;
|
|
436
|
-
},
|
|
437
|
-
isLastInputFocused: (ctx2) => ctx2.focusedIndex === ctx2.valueLength - 1,
|
|
438
|
-
hasIndex: (_ctx, evt) => evt.index !== void 0,
|
|
439
|
-
isDisabled: (ctx2) => !!ctx2.disabled
|
|
440
386
|
},
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
ctx2.value
|
|
387
|
+
{
|
|
388
|
+
guards: {
|
|
389
|
+
autoFocus: (ctx2) => !!ctx2.autoFocus,
|
|
390
|
+
isValueEmpty: (_ctx, evt) => evt.value === "",
|
|
391
|
+
hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
|
|
392
|
+
isValueComplete: (ctx2) => ctx2.isValueComplete,
|
|
393
|
+
isValidValue: (ctx2, evt) => {
|
|
394
|
+
if (!ctx2.pattern)
|
|
395
|
+
return isValidType(evt.value, ctx2.type);
|
|
396
|
+
const regex = new RegExp(ctx2.pattern, "g");
|
|
397
|
+
return regex.test(evt.value);
|
|
398
|
+
},
|
|
399
|
+
isFinalValue: (ctx2) => {
|
|
400
|
+
return ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex;
|
|
401
|
+
},
|
|
402
|
+
isLastInputFocused: (ctx2) => ctx2.focusedIndex === ctx2.valueLength - 1,
|
|
403
|
+
hasIndex: (_ctx, evt) => evt.index !== void 0,
|
|
404
|
+
isDisabled: (ctx2) => !!ctx2.disabled
|
|
446
405
|
},
|
|
447
|
-
|
|
448
|
-
|
|
406
|
+
actions: {
|
|
407
|
+
setupValue: (ctx2) => {
|
|
408
|
+
const inputs = dom.getElements(ctx2);
|
|
409
|
+
const empty = Array.from({ length: inputs.length }).map(() => "");
|
|
410
|
+
ctx2.value = Object.assign(empty, ctx2.value);
|
|
411
|
+
},
|
|
412
|
+
focusInput: (ctx2) => {
|
|
413
|
+
raf(() => {
|
|
414
|
+
var _a;
|
|
415
|
+
if (ctx2.focusedIndex === -1)
|
|
416
|
+
return;
|
|
417
|
+
(_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.focus();
|
|
418
|
+
});
|
|
419
|
+
},
|
|
420
|
+
invokeOnComplete: (ctx2) => {
|
|
449
421
|
var _a;
|
|
450
|
-
if (ctx2.
|
|
422
|
+
if (!ctx2.isValueComplete)
|
|
451
423
|
return;
|
|
452
|
-
(_a =
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
invokeOnComplete: (ctx2) => {
|
|
456
|
-
var _a;
|
|
457
|
-
if (!ctx2.isValueComplete)
|
|
458
|
-
return;
|
|
459
|
-
(_a = ctx2.onComplete) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
|
|
460
|
-
},
|
|
461
|
-
invokeOnChange: (ctx2, evt) => {
|
|
462
|
-
var _a;
|
|
463
|
-
if (evt.type === "SETUP")
|
|
464
|
-
return;
|
|
465
|
-
(_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value) });
|
|
466
|
-
dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
|
|
467
|
-
},
|
|
468
|
-
invokeOnInvalid: (ctx2, evt) => {
|
|
469
|
-
var _a;
|
|
470
|
-
(_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, { value: evt.value, index: ctx2.focusedIndex });
|
|
471
|
-
},
|
|
472
|
-
clearFocusedIndex: (ctx2) => {
|
|
473
|
-
ctx2.focusedIndex = -1;
|
|
474
|
-
},
|
|
475
|
-
setValue: (ctx2, evt) => {
|
|
476
|
-
assign(ctx2, evt.value);
|
|
477
|
-
},
|
|
478
|
-
setFocusedIndex: (ctx2, evt) => {
|
|
479
|
-
ctx2.focusedIndex = evt.index;
|
|
480
|
-
},
|
|
481
|
-
setFocusedValue: (ctx2, evt) => {
|
|
482
|
-
ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
|
|
483
|
-
},
|
|
484
|
-
setPastedValue(ctx2, evt) {
|
|
485
|
-
raf(() => {
|
|
486
|
-
const value = evt.value.substring(0, ctx2.valueLength);
|
|
487
|
-
assign(ctx2, value.split("").filter(Boolean));
|
|
488
|
-
});
|
|
489
|
-
},
|
|
490
|
-
setValueAtIndex: (ctx2, evt) => {
|
|
491
|
-
ctx2.value[evt.index] = lastChar(evt.value);
|
|
492
|
-
},
|
|
493
|
-
clearValue: (ctx2) => {
|
|
494
|
-
assign(ctx2, "");
|
|
495
|
-
},
|
|
496
|
-
clearFocusedValue: (ctx2) => {
|
|
497
|
-
ctx2.value[ctx2.focusedIndex] = "";
|
|
498
|
-
},
|
|
499
|
-
setFocusIndexToFirst: (ctx2) => {
|
|
500
|
-
ctx2.focusedIndex = 0;
|
|
501
|
-
},
|
|
502
|
-
setNextFocusedIndex: (ctx2) => {
|
|
503
|
-
ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
|
|
504
|
-
},
|
|
505
|
-
setPrevFocusedIndex: (ctx2) => {
|
|
506
|
-
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
507
|
-
},
|
|
508
|
-
setLastValueFocusIndex: (ctx2) => {
|
|
509
|
-
raf(() => {
|
|
510
|
-
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
511
|
-
});
|
|
512
|
-
},
|
|
513
|
-
preventDefault(_, evt) {
|
|
514
|
-
evt.preventDefault();
|
|
515
|
-
},
|
|
516
|
-
blurFocusedInputIfNeeded(ctx2) {
|
|
517
|
-
if (!ctx2.blurOnComplete)
|
|
518
|
-
return;
|
|
519
|
-
raf(() => {
|
|
424
|
+
(_a = ctx2.onComplete) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
|
|
425
|
+
},
|
|
426
|
+
invokeOnChange: (ctx2, evt) => {
|
|
520
427
|
var _a;
|
|
521
|
-
(
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
428
|
+
if (evt.type === "SETUP")
|
|
429
|
+
return;
|
|
430
|
+
(_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value) });
|
|
431
|
+
dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
|
|
432
|
+
},
|
|
433
|
+
invokeOnInvalid: (ctx2, evt) => {
|
|
434
|
+
var _a;
|
|
435
|
+
(_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, { value: evt.value, index: ctx2.focusedIndex });
|
|
436
|
+
},
|
|
437
|
+
clearFocusedIndex: (ctx2) => {
|
|
438
|
+
ctx2.focusedIndex = -1;
|
|
439
|
+
},
|
|
440
|
+
setValue: (ctx2, evt) => {
|
|
441
|
+
assign(ctx2, evt.value);
|
|
442
|
+
},
|
|
443
|
+
setFocusedIndex: (ctx2, evt) => {
|
|
444
|
+
ctx2.focusedIndex = evt.index;
|
|
445
|
+
},
|
|
446
|
+
setFocusedValue: (ctx2, evt) => {
|
|
447
|
+
ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
|
|
448
|
+
},
|
|
449
|
+
setPastedValue(ctx2, evt) {
|
|
450
|
+
raf(() => {
|
|
451
|
+
const value = evt.value.substring(0, ctx2.valueLength);
|
|
452
|
+
assign(ctx2, value.split("").filter(Boolean));
|
|
453
|
+
});
|
|
454
|
+
},
|
|
455
|
+
setValueAtIndex: (ctx2, evt) => {
|
|
456
|
+
ctx2.value[evt.index] = lastChar(evt.value);
|
|
457
|
+
},
|
|
458
|
+
clearValue: (ctx2) => {
|
|
459
|
+
assign(ctx2, "");
|
|
460
|
+
},
|
|
461
|
+
clearFocusedValue: (ctx2) => {
|
|
462
|
+
ctx2.value[ctx2.focusedIndex] = "";
|
|
463
|
+
},
|
|
464
|
+
setFocusIndexToFirst: (ctx2) => {
|
|
465
|
+
ctx2.focusedIndex = 0;
|
|
466
|
+
},
|
|
467
|
+
setNextFocusedIndex: (ctx2) => {
|
|
468
|
+
ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
|
|
469
|
+
},
|
|
470
|
+
setPrevFocusedIndex: (ctx2) => {
|
|
471
|
+
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
472
|
+
},
|
|
473
|
+
setLastValueFocusIndex: (ctx2) => {
|
|
474
|
+
raf(() => {
|
|
475
|
+
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
476
|
+
});
|
|
477
|
+
},
|
|
478
|
+
preventDefault(_, evt) {
|
|
479
|
+
evt.preventDefault();
|
|
480
|
+
},
|
|
481
|
+
blurFocusedInputIfNeeded(ctx2) {
|
|
482
|
+
if (!ctx2.blurOnComplete)
|
|
483
|
+
return;
|
|
484
|
+
raf(() => {
|
|
485
|
+
var _a;
|
|
486
|
+
(_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.blur();
|
|
487
|
+
});
|
|
488
|
+
},
|
|
489
|
+
requestFormSubmit(ctx2) {
|
|
490
|
+
var _a;
|
|
491
|
+
if (!ctx2.name)
|
|
492
|
+
return;
|
|
493
|
+
const input = dom.getHiddenInputEl(ctx2);
|
|
494
|
+
(_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
|
|
495
|
+
}
|
|
530
496
|
}
|
|
531
497
|
}
|
|
532
|
-
|
|
498
|
+
);
|
|
533
499
|
}
|
|
534
500
|
var REGEX = {
|
|
535
501
|
numeric: /^[0-9]+$/,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"description": "Core logic for the pin-input widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -29,20 +29,22 @@
|
|
|
29
29
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@zag-js/core": "0.1.
|
|
33
|
-
"@zag-js/types": "0.2.
|
|
32
|
+
"@zag-js/core": "0.1.9",
|
|
33
|
+
"@zag-js/types": "0.2.4"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@zag-js/dom-utils": "0.1.
|
|
37
|
-
"@zag-js/utils": "0.1.
|
|
36
|
+
"@zag-js/dom-utils": "0.1.10",
|
|
37
|
+
"@zag-js/form-utils": "0.1.0",
|
|
38
|
+
"@zag-js/utils": "0.1.3"
|
|
38
39
|
},
|
|
39
40
|
"scripts": {
|
|
40
|
-
"build
|
|
41
|
-
"start": "
|
|
42
|
-
"build": "
|
|
41
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
42
|
+
"start": "pnpm build --watch",
|
|
43
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
43
44
|
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
44
45
|
"lint": "eslint src --ext .ts,.tsx",
|
|
45
|
-
"test
|
|
46
|
-
"test
|
|
46
|
+
"test-ci": "pnpm test --ci --runInBand",
|
|
47
|
+
"test-watch": "pnpm test --watch -u",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
47
49
|
}
|
|
48
|
-
}
|
|
50
|
+
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { NormalizeProps, PropTypes } from "@zag-js/types";
|
|
2
|
-
import type { Send, State } from "./pin-input.types";
|
|
3
|
-
export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
4
|
-
value: string[];
|
|
5
|
-
valueAsString: string;
|
|
6
|
-
isValueComplete: boolean;
|
|
7
|
-
setValue(value: string[]): void;
|
|
8
|
-
clearValue(): void;
|
|
9
|
-
setValueAtIndex(index: number, value: string): void;
|
|
10
|
-
focus(): void;
|
|
11
|
-
rootProps: T["element"];
|
|
12
|
-
hiddenInputProps: T["input"];
|
|
13
|
-
getInputProps({ index }: {
|
|
14
|
-
index: number;
|
|
15
|
-
}): T["input"];
|
|
16
|
-
};
|