@zag-js/pin-input 0.0.0-dev-20220415160434 → 0.0.0-dev-20220417135151

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { connect } from "./pin-input.connect";
2
2
  export { machine } from "./pin-input.machine";
3
- export type { MachineContext, MachineState } from "./pin-input.types";
3
+ export type { UserDefinedContext as Context } from "./pin-input.types";
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,YAAY,EAAE,kBAAkB,IAAI,OAAO,EAAE,MAAM,mBAAmB,CAAA"}
package/dist/index.js CHANGED
@@ -1,7 +1,24 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
3
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
5
22
  var __export = (target, all) => {
6
23
  for (var name in all)
7
24
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -104,11 +121,11 @@ var dom = {
104
121
  },
105
122
  getRootId: (ctx) => {
106
123
  var _a, _b;
107
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input-${ctx.uid}`;
124
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.uid}`;
108
125
  },
109
126
  getInputId: (ctx, id) => {
110
127
  var _a, _b, _c;
111
- return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input-${ctx.uid}-${id}`;
128
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.uid}:${id}`;
112
129
  },
113
130
  getRootEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getRootId(ctx)),
114
131
  getElements: (ctx) => {
@@ -229,232 +246,235 @@ function connect(state, send, normalize = normalizeProp) {
229
246
  // src/pin-input.machine.ts
230
247
  var import_core = require("@zag-js/core");
231
248
  var { and, not } = import_core.guards;
232
- var machine = (0, import_core.createMachine)({
233
- id: "pin-input",
234
- initial: "unknown",
235
- context: {
236
- uid: "pin-input",
237
- value: [],
238
- focusedIndex: -1,
239
- placeholder: "\u25CB",
240
- otp: false,
241
- type: "numeric",
242
- messages: {
243
- inputLabel: (index, length) => `pin code ${index + 1} of ${length}`
244
- }
245
- },
246
- computed: {
247
- valueLength: (ctx) => ctx.value.length,
248
- filledValueLength: (ctx) => ctx.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
249
- isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,
250
- valueAsString: (ctx) => ctx.value.join("")
251
- },
252
- watch: {
253
- focusedIndex: "focusInput",
254
- value: "invokeOnChange",
255
- isValueComplete: ["invokeComplete", "blurFocusedInputIfNeeded"]
256
- },
257
- on: {
258
- SET_VALUE: [
259
- {
260
- guard: "hasIndex",
261
- actions: "setValueAtIndex"
262
- },
263
- { actions: "setValue" }
264
- ],
265
- CLEAR_VALUE: [
266
- {
267
- guard: "isDisabled",
268
- actions: "clearValue"
249
+ function machine(ctx = {}) {
250
+ return (0, import_core.createMachine)({
251
+ id: "pin-input",
252
+ initial: "unknown",
253
+ context: __spreadProps(__spreadValues({
254
+ uid: "pin-input",
255
+ value: [],
256
+ focusedIndex: -1,
257
+ placeholder: "\u25CB",
258
+ otp: false,
259
+ type: "numeric"
260
+ }, ctx), {
261
+ messages: __spreadValues({
262
+ inputLabel: (index, length) => `pin code ${index + 1} of ${length}`
263
+ }, ctx.messages)
264
+ }),
265
+ computed: {
266
+ valueLength: (ctx2) => ctx2.value.length,
267
+ filledValueLength: (ctx2) => ctx2.value.filter((v) => (v == null ? void 0 : v.trim()) !== "").length,
268
+ isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
269
+ valueAsString: (ctx2) => ctx2.value.join("")
270
+ },
271
+ watch: {
272
+ focusedIndex: "focusInput",
273
+ value: "invokeOnChange",
274
+ isValueComplete: ["invokeComplete", "blurFocusedInputIfNeeded"]
275
+ },
276
+ on: {
277
+ SET_VALUE: [
278
+ {
279
+ guard: "hasIndex",
280
+ actions: "setValueAtIndex"
281
+ },
282
+ { actions: "setValue" }
283
+ ],
284
+ CLEAR_VALUE: [
285
+ {
286
+ guard: "isDisabled",
287
+ actions: "clearValue"
288
+ },
289
+ {
290
+ actions: ["clearValue", "setFocusIndexToFirst"]
291
+ }
292
+ ]
293
+ },
294
+ states: {
295
+ unknown: {
296
+ on: {
297
+ SETUP: [
298
+ {
299
+ guard: "autoFocus",
300
+ target: "focused",
301
+ actions: ["setupDocument", "setupValue", "setFocusIndexToFirst"]
302
+ },
303
+ {
304
+ target: "idle",
305
+ actions: ["setupDocument", "setupValue"]
306
+ }
307
+ ]
308
+ }
269
309
  },
270
- {
271
- actions: ["clearValue", "setFocusIndexToFirst"]
272
- }
273
- ]
274
- },
275
- states: {
276
- unknown: {
277
- on: {
278
- SETUP: [
279
- {
280
- guard: "autoFocus",
310
+ idle: {
311
+ on: {
312
+ FOCUS: {
281
313
  target: "focused",
282
- actions: ["setupDocument", "setupValue", "setFocusIndexToFirst"]
283
- },
284
- {
285
- target: "idle",
286
- actions: ["setupDocument", "setupValue"]
314
+ actions: "setFocusedIndex"
287
315
  }
288
- ]
289
- }
290
- },
291
- idle: {
292
- on: {
293
- FOCUS: {
294
- target: "focused",
295
- actions: "setFocusedIndex"
296
316
  }
297
- }
298
- },
299
- focused: {
300
- on: {
301
- INPUT: [
302
- {
303
- guard: and("isFinalValue", "isValidValue"),
304
- actions: "setFocusedValue"
317
+ },
318
+ focused: {
319
+ on: {
320
+ INPUT: [
321
+ {
322
+ guard: and("isFinalValue", "isValidValue"),
323
+ actions: "setFocusedValue"
324
+ },
325
+ {
326
+ guard: and("hasValue", "isValidValue"),
327
+ actions: ["replaceFocusedValue", "setNextFocusedIndex"]
328
+ },
329
+ {
330
+ guard: "isValidValue",
331
+ actions: ["setFocusedValue", "setNextFocusedIndex"]
332
+ }
333
+ ],
334
+ PASTE: {
335
+ guard: "isValidValue",
336
+ actions: ["setPastedValue", "setLastValueFocusIndex"]
305
337
  },
306
- {
307
- guard: and("hasValue", "isValidValue"),
308
- actions: ["replaceFocusedValue", "setNextFocusedIndex"]
338
+ BLUR: {
339
+ target: "idle",
340
+ actions: "clearFocusedIndex"
309
341
  },
310
- {
311
- guard: "isValidValue",
312
- actions: ["setFocusedValue", "setNextFocusedIndex"]
313
- }
314
- ],
315
- PASTE: {
316
- guard: "isValidValue",
317
- actions: ["setPastedValue", "setLastValueFocusIndex"]
318
- },
319
- BLUR: {
320
- target: "idle",
321
- actions: "clearFocusedIndex"
322
- },
323
- DELETE: {
324
- guard: "hasValue",
325
- actions: "clearFocusedValue"
326
- },
327
- ARROW_LEFT: {
328
- actions: "setPrevFocusedIndex"
329
- },
330
- ARROW_RIGHT: {
331
- actions: "setNextFocusedIndex"
332
- },
333
- BACKSPACE: [
334
- {
342
+ DELETE: {
335
343
  guard: "hasValue",
336
344
  actions: "clearFocusedValue"
337
345
  },
338
- {
339
- actions: ["setPrevFocusedIndex", "clearFocusedValue"]
346
+ ARROW_LEFT: {
347
+ actions: "setPrevFocusedIndex"
348
+ },
349
+ ARROW_RIGHT: {
350
+ actions: "setNextFocusedIndex"
351
+ },
352
+ BACKSPACE: [
353
+ {
354
+ guard: "hasValue",
355
+ actions: "clearFocusedValue"
356
+ },
357
+ {
358
+ actions: ["setPrevFocusedIndex", "clearFocusedValue"]
359
+ }
360
+ ],
361
+ KEY_DOWN: {
362
+ guard: not("isValidValue"),
363
+ actions: ["preventDefault", "invokeOnInvalid"]
340
364
  }
341
- ],
342
- KEY_DOWN: {
343
- guard: not("isValidValue"),
344
- actions: ["preventDefault", "invokeOnInvalid"]
345
365
  }
346
366
  }
347
367
  }
348
- }
349
- }, {
350
- guards: {
351
- autoFocus: (ctx) => !!ctx.autoFocus,
352
- isValueEmpty: (_ctx, evt) => evt.value === "",
353
- hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== "",
354
- isValueComplete: (ctx) => ctx.isValueComplete,
355
- isValidValue: (ctx, evt) => isValidType(evt.value, ctx.type),
356
- isFinalValue: (ctx) => {
357
- return ctx.filledValueLength + 1 === ctx.valueLength && ctx.value.findIndex((v) => v.trim() === "") === ctx.focusedIndex;
358
- },
359
- isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,
360
- hasIndex: (_ctx, evt) => evt.index !== void 0,
361
- isDisabled: (ctx) => !!ctx.disabled
362
- },
363
- actions: {
364
- setupDocument: (ctx, evt) => {
365
- if (evt.doc)
366
- ctx.doc = (0, import_core.ref)(evt.doc);
367
- ctx.uid = evt.id;
368
- },
369
- setupValue: (ctx) => {
370
- nextTick(() => {
371
- const inputs = dom.getElements(ctx);
372
- const empty = fromLength(inputs.length).map(() => "");
373
- ctx.value = Object.assign(empty, ctx.value);
374
- });
368
+ }, {
369
+ guards: {
370
+ autoFocus: (ctx2) => !!ctx2.autoFocus,
371
+ isValueEmpty: (_ctx, evt) => evt.value === "",
372
+ hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
373
+ isValueComplete: (ctx2) => ctx2.isValueComplete,
374
+ isValidValue: (ctx2, evt) => isValidType(evt.value, ctx2.type),
375
+ isFinalValue: (ctx2) => {
376
+ return ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex;
377
+ },
378
+ isLastInputFocused: (ctx2) => ctx2.focusedIndex === ctx2.valueLength - 1,
379
+ hasIndex: (_ctx, evt) => evt.index !== void 0,
380
+ isDisabled: (ctx2) => !!ctx2.disabled
375
381
  },
376
- focusInput: (ctx) => {
377
- nextTick(() => {
382
+ actions: {
383
+ setupDocument: (ctx2, evt) => {
384
+ if (evt.doc)
385
+ ctx2.doc = (0, import_core.ref)(evt.doc);
386
+ ctx2.uid = evt.id;
387
+ },
388
+ setupValue: (ctx2) => {
389
+ nextTick(() => {
390
+ const inputs = dom.getElements(ctx2);
391
+ const empty = fromLength(inputs.length).map(() => "");
392
+ ctx2.value = Object.assign(empty, ctx2.value);
393
+ });
394
+ },
395
+ focusInput: (ctx2) => {
396
+ nextTick(() => {
397
+ var _a;
398
+ if (ctx2.focusedIndex === -1)
399
+ return;
400
+ (_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.focus();
401
+ });
402
+ },
403
+ invokeComplete: (ctx2) => {
404
+ var _a;
405
+ if (ctx2.isValueComplete) {
406
+ (_a = ctx2.onComplete) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
407
+ }
408
+ },
409
+ invokeOnChange: (ctx2, evt) => {
410
+ var _a;
411
+ if (evt.type !== "SETUP") {
412
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: Array.from(ctx2.value) });
413
+ }
414
+ },
415
+ invokeOnInvalid: (ctx2, evt) => {
378
416
  var _a;
379
- if (ctx.focusedIndex === -1)
417
+ (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, { value: evt.value, index: ctx2.focusedIndex });
418
+ },
419
+ clearFocusedIndex: (ctx2) => {
420
+ ctx2.focusedIndex = -1;
421
+ },
422
+ setValue: (ctx2, event) => {
423
+ assign(ctx2, event.value);
424
+ },
425
+ setFocusedIndex: (ctx2, event) => {
426
+ ctx2.focusedIndex = event.index;
427
+ },
428
+ setFocusedValue: (ctx2, event) => {
429
+ ctx2.value[ctx2.focusedIndex] = event.value;
430
+ },
431
+ replaceFocusedValue: (ctx2, evt) => {
432
+ const val = ctx2.value[ctx2.focusedIndex];
433
+ ctx2.value[ctx2.focusedIndex] = evt.value.replace(val, "").charAt(0);
434
+ },
435
+ setPastedValue(ctx2, evt) {
436
+ nextTick(() => {
437
+ const value = evt.value.substr(0, ctx2.valueLength);
438
+ assign(ctx2, value.split("").filter(Boolean));
439
+ });
440
+ },
441
+ setValueAtIndex: (ctx2, evt) => {
442
+ ctx2.value[evt.index] = evt.value;
443
+ },
444
+ clearValue: (ctx2) => {
445
+ assign(ctx2, "");
446
+ },
447
+ clearFocusedValue: (ctx2) => {
448
+ ctx2.value[ctx2.focusedIndex] = "";
449
+ },
450
+ setFocusIndexToFirst: (ctx2) => {
451
+ ctx2.focusedIndex = 0;
452
+ },
453
+ setNextFocusedIndex: (ctx2) => {
454
+ ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
455
+ },
456
+ setPrevFocusedIndex: (ctx2) => {
457
+ ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
458
+ },
459
+ setLastValueFocusIndex: (ctx2) => {
460
+ nextTick(() => {
461
+ ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
462
+ });
463
+ },
464
+ preventDefault(_, evt) {
465
+ evt.preventDefault();
466
+ },
467
+ blurFocusedInputIfNeeded(ctx2) {
468
+ if (!ctx2.blurOnComplete)
380
469
  return;
381
- (_a = dom.getFocusedEl(ctx)) == null ? void 0 : _a.focus();
382
- });
383
- },
384
- invokeComplete: (ctx) => {
385
- var _a;
386
- if (ctx.isValueComplete) {
387
- (_a = ctx.onComplete) == null ? void 0 : _a.call(ctx, { value: Array.from(ctx.value), valueAsString: ctx.valueAsString });
388
- }
389
- },
390
- invokeOnChange: (ctx, evt) => {
391
- var _a;
392
- if (evt.type !== "SETUP") {
393
- (_a = ctx.onChange) == null ? void 0 : _a.call(ctx, { value: Array.from(ctx.value) });
470
+ nextTick(() => {
471
+ var _a;
472
+ (_a = dom.getFocusedEl(ctx2)) == null ? void 0 : _a.blur();
473
+ });
394
474
  }
395
- },
396
- invokeOnInvalid: (ctx, evt) => {
397
- var _a;
398
- (_a = ctx.onInvalid) == null ? void 0 : _a.call(ctx, { value: evt.value, index: ctx.focusedIndex });
399
- },
400
- clearFocusedIndex: (ctx) => {
401
- ctx.focusedIndex = -1;
402
- },
403
- setValue: (ctx, event) => {
404
- assign(ctx, event.value);
405
- },
406
- setFocusedIndex: (ctx, event) => {
407
- ctx.focusedIndex = event.index;
408
- },
409
- setFocusedValue: (ctx, event) => {
410
- ctx.value[ctx.focusedIndex] = event.value;
411
- },
412
- replaceFocusedValue: (ctx, evt) => {
413
- const val = ctx.value[ctx.focusedIndex];
414
- ctx.value[ctx.focusedIndex] = evt.value.replace(val, "").charAt(0);
415
- },
416
- setPastedValue(ctx, evt) {
417
- nextTick(() => {
418
- const value = evt.value.substr(0, ctx.valueLength);
419
- assign(ctx, value.split("").filter(Boolean));
420
- });
421
- },
422
- setValueAtIndex: (ctx, evt) => {
423
- ctx.value[evt.index] = evt.value;
424
- },
425
- clearValue: (ctx) => {
426
- assign(ctx, "");
427
- },
428
- clearFocusedValue: (ctx) => {
429
- ctx.value[ctx.focusedIndex] = "";
430
- },
431
- setFocusIndexToFirst: (ctx) => {
432
- ctx.focusedIndex = 0;
433
- },
434
- setNextFocusedIndex: (ctx) => {
435
- ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1);
436
- },
437
- setPrevFocusedIndex: (ctx) => {
438
- ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0);
439
- },
440
- setLastValueFocusIndex: (ctx) => {
441
- nextTick(() => {
442
- ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1);
443
- });
444
- },
445
- preventDefault(_, evt) {
446
- evt.preventDefault();
447
- },
448
- blurFocusedInputIfNeeded(ctx) {
449
- if (!ctx.blurOnComplete)
450
- return;
451
- nextTick(() => {
452
- var _a;
453
- (_a = dom.getFocusedEl(ctx)) == null ? void 0 : _a.blur();
454
- });
455
475
  }
456
- }
457
- });
476
+ });
477
+ }
458
478
  var REGEX = {
459
479
  numeric: /^[0-9]+$/,
460
480
  alphabetic: /^[A-Za-z]+$/,