@zag-js/pin-input 0.2.11 → 0.2.12

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.
@@ -3,7 +3,7 @@ import {
3
3
  } from "./chunk-BJXKBZJG.mjs";
4
4
  import {
5
5
  dom
6
- } from "./chunk-T5OKDF74.mjs";
6
+ } from "./chunk-Y7IZ6OAH.mjs";
7
7
 
8
8
  // ../../utilities/dom/src/attrs.ts
9
9
  var dataAttr = (guard) => {
@@ -159,7 +159,7 @@ function connect(state, send, normalize) {
159
159
  send("BACKSPACE");
160
160
  return;
161
161
  }
162
- send({ type: "INPUT", value });
162
+ send({ type: "INPUT", value, index });
163
163
  },
164
164
  onKeyDown(event) {
165
165
  const evt = getNativeEvent(event);
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  dom,
6
6
  getWindow
7
- } from "./chunk-T5OKDF74.mjs";
7
+ } from "./chunk-Y7IZ6OAH.mjs";
8
8
 
9
9
  // src/pin-input.machine.ts
10
10
  import { createMachine, guards } from "@zag-js/core";
@@ -73,7 +73,7 @@ function machine(userContext) {
73
73
  },
74
74
  watch: {
75
75
  focusedIndex: ["focusInput", "setInputSelection"],
76
- value: ["dispatchInputEvent"],
76
+ value: ["dispatchInputEvent", "syncInputElements"],
77
77
  isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
78
78
  },
79
79
  entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
@@ -187,14 +187,14 @@ function machine(userContext) {
187
187
  raf(() => {
188
188
  if (ctx2.focusedIndex === -1)
189
189
  return;
190
- dom.getFocusedEl(ctx2)?.focus();
190
+ dom.getFocusedInputEl(ctx2)?.focus();
191
191
  });
192
192
  },
