@zag-js/pin-input 1.34.1 → 1.35.1
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.mts +8 -199
- package/dist/index.d.ts +8 -199
- package/dist/index.js +35 -545
- package/dist/index.mjs +9 -542
- package/dist/pin-input.anatomy.d.mts +6 -0
- package/dist/pin-input.anatomy.d.ts +6 -0
- package/dist/pin-input.anatomy.js +34 -0
- package/dist/pin-input.anatomy.mjs +8 -0
- package/dist/pin-input.connect.d.mts +7 -0
- package/dist/pin-input.connect.d.ts +7 -0
- package/dist/pin-input.connect.js +235 -0
- package/dist/pin-input.connect.mjs +210 -0
- package/dist/pin-input.dom.d.mts +16 -0
- package/dist/pin-input.dom.d.ts +16 -0
- package/dist/pin-input.dom.js +71 -0
- package/dist/pin-input.dom.mjs +35 -0
- package/dist/pin-input.machine.d.mts +7 -0
- package/dist/pin-input.machine.d.ts +7 -0
- package/dist/pin-input.machine.js +320 -0
- package/dist/pin-input.machine.mjs +285 -0
- package/dist/pin-input.props.d.mts +8 -0
- package/dist/pin-input.props.d.ts +8 -0
- package/dist/pin-input.props.js +61 -0
- package/dist/pin-input.props.mjs +35 -0
- package/dist/pin-input.types.d.mts +188 -0
- package/dist/pin-input.types.d.ts +188 -0
- package/dist/pin-input.types.js +18 -0
- package/dist/pin-input.types.mjs +0 -0
- package/dist/pin-input.utils.d.mts +10 -0
- package/dist/pin-input.utils.d.ts +10 -0
- package/dist/pin-input.utils.js +45 -0
- package/dist/pin-input.utils.mjs +19 -0
- package/package.json +17 -7
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/pin-input.machine.ts
|
|
31
|
+
var pin_input_machine_exports = {};
|
|
32
|
+
__export(pin_input_machine_exports, {
|
|
33
|
+
machine: () => machine
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(pin_input_machine_exports);
|
|
36
|
+
var import_core = require("@zag-js/core");
|
|
37
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
38
|
+
var import_utils = require("@zag-js/utils");
|
|
39
|
+
var dom = __toESM(require("./pin-input.dom.cjs"));
|
|
40
|
+
var { choose, createMachine } = (0, import_core.setup)();
|
|
41
|
+
var machine = createMachine({
|
|
42
|
+
props({ props }) {
|
|
43
|
+
return {
|
|
44
|
+
placeholder: "\u25CB",
|
|
45
|
+
otp: false,
|
|
46
|
+
type: "numeric",
|
|
47
|
+
defaultValue: props.count ? fill([], props.count) : [],
|
|
48
|
+
...props,
|
|
49
|
+
translations: {
|
|
50
|
+
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
51
|
+
...props.translations
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
initialState() {
|
|
56
|
+
return "idle";
|
|
57
|
+
},
|
|
58
|
+
context({ prop, bindable }) {
|
|
59
|
+
return {
|
|
60
|
+
value: bindable(() => ({
|
|
61
|
+
value: prop("value"),
|
|
62
|
+
defaultValue: prop("defaultValue"),
|
|
63
|
+
isEqual: import_utils.isEqual,
|
|
64
|
+
onChange(value) {
|
|
65
|
+
prop("onValueChange")?.({ value, valueAsString: value.join("") });
|
|
66
|
+
}
|
|
67
|
+
})),
|
|
68
|
+
focusedIndex: bindable(() => ({
|
|
69
|
+
sync: true,
|
|
70
|
+
defaultValue: -1
|
|
71
|
+
})),
|
|
72
|
+
// TODO: Move this to `props` in next major version
|
|
73
|
+
count: bindable(() => ({
|
|
74
|
+
defaultValue: prop("count")
|
|
75
|
+
}))
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
computed: {
|
|
79
|
+
_value: ({ context }) => fill(context.get("value"), context.get("count")),
|
|
80
|
+
valueLength: ({ computed }) => computed("_value").length,
|
|
81
|
+
filledValueLength: ({ computed }) => computed("_value").filter((v) => v?.trim() !== "").length,
|
|
82
|
+
isValueComplete: ({ computed }) => computed("valueLength") === computed("filledValueLength"),
|
|
83
|
+
valueAsString: ({ computed }) => computed("_value").join(""),
|
|
84
|
+
focusedValue: ({ computed, context }) => computed("_value")[context.get("focusedIndex")] || ""
|
|
85
|
+
},
|
|
86
|
+
entry: choose([
|
|
87
|
+
{
|
|
88
|
+
guard: "autoFocus",
|
|
89
|
+
actions: ["setInputCount", "setFocusIndexToFirst"]
|
|
90
|
+
},
|
|
91
|
+
{ actions: ["setInputCount"] }
|
|
92
|
+
]),
|
|
93
|
+
watch({ action, track, context, computed }) {
|
|
94
|
+
track([() => context.get("focusedIndex")], () => {
|
|
95
|
+
action(["focusInput", "selectInputIfNeeded"]);
|
|
96
|
+
});
|
|
97
|
+
track([() => context.get("value").join(",")], () => {
|
|
98
|
+
action(["syncInputElements", "dispatchInputEvent"]);
|
|
99
|
+
});
|
|
100
|
+
track([() => computed("isValueComplete")], () => {
|
|
101
|
+
action(["invokeOnComplete", "blurFocusedInputIfNeeded"]);
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
on: {
|
|
105
|
+
"VALUE.SET": [
|
|
106
|
+
{
|
|
107
|
+
guard: "hasIndex",
|
|
108
|
+
actions: ["setValueAtIndex"]
|
|
109
|
+
},
|
|
110
|
+
{ actions: ["setValue"] }
|
|
111
|
+
],
|
|
112
|
+
"VALUE.CLEAR": {
|
|
113
|
+
actions: ["clearValue", "setFocusIndexToFirst"]
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
states: {
|
|
117
|
+
idle: {
|
|
118
|
+
on: {
|
|
119
|
+
"INPUT.FOCUS": {
|
|
120
|
+
target: "focused",
|
|
121
|
+
actions: ["setFocusedIndex"]
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
focused: {
|
|
126
|
+
on: {
|
|
127
|
+
"INPUT.CHANGE": {
|
|
128
|
+
actions: ["setFocusedValue", "syncInputValue", "setNextFocusedIndex"]
|
|
129
|
+
},
|
|
130
|
+
"INPUT.PASTE": {
|
|
131
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
132
|
+
},
|
|
133
|
+
"INPUT.FOCUS": {
|
|
134
|
+
actions: ["setFocusedIndex"]
|
|
135
|
+
},
|
|
136
|
+
"INPUT.BLUR": {
|
|
137
|
+
target: "idle",
|
|
138
|
+
actions: ["clearFocusedIndex"]
|
|
139
|
+
},
|
|
140
|
+
"INPUT.DELETE": {
|
|
141
|
+
guard: "hasValue",
|
|
142
|
+
actions: ["clearFocusedValue"]
|
|
143
|
+
},
|
|
144
|
+
"INPUT.ARROW_LEFT": {
|
|
145
|
+
actions: ["setPrevFocusedIndex"]
|
|
146
|
+
},
|
|
147
|
+
"INPUT.ARROW_RIGHT": {
|
|
148
|
+
actions: ["setNextFocusedIndex"]
|
|
149
|
+
},
|
|
150
|
+
"INPUT.BACKSPACE": [
|
|
151
|
+
{
|
|
152
|
+
guard: "hasValue",
|
|
153
|
+
actions: ["clearFocusedValue"]
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
actions: ["setPrevFocusedIndex", "clearFocusedValue"]
|
|
157
|
+
}
|
|
158
|
+
],
|
|
159
|
+
"INPUT.ENTER": {
|
|
160
|
+
guard: "isValueComplete",
|
|
161
|
+
actions: ["requestFormSubmit"]
|
|
162
|
+
},
|
|
163
|
+
"VALUE.INVALID": {
|
|
164
|
+
actions: ["invokeOnInvalid"]
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
implementations: {
|
|
170
|
+
guards: {
|
|
171
|
+
autoFocus: ({ prop }) => !!prop("autoFocus"),
|
|
172
|
+
hasValue: ({ context }) => context.get("value")[context.get("focusedIndex")] !== "",
|
|
173
|
+
isValueComplete: ({ computed }) => computed("isValueComplete"),
|
|
174
|
+
hasIndex: ({ event }) => event.index !== void 0
|
|
175
|
+
},
|
|
176
|
+
actions: {
|
|
177
|
+
dispatchInputEvent({ computed, scope }) {
|
|
178
|
+
const inputEl = dom.getHiddenInputEl(scope);
|
|
179
|
+
(0, import_dom_query.dispatchInputValueEvent)(inputEl, { value: computed("valueAsString") });
|
|
180
|
+
},
|
|
181
|
+
setInputCount({ scope, context, prop }) {
|
|
182
|
+
if (prop("count")) return;
|
|
183
|
+
const inputEls = dom.getInputEls(scope);
|
|
184
|
+
context.set("count", inputEls.length);
|
|
185
|
+
},
|
|
186
|
+
focusInput({ context, scope }) {
|
|
187
|
+
const focusedIndex = context.get("focusedIndex");
|
|
188
|
+
if (focusedIndex === -1) return;
|
|
189
|
+
dom.getInputElAtIndex(scope, focusedIndex)?.focus({ preventScroll: true });
|
|
190
|
+
},
|
|
191
|
+
selectInputIfNeeded({ context, prop, scope }) {
|
|
192
|
+
const focusedIndex = context.get("focusedIndex");
|
|
193
|
+
if (!prop("selectOnFocus") || focusedIndex === -1) return;
|
|
194
|
+
(0, import_dom_query.raf)(() => {
|
|
195
|
+
dom.getInputElAtIndex(scope, focusedIndex)?.select();
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
invokeOnComplete({ computed, prop }) {
|
|
199
|
+
if (!computed("isValueComplete")) return;
|
|
200
|
+
prop("onValueComplete")?.({
|
|
201
|
+
value: computed("_value"),
|
|
202
|
+
valueAsString: computed("valueAsString")
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
invokeOnInvalid({ context, event, prop }) {
|
|
206
|
+
prop("onValueInvalid")?.({
|
|
207
|
+
value: event.value,
|
|
208
|
+
index: context.get("focusedIndex")
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
clearFocusedIndex({ context }) {
|
|
212
|
+
context.set("focusedIndex", -1);
|
|
213
|
+
},
|
|
214
|
+
setFocusedIndex({ context, event }) {
|
|
215
|
+
context.set("focusedIndex", event.index);
|
|
216
|
+
},
|
|
217
|
+
setValue({ context, event }) {
|
|
218
|
+
const value = fill(event.value, context.get("count"));
|
|
219
|
+
context.set("value", value);
|
|
220
|
+
},
|
|
221
|
+
setFocusedValue({ context, event, computed, flush }) {
|
|
222
|
+
const focusedValue = computed("focusedValue");
|
|
223
|
+
const focusedIndex = context.get("focusedIndex");
|
|
224
|
+
const value = getNextValue(focusedValue, event.value);
|
|
225
|
+
flush(() => {
|
|
226
|
+
context.set("value", (0, import_utils.setValueAtIndex)(computed("_value"), focusedIndex, value));
|
|
227
|
+
});
|
|
228
|
+
},
|
|
229
|
+
revertInputValue({ context, computed, scope }) {
|
|
230
|
+
const inputEl = dom.getInputElAtIndex(scope, context.get("focusedIndex"));
|
|
231
|
+
dom.setInputValue(inputEl, computed("focusedValue"));
|
|
232
|
+
},
|
|
233
|
+
syncInputValue({ context, event, scope }) {
|
|
234
|
+
const value = context.get("value");
|
|
235
|
+
const inputEl = dom.getInputElAtIndex(scope, event.index);
|
|
236
|
+
dom.setInputValue(inputEl, value[event.index]);
|
|
237
|
+
},
|
|
238
|
+
syncInputElements({ context, scope }) {
|
|
239
|
+
const inputEls = dom.getInputEls(scope);
|
|
240
|
+
const value = context.get("value");
|
|
241
|
+
inputEls.forEach((inputEl, index) => {
|
|
242
|
+
dom.setInputValue(inputEl, value[index]);
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
setPastedValue({ context, event, computed, flush }) {
|
|
246
|
+
(0, import_dom_query.raf)(() => {
|
|
247
|
+
const valueAsString = computed("valueAsString");
|
|
248
|
+
const focusedIndex = context.get("focusedIndex");
|
|
249
|
+
const valueLength = computed("valueLength");
|
|
250
|
+
const filledValueLength = computed("filledValueLength");
|
|
251
|
+
const startIndex = Math.min(focusedIndex, filledValueLength);
|
|
252
|
+
const left = startIndex > 0 ? valueAsString.substring(0, focusedIndex) : "";
|
|
253
|
+
const right = event.value.substring(0, valueLength - startIndex);
|
|
254
|
+
const value = fill(`${left}${right}`.split(""), valueLength);
|
|
255
|
+
flush(() => {
|
|
256
|
+
context.set("value", value);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
setValueAtIndex({ context, event, computed }) {
|
|
261
|
+
const nextValue = getNextValue(computed("focusedValue"), event.value);
|
|
262
|
+
context.set("value", (0, import_utils.setValueAtIndex)(computed("_value"), event.index, nextValue));
|
|
263
|
+
},
|
|
264
|
+
clearValue({ context }) {
|
|
265
|
+
const nextValue = Array.from({ length: context.get("count") }).fill("");
|
|
266
|
+
queueMicrotask(() => {
|
|
267
|
+
context.set("value", nextValue);
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
clearFocusedValue({ context, computed }) {
|
|
271
|
+
const focusedIndex = context.get("focusedIndex");
|
|
272
|
+
if (focusedIndex === -1) return;
|
|
273
|
+
context.set("value", (0, import_utils.setValueAtIndex)(computed("_value"), focusedIndex, ""));
|
|
274
|
+
},
|
|
275
|
+
setFocusIndexToFirst({ context }) {
|
|
276
|
+
context.set("focusedIndex", 0);
|
|
277
|
+
},
|
|
278
|
+
setNextFocusedIndex({ context, computed }) {
|
|
279
|
+
context.set("focusedIndex", Math.min(context.get("focusedIndex") + 1, computed("valueLength") - 1));
|
|
280
|
+
},
|
|
281
|
+
setPrevFocusedIndex({ context }) {
|
|
282
|
+
context.set("focusedIndex", Math.max(context.get("focusedIndex") - 1, 0));
|
|
283
|
+
},
|
|
284
|
+
setLastValueFocusIndex({ context, computed }) {
|
|
285
|
+
(0, import_dom_query.raf)(() => {
|
|
286
|
+
context.set("focusedIndex", Math.min(computed("filledValueLength"), computed("valueLength") - 1));
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
blurFocusedInputIfNeeded({ context, prop, scope }) {
|
|
290
|
+
if (!prop("blurOnComplete")) return;
|
|
291
|
+
(0, import_dom_query.raf)(() => {
|
|
292
|
+
dom.getInputElAtIndex(scope, context.get("focusedIndex"))?.blur();
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
requestFormSubmit({ computed, prop, scope }) {
|
|
296
|
+
if (!prop("name") || !computed("isValueComplete")) return;
|
|
297
|
+
const inputEl = dom.getHiddenInputEl(scope);
|
|
298
|
+
inputEl?.form?.requestSubmit();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
function getNextValue(current, next) {
|
|
304
|
+
let nextValue = next;
|
|
305
|
+
if (current[0] === next[0]) {
|
|
306
|
+
nextValue = next[1];
|
|
307
|
+
} else if (current[0] === next[1]) {
|
|
308
|
+
nextValue = next[0];
|
|
309
|
+
}
|
|
310
|
+
const chars = nextValue.split("");
|
|
311
|
+
nextValue = chars[chars.length - 1];
|
|
312
|
+
return nextValue ?? "";
|
|
313
|
+
}
|
|
314
|
+
function fill(value, count) {
|
|
315
|
+
return Array.from({ length: count }).fill("").map((v, i) => value[i] || v);
|
|
316
|
+
}
|
|
317
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
318
|
+
0 && (module.exports = {
|
|
319
|
+
machine
|
|
320
|
+
});
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// src/pin-input.machine.ts
|
|
2
|
+
import { setup } from "@zag-js/core";
|
|
3
|
+
import { dispatchInputValueEvent, raf } from "@zag-js/dom-query";
|
|
4
|
+
import { isEqual, setValueAtIndex } from "@zag-js/utils";
|
|
5
|
+
import * as dom from "./pin-input.dom.mjs";
|
|
6
|
+
var { choose, createMachine } = setup();
|
|
7
|
+
var machine = createMachine({
|
|
8
|
+
props({ props }) {
|
|
9
|
+
return {
|
|
10
|
+
placeholder: "\u25CB",
|
|
11
|
+
otp: false,
|
|
12
|
+
type: "numeric",
|
|
13
|
+
defaultValue: props.count ? fill([], props.count) : [],
|
|
14
|
+
...props,
|
|
15
|
+
translations: {
|
|
16
|
+
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
17
|
+
...props.translations
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
initialState() {
|
|
22
|
+
return "idle";
|
|
23
|
+
},
|
|
24
|
+
context({ prop, bindable }) {
|
|
25
|
+
return {
|
|
26
|
+
value: bindable(() => ({
|
|
27
|
+
value: prop("value"),
|
|
28
|
+
defaultValue: prop("defaultValue"),
|
|
29
|
+
isEqual,
|
|
30
|
+
onChange(value) {
|
|
31
|
+
prop("onValueChange")?.({ value, valueAsString: value.join("") });
|
|
32
|
+
}
|
|
33
|
+
})),
|
|
34
|
+
focusedIndex: bindable(() => ({
|
|
35
|
+
sync: true,
|
|
36
|
+
defaultValue: -1
|
|
37
|
+
})),
|
|
38
|
+
// TODO: Move this to `props` in next major version
|
|
39
|
+
count: bindable(() => ({
|
|
40
|
+
defaultValue: prop("count")
|
|
41
|
+
}))
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
computed: {
|
|
45
|
+
_value: ({ context }) => fill(context.get("value"), context.get("count")),
|
|
46
|
+
valueLength: ({ computed }) => computed("_value").length,
|
|
47
|
+
filledValueLength: ({ computed }) => computed("_value").filter((v) => v?.trim() !== "").length,
|
|
48
|
+
isValueComplete: ({ computed }) => computed("valueLength") === computed("filledValueLength"),
|
|
49
|
+
valueAsString: ({ computed }) => computed("_value").join(""),
|
|
50
|
+
focusedValue: ({ computed, context }) => computed("_value")[context.get("focusedIndex")] || ""
|
|
51
|
+
},
|
|
52
|
+
entry: choose([
|
|
53
|
+
{
|
|
54
|
+
guard: "autoFocus",
|
|
55
|
+
actions: ["setInputCount", "setFocusIndexToFirst"]
|
|
56
|
+
},
|
|
57
|
+
{ actions: ["setInputCount"] }
|
|
58
|
+
]),
|
|
59
|
+
watch({ action, track, context, computed }) {
|
|
60
|
+
track([() => context.get("focusedIndex")], () => {
|
|
61
|
+
action(["focusInput", "selectInputIfNeeded"]);
|
|
62
|
+
});
|
|
63
|
+
track([() => context.get("value").join(",")], () => {
|
|
64
|
+
action(["syncInputElements", "dispatchInputEvent"]);
|
|
65
|
+
});
|
|
66
|
+
track([() => computed("isValueComplete")], () => {
|
|
67
|
+
action(["invokeOnComplete", "blurFocusedInputIfNeeded"]);
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
on: {
|
|
71
|
+
"VALUE.SET": [
|
|
72
|
+
{
|
|
73
|
+
guard: "hasIndex",
|
|
74
|
+
actions: ["setValueAtIndex"]
|
|
75
|
+
},
|
|
76
|
+
{ actions: ["setValue"] }
|
|
77
|
+
],
|
|
78
|
+
"VALUE.CLEAR": {
|
|
79
|
+
actions: ["clearValue", "setFocusIndexToFirst"]
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
states: {
|
|
83
|
+
idle: {
|
|
84
|
+
on: {
|
|
85
|
+
"INPUT.FOCUS": {
|
|
86
|
+
target: "focused",
|
|
87
|
+
actions: ["setFocusedIndex"]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
focused: {
|
|
92
|
+
on: {
|
|
93
|
+
"INPUT.CHANGE": {
|
|
94
|
+
actions: ["setFocusedValue", "syncInputValue", "setNextFocusedIndex"]
|
|
95
|
+
},
|
|
96
|
+
"INPUT.PASTE": {
|
|
97
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
98
|
+
},
|
|
99
|
+
"INPUT.FOCUS": {
|
|
100
|
+
actions: ["setFocusedIndex"]
|
|
101
|
+
},
|
|
102
|
+
"INPUT.BLUR": {
|
|
103
|
+
target: "idle",
|
|
104
|
+
actions: ["clearFocusedIndex"]
|
|
105
|
+
},
|
|
106
|
+
"INPUT.DELETE": {
|
|
107
|
+
guard: "hasValue",
|
|
108
|
+
actions: ["clearFocusedValue"]
|
|
109
|
+
},
|
|
110
|
+
"INPUT.ARROW_LEFT": {
|
|
111
|
+
actions: ["setPrevFocusedIndex"]
|
|
112
|
+
},
|
|
113
|
+
"INPUT.ARROW_RIGHT": {
|
|
114
|
+
actions: ["setNextFocusedIndex"]
|
|
115
|
+
},
|
|
116
|
+
"INPUT.BACKSPACE": [
|
|
117
|
+
{
|
|
118
|
+
guard: "hasValue",
|
|
119
|
+
actions: ["clearFocusedValue"]
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
actions: ["setPrevFocusedIndex", "clearFocusedValue"]
|
|
123
|
+
}
|
|
124
|
+
],
|
|
125
|
+
"INPUT.ENTER": {
|
|
126
|
+
guard: "isValueComplete",
|
|
127
|
+
actions: ["requestFormSubmit"]
|
|
128
|
+
},
|
|
129
|
+
"VALUE.INVALID": {
|
|
130
|
+
actions: ["invokeOnInvalid"]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
implementations: {
|
|
136
|
+
guards: {
|
|
137
|
+
autoFocus: ({ prop }) => !!prop("autoFocus"),
|
|
138
|
+
hasValue: ({ context }) => context.get("value")[context.get("focusedIndex")] !== "",
|
|
139
|
+
isValueComplete: ({ computed }) => computed("isValueComplete"),
|
|
140
|
+
hasIndex: ({ event }) => event.index !== void 0
|
|
141
|
+
},
|
|
142
|
+
actions: {
|
|
143
|
+
dispatchInputEvent({ computed, scope }) {
|
|
144
|
+
const inputEl = dom.getHiddenInputEl(scope);
|
|
145
|
+
dispatchInputValueEvent(inputEl, { value: computed("valueAsString") });
|
|
146
|
+
},
|
|
147
|
+
setInputCount({ scope, context, prop }) {
|
|
148
|
+
if (prop("count")) return;
|
|
149
|
+
const inputEls = dom.getInputEls(scope);
|
|
150
|
+
context.set("count", inputEls.length);
|
|
151
|
+
},
|
|
152
|
+
focusInput({ context, scope }) {
|
|
153
|
+
const focusedIndex = context.get("focusedIndex");
|
|
154
|
+
if (focusedIndex === -1) return;
|
|
155
|
+
dom.getInputElAtIndex(scope, focusedIndex)?.focus({ preventScroll: true });
|
|
156
|
+
},
|
|
157
|
+
selectInputIfNeeded({ context, prop, scope }) {
|
|
158
|
+
const focusedIndex = context.get("focusedIndex");
|
|
159
|
+
if (!prop("selectOnFocus") || focusedIndex === -1) return;
|
|
160
|
+
raf(() => {
|
|
161
|
+
dom.getInputElAtIndex(scope, focusedIndex)?.select();
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
invokeOnComplete({ computed, prop }) {
|
|
165
|
+
if (!computed("isValueComplete")) return;
|
|
166
|
+
prop("onValueComplete")?.({
|
|
167
|
+
value: computed("_value"),
|
|
168
|
+
valueAsString: computed("valueAsString")
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
invokeOnInvalid({ context, event, prop }) {
|
|
172
|
+
prop("onValueInvalid")?.({
|
|
173
|
+
value: event.value,
|
|
174
|
+
index: context.get("focusedIndex")
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
clearFocusedIndex({ context }) {
|
|
178
|
+
context.set("focusedIndex", -1);
|
|
179
|
+
},
|
|
180
|
+
setFocusedIndex({ context, event }) {
|
|
181
|
+
context.set("focusedIndex", event.index);
|
|
182
|
+
},
|
|
183
|
+
setValue({ context, event }) {
|
|
184
|
+
const value = fill(event.value, context.get("count"));
|
|
185
|
+
context.set("value", value);
|
|
186
|
+
},
|
|
187
|
+
setFocusedValue({ context, event, computed, flush }) {
|
|
188
|
+
const focusedValue = computed("focusedValue");
|
|
189
|
+
const focusedIndex = context.get("focusedIndex");
|
|
190
|
+
const value = getNextValue(focusedValue, event.value);
|
|
191
|
+
flush(() => {
|
|
192
|
+
context.set("value", setValueAtIndex(computed("_value"), focusedIndex, value));
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
revertInputValue({ context, computed, scope }) {
|
|
196
|
+
const inputEl = dom.getInputElAtIndex(scope, context.get("focusedIndex"));
|
|
197
|
+
dom.setInputValue(inputEl, computed("focusedValue"));
|
|
198
|
+
},
|
|
199
|
+
syncInputValue({ context, event, scope }) {
|
|
200
|
+
const value = context.get("value");
|
|
201
|
+
const inputEl = dom.getInputElAtIndex(scope, event.index);
|
|
202
|
+
dom.setInputValue(inputEl, value[event.index]);
|
|
203
|
+
},
|
|
204
|
+
syncInputElements({ context, scope }) {
|
|
205
|
+
const inputEls = dom.getInputEls(scope);
|
|
206
|
+
const value = context.get("value");
|
|
207
|
+
inputEls.forEach((inputEl, index) => {
|
|
208
|
+
dom.setInputValue(inputEl, value[index]);
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
setPastedValue({ context, event, computed, flush }) {
|
|
212
|
+
raf(() => {
|
|
213
|
+
const valueAsString = computed("valueAsString");
|
|
214
|
+
const focusedIndex = context.get("focusedIndex");
|
|
215
|
+
const valueLength = computed("valueLength");
|
|
216
|
+
const filledValueLength = computed("filledValueLength");
|
|
217
|
+
const startIndex = Math.min(focusedIndex, filledValueLength);
|
|
218
|
+
const left = startIndex > 0 ? valueAsString.substring(0, focusedIndex) : "";
|
|
219
|
+
const right = event.value.substring(0, valueLength - startIndex);
|
|
220
|
+
const value = fill(`${left}${right}`.split(""), valueLength);
|
|
221
|
+
flush(() => {
|
|
222
|
+
context.set("value", value);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
setValueAtIndex({ context, event, computed }) {
|
|
227
|
+
const nextValue = getNextValue(computed("focusedValue"), event.value);
|
|
228
|
+
context.set("value", setValueAtIndex(computed("_value"), event.index, nextValue));
|
|
229
|
+
},
|
|
230
|
+
clearValue({ context }) {
|
|
231
|
+
const nextValue = Array.from({ length: context.get("count") }).fill("");
|
|
232
|
+
queueMicrotask(() => {
|
|
233
|
+
context.set("value", nextValue);
|
|
234
|
+
});
|
|
235
|
+
},
|
|
236
|
+
clearFocusedValue({ context, computed }) {
|
|
237
|
+
const focusedIndex = context.get("focusedIndex");
|
|
238
|
+
if (focusedIndex === -1) return;
|
|
239
|
+
context.set("value", setValueAtIndex(computed("_value"), focusedIndex, ""));
|
|
240
|
+
},
|
|
241
|
+
setFocusIndexToFirst({ context }) {
|
|
242
|
+
context.set("focusedIndex", 0);
|
|
243
|
+
},
|
|
244
|
+
setNextFocusedIndex({ context, computed }) {
|
|
245
|
+
context.set("focusedIndex", Math.min(context.get("focusedIndex") + 1, computed("valueLength") - 1));
|
|
246
|
+
},
|
|
247
|
+
setPrevFocusedIndex({ context }) {
|
|
248
|
+
context.set("focusedIndex", Math.max(context.get("focusedIndex") - 1, 0));
|
|
249
|
+
},
|
|
250
|
+
setLastValueFocusIndex({ context, computed }) {
|
|
251
|
+
raf(() => {
|
|
252
|
+
context.set("focusedIndex", Math.min(computed("filledValueLength"), computed("valueLength") - 1));
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
blurFocusedInputIfNeeded({ context, prop, scope }) {
|
|
256
|
+
if (!prop("blurOnComplete")) return;
|
|
257
|
+
raf(() => {
|
|
258
|
+
dom.getInputElAtIndex(scope, context.get("focusedIndex"))?.blur();
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
requestFormSubmit({ computed, prop, scope }) {
|
|
262
|
+
if (!prop("name") || !computed("isValueComplete")) return;
|
|
263
|
+
const inputEl = dom.getHiddenInputEl(scope);
|
|
264
|
+
inputEl?.form?.requestSubmit();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
function getNextValue(current, next) {
|
|
270
|
+
let nextValue = next;
|
|
271
|
+
if (current[0] === next[0]) {
|
|
272
|
+
nextValue = next[1];
|
|
273
|
+
} else if (current[0] === next[1]) {
|
|
274
|
+
nextValue = next[0];
|
|
275
|
+
}
|
|
276
|
+
const chars = nextValue.split("");
|
|
277
|
+
nextValue = chars[chars.length - 1];
|
|
278
|
+
return nextValue ?? "";
|
|
279
|
+
}
|
|
280
|
+
function fill(value, count) {
|
|
281
|
+
return Array.from({ length: count }).fill("").map((v, i) => value[i] || v);
|
|
282
|
+
}
|
|
283
|
+
export {
|
|
284
|
+
machine
|
|
285
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PinInputProps } from './pin-input.types.mjs';
|
|
2
|
+
import '@zag-js/core';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare const props: (keyof PinInputProps)[];
|
|
6
|
+
declare const splitProps: <Props extends Partial<PinInputProps>>(props: Props) => [Partial<PinInputProps>, Omit<Props, keyof PinInputProps>];
|
|
7
|
+
|
|
8
|
+
export { props, splitProps };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PinInputProps } from './pin-input.types.js';
|
|
2
|
+
import '@zag-js/core';
|
|
3
|
+
import '@zag-js/types';
|
|
4
|
+
|
|
5
|
+
declare const props: (keyof PinInputProps)[];
|
|
6
|
+
declare const splitProps: <Props extends Partial<PinInputProps>>(props: Props) => [Partial<PinInputProps>, Omit<Props, keyof PinInputProps>];
|
|
7
|
+
|
|
8
|
+
export { props, splitProps };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
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);
|
|
19
|
+
|
|
20
|
+
// src/pin-input.props.ts
|
|
21
|
+
var pin_input_props_exports = {};
|
|
22
|
+
__export(pin_input_props_exports, {
|
|
23
|
+
props: () => props,
|
|
24
|
+
splitProps: () => splitProps
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(pin_input_props_exports);
|
|
27
|
+
var import_types = require("@zag-js/types");
|
|
28
|
+
var import_utils = require("@zag-js/utils");
|
|
29
|
+
var props = (0, import_types.createProps)()([
|
|
30
|
+
"autoFocus",
|
|
31
|
+
"blurOnComplete",
|
|
32
|
+
"count",
|
|
33
|
+
"defaultValue",
|
|
34
|
+
"dir",
|
|
35
|
+
"disabled",
|
|
36
|
+
"form",
|
|
37
|
+
"getRootNode",
|
|
38
|
+
"id",
|
|
39
|
+
"ids",
|
|
40
|
+
"invalid",
|
|
41
|
+
"mask",
|
|
42
|
+
"name",
|
|
43
|
+
"onValueChange",
|
|
44
|
+
"onValueComplete",
|
|
45
|
+
"onValueInvalid",
|
|
46
|
+
"otp",
|
|
47
|
+
"pattern",
|
|
48
|
+
"placeholder",
|
|
49
|
+
"readOnly",
|
|
50
|
+
"required",
|
|
51
|
+
"selectOnFocus",
|
|
52
|
+
"translations",
|
|
53
|
+
"type",
|
|
54
|
+
"value"
|
|
55
|
+
]);
|
|
56
|
+
var splitProps = (0, import_utils.createSplitProps)(props);
|
|
57
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
58
|
+
0 && (module.exports = {
|
|
59
|
+
props,
|
|
60
|
+
splitProps
|
|
61
|
+
});
|