@zag-js/pin-input 0.10.4 → 0.11.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.mjs CHANGED
@@ -1,3 +1,475 @@
1
- export { anatomy } from './pin-input.anatomy.mjs';
2
- export { connect } from './pin-input.connect.mjs';
3
- export { machine } from './pin-input.machine.mjs';
1
+ // src/pin-input.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("pinInput").parts("root", "label", "hiddenInput", "input", "control");
4
+ var parts = anatomy.build();
5
+
6
+ // src/pin-input.connect.ts
7
+ import { getEventKey, getNativeEvent, isModifiedEvent } from "@zag-js/dom-event";
8
+ import { ariaAttr, dataAttr } from "@zag-js/dom-query";
9
+ import { invariant } from "@zag-js/utils";
10
+ import { visuallyHiddenStyle } from "@zag-js/visually-hidden";
11
+
12
+ // src/pin-input.dom.ts
13
+ import { createScope, queryAll } from "@zag-js/dom-query";
14
+ var dom = createScope({
15
+ getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
16
+ getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
17
+ getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
18
+ getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
19
+ getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
20
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
21
+ getElements: (ctx) => {
22
+ const ownerId = CSS.escape(dom.getRootId(ctx));
23
+ const selector = `input[data-ownedby=${ownerId}]`;
24
+ return queryAll(dom.getRootEl(ctx), selector);
25
+ },
26
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
27
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
28
+ getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
29
+ getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
30
+ });
31
+
32
+ // src/pin-input.connect.ts
33
+ function connect(state, send, normalize) {
34
+ const isValueComplete = state.context.isValueComplete;
35
+ const isInvalid = state.context.invalid;
36
+ const focusedIndex = state.context.focusedIndex;
37
+ const translations = state.context.translations;
38
+ function focus() {
39
+ dom.getFirstInputEl(state.context)?.focus();
40
+ }
41
+ return {
42
+ /**
43
+ * The value of the input as an array of strings.
44
+ */
45
+ value: state.context.value,
46
+ /**
47
+ * The value of the input as a string.
48
+ */
49
+ valueAsString: state.context.valueAsString,
50
+ /**
51
+ * Whether all inputs are filled.
52
+ */
53
+ isValueComplete,
54
+ /**
55
+ * Function to set the value of the inputs.
56
+ */
57
+ setValue(value) {
58
+ if (!Array.isArray(value)) {
59
+ invariant("[pin-input/setValue] value must be an array");
60
+ }
61
+ send({ type: "SET_VALUE", value });
62
+ },
63
+ /**
64
+ * Function to clear the value of the inputs.
65
+ */
66
+ clearValue() {
67
+ send({ type: "CLEAR_VALUE" });
68
+ },
69
+ /**
70
+ * Function to set the value of the input at a specific index.
71
+ */
72
+ setValueAtIndex(index, value) {
73
+ send({ type: "SET_VALUE", value, index });
74
+ },
75
+ /**
76
+ * Function to focus the pin-input. This will focus the first input.
77
+ */
78
+ focus,
79
+ rootProps: normalize.element({
80
+ dir: state.context.dir,
81
+ ...parts.root.attrs,
82
+ id: dom.getRootId(state.context),
83
+ "data-invalid": dataAttr(isInvalid),
84
+ "data-disabled": dataAttr(state.context.disabled),
85
+ "data-complete": dataAttr(isValueComplete)
86
+ }),
87
+ labelProps: normalize.label({
88
+ ...parts.label.attrs,
89
+ htmlFor: dom.getHiddenInputId(state.context),
90
+ id: dom.getLabelId(state.context),
91
+ "data-invalid": dataAttr(isInvalid),
92
+ "data-disabled": dataAttr(state.context.disabled),
93
+ "data-complete": dataAttr(isValueComplete),
94
+ onClick: (event) => {
95
+ event.preventDefault();
96
+ focus();
97
+ }
98
+ }),
99
+ hiddenInputProps: normalize.input({
100
+ ...parts.hiddenInput.attrs,
101
+ "aria-hidden": true,
102
+ type: "text",
103
+ tabIndex: -1,
104
+ id: dom.getHiddenInputId(state.context),
105
+ name: state.context.name,
106
+ form: state.context.form,
107
+ style: visuallyHiddenStyle,
108
+ maxLength: state.context.valueLength,
109
+ defaultValue: state.context.valueAsString
110
+ }),
111
+ controlProps: normalize.element({
112
+ ...parts.control.attrs,
113
+ id: dom.getControlId(state.context)
114
+ }),
115
+ getInputProps({ index }) {
116
+ const inputType = state.context.type === "numeric" ? "tel" : "text";
117
+ return normalize.input({
118
+ ...parts.input.attrs,
119
+ disabled: state.context.disabled,
120
+ "data-disabled": dataAttr(state.context.disabled),
121
+ "data-complete": dataAttr(isValueComplete),
122
+ id: dom.getInputId(state.context, index.toString()),
123
+ "data-ownedby": dom.getRootId(state.context),
124
+ "aria-label": translations.inputLabel(index, state.context.valueLength),
125
+ inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
126
+ "aria-invalid": ariaAttr(isInvalid),
127
+ "data-invalid": dataAttr(isInvalid),
128
+ type: state.context.mask ? "password" : inputType,
129
+ defaultValue: state.context.value[index] || "",
130
+ autoCapitalize: "none",
131
+ autoComplete: state.context.otp ? "one-time-code" : "off",
132
+ placeholder: focusedIndex === index ? "" : state.context.placeholder,
133
+ onChange(event) {
134
+ const evt = getNativeEvent(event);
135
+ const { value } = event.currentTarget;
136
+ if (evt.inputType === "insertFromPaste" || value.length > 2) {
137
+ send({ type: "PASTE", value });
138
+ event.preventDefault();
139
+ return;
140
+ }
141
+ if (evt.inputType === "deleteContentBackward") {
142
+ send("BACKSPACE");
143
+ return;
144
+ }
145
+ send({ type: "INPUT", value, index });
146
+ },
147
+ onKeyDown(event) {
148
+ const evt = getNativeEvent(event);
149
+ if (evt.isComposing || isModifiedEvent(evt))
150
+ return;
151
+ const keyMap = {
152
+ Backspace() {
153
+ send("BACKSPACE");
154
+ },
155
+ Delete() {
156
+ send("DELETE");
157
+ },
158
+ ArrowLeft() {
159
+ send("ARROW_LEFT");
160
+ },
161
+ ArrowRight() {
162
+ send("ARROW_RIGHT");
163
+ },
164
+ Enter() {
165
+ send("ENTER");
166
+ }
167
+ };
168
+ const key = getEventKey(event, { dir: state.context.dir });
169
+ const exec = keyMap[key];
170
+ if (exec) {
171
+ exec(event);
172
+ event.preventDefault();
173
+ } else {
174
+ if (key === "Tab")
175
+ return;
176
+ send({ type: "KEY_DOWN", value: key, preventDefault: () => event.preventDefault() });
177
+ }
178
+ },
179
+ onFocus() {
180
+ send({ type: "FOCUS", index });
181
+ },
182
+ onBlur() {
183
+ send({ type: "BLUR", index });
184
+ }
185
+ });
186
+ }
187
+ };
188
+ }
189
+
190
+ // src/pin-input.machine.ts
191
+ import { createMachine, guards } from "@zag-js/core";
192
+ import { raf } from "@zag-js/dom-query";
193
+ import { dispatchInputValueEvent } from "@zag-js/form-utils";
194
+ import { compact } from "@zag-js/utils";
195
+ var { and, not } = guards;
196
+ function machine(userContext) {
197
+ const ctx = compact(userContext);
198
+ return createMachine(
199
+ {
200
+ id: "pin-input",
201
+ initial: ctx.autoFocus ? "focused" : "idle",
202
+ context: {
203
+ value: [],
204
+ focusedIndex: -1,
205
+ placeholder: "\u25CB",
206
+ otp: false,
207
+ type: "numeric",
208
+ ...ctx,
209
+ translations: {
210
+ inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
211
+ ...ctx.translations
212
+ }
213
+ },
214
+ computed: {
215
+ valueLength: (ctx2) => ctx2.value.length,
216
+ filledValueLength: (ctx2) => ctx2.value.filter((v) => v?.trim() !== "").length,
217
+ isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
218
+ valueAsString: (ctx2) => ctx2.value.join(""),
219
+ focusedValue: (ctx2) => ctx2.value[ctx2.focusedIndex]
220
+ },
221
+ watch: {
222
+ focusedIndex: ["focusInput", "setInputSelection"],
223
+ value: ["dispatchInputEvent", "syncInputElements"],
224
+ isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
225
+ },
226
+ entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
227
+ on: {
228
+ SET_VALUE: [
229
+ {
230
+ guard: "hasIndex",
231
+ actions: ["setValueAtIndex", "invokeOnChange"]
232
+ },
233
+ { actions: ["setValue", "invokeOnChange"] }
234
+ ],
235
+ CLEAR_VALUE: [
236
+ {
237
+ guard: "isDisabled",
238
+ actions: ["clearValue", "invokeOnChange"]
239
+ },
240
+ {
241
+ actions: ["clearValue", "invokeOnChange", "setFocusIndexToFirst"]
242
+ }
243
+ ]
244
+ },
245
+ states: {
246
+ idle: {
247
+ on: {
248
+ FOCUS: {
249
+ target: "focused",
250
+ actions: "setFocusedIndex"
251
+ }
252
+ }
253
+ },
254
+ focused: {
255
+ on: {
256
+ INPUT: [
257
+ {
258
+ guard: and("isFinalValue", "isValidValue"),
259
+ actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
260
+ },
261
+ {
262
+ guard: "isValidValue",
263
+ actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
264
+ }
265
+ ],
266
+ PASTE: [
267
+ {
268
+ guard: "isValidValue",
269
+ actions: ["setPastedValue", "invokeOnChange", "setLastValueFocusIndex"]
270
+ },
271
+ { actions: ["resetFocusedValue", "invokeOnChange"] }
272
+ ],
273
+ BLUR: {
274
+ target: "idle",
275
+ actions: "clearFocusedIndex"
276
+ },
277
+ DELETE: {
278
+ guard: "hasValue",
279
+ actions: ["clearFocusedValue", "invokeOnChange"]
280
+ },
281
+ ARROW_LEFT: {
282
+ actions: "setPrevFocusedIndex"
283
+ },
284
+ ARROW_RIGHT: {
285
+ actions: "setNextFocusedIndex"
286
+ },
287
+ BACKSPACE: [
288
+ {
289
+ guard: "hasValue",
290
+ actions: ["clearFocusedValue", "invokeOnChange"]
291
+ },
292
+ {
293
+ actions: ["setPrevFocusedIndex", "clearFocusedValue", "invokeOnChange"]
294
+ }
295
+ ],
296
+ ENTER: {
297
+ guard: "isValueComplete",
298
+ actions: "requestFormSubmit"
299
+ },
300
+ KEY_DOWN: {
301
+ guard: not("isValidValue"),
302
+ actions: ["preventDefault", "invokeOnInvalid"]
303
+ }
304
+ }
305
+ }
306
+ }
307
+ },
308
+ {
309
+ guards: {
310
+ autoFocus: (ctx2) => !!ctx2.autoFocus,
311
+ isValueEmpty: (_ctx, evt) => evt.value === "",
312
+ hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
313
+ isValueComplete: (ctx2) => ctx2.isValueComplete,
314
+ isValidValue: (ctx2, evt) => {
315
+ if (!ctx2.pattern)
316
+ return isValidType(evt.value, ctx2.type);
317
+ const regex = new RegExp(ctx2.pattern, "g");
318
+ return regex.test(evt.value);
319
+ },
320
+ isFinalValue: (ctx2) => {
321
+ return ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex;
322
+ },
323
+ isLastInputFocused: (ctx2) => ctx2.focusedIndex === ctx2.valueLength - 1,
324
+ hasIndex: (_ctx, evt) => evt.index !== void 0,
325
+ isDisabled: (ctx2) => !!ctx2.disabled
326
+ },
327
+ actions: {
328
+ setupValue: (ctx2) => {
329
+ if (ctx2.value.length)
330
+ return;
331
+ const inputs = dom.getElements(ctx2);
332
+ const emptyValues = Array.from({ length: inputs.length }).fill("");
333
+ assign(ctx2, emptyValues);
334
+ },
335
+ focusInput: (ctx2) => {
336
+ raf(() => {
337
+ if (ctx2.focusedIndex === -1)
338
+ return;
339
+ dom.getFocusedInputEl(ctx2)?.focus();
340
+ });
341
+ },
342
+ setInputSelection: (ctx2) => {
343
+ raf(() => {
344
+ if (ctx2.focusedIndex === -1)
345
+ return;
346
+ const input = dom.getFocusedInputEl(ctx2);
347
+ const length = input.value.length;
348
+ input.selectionStart = ctx2.selectOnFocus ? 0 : length;
349
+ input.selectionEnd = length;
350
+ });
351
+ },
352
+ invokeOnComplete: (ctx2) => {
353
+ if (!ctx2.isValueComplete)
354
+ return;
355
+ ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
356
+ },
357
+ invokeOnChange: (ctx2) => {
358
+ ctx2.onChange?.({ value: Array.from(ctx2.value) });
359
+ },
360
+ dispatchInputEvent: (ctx2) => {
361
+ const inputEl = dom.getHiddenInputEl(ctx2);
362
+ dispatchInputValueEvent(inputEl, { value: ctx2.valueAsString });
363
+ },
364
+ invokeOnInvalid: (ctx2, evt) => {
365
+ ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
366
+ },
367
+ clearFocusedIndex: (ctx2) => {
368
+ ctx2.focusedIndex = -1;
369
+ },
370
+ setValue: (ctx2, evt) => {
371
+ assign(ctx2, evt.value);
372
+ },
373
+ setFocusedIndex: (ctx2, evt) => {
374
+ ctx2.focusedIndex = evt.index;
375
+ },
376
+ setFocusedValue: (ctx2, evt) => {
377
+ ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
378
+ },
379
+ syncInputValue(ctx2, evt) {
380
+ const input = dom.getInputEl(ctx2, evt.index.toString());
381
+ if (!input)
382
+ return;
383
+ input.value = ctx2.value[evt.index];
384
+ },
385
+ syncInputElements(ctx2) {
386
+ const inputs = dom.getElements(ctx2);
387
+ inputs.forEach((input, index) => {
388
+ input.value = ctx2.value[index];
389
+ });
390
+ },
391
+ setPastedValue(ctx2, evt) {
392
+ raf(() => {
393
+ const startIndex = ctx2.focusedValue ? 1 : 0;
394
+ const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
395
+ assign(ctx2, value);
396
+ });
397
+ },
398
+ setValueAtIndex: (ctx2, evt) => {
399
+ ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
400
+ },
401
+ clearValue: (ctx2) => {
402
+ const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
403
+ assign(ctx2, nextValue);
404
+ },
405
+ clearFocusedValue: (ctx2) => {
406
+ ctx2.value[ctx2.focusedIndex] = "";
407
+ },
408
+ resetFocusedValue: (ctx2) => {
409
+ const input = dom.getFocusedInputEl(ctx2);
410
+ input.value = ctx2.focusedValue;
411
+ },
412
+ setFocusIndexToFirst: (ctx2) => {
413
+ ctx2.focusedIndex = 0;
414
+ },
415
+ setNextFocusedIndex: (ctx2) => {
416
+ ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
417
+ },
418
+ setPrevFocusedIndex: (ctx2) => {
419
+ ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
420
+ },
421
+ setLastValueFocusIndex: (ctx2) => {
422
+ raf(() => {
423
+ ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
424
+ });
425
+ },
426
+ preventDefault(_, evt) {
427
+ evt.preventDefault();
428
+ },
429
+ blurFocusedInputIfNeeded(ctx2) {
430
+ if (!ctx2.blurOnComplete)
431
+ return;
432
+ raf(() => {
433
+ dom.getFocusedInputEl(ctx2)?.blur();
434
+ });
435
+ },
436
+ requestFormSubmit(ctx2) {
437
+ if (!ctx2.name || !ctx2.isValueComplete)
438
+ return;
439
+ const input = dom.getHiddenInputEl(ctx2);
440
+ input?.form?.requestSubmit();
441
+ }
442
+ }
443
+ }
444
+ );
445
+ }
446
+ var REGEX = {
447
+ numeric: /^[0-9]+$/,
448
+ alphabetic: /^[A-Za-z]+$/,
449
+ alphanumeric: /^[a-zA-Z0-9]+$/i
450
+ };
451
+ function isValidType(value, type) {
452
+ if (!type)
453
+ return true;
454
+ return !!REGEX[type]?.test(value);
455
+ }
456
+ function assign(ctx, value) {
457
+ const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
458
+ arr.forEach((value2, index) => {
459
+ ctx.value[index] = value2;
460
+ });
461
+ }
462
+ function getNextValue(current, next) {
463
+ let nextValue = next;
464
+ if (current[0] === next[0])
465
+ nextValue = next[1];
466
+ else if (current[0] === next[1])
467
+ nextValue = next[0];
468
+ return nextValue;
469
+ }
470
+ export {
471
+ anatomy,
472
+ connect,
473
+ machine
474
+ };
475
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/pin-input.anatomy.ts","../src/pin-input.connect.ts","../src/pin-input.dom.ts","../src/pin-input.machine.ts"],"sourcesContent":["import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"pinInput\").parts(\"root\", \"label\", \"hiddenInput\", \"input\", \"control\")\nexport const parts = anatomy.build()\n","import { EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from \"@zag-js/dom-event\"\nimport { ariaAttr, dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { invariant } from \"@zag-js/utils\"\nimport { visuallyHiddenStyle } from \"@zag-js/visually-hidden\"\nimport { parts } from \"./pin-input.anatomy\"\nimport { dom } from \"./pin-input.dom\"\nimport type { Send, State } from \"./pin-input.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>) {\n const isValueComplete = state.context.isValueComplete\n const isInvalid = state.context.invalid\n const focusedIndex = state.context.focusedIndex\n const translations = state.context.translations\n\n function focus() {\n dom.getFirstInputEl(state.context)?.focus()\n }\n\n return {\n /**\n * The value of the input as an array of strings.\n */\n value: state.context.value,\n /**\n * The value of the input as a string.\n */\n valueAsString: state.context.valueAsString,\n /**\n * Whether all inputs are filled.\n */\n isValueComplete: isValueComplete,\n /**\n * Function to set the value of the inputs.\n */\n setValue(value: string[]) {\n if (!Array.isArray(value)) {\n invariant(\"[pin-input/setValue] value must be an array\")\n }\n send({ type: \"SET_VALUE\", value })\n },\n /**\n * Function to clear the value of the inputs.\n */\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n /**\n * Function to set the value of the input at a specific index.\n */\n setValueAtIndex(index: number, value: string) {\n send({ type: \"SET_VALUE\", value, index })\n },\n /**\n * Function to focus the pin-input. This will focus the first input.\n */\n focus,\n\n rootProps: normalize.element({\n dir: state.context.dir,\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n }),\n\n labelProps: normalize.label({\n ...parts.label.attrs,\n htmlFor: dom.getHiddenInputId(state.context),\n id: dom.getLabelId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n onClick: (event) => {\n event.preventDefault()\n focus()\n },\n }),\n\n hiddenInputProps: normalize.input({\n ...parts.hiddenInput.attrs,\n \"aria-hidden\": true,\n type: \"text\",\n tabIndex: -1,\n id: dom.getHiddenInputId(state.context),\n name: state.context.name,\n form: state.context.form,\n style: visuallyHiddenStyle,\n maxLength: state.context.valueLength,\n defaultValue: state.context.valueAsString,\n }),\n\n controlProps: normalize.element({\n ...parts.control.attrs,\n id: dom.getControlId(state.context),\n }),\n\n getInputProps({ index }: { index: number }) {\n const inputType = state.context.type === \"numeric\" ? \"tel\" : \"text\"\n return normalize.input({\n ...parts.input.attrs,\n disabled: state.context.disabled,\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n id: dom.getInputId(state.context, index.toString()),\n \"data-ownedby\": dom.getRootId(state.context),\n \"aria-label\": translations.inputLabel(index, state.context.valueLength),\n inputMode: state.context.otp || state.context.type === \"numeric\" ? \"numeric\" : \"text\",\n \"aria-invalid\": ariaAttr(isInvalid),\n \"data-invalid\": dataAttr(isInvalid),\n type: state.context.mask ? \"password\" : inputType,\n defaultValue: state.context.value[index] || \"\",\n autoCapitalize: \"none\",\n autoComplete: state.context.otp ? \"one-time-code\" : \"off\",\n placeholder: focusedIndex === index ? \"\" : state.context.placeholder,\n onChange(event) {\n const evt = getNativeEvent(event)\n const { value } = event.currentTarget\n if (evt.inputType === \"insertFromPaste\" || value.length > 2) {\n send({ type: \"PASTE\", value })\n event.preventDefault()\n return\n }\n\n if (evt.inputType === \"deleteContentBackward\") {\n send(\"BACKSPACE\")\n return\n }\n send({ type: \"INPUT\", value, index })\n },\n onKeyDown(event) {\n const evt = getNativeEvent(event)\n if (evt.isComposing || isModifiedEvent(evt)) return\n\n const keyMap: EventKeyMap = {\n Backspace() {\n send(\"BACKSPACE\")\n },\n Delete() {\n send(\"DELETE\")\n },\n ArrowLeft() {\n send(\"ARROW_LEFT\")\n },\n ArrowRight() {\n send(\"ARROW_RIGHT\")\n },\n Enter() {\n send(\"ENTER\")\n },\n }\n\n const key = getEventKey(event, { dir: state.context.dir })\n const exec = keyMap[key]\n\n if (exec) {\n exec(event)\n event.preventDefault()\n } else {\n if (key === \"Tab\") return\n send({ type: \"KEY_DOWN\", value: key, preventDefault: () => event.preventDefault() })\n }\n },\n onFocus() {\n send({ type: \"FOCUS\", index })\n },\n onBlur() {\n send({ type: \"BLUR\", index })\n },\n })\n },\n }\n}\n","import { createScope, queryAll } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./pin-input.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,\n getInputId: (ctx: Ctx, id: string) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,\n getHiddenInputId: (ctx: Ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,\n getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,\n getControlId: (ctx: Ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,\n\n getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getRootId(ctx))\n const selector = `input[data-ownedby=${ownerId}]`\n return queryAll<HTMLInputElement>(dom.getRootEl(ctx), selector)\n },\n getInputEl: (ctx: Ctx, id: string) => dom.getById<HTMLInputElement>(ctx, dom.getInputId(ctx, id)),\n getFocusedInputEl: (ctx: Ctx) => dom.getElements(ctx)[ctx.focusedIndex],\n getFirstInputEl: (ctx: Ctx) => dom.getElements(ctx)[0],\n getHiddenInputEl: (ctx: Ctx) => dom.getById<HTMLInputElement>(ctx, dom.getHiddenInputId(ctx)),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { raf } from \"@zag-js/dom-query\"\nimport { dispatchInputValueEvent } from \"@zag-js/form-utils\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./pin-input.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./pin-input.types\"\n\nconst { and, not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"pin-input\",\n initial: ctx.autoFocus ? \"focused\" : \"idle\",\n context: {\n value: [],\n focusedIndex: -1,\n placeholder: \"○\",\n otp: false,\n type: \"numeric\",\n ...ctx,\n translations: {\n inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,\n ...ctx.translations,\n },\n },\n\n computed: {\n valueLength: (ctx) => ctx.value.length,\n filledValueLength: (ctx) => ctx.value.filter((v) => v?.trim() !== \"\").length,\n isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,\n valueAsString: (ctx) => ctx.value.join(\"\"),\n focusedValue: (ctx) => ctx.value[ctx.focusedIndex],\n },\n\n watch: {\n focusedIndex: [\"focusInput\", \"setInputSelection\"],\n value: [\"dispatchInputEvent\", \"syncInputElements\"],\n isValueComplete: [\"invokeOnComplete\", \"blurFocusedInputIfNeeded\"],\n },\n\n entry: ctx.autoFocus ? [\"setupValue\", \"setFocusIndexToFirst\"] : [\"setupValue\"],\n\n on: {\n SET_VALUE: [\n {\n guard: \"hasIndex\",\n actions: [\"setValueAtIndex\", \"invokeOnChange\"],\n },\n { actions: [\"setValue\", \"invokeOnChange\"] },\n ],\n CLEAR_VALUE: [\n {\n guard: \"isDisabled\",\n actions: [\"clearValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"clearValue\", \"invokeOnChange\", \"setFocusIndexToFirst\"],\n },\n ],\n },\n\n states: {\n idle: {\n on: {\n FOCUS: {\n target: \"focused\",\n actions: \"setFocusedIndex\",\n },\n },\n },\n focused: {\n on: {\n INPUT: [\n {\n guard: and(\"isFinalValue\", \"isValidValue\"),\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"syncInputValue\"],\n },\n {\n guard: \"isValidValue\",\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"setNextFocusedIndex\", \"syncInputValue\"],\n },\n ],\n PASTE: [\n {\n guard: \"isValidValue\",\n actions: [\"setPastedValue\", \"invokeOnChange\", \"setLastValueFocusIndex\"],\n },\n { actions: [\"resetFocusedValue\", \"invokeOnChange\"] },\n ],\n BLUR: {\n target: \"idle\",\n actions: \"clearFocusedIndex\",\n },\n DELETE: {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n ARROW_LEFT: {\n actions: \"setPrevFocusedIndex\",\n },\n ARROW_RIGHT: {\n actions: \"setNextFocusedIndex\",\n },\n BACKSPACE: [\n {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"setPrevFocusedIndex\", \"clearFocusedValue\", \"invokeOnChange\"],\n },\n ],\n ENTER: {\n guard: \"isValueComplete\",\n actions: \"requestFormSubmit\",\n },\n KEY_DOWN: {\n guard: not(\"isValidValue\"),\n actions: [\"preventDefault\", \"invokeOnInvalid\"],\n },\n },\n },\n },\n },\n {\n guards: {\n autoFocus: (ctx) => !!ctx.autoFocus,\n isValueEmpty: (_ctx, evt) => evt.value === \"\",\n hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== \"\",\n isValueComplete: (ctx) => ctx.isValueComplete,\n isValidValue: (ctx, evt) => {\n if (!ctx.pattern) return isValidType(evt.value, ctx.type)\n const regex = new RegExp(ctx.pattern, \"g\")\n return regex.test(evt.value)\n },\n isFinalValue: (ctx) => {\n return (\n ctx.filledValueLength + 1 === ctx.valueLength &&\n ctx.value.findIndex((v) => v.trim() === \"\") === ctx.focusedIndex\n )\n },\n isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,\n hasIndex: (_ctx, evt) => evt.index !== undefined,\n isDisabled: (ctx) => !!ctx.disabled,\n },\n actions: {\n setupValue: (ctx) => {\n if (ctx.value.length) return\n const inputs = dom.getElements(ctx)\n const emptyValues = Array.from<string>({ length: inputs.length }).fill(\"\")\n assign(ctx, emptyValues)\n },\n focusInput: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n dom.getFocusedInputEl(ctx)?.focus()\n })\n },\n setInputSelection: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n const input = dom.getFocusedInputEl(ctx)\n const length = input.value.length\n input.selectionStart = ctx.selectOnFocus ? 0 : length\n input.selectionEnd = length\n })\n },\n invokeOnComplete: (ctx) => {\n if (!ctx.isValueComplete) return\n ctx.onComplete?.({ value: Array.from(ctx.value), valueAsString: ctx.valueAsString })\n },\n invokeOnChange: (ctx) => {\n ctx.onChange?.({ value: Array.from(ctx.value) })\n },\n dispatchInputEvent: (ctx) => {\n const inputEl = dom.getHiddenInputEl(ctx)\n dispatchInputValueEvent(inputEl, { value: ctx.valueAsString })\n },\n invokeOnInvalid: (ctx, evt) => {\n ctx.onInvalid?.({ value: evt.value, index: ctx.focusedIndex })\n },\n clearFocusedIndex: (ctx) => {\n ctx.focusedIndex = -1\n },\n setValue: (ctx, evt) => {\n assign(ctx, evt.value)\n },\n setFocusedIndex: (ctx, evt) => {\n ctx.focusedIndex = evt.index\n },\n setFocusedValue: (ctx, evt) => {\n ctx.value[ctx.focusedIndex] = getNextValue(ctx.focusedValue, evt.value)\n },\n syncInputValue(ctx, evt) {\n const input = dom.getInputEl(ctx, evt.index.toString())\n if (!input) return\n input.value = ctx.value[evt.index]\n },\n syncInputElements(ctx) {\n const inputs = dom.getElements(ctx)\n inputs.forEach((input, index) => {\n input.value = ctx.value[index]\n })\n },\n setPastedValue(ctx, evt) {\n raf(() => {\n const startIndex = ctx.focusedValue ? 1 : 0\n const value = evt.value.substring(startIndex, startIndex + ctx.valueLength)\n assign(ctx, value)\n })\n },\n setValueAtIndex: (ctx, evt) => {\n ctx.value[evt.index] = getNextValue(ctx.focusedValue, evt.value)\n },\n clearValue: (ctx) => {\n const nextValue = Array.from<string>({ length: ctx.valueLength }).fill(\"\")\n assign(ctx, nextValue)\n },\n clearFocusedValue: (ctx) => {\n ctx.value[ctx.focusedIndex] = \"\"\n },\n resetFocusedValue: (ctx) => {\n const input = dom.getFocusedInputEl(ctx)\n input.value = ctx.focusedValue\n },\n setFocusIndexToFirst: (ctx) => {\n ctx.focusedIndex = 0\n },\n setNextFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1)\n },\n setPrevFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0)\n },\n setLastValueFocusIndex: (ctx) => {\n raf(() => {\n ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1)\n })\n },\n preventDefault(_, evt) {\n evt.preventDefault()\n },\n blurFocusedInputIfNeeded(ctx) {\n if (!ctx.blurOnComplete) return\n raf(() => {\n dom.getFocusedInputEl(ctx)?.blur()\n })\n },\n requestFormSubmit(ctx) {\n if (!ctx.name || !ctx.isValueComplete) return\n const input = dom.getHiddenInputEl(ctx)\n input?.form?.requestSubmit()\n },\n },\n },\n )\n}\n\nconst REGEX = {\n numeric: /^[0-9]+$/,\n alphabetic: /^[A-Za-z]+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/i,\n}\n\nfunction isValidType(value: string, type: MachineContext[\"type\"]) {\n if (!type) return true\n return !!REGEX[type]?.test(value)\n}\n\nfunction assign(ctx: MachineContext, value: string | string[]) {\n const arr = Array.isArray(value) ? value : value.split(\"\").filter(Boolean)\n arr.forEach((value, index) => {\n ctx.value[index] = value\n })\n}\n\nfunction getNextValue(current: string, next: string) {\n let nextValue = next\n if (current[0] === next[0]) nextValue = next[1]\n else if (current[0] === next[1]) nextValue = next[0]\n return nextValue\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,UAAU,EAAE,MAAM,QAAQ,SAAS,eAAe,SAAS,SAAS;AAClG,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,SAAsB,aAAa,gBAAgB,uBAAuB;AAC1E,SAAS,UAAU,gBAAgB;AAEnC,SAAS,iBAAiB;AAC1B,SAAS,2BAA2B;;;ACJpC,SAAS,aAAa,gBAAgB;AAG/B,IAAM,MAAM,YAAY;AAAA,EAC7B,WAAW,CAAC,QAAa,IAAI,KAAK,QAAQ,aAAa,IAAI;AAAA,EAC3D,YAAY,CAAC,KAAU,OAAe,IAAI,KAAK,QAAQ,EAAE,KAAK,aAAa,IAAI,MAAM;AAAA,EACrF,kBAAkB,CAAC,QAAa,IAAI,KAAK,eAAe,aAAa,IAAI;AAAA,EACzE,YAAY,CAAC,QAAa,IAAI,KAAK,SAAS,aAAa,IAAI;AAAA,EAC7D,cAAc,CAAC,QAAa,IAAI,KAAK,WAAW,aAAa,IAAI;AAAA,EAEjE,WAAW,CAAC,QAAa,IAAI,QAAQ,KAAK,IAAI,UAAU,GAAG,CAAC;AAAA,EAC5D,aAAa,CAAC,QAAa;AACzB,UAAM,UAAU,IAAI,OAAO,IAAI,UAAU,GAAG,CAAC;AAC7C,UAAM,WAAW,sBAAsB;AACvC,WAAO,SAA2B,IAAI,UAAU,GAAG,GAAG,QAAQ;AAAA,EAChE;AAAA,EACA,YAAY,CAAC,KAAU,OAAe,IAAI,QAA0B,KAAK,IAAI,WAAW,KAAK,EAAE,CAAC;AAAA,EAChG,mBAAmB,CAAC,QAAa,IAAI,YAAY,GAAG,EAAE,IAAI,YAAY;AAAA,EACtE,iBAAiB,CAAC,QAAa,IAAI,YAAY,GAAG,EAAE,CAAC;AAAA,EACr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QAAO,UAAU;AAC5B,QAAI,MAAM,KAAK,IAAIA;AAAA,EACrB,CAAC;AACH;AAEA,SAAS,aAAa,SAAiB,MAAc;AACnD,MAAI,YAAY;AAChB,MAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AAAA,WACrC,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AACnD,SAAO;AACT;","names":["ctx","value"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/pin-input",
3
- "version": "0.10.4",
3
+ "version": "0.11.0",
4
4
  "description": "Core logic for the pin-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,14 +27,14 @@
27
27
  "url": "https://github.com/chakra-ui/zag/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@zag-js/anatomy": "0.10.4",
31
- "@zag-js/dom-query": "0.10.4",
32
- "@zag-js/dom-event": "0.10.4",
33
- "@zag-js/form-utils": "0.10.4",
34
- "@zag-js/visually-hidden": "0.10.4",
35
- "@zag-js/utils": "0.10.4",
36
- "@zag-js/core": "0.10.4",
37
- "@zag-js/types": "0.10.4"
30
+ "@zag-js/anatomy": "0.11.0",
31
+ "@zag-js/dom-query": "0.11.0",
32
+ "@zag-js/dom-event": "0.11.0",
33
+ "@zag-js/form-utils": "0.11.0",
34
+ "@zag-js/visually-hidden": "0.11.0",
35
+ "@zag-js/utils": "0.11.0",
36
+ "@zag-js/core": "0.11.0",
37
+ "@zag-js/types": "0.11.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "clean-package": "2.2.0"
@@ -52,7 +52,7 @@
52
52
  "./package.json": "./package.json"
53
53
  },