193
193
  setInputSelection: (ctx2) => {
194
194
  raf(() => {
195
195
  if (ctx2.focusedIndex === -1)
196
196
  return;
197
- const input = dom.getFocusedEl(ctx2);
197
+ const input = dom.getFocusedInputEl(ctx2);
198
198
  const length = input.value.length;
199
199
  input.selectionStart = ctx2.selectOnFocus ? 0 : length;
200
200
  input.selectionEnd = length;
@@ -205,20 +205,11 @@ function machine(userContext) {
205
205
  return;
206
206
  ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
207
207
  },
208
- invokeOnChange: (ctx2, evt) => {
209
- if (evt.type === "SETUP")
210
- return;
208
+ invokeOnChange: (ctx2) => {
211
209
  ctx2.onChange?.({ value: Array.from(ctx2.value) });
212
210
  },
213
- dispatchInputEvent: (ctx2, evt) => {
214
- if (evt.type === "SETUP")
215
- return;
211
+ dispatchInputEvent: (ctx2) => {
216
212
  dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
217
- const inputs = dom.getElements(ctx2);
218
- ctx2.value.forEach((val, index) => {
219
- const input = inputs[index];
220
- input.value = val || "";
221
- });
222
213
  },
223
214
  invokeOnInvalid: (ctx2, evt) => {
224
215
  ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
@@ -235,14 +226,17 @@ function machine(userContext) {
235
226
  setFocusedValue: (ctx2, evt) => {
236
227
  ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
237
228
  },
238
- syncInputValue: (ctx2, evt) => {
239
- const nextValue = getNextValue(ctx2.focusedValue, evt.value);
240
- const changed = nextValue !== ctx2.focusedValue;
241
- if (evt.value.length <= 1 || changed)
229
+ syncInputValue(ctx2, evt) {
230
+ const input = dom.getInputEl(ctx2, evt.index.toString());
231
+ if (!input)
242
232
  return;
233
+ input.value = ctx2.value[evt.index];
234
+ },
235
+ syncInputElements(ctx2) {
243
236
  const inputs = dom.getElements(ctx2);
244
- const input = inputs[ctx2.focusedIndex];
245
- input.value = nextValue;
237
+ inputs.forEach((input, index) => {
238
+ input.value = ctx2.value[index];
239
+ });
246
240
  },
247
241
  setPastedValue(ctx2, evt) {
248
242
  raf(() => {
@@ -262,7 +256,7 @@ function machine(userContext) {
262
256
  ctx2.value[ctx2.focusedIndex] = "";
263
257
  },
264
258
  resetFocusedValue: (ctx2) => {
265
- const input = dom.getFocusedEl(ctx2);
259
+ const input = dom.getFocusedInputEl(ctx2);
266
260
  input.value = ctx2.focusedValue;
267
261
  },
268
262
  setFocusIndexToFirst: (ctx2) => {
@@ -286,7 +280,7 @@ function machine(userContext) {
286
280
  if (!ctx2.blurOnComplete)
287
281
  return;
288
282
  raf(() => {
289
- dom.getFocusedEl(ctx2)?.blur();
283
+ dom.getFocusedInputEl(ctx2)?.blur();
290
284
  });
291
285
  },
292
286
  requestFormSubmit(ctx2) {
@@ -47,7 +47,8 @@ var dom = defineDomHelpers({
47
47
  const selector = `input[data-ownedby=${ownerId}]`;
48
48
  return queryAll(dom.getRootEl(ctx), selector);
49
49
  },
50
- getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
50
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
51
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
51
52
  getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
52
53
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
53
54
  });
package/dist/index.js CHANGED
@@ -163,7 +163,8 @@ var dom = defineDomHelpers({
163
163
  const selector = `input[data-ownedby=${ownerId}]`;
164
164
  return queryAll(dom.getRootEl(ctx), selector);
165
165
  },
166
- getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
166
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
167
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
167
168
  getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
168
169
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
169
170
  });
@@ -260,7 +261,7 @@ function connect(state, send, normalize) {
260
261
  send("BACKSPACE");
261
262
  return;
262
263
  }
263
- send({ type: "INPUT", value });
264
+ send({ type: "INPUT", value, index });
264
265
  },
265
266
  onKeyDown(event) {
266
267
  const evt = getNativeEvent(event);
@@ -355,7 +356,7 @@ function machine(userContext) {
355
356
  },
356
357
  watch: {
357
358
  focusedIndex: ["focusInput", "setInputSelection"],
358
- value: ["dispatchInputEvent"],
359
+ value: ["dispatchInputEvent", "syncInputElements"],
359
360
  isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
360
361
  },
361
362
  entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
@@ -469,14 +470,14 @@ function machine(userContext) {
469
470
  raf(() => {
470
471
  if (ctx2.focusedIndex === -1)
471
472
  return;
472
- dom.getFocusedEl(ctx2)?.focus();
473
+ dom.getFocusedInputEl(ctx2)?.focus();
473
474
  });
474
475
  },
475
476
  setInputSelection: (ctx2) => {
476
477
  raf(() => {
477
478
  if (ctx2.focusedIndex === -1)
478
479
  return;
479
- const input = dom.getFocusedEl(ctx2);
480
+ const input = dom.getFocusedInputEl(ctx2);
480
481
  const length = input.value.length;
481
482
  input.selectionStart = ctx2.selectOnFocus ? 0 : length;
482
483
  input.selectionEnd = length;
@@ -487,20 +488,11 @@ function machine(userContext) {
487
488
  return;
488
489
  ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
489
490
  },
490
- invokeOnChange: (ctx2, evt) => {
491
- if (evt.type === "SETUP")
492
- return;
491
+ invokeOnChange: (ctx2) => {
493
492
  ctx2.onChange?.({ value: Array.from(ctx2.value) });
494
493
  },
495
- dispatchInputEvent: (ctx2, evt) => {
496
- if (evt.type === "SETUP")
497
- return;
494
+ dispatchInputEvent: (ctx2) => {
498
495
  dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
499
- const inputs = dom.getElements(ctx2);
500
- ctx2.value.forEach((val, index) => {
501
- const input = inputs[index];
502
- input.value = val || "";
503
- });
504
496
  },
505
497
  invokeOnInvalid: (ctx2, evt) => {
506
498
  ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
@@ -517,14 +509,17 @@ function machine(userContext) {
517
509
  setFocusedValue: (ctx2, evt) => {
518
510
  ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
519
511
  },
520
- syncInputValue: (ctx2, evt) => {
521
- const nextValue = getNextValue(ctx2.focusedValue, evt.value);
522
- const changed = nextValue !== ctx2.focusedValue;
523
- if (evt.value.length <= 1 || changed)
512
+ syncInputValue(ctx2, evt) {
513
+ const input = dom.getInputEl(ctx2, evt.index.toString());
514
+ if (!input)
524
515
  return;
516
+ input.value = ctx2.value[evt.index];
517
+ },
518
+ syncInputElements(ctx2) {
525
519
  const inputs = dom.getElements(ctx2);
526
- const input = inputs[ctx2.focusedIndex];
527
- input.value = nextValue;
520
+ inputs.forEach((input, index) => {
521
+ input.value = ctx2.value[index];
522
+ });
528
523
  },
529
524
  setPastedValue(ctx2, evt) {
530
525
  raf(() => {
@@ -544,7 +539,7 @@ function machine(userContext) {
544
539
  ctx2.value[ctx2.focusedIndex] = "";
545
540
  },
546
541
  resetFocusedValue: (ctx2) => {
547
- const input = dom.getFocusedEl(ctx2);
542
+ const input = dom.getFocusedInputEl(ctx2);
548
543
  input.value = ctx2.focusedValue;
549
544
  },
550
545
  setFocusIndexToFirst: (ctx2) => {
@@ -568,7 +563,7 @@ function machine(userContext) {
568
563
  if (!ctx2.blurOnComplete)
569
564
  return;
570
565
  raf(() => {
571
- dom.getFocusedEl(ctx2)?.blur();
566
+ dom.getFocusedInputEl(ctx2)?.blur();
572
567
  });
573
568
  },
574
569
  requestFormSubmit(ctx2) {
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  connect
3
- } from "./chunk-QVOI2QEW.mjs";
3
+ } from "./chunk-IGE2OJGQ.mjs";
4
4
  import {
5
5
  anatomy
6
6
  } from "./chunk-BJXKBZJG.mjs";
7
7
  import {
8
8
  machine
9
- } from "./chunk-6TEZTVFI.mjs";
9
+ } from "./chunk-VZQYAAIE.mjs";
10
10
  import "./chunk-NT4W6JYX.mjs";
11
- import "./chunk-T5OKDF74.mjs";
11
+ import "./chunk-Y7IZ6OAH.mjs";
12
12
  export {
13
13
  anatomy,
14
14
  connect,
@@ -137,7 +137,8 @@ var dom = defineDomHelpers({
137
137
  const selector = `input[data-ownedby=${ownerId}]`;
138
138
  return queryAll(dom.getRootEl(ctx), selector);
139
139
  },
140
- getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
140
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
141
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
141
142
  getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
142
143
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
143
144
  });
@@ -234,7 +235,7 @@ function connect(state, send, normalize) {
234
235
  send("BACKSPACE");
235
236
  return;
236
237
  }
237
- send({ type: "INPUT", value });
238
+ send({ type: "INPUT", value, index });
238
239
  },
239
240
  onKeyDown(event) {
240
241
  const evt = getNativeEvent(event);
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  connect
3
- } from "./chunk-QVOI2QEW.mjs";
3
+ } from "./chunk-IGE2OJGQ.mjs";
4
4
  import "./chunk-BJXKBZJG.mjs";
5
5
  import "./chunk-NT4W6JYX.mjs";
6
- import "./chunk-T5OKDF74.mjs";
6
+ import "./chunk-Y7IZ6OAH.mjs";
7
7
  export {
8
8
  connect
9
9
  };
@@ -26,7 +26,8 @@ declare const dom: {
26
26
  getControlId: (ctx: MachineContext) => string;
27
27
  getRootEl: (ctx: MachineContext) => HTMLElement | null;
28
28
  getElements: (ctx: MachineContext) => HTMLInputElement[];
29
- getFocusedEl: (ctx: MachineContext) => HTMLInputElement;
29
+ getInputEl: (ctx: MachineContext, id: string) => HTMLInputElement | null;
30
+ getFocusedInputEl: (ctx: MachineContext) => HTMLInputElement;
30
31
  getFirstInputEl: (ctx: MachineContext) => HTMLInputElement;
31
32
  getHiddenInputEl: (ctx: MachineContext) => HTMLInputElement | null;
32
33
  };
@@ -70,7 +70,8 @@ var dom = defineDomHelpers({
70
70
  const selector = `input[data-ownedby=${ownerId}]`;
71
71
  return queryAll(dom.getRootEl(ctx), selector);
72
72
  },
73
- getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
73
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
74
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
74
75
  getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
75
76
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
76
77
  });
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  dom
3
- } from "./chunk-T5OKDF74.mjs";
3
+ } from "./chunk-Y7IZ6OAH.mjs";
4
4
  export {
5
5
  dom
6
6
  };
@@ -113,7 +113,8 @@ var dom = defineDomHelpers({
113
113
  const selector = `input[data-ownedby=${ownerId}]`;
114
114
  return queryAll(dom.getRootEl(ctx), selector);
115
115
  },
116
- getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
116
+ getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
117
+ getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
117
118
  getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
118
119
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
119
120
  });
@@ -147,7 +148,7 @@ function machine(userContext) {
147
148
  },
148
149
  watch: {
149
150
  focusedIndex: ["focusInput", "setInputSelection"],
150
- value: ["dispatchInputEvent"],
151
+ value: ["dispatchInputEvent", "syncInputElements"],
151
152
  isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
152
153
  },
153
154
  entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
@@ -261,14 +262,14 @@ function machine(userContext) {
261
262
  raf(() => {
262
263
  if (ctx2.focusedIndex === -1)
263
264
  return;
264
- dom.getFocusedEl(ctx2)?.focus();
265
+ dom.getFocusedInputEl(ctx2)?.focus();
265
266
  });
266
267
  },
267
268
  setInputSelection: (ctx2) => {
268
269
  raf(() => {
269
270
  if (ctx2.focusedIndex === -1)
270
271
  return;
271
- const input = dom.getFocusedEl(ctx2);
272
+ const input = dom.getFocusedInputEl(ctx2);
272
273
  const length = input.value.length;
273
274
  input.selectionStart = ctx2.selectOnFocus ? 0 : length;
274
275
  input.selectionEnd = length;
@@ -279,20 +280,11 @@ function machine(userContext) {
279
280
  return;
280
281
  ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
281
282
  },
282
- invokeOnChange: (ctx2, evt) => {
283
- if (evt.type === "SETUP")
284
- return;
283
+ invokeOnChange: (ctx2) => {
285
284
  ctx2.onChange?.({ value: Array.from(ctx2.value) });
286
285
  },
287
- dispatchInputEvent: (ctx2, evt) => {
288
- if (evt.type === "SETUP")
289
- return;
286
+ dispatchInputEvent: (ctx2) => {
290
287
  dispatchInputValueEvent(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
291
- const inputs = dom.getElements(ctx2);
292
- ctx2.value.forEach((val, index) => {
293
- const input = inputs[index];
294
- input.value = val || "";
295
- });
296
288
  },
297
289
  invokeOnInvalid: (ctx2, evt) => {
298
290
  ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
@@ -309,14 +301,17 @@ function machine(userContext) {
309
301
  setFocusedValue: (ctx2, evt) => {
310
302
  ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
311
303
  },
312
- syncInputValue: (ctx2, evt) => {
313
- const nextValue = getNextValue(ctx2.focusedValue, evt.value);
314
- const changed = nextValue !== ctx2.focusedValue;
315
- if (evt.value.length <= 1 || changed)
304
+ syncInputValue(ctx2, evt) {
305
+ const input = dom.getInputEl(ctx2, evt.index.toString());
306
+ if (!input)
316
307
  return;
308
+ input.value = ctx2.value[evt.index];
309
+ },
310
+ syncInputElements(ctx2) {
317
311
  const inputs = dom.getElements(ctx2);
318
- const input = inputs[ctx2.focusedIndex];
319
- input.value = nextValue;
312
+ inputs.forEach((input, index) => {
313
+ input.value = ctx2.value[index];
314
+ });
320
315
  },
321
316
  setPastedValue(ctx2, evt) {
322
317
  raf(() => {
@@ -336,7 +331,7 @@ function machine(userContext) {
336
331
  ctx2.value[ctx2.focusedIndex] = "";
337
332
  },
338
333
  resetFocusedValue: (ctx2) => {
339
- const input = dom.getFocusedEl(ctx2);
334
+ const input = dom.getFocusedInputEl(ctx2);
340
335
  input.value = ctx2.focusedValue;
341
336
  },
342
337
  setFocusIndexToFirst: (ctx2) => {
@@ -360,7 +355,7 @@ function machine(userContext) {
360
355
  if (!ctx2.blurOnComplete)
361
356
  return;
362
357
  raf(() => {
363
- dom.getFocusedEl(ctx2)?.blur();
358
+ dom.getFocusedInputEl(ctx2)?.blur();
364
359
  });
365
360
  },
366
361
  requestFormSubmit(ctx2) {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  machine
3
- } from "./chunk-6TEZTVFI.mjs";
3
+ } from "./chunk-VZQYAAIE.mjs";
4
4
  import "./chunk-NT4W6JYX.mjs";
5
- import "./chunk-T5OKDF74.mjs";
5
+ import "./chunk-Y7IZ6OAH.mjs";
6
6
  export {
7
7
  machine
8
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/pin-input",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Core logic for the pin-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@zag-js/anatomy": "0.1.4",
30
- "@zag-js/core": "0.2.8",
30
+ "@zag-js/core": "0.2.9",
31
31
  "@zag-js/types": "0.3.4"
32
32
  },
33
33
  "devDependencies": {