@zag-js/pin-input 0.2.11 → 0.2.13
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/chunk-CPXMEVTH.mjs +23 -0
- package/dist/{chunk-6TEZTVFI.mjs → chunk-H5AVP4J4.mjs} +21 -65
- package/dist/{chunk-QVOI2QEW.mjs → chunk-KLVAQVBX.mjs} +27 -64
- package/dist/index.js +74 -187
- package/dist/index.mjs +3 -4
- package/dist/pin-input.anatomy.d.ts +2 -2
- package/dist/pin-input.connect.d.ts +21 -0
- package/dist/pin-input.connect.js +47 -115
- package/dist/pin-input.connect.mjs +2 -3
- package/dist/pin-input.dom.d.ts +6 -2
- package/dist/pin-input.dom.js +5 -38
- package/dist/pin-input.dom.mjs +1 -1
- package/dist/pin-input.machine.js +31 -107
- package/dist/pin-input.machine.mjs +2 -3
- package/package.json +9 -7
- package/dist/chunk-NT4W6JYX.mjs +0 -7
- package/dist/chunk-T5OKDF74.mjs +0 -58
|
@@ -23,101 +23,10 @@ __export(pin_input_connect_exports, {
|
|
|
23
23
|
connect: () => connect
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(pin_input_connect_exports);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
};
|
|
31
|
-
var ariaAttr = (guard) => {
|
|
32
|
-
return guard ? "true" : void 0;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// ../../utilities/core/src/warning.ts
|
|
36
|
-
function invariant(...a) {
|
|
37
|
-
const m = a.length === 1 ? a[0] : a[1];
|
|
38
|
-
const c = a.length === 2 ? a[0] : true;
|
|
39
|
-
if (c && process.env.NODE_ENV !== "production") {
|
|
40
|
-
throw new Error(m);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ../../utilities/dom/src/query.ts
|
|
45
|
-
function isDocument(el) {
|
|
46
|
-
return el.nodeType === Node.DOCUMENT_NODE;
|
|
47
|
-
}
|
|
48
|
-
function isWindow(value) {
|
|
49
|
-
return value?.toString() === "[object Window]";
|
|
50
|
-
}
|
|
51
|
-
function getDocument(el) {
|
|
52
|
-
if (isWindow(el))
|
|
53
|
-
return el.document;
|
|
54
|
-
if (isDocument(el))
|
|
55
|
-
return el;
|
|
56
|
-
return el?.ownerDocument ?? document;
|
|
57
|
-
}
|
|
58
|
-
function defineDomHelpers(helpers) {
|
|
59
|
-
const dom2 = {
|
|
60
|
-
getRootNode: (ctx) => ctx.getRootNode?.() ?? document,
|
|
61
|
-
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
62
|
-
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
63
|
-
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
64
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
...dom2,
|
|
68
|
-
...helpers
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// ../../utilities/dom/src/event.ts
|
|
73
|
-
function getNativeEvent(e) {
|
|
74
|
-
return e.nativeEvent ?? e;
|
|
75
|
-
}
|
|
76
|
-
var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
|
|
77
|
-
|
|
78
|
-
// ../../utilities/dom/src/keyboard-event.ts
|
|
79
|
-
var rtlKeyMap = {
|
|
80
|
-
ArrowLeft: "ArrowRight",
|
|
81
|
-
ArrowRight: "ArrowLeft"
|
|
82
|
-
};
|
|
83
|
-
var sameKeyMap = {
|
|
84
|
-
Up: "ArrowUp",
|
|
85
|
-
Down: "ArrowDown",
|
|
86
|
-
Esc: "Escape",
|
|
87
|
-
" ": "Space",
|
|
88
|
-
",": "Comma",
|
|
89
|
-
Left: "ArrowLeft",
|
|
90
|
-
Right: "ArrowRight"
|
|
91
|
-
};
|
|
92
|
-
function getEventKey(event, options = {}) {
|
|
93
|
-
const { dir = "ltr", orientation = "horizontal" } = options;
|
|
94
|
-
let { key } = event;
|
|
95
|
-
key = sameKeyMap[key] ?? key;
|
|
96
|
-
const isRtl = dir === "rtl" && orientation === "horizontal";
|
|
97
|
-
if (isRtl && key in rtlKeyMap) {
|
|
98
|
-
key = rtlKeyMap[key];
|
|
99
|
-
}
|
|
100
|
-
return key;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// ../../utilities/dom/src/nodelist.ts
|
|
104
|
-
function queryAll(root, selector) {
|
|
105
|
-
return Array.from(root?.querySelectorAll(selector) ?? []);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// ../../utilities/dom/src/visually-hidden.ts
|
|
109
|
-
var visuallyHiddenStyle = {
|
|
110
|
-
border: "0",
|
|
111
|
-
clip: "rect(0 0 0 0)",
|
|
112
|
-
height: "1px",
|
|
113
|
-
margin: "-1px",
|
|
114
|
-
overflow: "hidden",
|
|
115
|
-
padding: "0",
|
|
116
|
-
position: "absolute",
|
|
117
|
-
width: "1px",
|
|
118
|
-
whiteSpace: "nowrap",
|
|
119
|
-
wordWrap: "normal"
|
|
120
|
-
};
|
|
26
|
+
var import_dom_event = require("@zag-js/dom-event");
|
|
27
|
+
var import_dom_query2 = require("@zag-js/dom-query");
|
|
28
|
+
var import_utils = require("@zag-js/utils");
|
|
29
|
+
var import_visually_hidden = require("@zag-js/visually-hidden");
|
|
121
30
|
|
|
122
31
|
// src/pin-input.anatomy.ts
|
|
123
32
|
var import_anatomy = require("@zag-js/anatomy");
|
|
@@ -125,7 +34,8 @@ var anatomy = (0, import_anatomy.createAnatomy)("pinInput").parts("root", "label
|
|
|
125
34
|
var parts = anatomy.build();
|
|
126
35
|
|
|
127
36
|
// src/pin-input.dom.ts
|
|
128
|
-
var
|
|
37
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
38
|
+
var dom = (0, import_dom_query.createScope)({
|
|
129
39
|
getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
|
|
130
40
|
getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
|
|
131
41
|
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
@@ -135,9 +45,10 @@ var dom = defineDomHelpers({
|
|
|
135
45
|
getElements: (ctx) => {
|
|
136
46
|
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
137
47
|
const selector = `input[data-ownedby=${ownerId}]`;
|
|
138
|
-
return queryAll(dom.getRootEl(ctx), selector);
|
|
48
|
+
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
139
49
|
},
|
|
140
|
-
|
|
50
|
+
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
51
|
+
getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
|
|
141
52
|
getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
|
|
142
53
|
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
143
54
|
});
|
|
@@ -152,37 +63,58 @@ function connect(state, send, normalize) {
|
|
|
152
63
|
dom.getFirstInputEl(state.context)?.focus();
|
|
153
64
|
}
|
|
154
65
|
return {
|
|
66
|
+
/**
|
|
67
|
+
* The value of the input as an array of strings.
|
|
68
|
+
*/
|
|
155
69
|
value: state.context.value,
|
|
70
|
+
/**
|
|
71
|
+
* The value of the input as a string.
|
|
72
|
+
*/
|
|
156
73
|
valueAsString: state.context.valueAsString,
|
|
74
|
+
/**
|
|
75
|
+
* Whether all inputs are filled.
|
|
76
|
+
*/
|
|
157
77
|
isValueComplete,
|
|
78
|
+
/**
|
|
79
|
+
* Function to set the value of the inputs.
|
|
80
|
+
*/
|
|
158
81
|
setValue(value) {
|
|
159
82
|
if (!Array.isArray(value)) {
|
|
160
|
-
invariant("[pin-input/setValue] value must be an array");
|
|
83
|
+
(0, import_utils.invariant)("[pin-input/setValue] value must be an array");
|
|
161
84
|
}
|
|
162
85
|
send({ type: "SET_VALUE", value });
|
|
163
86
|
},
|
|
87
|
+
/**
|
|
88
|
+
* Function to clear the value of the inputs.
|
|
89
|
+
*/
|
|
164
90
|
clearValue() {
|
|
165
91
|
send({ type: "CLEAR_VALUE" });
|
|
166
92
|
},
|
|
93
|
+
/**
|
|
94
|
+
* Function to set the value of the input at a specific index.
|
|
95
|
+
*/
|
|
167
96
|
setValueAtIndex(index, value) {
|
|
168
97
|
send({ type: "SET_VALUE", value, index });
|
|
169
98
|
},
|
|
99
|
+
/**
|
|
100
|
+
* Function to focus the pin-input. This will focus the first input.
|
|
101
|
+
*/
|
|
170
102
|
focus,
|
|
171
103
|
rootProps: normalize.element({
|
|
172
104
|
dir: state.context.dir,
|
|
173
105
|
...parts.root.attrs,
|
|
174
106
|
id: dom.getRootId(state.context),
|
|
175
|
-
"data-invalid": dataAttr(isInvalid),
|
|
176
|
-
"data-disabled": dataAttr(state.context.disabled),
|
|
177
|
-
"data-complete": dataAttr(isValueComplete)
|
|
107
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(isInvalid),
|
|
108
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
109
|
+
"data-complete": (0, import_dom_query2.dataAttr)(isValueComplete)
|
|
178
110
|
}),
|
|
179
111
|
labelProps: normalize.label({
|
|
180
112
|
...parts.label.attrs,
|
|
181
113
|
htmlFor: dom.getHiddenInputId(state.context),
|
|
182
114
|
id: dom.getLabelId(state.context),
|
|
183
|
-
"data-invalid": dataAttr(isInvalid),
|
|
184
|
-
"data-disabled": dataAttr(state.context.disabled),
|
|
185
|
-
"data-complete": dataAttr(isValueComplete),
|
|
115
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(isInvalid),
|
|
116
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
117
|
+
"data-complete": (0, import_dom_query2.dataAttr)(isValueComplete),
|
|
186
118
|
onClick: (event) => {
|
|
187
119
|
event.preventDefault();
|
|
188
120
|
focus();
|
|
@@ -196,7 +128,7 @@ function connect(state, send, normalize) {
|
|
|
196
128
|
id: dom.getHiddenInputId(state.context),
|
|
197
129
|
name: state.context.name,
|
|
198
130
|
form: state.context.form,
|
|
199
|
-
style: visuallyHiddenStyle,
|
|
131
|
+
style: import_visually_hidden.visuallyHiddenStyle,
|
|
200
132
|
maxLength: state.context.valueLength,
|
|
201
133
|
defaultValue: state.context.valueAsString
|
|
202
134
|
}),
|
|
@@ -209,21 +141,21 @@ function connect(state, send, normalize) {
|
|
|
209
141
|
return normalize.input({
|
|
210
142
|
...parts.input.attrs,
|
|
211
143
|
disabled: state.context.disabled,
|
|
212
|
-
"data-disabled": dataAttr(state.context.disabled),
|
|
213
|
-
"data-complete": dataAttr(isValueComplete),
|
|
144
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
145
|
+
"data-complete": (0, import_dom_query2.dataAttr)(isValueComplete),
|
|
214
146
|
id: dom.getInputId(state.context, index.toString()),
|
|
215
147
|
"data-ownedby": dom.getRootId(state.context),
|
|
216
148
|
"aria-label": translations.inputLabel(index, state.context.valueLength),
|
|
217
149
|
inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
|
|
218
|
-
"aria-invalid": ariaAttr(isInvalid),
|
|
219
|
-
"data-invalid": dataAttr(isInvalid),
|
|
150
|
+
"aria-invalid": (0, import_dom_query2.ariaAttr)(isInvalid),
|
|
151
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(isInvalid),
|
|
220
152
|
type: state.context.mask ? "password" : inputType,
|
|
221
153
|
defaultValue: state.context.value[index] || "",
|
|
222
154
|
autoCapitalize: "none",
|
|
223
155
|
autoComplete: state.context.otp ? "one-time-code" : "off",
|
|
224
156
|
placeholder: focusedIndex === index ? "" : state.context.placeholder,
|
|
225
157
|
onChange(event) {
|
|
226
|
-
const evt = getNativeEvent(event);
|
|
158
|
+
const evt = (0, import_dom_event.getNativeEvent)(event);
|
|
227
159
|
const { value } = event.currentTarget;
|
|
228
160
|
if (evt.inputType === "insertFromPaste" || value.length > 2) {
|
|
229
161
|
send({ type: "PASTE", value });
|
|
@@ -234,11 +166,11 @@ function connect(state, send, normalize) {
|
|
|
234
166
|
send("BACKSPACE");
|
|
235
167
|
return;
|
|
236
168
|
}
|
|
237
|
-
send({ type: "INPUT", value });
|
|
169
|
+
send({ type: "INPUT", value, index });
|
|
238
170
|
},
|
|
239
171
|
onKeyDown(event) {
|
|
240
|
-
const evt = getNativeEvent(event);
|
|
241
|
-
if (evt.isComposing || isModifiedEvent(evt))
|
|
172
|
+
const evt = (0, import_dom_event.getNativeEvent)(event);
|
|
173
|
+
if (evt.isComposing || (0, import_dom_event.isModifiedEvent)(evt))
|
|
242
174
|
return;
|
|
243
175
|
const keyMap = {
|
|
244
176
|
Backspace() {
|
|
@@ -257,7 +189,7 @@ function connect(state, send, normalize) {
|
|
|
257
189
|
send("ENTER");
|
|
258
190
|
}
|
|
259
191
|
};
|
|
260
|
-
const key = getEventKey(event, { dir: state.context.dir });
|
|
192
|
+
const key = (0, import_dom_event.getEventKey)(event, { dir: state.context.dir });
|
|
261
193
|
const exec = keyMap[key];
|
|
262
194
|
if (exec) {
|
|
263
195
|
exec(event);
|
package/dist/pin-input.dom.d.ts
CHANGED
|
@@ -15,9 +15,12 @@ declare const dom: {
|
|
|
15
15
|
getActiveElement: (ctx: {
|
|
16
16
|
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
17
17
|
}) => HTMLElement | null;
|
|
18
|
-
getById: <T = HTMLElement>(ctx: {
|
|
18
|
+
getById: <T extends HTMLElement = HTMLElement>(ctx: {
|
|
19
19
|
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
20
20
|
}, id: string) => T | null;
|
|
21
|
+
queryById: <T_1 extends HTMLElement = HTMLElement>(ctx: {
|
|
22
|
+
getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
|
|
23
|
+
}, id: string) => T_1;
|
|
21
24
|
} & {
|
|
22
25
|
getRootId: (ctx: MachineContext) => string;
|
|
23
26
|
getInputId: (ctx: MachineContext, id: string) => string;
|
|
@@ -26,7 +29,8 @@ declare const dom: {
|
|
|
26
29
|
getControlId: (ctx: MachineContext) => string;
|
|
27
30
|
getRootEl: (ctx: MachineContext) => HTMLElement | null;
|
|
28
31
|
getElements: (ctx: MachineContext) => HTMLInputElement[];
|
|
29
|
-
|
|
32
|
+
getInputEl: (ctx: MachineContext, id: string) => HTMLInputElement | null;
|
|
33
|
+
getFocusedInputEl: (ctx: MachineContext) => HTMLInputElement;
|
|
30
34
|
getFirstInputEl: (ctx: MachineContext) => HTMLInputElement;
|
|
31
35
|
getHiddenInputEl: (ctx: MachineContext) => HTMLInputElement | null;
|
|
32
36
|
};
|
package/dist/pin-input.dom.js
CHANGED
|
@@ -23,42 +23,8 @@ __export(pin_input_dom_exports, {
|
|
|
23
23
|
dom: () => dom
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(pin_input_dom_exports);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
function isDocument(el) {
|
|
29
|
-
return el.nodeType === Node.DOCUMENT_NODE;
|
|
30
|
-
}
|
|
31
|
-
function isWindow(value) {
|
|
32
|
-
return value?.toString() === "[object Window]";
|
|
33
|
-
}
|
|
34
|
-
function getDocument(el) {
|
|
35
|
-
if (isWindow(el))
|
|
36
|
-
return el.document;
|
|
37
|
-
if (isDocument(el))
|
|
38
|
-
return el;
|
|
39
|
-
return el?.ownerDocument ?? document;
|
|
40
|
-
}
|
|
41
|
-
function defineDomHelpers(helpers) {
|
|
42
|
-
const dom2 = {
|
|
43
|
-
getRootNode: (ctx) => ctx.getRootNode?.() ?? document,
|
|
44
|
-
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
45
|
-
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
46
|
-
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
47
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
48
|
-
};
|
|
49
|
-
return {
|
|
50
|
-
...dom2,
|
|
51
|
-
...helpers
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// ../../utilities/dom/src/nodelist.ts
|
|
56
|
-
function queryAll(root, selector) {
|
|
57
|
-
return Array.from(root?.querySelectorAll(selector) ?? []);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// src/pin-input.dom.ts
|
|
61
|
-
var dom = defineDomHelpers({
|
|
26
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
27
|
+
var dom = (0, import_dom_query.createScope)({
|
|
62
28
|
getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
|
|
63
29
|
getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
|
|
64
30
|
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
@@ -68,9 +34,10 @@ var dom = defineDomHelpers({
|
|
|
68
34
|
getElements: (ctx) => {
|
|
69
35
|
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
70
36
|
const selector = `input[data-ownedby=${ownerId}]`;
|
|
71
|
-
return queryAll(dom.getRootEl(ctx), selector);
|
|
37
|
+
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
72
38
|
},
|
|
73
|
-
|
|
39
|
+
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
40
|
+
getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
|
|
74
41
|
getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
|
|
75
42
|
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
76
43
|
});
|
package/dist/pin-input.dom.mjs
CHANGED
|
@@ -24,84 +24,13 @@ __export(pin_input_machine_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(pin_input_machine_exports);
|
|
26
26
|
var import_core = require("@zag-js/core");
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
|
|
31
|
-
|
|
32
|
-
// ../../utilities/core/src/object.ts
|
|
33
|
-
function compact(obj) {
|
|
34
|
-
if (obj === void 0)
|
|
35
|
-
return obj;
|
|
36
|
-
return Object.fromEntries(
|
|
37
|
-
Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// ../../utilities/dom/src/query.ts
|
|
42
|
-
function isDocument(el) {
|
|
43
|
-
return el.nodeType === Node.DOCUMENT_NODE;
|
|
44
|
-
}
|
|
45
|
-
function isWindow(value) {
|
|
46
|
-
return value?.toString() === "[object Window]";
|
|
47
|
-
}
|
|
48
|
-
function getDocument(el) {
|
|
49
|
-
if (isWindow(el))
|
|
50
|
-
return el.document;
|
|
51
|
-
if (isDocument(el))
|
|
52
|
-
return el;
|
|
53
|
-
return el?.ownerDocument ?? document;
|
|
54
|
-
}
|
|
55
|
-
function getWindow(el) {
|
|
56
|
-
return el?.ownerDocument.defaultView ?? window;
|
|
57
|
-
}
|
|
58
|
-
function defineDomHelpers(helpers) {
|
|
59
|
-
const dom2 = {
|
|
60
|
-
getRootNode: (ctx) => ctx.getRootNode?.() ?? document,
|
|
61
|
-
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
|
|
62
|
-
getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
|
|
63
|
-
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
|
|
64
|
-
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
...dom2,
|
|
68
|
-
...helpers
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// ../../utilities/dom/src/next-tick.ts
|
|
73
|
-
function raf(fn) {
|
|
74
|
-
const id = globalThis.requestAnimationFrame(fn);
|
|
75
|
-
return function cleanup() {
|
|
76
|
-
globalThis.cancelAnimationFrame(id);
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ../../utilities/dom/src/nodelist.ts
|
|
81
|
-
function queryAll(root, selector) {
|
|
82
|
-
return Array.from(root?.querySelectorAll(selector) ?? []);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ../../utilities/form-utils/src/input-event.ts
|
|
86
|
-
function getDescriptor(el, options) {
|
|
87
|
-
const { type, property = "value" } = options;
|
|
88
|
-
const proto = getWindow(el)[type].prototype;
|
|
89
|
-
return Object.getOwnPropertyDescriptor(proto, property) ?? {};
|
|
90
|
-
}
|
|
91
|
-
function dispatchInputValueEvent(el, value) {
|
|
92
|
-
if (!el)
|
|
93
|
-
return;
|
|
94
|
-
const win = getWindow(el);
|
|
95
|
-
if (!(el instanceof win.HTMLInputElement))
|
|
96
|
-
return;
|
|
97
|
-
const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
|
|
98
|
-
desc.set?.call(el, value);
|
|
99
|
-
const event = new win.Event("input", { bubbles: true });
|
|
100
|
-
el.dispatchEvent(event);
|
|
101
|
-
}
|
|
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");
|
|
102
30
|
|
|
103
31
|
// src/pin-input.dom.ts
|
|
104
|
-
var
|
|
32
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
33
|
+
var dom = (0, import_dom_query.createScope)({
|
|
105
34
|
getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
|
|
106
35
|
getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
|
|
107
36
|
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
@@ -111,9 +40,10 @@ var dom = defineDomHelpers({
|
|
|
111
40
|
getElements: (ctx) => {
|
|
112
41
|
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
113
42
|
const selector = `input[data-ownedby=${ownerId}]`;
|
|
114
|
-
return queryAll(dom.getRootEl(ctx), selector);
|
|
43
|
+
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
115
44
|
},
|
|
116
|
-
|
|
45
|
+
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
46
|
+
getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
|
|
117
47
|
getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
|
|
118
48
|
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
119
49
|
});
|
|
@@ -121,7 +51,7 @@ var dom = defineDomHelpers({
|
|
|
121
51
|
// src/pin-input.machine.ts
|
|
122
52
|
var { and, not } = import_core.guards;
|
|
123
53
|
function machine(userContext) {
|
|
124
|
-
const ctx = compact(userContext);
|
|
54
|
+
const ctx = (0, import_utils.compact)(userContext);
|
|
125
55
|
return (0, import_core.createMachine)(
|
|
126
56
|
{
|
|
127
57
|
id: "pin-input",
|
|
@@ -147,7 +77,7 @@ function machine(userContext) {
|
|
|
147
77
|
},
|
|
148
78
|
watch: {
|
|
149
79
|
focusedIndex: ["focusInput", "setInputSelection"],
|
|
150
|
-
value: ["dispatchInputEvent"],
|
|
80
|
+
value: ["dispatchInputEvent", "syncInputElements"],
|
|
151
81
|
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
152
82
|
},
|
|
153
83
|
entry: ctx.autoFocus ? ["setupValue", "setFocusIndexToFirst"] : ["setupValue"],
|
|
@@ -258,17 +188,17 @@ function machine(userContext) {
|
|
|
258
188
|
assign(ctx2, emptyValues);
|
|
259
189
|
},
|
|
260
190
|
focusInput: (ctx2) => {
|
|
261
|
-
raf(() => {
|
|
191
|
+
(0, import_dom_query2.raf)(() => {
|
|
262
192
|
if (ctx2.focusedIndex === -1)
|
|
263
193
|
return;
|
|
264
|
-
dom.
|
|
194
|
+
dom.getFocusedInputEl(ctx2)?.focus();
|
|
265
195
|
});
|
|
266
196
|
},
|
|
267
197
|
setInputSelection: (ctx2) => {
|
|
268
|
-
raf(() => {
|
|
198
|
+
(0, import_dom_query2.raf)(() => {
|
|
269
199
|
if (ctx2.focusedIndex === -1)
|
|
270
200
|
return;
|
|
271
|
-
const input = dom.
|
|
201
|
+
const input = dom.getFocusedInputEl(ctx2);
|
|
272
202
|
const length = input.value.length;
|
|
273
203
|
input.selectionStart = ctx2.selectOnFocus ? 0 : length;
|
|
274
204
|
input.selectionEnd = length;
|
|
@@ -279,20 +209,11 @@ function machine(userContext) {
|
|
|
279
209
|
return;
|
|
280
210
|
ctx2.onComplete?.({ value: Array.from(ctx2.value), valueAsString: ctx2.valueAsString });
|
|
281
211
|
},
|
|
282
|
-
invokeOnChange: (ctx2
|
|
283
|
-
if (evt.type === "SETUP")
|
|
284
|
-
return;
|
|
212
|
+
invokeOnChange: (ctx2) => {
|
|
285
213
|
ctx2.onChange?.({ value: Array.from(ctx2.value) });
|
|
286
214
|
},
|
|
287
|
-
dispatchInputEvent: (ctx2
|
|
288
|
-
|
|
289
|
-
return;
|
|
290
|
-
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
|
-
});
|
|
215
|
+
dispatchInputEvent: (ctx2) => {
|
|
216
|
+
(0, import_form_utils.dispatchInputValueEvent)(dom.getHiddenInputEl(ctx2), ctx2.valueAsString);
|
|
296
217
|
},
|
|
297
218
|
invokeOnInvalid: (ctx2, evt) => {
|
|
298
219
|
ctx2.onInvalid?.({ value: evt.value, index: ctx2.focusedIndex });
|
|
@@ -309,17 +230,20 @@ function machine(userContext) {
|
|
|
309
230
|
setFocusedValue: (ctx2, evt) => {
|
|
310
231
|
ctx2.value[ctx2.focusedIndex] = getNextValue(ctx2.focusedValue, evt.value);
|
|
311
232
|
},
|
|
312
|
-
syncInputValue
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
if (evt.value.length <= 1 || changed)
|
|
233
|
+
syncInputValue(ctx2, evt) {
|
|
234
|
+
const input = dom.getInputEl(ctx2, evt.index.toString());
|
|
235
|
+
if (!input)
|
|
316
236
|
return;
|
|
237
|
+
input.value = ctx2.value[evt.index];
|
|
238
|
+
},
|
|
239
|
+
syncInputElements(ctx2) {
|
|
317
240
|
const inputs = dom.getElements(ctx2);
|
|
318
|
-
|
|
319
|
-
|
|
241
|
+
inputs.forEach((input, index) => {
|
|
242
|
+
input.value = ctx2.value[index];
|
|
243
|
+
});
|
|
320
244
|
},
|
|
321
245
|
setPastedValue(ctx2, evt) {
|
|
322
|
-
raf(() => {
|
|
246
|
+
(0, import_dom_query2.raf)(() => {
|
|
323
247
|
const startIndex = ctx2.focusedValue ? 1 : 0;
|
|
324
248
|
const value = evt.value.substring(startIndex, startIndex + ctx2.valueLength);
|
|
325
249
|
assign(ctx2, value);
|
|
@@ -336,7 +260,7 @@ function machine(userContext) {
|
|
|
336
260
|
ctx2.value[ctx2.focusedIndex] = "";
|
|
337
261
|
},
|
|
338
262
|
resetFocusedValue: (ctx2) => {
|
|
339
|
-
const input = dom.
|
|
263
|
+
const input = dom.getFocusedInputEl(ctx2);
|
|
340
264
|
input.value = ctx2.focusedValue;
|
|
341
265
|
},
|
|
342
266
|
setFocusIndexToFirst: (ctx2) => {
|
|
@@ -349,7 +273,7 @@ function machine(userContext) {
|
|
|
349
273
|
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
350
274
|
},
|
|
351
275
|
setLastValueFocusIndex: (ctx2) => {
|
|
352
|
-
raf(() => {
|
|
276
|
+
(0, import_dom_query2.raf)(() => {
|
|
353
277
|
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
354
278
|
});
|
|
355
279
|
},
|
|
@@ -359,8 +283,8 @@ function machine(userContext) {
|
|
|
359
283
|
blurFocusedInputIfNeeded(ctx2) {
|
|
360
284
|
if (!ctx2.blurOnComplete)
|
|
361
285
|
return;
|
|
362
|
-
raf(() => {
|
|
363
|
-
dom.
|
|
286
|
+
(0, import_dom_query2.raf)(() => {
|
|
287
|
+
dom.getFocusedInputEl(ctx2)?.blur();
|
|
364
288
|
});
|
|
365
289
|
},
|
|
366
290
|
requestFormSubmit(ctx2) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "Core logic for the pin-input widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -27,14 +27,16 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@zag-js/anatomy": "0.1.4",
|
|
30
|
-
"@zag-js/
|
|
31
|
-
"@zag-js/
|
|
30
|
+
"@zag-js/dom-query": "0.1.3",
|
|
31
|
+
"@zag-js/dom-event": "0.0.1",
|
|
32
|
+
"@zag-js/core": "0.2.10",
|
|
33
|
+
"@zag-js/types": "0.3.4",
|
|
34
|
+
"@zag-js/visually-hidden": "0.0.1",
|
|
35
|
+
"@zag-js/form-utils": "0.2.5",
|
|
36
|
+
"@zag-js/utils": "0.3.3"
|
|
32
37
|
},
|
|
33
38
|
"devDependencies": {
|
|
34
|
-
"clean-package": "2.2.0"
|
|
35
|
-
"@zag-js/dom-utils": "0.2.4",
|
|
36
|
-
"@zag-js/form-utils": "0.2.4",
|
|
37
|
-
"@zag-js/utils": "0.3.3"
|
|
39
|
+
"clean-package": "2.2.0"
|
|
38
40
|
},
|
|
39
41
|
"clean-package": "../../../clean-package.config.json",
|
|
40
42
|
"main": "dist/index.js",
|