@zag-js/pin-input 0.10.2 → 0.10.4
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 +4 -7
- package/dist/index.js +8 -498
- package/dist/index.mjs +3 -15
- package/dist/pin-input.anatomy.d.ts +3 -6
- package/dist/pin-input.anatomy.js +10 -33
- package/dist/pin-input.anatomy.mjs +6 -8
- package/dist/pin-input.connect.d.ts +3 -7
- package/dist/pin-input.connect.js +39 -86
- package/dist/pin-input.connect.mjs +165 -8
- package/dist/pin-input.dom.d.ts +13 -18
- package/dist/pin-input.dom.js +9 -31
- package/dist/pin-input.dom.mjs +21 -6
- package/dist/pin-input.machine.d.ts +3 -7
- package/dist/pin-input.machine.js +29 -72
- package/dist/pin-input.machine.mjs +282 -6
- package/dist/pin-input.types.d.ts +8 -10
- package/package.json +10 -15
- package/dist/chunk-BJXKBZJG.mjs +0 -9
- package/dist/chunk-CPXMEVTH.mjs +0 -23
- package/dist/chunk-KLVAQVBX.mjs +0 -172
- package/dist/chunk-RXPBAYJD.mjs +0 -288
- package/dist/pin-input.types.js +0 -18
- package/dist/pin-input.types.mjs +0 -0
|
@@ -1,65 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var pin_input_machine_exports = {};
|
|
22
|
-
__export(pin_input_machine_exports, {
|
|
23
|
-
machine: () => machine
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(pin_input_machine_exports);
|
|
26
|
-
var import_core = require("@zag-js/core");
|
|
27
|
-
var import_dom_query2 = require("@zag-js/dom-query");
|
|
28
|
-
var import_form_utils = require("@zag-js/form-utils");
|
|
29
|
-
var import_utils = require("@zag-js/utils");
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
30
4
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
37
|
-
getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
|
|
38
|
-
getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
|
|
39
|
-
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
40
|
-
getElements: (ctx) => {
|
|
41
|
-
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
42
|
-
const selector = `input[data-ownedby=${ownerId}]`;
|
|
43
|
-
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
44
|
-
},
|
|
45
|
-
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
46
|
-
getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
|
|
47
|
-
getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
|
|
48
|
-
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
49
|
-
});
|
|
5
|
+
const core = require('@zag-js/core');
|
|
6
|
+
const domQuery = require('@zag-js/dom-query');
|
|
7
|
+
const formUtils = require('@zag-js/form-utils');
|
|
8
|
+
const utils = require('@zag-js/utils');
|
|
9
|
+
const pinInput_dom = require('./pin-input.dom.js');
|
|
50
10
|
|
|
51
|
-
|
|
52
|
-
var { and, not } = import_core.guards;
|
|
11
|
+
const { and, not } = core.guards;
|
|
53
12
|
function machine(userContext) {
|
|
54
|
-
const ctx =
|
|
55
|
-
return
|
|
13
|
+
const ctx = utils.compact(userContext);
|
|
14
|
+
return core.createMachine(
|
|
56
15
|
{
|
|
57
16
|
id: "pin-input",
|
|
58
17
|
initial: ctx.autoFocus ? "focused" : "idle",
|
|
59
18
|
context: {
|
|
60
19
|
value: [],
|
|
61
20
|
focusedIndex: -1,
|
|
62
|
-
placeholder: "
|
|
21
|
+
placeholder: "○",
|
|
63
22
|
otp: false,
|
|
64
23
|
type: "numeric",
|
|
65
24
|
...ctx,
|
|
@@ -185,22 +144,22 @@ function machine(userContext) {
|
|
|
185
144
|
setupValue: (ctx2) => {
|
|
186
145
|
if (ctx2.value.length)
|
|
187
146
|
return;
|
|
188
|
-
const inputs = dom.getElements(ctx2);
|
|
147
|
+
const inputs = pinInput_dom.dom.getElements(ctx2);
|
|
189
148
|
const emptyValues = Array.from({ length: inputs.length }).fill("");
|
|
190
149
|
assign(ctx2, emptyValues);
|
|
191
150
|
},
|
|
192
151
|
focusInput: (ctx2) => {
|
|
193
|
-
|
|
152
|
+
domQuery.raf(() => {
|
|
194
153
|
if (ctx2.focusedIndex === -1)
|
|
195
154
|
return;
|
|
196
|
-
dom.getFocusedInputEl(ctx2)?.focus();
|
|
155
|
+
pinInput_dom.dom.getFocusedInputEl(ctx2)?.focus();
|
|
197
156
|
});
|
|
198
157
|
},
|
|
199
158
|
setInputSelection: (ctx2) => {
|
|
200
|
-
|
|
159
|
+
domQuery.raf(() => {
|
|
201
160
|
if (ctx2.focusedIndex === -1)
|
|
202
161
|
return;
|
|
203
|
-
const input = dom.getFocusedInputEl(ctx2);
|
|
162
|
+
const input = pinInput_dom.dom.getFocusedInputEl(ctx2);
|
|
204
163
|
const length = input.value.length;
|
|
205
164
|
input.selectionStart = ctx2.selectOnFocus ? 0 : length;
|
|
206
165
|
input.selectionEnd = length;
|
|
@@ -215,8 +174,8 @@ function machine(userContext) {
|
|
|
215
174
|
ctx2.onChange?.({ value: Array.from(ctx2.value) });
|
|
216
175
|
},
|
|
217
176
|
dispatchInputEvent: (ctx2) => {
|
|
218
|
-
const inputEl = dom.getHiddenInputEl(ctx2);
|
|
219
|
-
|
|
177
|
+
const inputEl = pinInput_dom.dom.getHiddenInputEl(ctx2);
|
|
178
|
+
formUtils.dispatchInputValueEvent(inputEl, { value: ctx2.valueAsString });
|
|
220
179
|
},
|
|
221
180
|
invokeOnInvalid: (ctx2, evt) => {
|
|
222
181
|
ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
|
|
@@ -234,19 +193,19 @@ function machine(userContext) {
|
|
|
234
193
|
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
235
194
|
},
|
|
236
195
|
syncInputValue(ctx2, evt) {
|
|
237
|
-
const input = dom.getInputEl(ctx2, evt.index.toString());
|
|
196
|
+
const input = pinInput_dom.dom.getInputEl(ctx2, evt.index.toString());
|
|
238
197
|
if (!input)
|
|
239
198
|
return;
|
|
240
199
|
input.value = ctx2.value[evt.index];
|
|
241
200
|
},
|
|
242
201
|
syncInputElements(ctx2) {
|
|
243
|
-
const inputs = dom.getElements(ctx2);
|
|
202
|
+
const inputs = pinInput_dom.dom.getElements(ctx2);
|
|
244
203
|
inputs.forEach((input, index) => {
|
|
245
204
|
input.value = ctx2.value[index];
|
|
246
205
|
});
|
|
247
206
|
},
|
|
248
207
|
setPastedValue(ctx2, evt) {
|
|
249
|
-
|
|
208
|
+
domQuery.raf(() => {
|
|
250
209
|
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
251
210
|
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
252
211
|
assign(ctx2, value);
|
|
@@ -263,7 +222,7 @@ function machine(userContext) {
|
|
|
263
222
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
264
223
|
},
|
|
265
224
|
resetFocusedValue: (ctx2) => {
|
|
266
|
-
const input = dom.getFocusedInputEl(ctx2);
|
|
225
|
+
const input = pinInput_dom.dom.getFocusedInputEl(ctx2);
|
|
267
226
|
input.value = ctx2.focusedValue;
|
|
268
227
|
},
|
|
269
228
|
setFocusIndexToFirst: (ctx2) => {
|
|
@@ -276,7 +235,7 @@ function machine(userContext) {
|
|
|
276
235
|
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
277
236
|
},
|
|
278
237
|
setLastValueFocusIndex: (ctx2) => {
|
|
279
|
-
|
|
238
|
+
domQuery.raf(() => {
|
|
280
239
|
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
281
240
|
});
|
|
282
241
|
},
|
|
@@ -286,21 +245,21 @@ function machine(userContext) {
|
|
|
286
245
|
blurFocusedInputIfNeeded(ctx2) {
|
|
287
246
|
if (!ctx2.blurOnComplete)
|
|
288
247
|
return;
|
|
289
|
-
|
|
290
|
-
dom.getFocusedInputEl(ctx2)?.blur();
|
|
248
|
+
domQuery.raf(() => {
|
|
249
|
+
pinInput_dom.dom.getFocusedInputEl(ctx2)?.blur();
|
|
291
250
|
});
|
|
292
251
|
},
|
|
293
252
|
requestFormSubmit(ctx2) {
|
|
294
253
|
if (!ctx2.name || !ctx2.isValueComplete)
|
|
295
254
|
return;
|
|
296
|
-
const input = dom.getHiddenInputEl(ctx2);
|
|
255
|
+
const input = pinInput_dom.dom.getHiddenInputEl(ctx2);
|
|
297
256
|
input?.form?.requestSubmit();
|
|
298
257
|
}
|
|
299
258
|
}
|
|
300
259
|
}
|
|
301
260
|
);
|
|
302
261
|
}
|
|
303
|
-
|
|
262
|
+
const REGEX = {
|
|
304
263
|
numeric: /^[0-9]+$/,
|
|
305
264
|
alphabetic: /^[A-Za-z]+$/,
|
|
306
265
|
alphanumeric: /^[a-zA-Z0-9]+$/i
|
|
@@ -324,7 +283,5 @@ function getNextValue(current, next) {
|
|
|
324
283
|
nextValue = next[0];
|
|
325
284
|
return nextValue;
|
|
326
285
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
machine
|
|
330
|
-
});
|
|
286
|
+
|
|
287
|
+
exports.machine = machine;
|
|
@@ -1,7 +1,283 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { createMachine, guards } from '@zag-js/core';
|
|
2
|
+
import { raf } from '@zag-js/dom-query';
|
|
3
|
+
import { dispatchInputValueEvent } from '@zag-js/form-utils';
|
|
4
|
+
import { compact } from '@zag-js/utils';
|
|
5
|
+
import { dom } from './pin-input.dom.mjs';
|
|
6
|
+
|
|
7
|
+
const { and, not } = guards;
|
|
8
|
+
function machine(userContext) {
|
|
9
|
+
const ctx = compact(userContext);
|
|
10
|
+
return createMachine(
|
|
11
|
+
{
|
|
12
|
+
id: "pin-input",
|
|
13
|
+
initial: ctx.autoFocus ? "focused" : "idle",
|
|
14
|
+
context: {
|
|
15
|
+
value: [],
|
|
16
|
+
focusedIndex: -1,
|
|
17
|
+
placeholder: "○",
|
|
18
|
+
otp: false,
|
|
19
|
+
type: "numeric",
|
|
20
|
+
...ctx,
|
|
21
|
+
translations: {
|
|
22
|
+
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
23
|
+
...ctx.translations
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
computed: {
|
|
27
|
+
valueLength: (ctx2) => ctx2.value.length,
|
|
28
|
+
filledValueLength: (ctx2) => ctx2.value.filter((v) => v?.trim() !== "").length,
|
|
29
|
+
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
30
|
+
valueAsString: (ctx2) => ctx2.value.join(""),
|
|
31
|
+
focusedValue: (ctx2) => ctx2.value[ctx2.focusedIndex]
|
|
32
|
+
},
|
|
33
|
+
watch: {
|
|
34
|
+
focusedIndex: ["focusInput", "setInputSelection"],
|
|
35
|
+
value: ["dispatchInputEvent", "syncInputElements"],
|
|
36
|
+
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
37
|
+
},
|
|
38
|
+
entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
|
|
39
|
+
on: {
|
|
40
|
+
SET_VALUE: [
|
|
41
|
+
{
|
|
42
|
+
guard: "hasIndex",
|
|
43
|
+
actions: ["setValueAtIndex", "invokeOnChange"]
|
|
44
|
+
},
|
|
45
|
+
{ actions: ["setValue", "invokeOnChange"] }
|
|
46
|
+
],
|
|
47
|
+
CLEAR_VALUE: [
|
|
48
|
+
{
|
|
49
|
+
guard: "isDisabled",
|
|
50
|
+
actions: ["clearValue", "invokeOnChange"]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
actions: ["clearValue", "invokeOnChange", "setFocusIndexToFirst"]
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
states: {
|
|
58
|
+
idle: {
|
|
59
|
+
on: {
|
|
60
|
+
FOCUS: {
|
|
61
|
+
target: "focused",
|
|
62
|
+
actions: "setFocusedIndex"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
focused: {
|
|
67
|
+
on: {
|
|
68
|
+
INPUT: [
|
|
69
|
+
{
|
|
70
|
+
guard: and("isFinalValue", "isValidValue"),
|
|
71
|
+
actions: ["setFocusedValue", "invokeOnChange", "syncInputValue"]
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
guard: "isValidValue",
|
|
75
|
+
actions: ["setFocusedValue", "invokeOnChange", "setNextFocusedIndex", "syncInputValue"]
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
PASTE: [
|
|
79
|
+
{
|
|
80
|
+
guard: "isValidValue",
|
|
81
|
+
actions: ["setPastedValue", "invokeOnChange", "setLastValueFocusIndex"]
|
|
82
|
+
},
|
|
83
|
+
{ actions: ["resetFocusedValue", "invokeOnChange"] }
|
|
84
|
+
],
|
|
85
|
+
BLUR: {
|
|
86
|
+
target: "idle",
|
|
87
|
+
actions: "clearFocusedIndex"
|
|
88
|
+
},
|
|
89
|
+
DELETE: {
|
|
90
|
+
guard: "hasValue",
|
|
91
|
+
actions: ["clearFocusedValue", "invokeOnChange"]
|
|
92
|
+
},
|
|
93
|
+
ARROW_LEFT: {
|
|
94
|
+
actions: "setPrevFocusedIndex"
|
|
95
|
+
},
|
|
96
|
+
ARROW_RIGHT: {
|
|
97
|
+
actions: "setNextFocusedIndex"
|
|
98
|
+
},
|
|
99
|
+
BACKSPACE: [
|
|
100
|
+
{
|
|
101
|
+
guard: "hasValue",
|
|
102
|
+
actions: ["clearFocusedValue", "invokeOnChange"]
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
actions: ["setPrevFocusedIndex", "clearFocusedValue", "invokeOnChange"]
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
ENTER: {
|
|
109
|
+
guard: "isValueComplete",
|
|
110
|
+
actions: "requestFormSubmit"
|
|
111
|
+
},
|
|
112
|
+
KEY_DOWN: {
|
|
113
|
+
guard: not("isValidValue"),
|
|
114
|
+
actions: ["preventDefault", "invokeOnInvalid"]
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
guards: {
|
|
122
|
+
autoFocus: (ctx2) => !!ctx2.autoFocus,
|
|
123
|
+
isValueEmpty: (_ctx, evt) => evt.value === "",
|
|
124
|
+
hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
|
|
125
|
+
isValueComplete: (ctx2) => ctx2.isValueComplete,
|
|
126
|
+
isValidValue: (ctx2, evt) => {
|
|
127
|
+
if (!ctx2.pattern)
|
|
128
|
+
return isValidType(evt.value, ctx2.type);
|
|
129
|
+
const regex = new RegExp(ctx2.pattern, "g");
|
|
130
|
+
return regex.test(evt.value);
|
|
131
|
+
},
|
|
132
|
+
isFinalValue: (ctx2) => {
|
|
133
|
+
return ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex;
|
|
134
|
+
},
|
|
135
|
+
isLastInputFocused: (ctx2) => ctx2.focusedIndex === ctx2.valueLength - 1,
|
|
136
|
+
hasIndex: (_ctx, evt) => evt.index !== void 0,
|
|
137
|
+
isDisabled: (ctx2) => !!ctx2.disabled
|
|
138
|
+
},
|
|
139
|
+
actions: {
|
|
140
|
+
setupValue: (ctx2) => {
|
|
141
|
+
if (ctx2.value.length)
|
|
142
|
+
return;
|
|
143
|
+
const inputs = dom.getElements(ctx2);
|
|
144
|
+
const emptyValues = Array.from({ length: inputs.length }).fill("");
|
|
145
|
+
assign(ctx2, emptyValues);
|
|
146
|
+
},
|
|
147
|
+
focusInput: (ctx2) => {
|
|
148
|
+
raf(() => {
|
|
149
|
+
if (ctx2.focusedIndex === -1)
|
|
150
|
+
return;
|
|
151
|
+
dom.getFocusedInputEl(ctx2)?.focus();
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
setInputSelection: (ctx2) => {
|
|
155
|
+
raf(() => {
|
|
156
|
+
if (ctx2.focusedIndex === -1)
|
|
157
|
+
return;
|
|
158
|
+
const input = dom.getFocusedInputEl(ctx2);
|
|
159
|
+
const length = input.value.length;
|
|
160
|
+
input.selectionStart = ctx2.selectOnFocus ? 0 : length;
|
|
161
|
+
input.selectionEnd = length;
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
invokeOnComplete: (ctx2) => {
|
|
165
|
+
if (!ctx2.isValueComplete)
|
|
166
|
+
return;
|
|
167
|
+
ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
|
|
168
|
+
},
|
|
169
|
+
invokeOnChange: (ctx2) => {
|
|
170
|
+
ctx2.onChange?.({ value: Array.from(ctx2.value) });
|
|
171
|
+
},
|
|
172
|
+
dispatchInputEvent: (ctx2) => {
|
|
173
|
+
const inputEl = dom.getHiddenInputEl(ctx2);
|
|
174
|
+
dispatchInputValueEvent(inputEl, { value: ctx2.valueAsString });
|
|
175
|
+
},
|
|
176
|
+
invokeOnInvalid: (ctx2, evt) => {
|
|
177
|
+
ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
|
|
178
|
+
},
|
|
179
|
+
clearFocusedIndex: (ctx2) => {
|
|
180
|
+
ctx2.focusedIndex = -1;
|
|
181
|
+
},
|
|
182
|
+
setValue: (ctx2, evt) => {
|
|
183
|
+
assign(ctx2, evt.value);
|
|
184
|
+
},
|
|
185
|
+
setFocusedIndex: (ctx2, evt) => {
|
|
186
|
+
ctx2.focusedIndex = evt.index;
|
|
187
|
+
},
|
|
188
|
+
setFocusedValue: (ctx2, evt) => {
|
|
189
|
+
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
190
|
+
},
|
|
191
|
+
syncInputValue(ctx2, evt) {
|
|
192
|
+
const input = dom.getInputEl(ctx2, evt.index.toString());
|
|
193
|
+
if (!input)
|
|
194
|
+
return;
|
|
195
|
+
input.value = ctx2.value[evt.index];
|
|
196
|
+
},
|
|
197
|
+
syncInputElements(ctx2) {
|
|
198
|
+
const inputs = dom.getElements(ctx2);
|
|
199
|
+
inputs.forEach((input, index) => {
|
|
200
|
+
input.value = ctx2.value[index];
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
setPastedValue(ctx2, evt) {
|
|
204
|
+
raf(() => {
|
|
205
|
+
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
206
|
+
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
207
|
+
assign(ctx2, value);
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
setValueAtIndex: (ctx2, evt) => {
|
|
211
|
+
ctx2.value[evt.index] = getNextValue(ctx2.focusedValue, evt.value);
|
|
212
|
+
},
|
|
213
|
+
clearValue: (ctx2) => {
|
|
214
|
+
const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
|
|
215
|
+
assign(ctx2, nextValue);
|
|
216
|
+
},
|
|
217
|
+
clearFocusedValue: (ctx2) => {
|
|
218
|
+
ctx2.value[ctx2.focusedIndex] = "";
|
|
219
|
+
},
|
|
220
|
+
resetFocusedValue: (ctx2) => {
|
|
221
|
+
const input = dom.getFocusedInputEl(ctx2);
|
|
222
|
+
input.value = ctx2.focusedValue;
|
|
223
|
+
},
|
|
224
|
+
setFocusIndexToFirst: (ctx2) => {
|
|
225
|
+
ctx2.focusedIndex = 0;
|
|
226
|
+
},
|
|
227
|
+
setNextFocusedIndex: (ctx2) => {
|
|
228
|
+
ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
|
|
229
|
+
},
|
|
230
|
+
setPrevFocusedIndex: (ctx2) => {
|
|
231
|
+
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
232
|
+
},
|
|
233
|
+
setLastValueFocusIndex: (ctx2) => {
|
|
234
|
+
raf(() => {
|
|
235
|
+
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
preventDefault(_, evt) {
|
|
239
|
+
evt.preventDefault();
|
|
240
|
+
},
|
|
241
|
+
blurFocusedInputIfNeeded(ctx2) {
|
|
242
|
+
if (!ctx2.blurOnComplete)
|
|
243
|
+
return;
|
|
244
|
+
raf(() => {
|
|
245
|
+
dom.getFocusedInputEl(ctx2)?.blur();
|
|
246
|
+
});
|
|
247
|
+
},
|
|
248
|
+
requestFormSubmit(ctx2) {
|
|
249
|
+
if (!ctx2.name || !ctx2.isValueComplete)
|
|
250
|
+
return;
|
|
251
|
+
const input = dom.getHiddenInputEl(ctx2);
|
|
252
|
+
input?.form?.requestSubmit();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const REGEX = {
|
|
259
|
+
numeric: /^[0-9]+$/,
|
|
260
|
+
alphabetic: /^[A-Za-z]+$/,
|
|
261
|
+
alphanumeric: /^[a-zA-Z0-9]+$/i
|
|
7
262
|
};
|
|
263
|
+
function isValidType(value, type) {
|
|
264
|
+
if (!type)
|
|
265
|
+
return true;
|
|
266
|
+
return !!REGEX[type]?.test(value);
|
|
267
|
+
}
|
|
268
|
+
function assign(ctx, value) {
|
|
269
|
+
const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
|
|
270
|
+
arr.forEach((value2, index) => {
|
|
271
|
+
ctx.value[index] = value2;
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
function getNextValue(current, next) {
|
|
275
|
+
let nextValue = next;
|
|
276
|
+
if (current[0] === next[0])
|
|
277
|
+
nextValue = next[1];
|
|
278
|
+
else if (current[0] === next[1])
|
|
279
|
+
nextValue = next[0];
|
|
280
|
+
return nextValue;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export { machine };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { StateMachine } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { StateMachine as S } from "@zag-js/core";
|
|
2
|
+
import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
|
|
4
3
|
type IntlTranslations = {
|
|
5
4
|
inputLabel: (index: number, length: number) => string;
|
|
6
5
|
};
|
|
@@ -94,7 +93,7 @@ type PublicContext = DirectionProperty & CommonProperties & {
|
|
|
94
93
|
*/
|
|
95
94
|
translations: IntlTranslations;
|
|
96
95
|
};
|
|
97
|
-
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
96
|
+
export type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
98
97
|
type ComputedContext = Readonly<{
|
|
99
98
|
/**
|
|
100
99
|
* @computed
|
|
@@ -123,11 +122,10 @@ type ComputedContext = Readonly<{
|
|
|
123
122
|
focusedValue: string;
|
|
124
123
|
}>;
|
|
125
124
|
type PrivateContext = Context<{}>;
|
|
126
|
-
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
127
|
-
type MachineState = {
|
|
125
|
+
export type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
126
|
+
export type MachineState = {
|
|
128
127
|
value: "idle" | "focused";
|
|
129
128
|
};
|
|
130
|
-
type State =
|
|
131
|
-
type Send =
|
|
132
|
-
|
|
133
|
-
export { MachineContext, MachineState, Send, State, UserDefinedContext };
|
|
129
|
+
export type State = S.State<MachineContext, MachineState>;
|
|
130
|
+
export type Send = S.Send<S.AnyEventObject>;
|
|
131
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.4",
|
|
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.
|
|
31
|
-
"@zag-js/dom-query": "0.10.
|
|
32
|
-
"@zag-js/dom-event": "0.10.
|
|
33
|
-
"@zag-js/form-utils": "0.10.
|
|
34
|
-
"@zag-js/visually-hidden": "0.10.
|
|
35
|
-
"@zag-js/utils": "0.10.
|
|
36
|
-
"@zag-js/core": "0.10.
|
|
37
|
-
"@zag-js/types": "0.10.
|
|
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"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"clean-package": "2.2.0"
|
|
@@ -52,13 +52,8 @@
|
|
|
52
52
|
"./package.json": "./package.json"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
|
-
"build
|
|
56
|
-
"start": "pnpm build --watch",
|
|
57
|
-
"build": "tsup src --dts",
|
|
58
|
-
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
55
|
+
"build": "vite build -c ../../../vite.config.ts",
|
|
59
56
|
"lint": "eslint src --ext .ts,.tsx",
|
|
60
|
-
"test-ci": "pnpm test --ci --runInBand",
|
|
61
|
-
"test-watch": "pnpm test --watch -u",
|
|
62
57
|
"typecheck": "tsc --noEmit"
|
|
63
58
|
}
|
|
64
59
|
}
|
package/dist/chunk-BJXKBZJG.mjs
DELETED
package/dist/chunk-CPXMEVTH.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// src/pin-input.dom.ts
|
|
2
|
-
import { createScope, queryAll } from "@zag-js/dom-query";
|
|
3
|
-
var dom = createScope({
|
|
4
|
-
getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
|
|
5
|
-
getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
|
|
6
|
-
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
7
|
-
getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
|
|
8
|
-
getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
|
|
9
|
-
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
10
|
-
getElements: (ctx) => {
|
|
11
|
-
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
12
|
-
const selector = `input[data-ownedby=${ownerId}]`;
|
|
13
|
-
return queryAll(dom.getRootEl(ctx), selector);
|
|
14
|
-
},
|
|
15
|
-
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
16
|
-
getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
|
|
17
|
-
getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
|
|
18
|
-
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
export {
|
|
22
|
-
dom
|
|
23
|
-
};
|