54
54
  "scripts": {
55
- "build": "vite build -c ../../../vite.config.ts",
55
+ "build": "tsup",
56
56
  "lint": "eslint src --ext .ts,.tsx",
57
57
  "typecheck": "tsc --noEmit"
58
58
  }
@@ -1,3 +0,0 @@
1
- import { AnatomyInstance, AnatomyPart } from '@zag-js/anatomy';
2
- export declare const anatomy: AnatomyInstance<"root" | "label" | "hiddenInput" | "input" | "control">;
3
- export declare const parts: Record<"root" | "label" | "hiddenInput" | "input" | "control", AnatomyPart>;
@@ -1,11 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const anatomy$1 = require('@zag-js/anatomy');
6
-
7
- const anatomy = anatomy$1.createAnatomy("pinInput").parts("root", "label", "hiddenInput", "input", "control");
8
- const parts = anatomy.build();
9
-
10
- exports.anatomy = anatomy;
11
- exports.parts = parts;
@@ -1,6 +0,0 @@
1
- import { createAnatomy } from '@zag-js/anatomy';
2
-
3
- const anatomy = createAnatomy("pinInput").parts("root", "label", "hiddenInput", "input", "control");
4
- const parts = anatomy.build();
5
-
6
- export { anatomy, parts };
@@ -1,39 +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
- /**
5
- * The value of the input as an array of strings.
6
- */
7
- value: string[];
8
- /**
9
- * The value of the input as a string.
10
- */
11
- valueAsString: string;
12
- /**
13
- * Whether all inputs are filled.
14
- */
15
- isValueComplete: boolean;
16
- /**
17
- * Function to set the value of the inputs.
18
- */
19
- setValue(value: string[]): void;
20
- /**
21
- * Function to clear the value of the inputs.
22
- */
23
- clearValue(): void;
24
- /**
25
- * Function to set the value of the input at a specific index.
26
- */
27
- setValueAtIndex(index: number, value: string): void;
28
- /**
29
- * Function to focus the pin-input. This will focus the first input.
30
- */
31
- focus: () => void;
32
- rootProps: T["element"];
33
- labelProps: T["label"];
34
- hiddenInputProps: T["input"];
35
- controlProps: T["element"];
36
- getInputProps({ index }: {
37
- index: number;
38
- }): T["input"];
39
- };