@zag-js/pin-input 0.2.8 → 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.
File without changes
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  isObject
3
- } from "./chunk-NT4W6JYX.mjs";
3
+ } from "./chunk-KV727BEF.mjs";
4
4
  import {
5
5
  dom,
6
6
  getWindow
7
- } from "./chunk-APRO2TYE.mjs";
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", "dispatchInputEventIfNeeded"]
128
+ actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
129
129
  },
130
130
  {
131
131
  guard: "isValidValue",
132
- actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "dispatchInputEventIfNeeded"]
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 empty = Array.from({ length: inputs.length }).map(() => "");
200
- ctx2.value = Object.assign(empty, ctx2.value);
199
+ const emptyValues = Array.from({ length: inputs.length }).fill("");
200
+ assign(ctx2, emptyValues);
201
201
  },
202
202
  focusInput: (ctx2) => {
203
203
  raf(() => {
@@ -253,15 +253,16 @@ function machine(userContext) {
253
253
  ctx2.focusedIndex = evt.index;
254
254
  },
255
255
  setFocusedValue: (ctx2, evt) => {
256
- ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
256
+ ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
257
257
  },
258
- dispatchInputEventIfNeeded: (ctx2, evt) => {
259
- const valueIsChanged = lastChar(evt.value) !== ctx2.focusedValue;
260
- if (evt.value.length <= 1 || valueIsChanged)
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 = lastChar(evt.value);
265
+ input.value = nextValue;
265
266
  },
266
267
  setPastedValue(ctx2, evt) {
267
268
  raf(() => {
@@ -271,10 +272,11 @@ function machine(userContext) {
271
272
  });
272
273
  },
273
274
  setValueAtIndex: (ctx2, evt) => {
274
- ctx2.value[evt.index] = lastChar(evt.value);
275
+ ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
275
276
  },
276
277
  clearValue: (ctx2) => {
277
- ctx2.value = ctx2.value.map(() => "");
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 valueArr = Array.isArray(value) ? value : value.split("").filter(Boolean);
335
- const valueObj = Object.assign({}, ctx.value, valueArr);
336
- ctx.value = Object.values(valueObj);
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 lastChar(value) {
339
- return value.charAt(value.length - 1);
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-BJXKBZJG.mjs";
3
+ } from "./chunk-LUBGS2VH.mjs";
4
4
  import {
5
5
  dom
6
- } from "./chunk-APRO2TYE.mjs";
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 && process.env.NODE_ENV !== "production") {
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 && process.env.NODE_ENV !== "production") {
59
+ if (c && true) {
60
60
  throw new Error(m);
61
61
  }
62
62
  }
@@ -434,11 +434,11 @@ function machine(userContext) {
434
434
  INPUT: [
435
435
  {
436
436
  guard: and("isFinalValue", "isValidValue"),
437
- actions: ["setFocusedValue", "invokeOnChange", "dispatchInputEventIfNeeded"]
437
+ actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
438
438
  },
439
439
  {
440
440
  guard: "isValidValue",
441
- actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "dispatchInputEventIfNeeded"]
441
+ actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
442
442
  }
443
443
  ],
444
444
  PASTE: [
@@ -505,8 +505,8 @@ function machine(userContext) {
505
505
  actions: {
506
506
  setupValue: (ctx2) => {
507
507
  const inputs = dom.getElements(ctx2);
508
- const empty = Array.from({ length: inputs.length }).map(() => "");
509
- ctx2.value = Object.assign(empty, ctx2.value);
508
+ const emptyValues = Array.from({ length: inputs.length }).fill("");
509
+ assign(ctx2, emptyValues);
510
510
  },
511
511
  focusInput: (ctx2) => {
512
512
  raf(() => {
@@ -562,15 +562,16 @@ function machine(userContext) {
562
562
  ctx2.focusedIndex = evt.index;
563
563
  },
564
564
  setFocusedValue: (ctx2, evt) => {
565
- ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
565
+ ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
566
566
  },
567
- dispatchInputEventIfNeeded: (ctx2, evt) => {
568
- const valueIsChanged = lastChar(evt.value) !== ctx2.focusedValue;
569
- if (evt.value.length <= 1 || valueIsChanged)
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)
570
571
  return;
571
572
  const inputs = dom.getElements(ctx2);
572
573
  const input = inputs[ctx2.focusedIndex];
573
- input.value = lastChar(evt.value);
574
+ input.value = nextValue;
574
575
  },
575
576
  setPastedValue(ctx2, evt) {
576
577
  raf(() => {
@@ -580,10 +581,11 @@ function machine(userContext) {
580
581
  });
581
582
  },
582
583
  setValueAtIndex: (ctx2, evt) => {
583
- ctx2.value[evt.index] = lastChar(evt.value);
584
+ ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
584
585
  },
585
586
  clearValue: (ctx2) => {
586
- ctx2.value = ctx2.value.map(() => "");
587
+ const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
588
+ assign(ctx2, nextValue);
587
589
  },
588
590
  clearFocusedValue: (ctx2) => {
589
591
  ctx2.value[ctx2.focusedIndex] = "";
@@ -619,7 +621,7 @@ function machine(userContext) {
619
621
  },
620
622
  requestFormSubmit(ctx2) {
621
623
  var _a;
622
- if (!ctx2.name)
624
+ if (!ctx2.name || !ctx2.isValueComplete)
623
625
  return;
624
626
  const input = dom.getHiddenInputEl(ctx2);
625
627
  (_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
@@ -640,12 +642,18 @@ function isValidType(value, type) {
640
642
  return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
641
643
  }
642
644
  function assign(ctx, value) {
643
- const valueArr = Array.isArray(value) ? value : value.split("").filter(Boolean);
644
- const valueObj = Object.assign({}, ctx.value, valueArr);
645
- ctx.value = Object.values(valueObj);
645
+ const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
646
+ arr.forEach((value2, index) => {
647
+ ctx.value[index] = value2;
648
+ });
646
649
  }
647
- function lastChar(value) {
648
- return value.charAt(value.length - 1);
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;
649
657
  }
650
658
  // Annotate the CommonJS export names for ESM import in node:
651
659
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  connect
3
- } from "./chunk-3SENCEI3.mjs";
3
+ } from "./chunk-THBDCFBU.mjs";
4
4
  import {
5
5
  anatomy
6
- } from "./chunk-BJXKBZJG.mjs";
6
+ } from "./chunk-LUBGS2VH.mjs";
7
7
  import {
8
8
  machine
9
- } from "./chunk-CDDLX3AG.mjs";
10
- import "./chunk-NT4W6JYX.mjs";
11
- import "./chunk-APRO2TYE.mjs";
9
+ } from "./chunk-IOTDORZW.mjs";
10
+ import "./chunk-KV727BEF.mjs";
11
+ import "./chunk-I5GCVNBL.mjs";
12
12
  export {
13
13
  anatomy,
14
14
  connect,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  anatomy,
3
3
  parts
4
- } from "./chunk-BJXKBZJG.mjs";
4
+ } from "./chunk-LUBGS2VH.mjs";
5
5
  export {
6
6
  anatomy,
7
7
  parts
@@ -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 && process.env.NODE_ENV !== "production") {
39
+ if (c && true) {
40
40
  throw new Error(m);
41
41
  }
42
42
  }
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  connect
3
- } from "./chunk-3SENCEI3.mjs";
4
- import "./chunk-BJXKBZJG.mjs";
5
- import "./chunk-NT4W6JYX.mjs";
6
- import "./chunk-APRO2TYE.mjs";
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
  };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  dom
3
- } from "./chunk-APRO2TYE.mjs";
3
+ } from "./chunk-I5GCVNBL.mjs";
4
4
  export {
5
5
  dom
6
6
  };
@@ -223,11 +223,11 @@ function machine(userContext) {
223
223
  INPUT: [
224
224
  {
225
225
  guard: and("isFinalValue", "isValidValue"),
226
- actions: ["setFocusedValue", "invokeOnChange", "dispatchInputEventIfNeeded"]
226
+ actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
227
227
  },
228
228
  {
229
229
  guard: "isValidValue",
230
- actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "dispatchInputEventIfNeeded"]
230
+ actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
231
231
  }
232
232
  ],
233
233
  PASTE: [
@@ -294,8 +294,8 @@ function machine(userContext) {
294
294
  actions: {
295
295
  setupValue: (ctx2) => {
296
296
  const inputs = dom.getElements(ctx2);
297
- const empty = Array.from({ length: inputs.length }).map(() => "");
298
- ctx2.value = Object.assign(empty, ctx2.value);
297
+ const emptyValues = Array.from({ length: inputs.length }).fill("");
298
+ assign(ctx2, emptyValues);
299
299
  },
300
300
  focusInput: (ctx2) => {
301
301
  raf(() => {
@@ -351,15 +351,16 @@ function machine(userContext) {
351
351
  ctx2.focusedIndex = evt.index;
352
352
  },
353
353
  setFocusedValue: (ctx2, evt) => {
354
- ctx2.value[ctx2.focusedIndex] = lastChar(evt.value);
354
+ ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
355
355
  },
356
- dispatchInputEventIfNeeded: (ctx2, evt) => {
357
- const valueIsChanged = lastChar(evt.value) !== ctx2.focusedValue;
358
- if (evt.value.length <= 1 || valueIsChanged)
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)
359
360
  return;
360
361
  const inputs = dom.getElements(ctx2);
361
362
  const input = inputs[ctx2.focusedIndex];
362
- input.value = lastChar(evt.value);
363
+ input.value = nextValue;
363
364
  },
364
365
  setPastedValue(ctx2, evt) {
365
366
  raf(() => {
@@ -369,10 +370,11 @@ function machine(userContext) {
369
370
  });
370
371
  },
371
372
  setValueAtIndex: (ctx2, evt) => {
372
- ctx2.value[evt.index] = lastChar(evt.value);
373
+ ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
373
374
  },
374
375
  clearValue: (ctx2) => {
375
- ctx2.value = ctx2.value.map(() => "");
376
+ const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
377
+ assign(ctx2, nextValue);
376
378
  },
377
379
  clearFocusedValue: (ctx2) => {
378
380
  ctx2.value[ctx2.focusedIndex] = "";
@@ -408,7 +410,7 @@ function machine(userContext) {
408
410
  },
409
411
  requestFormSubmit(ctx2) {
410
412
  var _a;
411
- if (!ctx2.name)
413
+ if (!ctx2.name || !ctx2.isValueComplete)
412
414
  return;
413
415
  const input = dom.getHiddenInputEl(ctx2);
414
416
  (_a = input == null ? void 0 : input.form) == null ? void 0 : _a.requestSubmit();
@@ -429,12 +431,18 @@ function isValidType(value, type) {
429
431
  return !!((_a = REGEX[type]) == null ? void 0 : _a.test(value));
430
432
  }
431
433
  function assign(ctx, value) {
432
- const valueArr = Array.isArray(value) ? value : value.split("").filter(Boolean);
433
- const valueObj = Object.assign({}, ctx.value, valueArr);
434
- ctx.value = Object.values(valueObj);
434
+ const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
435
+ arr.forEach((value2, index) => {
436
+ ctx.value[index] = value2;
437
+ });
435
438
  }
436
- function lastChar(value) {
437
- return value.charAt(value.length - 1);
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;
438
446
  }
439
447
  // Annotate the CommonJS export names for ESM import in node:
440
448
  0 && (module.exports = {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  machine
3
- } from "./chunk-CDDLX3AG.mjs";
4
- import "./chunk-NT4W6JYX.mjs";
5
- import "./chunk-APRO2TYE.mjs";
3
+ } from "./chunk-IOTDORZW.mjs";
4
+ import "./chunk-KV727BEF.mjs";
5
+ import "./chunk-I5GCVNBL.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.8",
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,7 +27,7 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@zag-js/anatomy": "0.1.3",
30
- "@zag-js/core": "0.2.5",
30
+ "@zag-js/core": "0.2.6",
31
31
  "@zag-js/types": "0.3.3"
32
32
  },
33
33
  "devDependencies": {