@zag-js/pin-input 1.41.1 → 2.0.0-next.0
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/pin-input.connect.js +7 -10
- package/dist/pin-input.connect.mjs +7 -10
- package/dist/pin-input.dom.d.mts +1 -2
- package/dist/pin-input.dom.d.ts +1 -2
- package/dist/pin-input.dom.js +8 -13
- package/dist/pin-input.dom.mjs +8 -12
- package/dist/pin-input.machine.js +9 -18
- package/dist/pin-input.machine.mjs +10 -19
- package/dist/pin-input.types.d.mts +3 -5
- package/dist/pin-input.types.d.ts +3 -5
- package/package.json +6 -6
|
@@ -52,8 +52,8 @@ function connect(service, normalize) {
|
|
|
52
52
|
}
|
|
53
53
|
return {
|
|
54
54
|
focus,
|
|
55
|
-
count:
|
|
56
|
-
items: Array.from({ length:
|
|
55
|
+
count: prop("count"),
|
|
56
|
+
items: Array.from({ length: prop("count") }).map((_, i) => i),
|
|
57
57
|
value: context.get("value"),
|
|
58
58
|
valueAsString: computed("valueAsString"),
|
|
59
59
|
complete,
|
|
@@ -72,8 +72,7 @@ function connect(service, normalize) {
|
|
|
72
72
|
getRootProps() {
|
|
73
73
|
return normalize.element({
|
|
74
74
|
dir: prop("dir"),
|
|
75
|
-
...import_pin_input.parts.root.attrs,
|
|
76
|
-
id: dom.getRootId(scope),
|
|
75
|
+
...import_pin_input.parts.root.attrs(scope.id),
|
|
77
76
|
"data-invalid": (0, import_dom_query.dataAttr)(invalid),
|
|
78
77
|
"data-disabled": (0, import_dom_query.dataAttr)(disabled),
|
|
79
78
|
"data-complete": (0, import_dom_query.dataAttr)(complete),
|
|
@@ -82,7 +81,7 @@ function connect(service, normalize) {
|
|
|
82
81
|
},
|
|
83
82
|
getLabelProps() {
|
|
84
83
|
return normalize.label({
|
|
85
|
-
...import_pin_input.parts.label.attrs,
|
|
84
|
+
...import_pin_input.parts.label.attrs(scope.id),
|
|
86
85
|
dir: prop("dir"),
|
|
87
86
|
htmlFor: dom.getHiddenInputId(scope),
|
|
88
87
|
id: dom.getLabelId(scope),
|
|
@@ -115,9 +114,8 @@ function connect(service, normalize) {
|
|
|
115
114
|
},
|
|
116
115
|
getControlProps() {
|
|
117
116
|
return normalize.element({
|
|
118
|
-
...import_pin_input.parts.control.attrs,
|
|
119
|
-
dir: prop("dir")
|
|
120
|
-
id: dom.getControlId(scope)
|
|
117
|
+
...import_pin_input.parts.control.attrs(scope.id),
|
|
118
|
+
dir: prop("dir")
|
|
121
119
|
});
|
|
122
120
|
},
|
|
123
121
|
getInputProps(props) {
|
|
@@ -126,7 +124,7 @@ function connect(service, normalize) {
|
|
|
126
124
|
const valueLength = computed("valueLength");
|
|
127
125
|
const tabbableIndex = focusedIndex !== -1 ? focusedIndex : Math.min(computed("filledValueLength"), valueLength - 1);
|
|
128
126
|
return normalize.input({
|
|
129
|
-
...import_pin_input.parts.input.attrs,
|
|
127
|
+
...import_pin_input.parts.input.attrs(scope.id),
|
|
130
128
|
dir: prop("dir"),
|
|
131
129
|
disabled,
|
|
132
130
|
tabIndex: index === tabbableIndex ? 0 : -1,
|
|
@@ -135,7 +133,6 @@ function connect(service, normalize) {
|
|
|
135
133
|
"data-filled": (0, import_dom_query.dataAttr)(context.get("value")[index] !== ""),
|
|
136
134
|
id: dom.getInputId(scope, index.toString()),
|
|
137
135
|
"data-index": index,
|
|
138
|
-
"data-ownedby": dom.getRootId(scope),
|
|
139
136
|
"aria-label": translations?.inputLabel?.(index, computed("valueLength")),
|
|
140
137
|
inputMode: prop("otp") || prop("type") === "numeric" ? "numeric" : "text",
|
|
141
138
|
"aria-invalid": (0, import_dom_query.ariaAttr)(invalid),
|
|
@@ -28,8 +28,8 @@ function connect(service, normalize) {
|
|
|
28
28
|
}
|
|
29
29
|
return {
|
|
30
30
|
focus,
|
|
31
|
-
count:
|
|
32
|
-
items: Array.from({ length:
|
|
31
|
+
count: prop("count"),
|
|
32
|
+
items: Array.from({ length: prop("count") }).map((_, i) => i),
|
|
33
33
|
value: context.get("value"),
|
|
34
34
|
valueAsString: computed("valueAsString"),
|
|
35
35
|
complete,
|
|
@@ -48,8 +48,7 @@ function connect(service, normalize) {
|
|
|
48
48
|
getRootProps() {
|
|
49
49
|
return normalize.element({
|
|
50
50
|
dir: prop("dir"),
|
|
51
|
-
...parts.root.attrs,
|
|
52
|
-
id: dom.getRootId(scope),
|
|
51
|
+
...parts.root.attrs(scope.id),
|
|
53
52
|
"data-invalid": dataAttr(invalid),
|
|
54
53
|
"data-disabled": dataAttr(disabled),
|
|
55
54
|
"data-complete": dataAttr(complete),
|
|
@@ -58,7 +57,7 @@ function connect(service, normalize) {
|
|
|
58
57
|
},
|
|
59
58
|
getLabelProps() {
|
|
60
59
|
return normalize.label({
|
|
61
|
-
...parts.label.attrs,
|
|
60
|
+
...parts.label.attrs(scope.id),
|
|
62
61
|
dir: prop("dir"),
|
|
63
62
|
htmlFor: dom.getHiddenInputId(scope),
|
|
64
63
|
id: dom.getLabelId(scope),
|
|
@@ -91,9 +90,8 @@ function connect(service, normalize) {
|
|
|
91
90
|
},
|
|
92
91
|
getControlProps() {
|
|
93
92
|
return normalize.element({
|
|
94
|
-
...parts.control.attrs,
|
|
95
|
-
dir: prop("dir")
|
|
96
|
-
id: dom.getControlId(scope)
|
|
93
|
+
...parts.control.attrs(scope.id),
|
|
94
|
+
dir: prop("dir")
|
|
97
95
|
});
|
|
98
96
|
},
|
|
99
97
|
getInputProps(props) {
|
|
@@ -102,7 +100,7 @@ function connect(service, normalize) {
|
|
|
102
100
|
const valueLength = computed("valueLength");
|
|
103
101
|
const tabbableIndex = focusedIndex !== -1 ? focusedIndex : Math.min(computed("filledValueLength"), valueLength - 1);
|
|
104
102
|
return normalize.input({
|
|
105
|
-
...parts.input.attrs,
|
|
103
|
+
...parts.input.attrs(scope.id),
|
|
106
104
|
dir: prop("dir"),
|
|
107
105
|
disabled,
|
|
108
106
|
tabIndex: index === tabbableIndex ? 0 : -1,
|
|
@@ -111,7 +109,6 @@ function connect(service, normalize) {
|
|
|
111
109
|
"data-filled": dataAttr(context.get("value")[index] !== ""),
|
|
112
110
|
id: dom.getInputId(scope, index.toString()),
|
|
113
111
|
"data-index": index,
|
|
114
|
-
"data-ownedby": dom.getRootId(scope),
|
|
115
112
|
"aria-label": translations?.inputLabel?.(index, computed("valueLength")),
|
|
116
113
|
inputMode: prop("otp") || prop("type") === "numeric" ? "numeric" : "text",
|
|
117
114
|
"aria-invalid": ariaAttr(invalid),
|
package/dist/pin-input.dom.d.mts
CHANGED
|
@@ -7,10 +7,9 @@ declare const getLabelId: (ctx: Scope) => any;
|
|
|
7
7
|
declare const getControlId: (ctx: Scope) => any;
|
|
8
8
|
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
9
9
|
declare const getInputEls: (ctx: Scope) => HTMLInputElement[];
|
|
10
|
-
declare const getInputEl: (ctx: Scope, id: string) => HTMLInputElement | null;
|
|
11
10
|
declare const getInputElAtIndex: (ctx: Scope, index: number) => HTMLInputElement;
|
|
12
11
|
declare const getFirstInputEl: (ctx: Scope) => HTMLInputElement;
|
|
13
12
|
declare const getHiddenInputEl: (ctx: Scope) => HTMLInputElement | null;
|
|
14
13
|
declare const setInputValue: (inputEl: HTMLInputElement, value: string) => void;
|
|
15
14
|
|
|
16
|
-
export { getControlId, getFirstInputEl, getHiddenInputEl, getHiddenInputId,
|
|
15
|
+
export { getControlId, getFirstInputEl, getHiddenInputEl, getHiddenInputId, getInputElAtIndex, getInputEls, getInputId, getLabelId, getRootEl, getRootId, setInputValue };
|
package/dist/pin-input.dom.d.ts
CHANGED
|
@@ -7,10 +7,9 @@ declare const getLabelId: (ctx: Scope) => any;
|
|
|
7
7
|
declare const getControlId: (ctx: Scope) => any;
|
|
8
8
|
declare const getRootEl: (ctx: Scope) => HTMLElement | null;
|
|
9
9
|
declare const getInputEls: (ctx: Scope) => HTMLInputElement[];
|
|
10
|
-
declare const getInputEl: (ctx: Scope, id: string) => HTMLInputElement | null;
|
|
11
10
|
declare const getInputElAtIndex: (ctx: Scope, index: number) => HTMLInputElement;
|
|
12
11
|
declare const getFirstInputEl: (ctx: Scope) => HTMLInputElement;
|
|
13
12
|
declare const getHiddenInputEl: (ctx: Scope) => HTMLInputElement | null;
|
|
14
13
|
declare const setInputValue: (inputEl: HTMLInputElement, value: string) => void;
|
|
15
14
|
|
|
16
|
-
export { getControlId, getFirstInputEl, getHiddenInputEl, getHiddenInputId,
|
|
15
|
+
export { getControlId, getFirstInputEl, getHiddenInputEl, getHiddenInputId, getInputElAtIndex, getInputEls, getInputId, getLabelId, getRootEl, getRootId, setInputValue };
|
package/dist/pin-input.dom.js
CHANGED
|
@@ -24,7 +24,6 @@ __export(pin_input_dom_exports, {
|
|
|
24
24
|
getFirstInputEl: () => getFirstInputEl,
|
|
25
25
|
getHiddenInputEl: () => getHiddenInputEl,
|
|
26
26
|
getHiddenInputId: () => getHiddenInputId,
|
|
27
|
-
getInputEl: () => getInputEl,
|
|
28
27
|
getInputElAtIndex: () => getInputElAtIndex,
|
|
29
28
|
getInputEls: () => getInputEls,
|
|
30
29
|
getInputId: () => getInputId,
|
|
@@ -34,19 +33,16 @@ __export(pin_input_dom_exports, {
|
|
|
34
33
|
setInputValue: () => setInputValue
|
|
35
34
|
});
|
|
36
35
|
module.exports = __toCommonJS(pin_input_dom_exports);
|
|
37
|
-
var
|
|
38
|
-
var getRootId = (ctx) => ctx.ids?.root ??
|
|
39
|
-
var getInputId = (ctx, id) => ctx.ids?.input?.(id) ??
|
|
40
|
-
var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ??
|
|
41
|
-
var getLabelId = (ctx) => ctx.ids?.label ??
|
|
42
|
-
var getControlId = (ctx) => ctx.ids?.control ??
|
|
43
|
-
var getRootEl = (ctx) => ctx.
|
|
36
|
+
var import_pin_input = require("./pin-input.anatomy.js");
|
|
37
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `${ctx.id}`;
|
|
38
|
+
var getInputId = (ctx, id) => ctx.ids?.input?.(id) ?? `${ctx.id}:${id}`;
|
|
39
|
+
var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ?? `${ctx.id}:hidden`;
|
|
40
|
+
var getLabelId = (ctx) => ctx.ids?.label ?? `${ctx.id}:label`;
|
|
41
|
+
var getControlId = (ctx) => ctx.ids?.control ?? `${ctx.id}:control`;
|
|
42
|
+
var getRootEl = (ctx) => ctx.query(ctx.selector(import_pin_input.parts.root));
|
|
44
43
|
var getInputEls = (ctx) => {
|
|
45
|
-
|
|
46
|
-
const selector = `input[data-ownedby=${ownerId}]`;
|
|
47
|
-
return (0, import_dom_query.queryAll)(getRootEl(ctx), selector);
|
|
44
|
+
return ctx.queryAll(ctx.selector(import_pin_input.parts.input));
|
|
48
45
|
};
|
|
49
|
-
var getInputEl = (ctx, id) => ctx.getById(getInputId(ctx, id));
|
|
50
46
|
var getInputElAtIndex = (ctx, index) => getInputEls(ctx)[index];
|
|
51
47
|
var getFirstInputEl = (ctx) => getInputEls(ctx)[0];
|
|
52
48
|
var getHiddenInputEl = (ctx) => ctx.getById(getHiddenInputId(ctx));
|
|
@@ -60,7 +56,6 @@ var setInputValue = (inputEl, value) => {
|
|
|
60
56
|
getFirstInputEl,
|
|
61
57
|
getHiddenInputEl,
|
|
62
58
|
getHiddenInputId,
|
|
63
|
-
getInputEl,
|
|
64
59
|
getInputElAtIndex,
|
|
65
60
|
getInputEls,
|
|
66
61
|
getInputId,
|
package/dist/pin-input.dom.mjs
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
// src/pin-input.dom.ts
|
|
2
|
-
import {
|
|
3
|
-
var getRootId = (ctx) => ctx.ids?.root ??
|
|
4
|
-
var getInputId = (ctx, id) => ctx.ids?.input?.(id) ??
|
|
5
|
-
var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ??
|
|
6
|
-
var getLabelId = (ctx) => ctx.ids?.label ??
|
|
7
|
-
var getControlId = (ctx) => ctx.ids?.control ??
|
|
8
|
-
var getRootEl = (ctx) => ctx.
|
|
2
|
+
import { parts } from "./pin-input.anatomy.mjs";
|
|
3
|
+
var getRootId = (ctx) => ctx.ids?.root ?? `${ctx.id}`;
|
|
4
|
+
var getInputId = (ctx, id) => ctx.ids?.input?.(id) ?? `${ctx.id}:${id}`;
|
|
5
|
+
var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ?? `${ctx.id}:hidden`;
|
|
6
|
+
var getLabelId = (ctx) => ctx.ids?.label ?? `${ctx.id}:label`;
|
|
7
|
+
var getControlId = (ctx) => ctx.ids?.control ?? `${ctx.id}:control`;
|
|
8
|
+
var getRootEl = (ctx) => ctx.query(ctx.selector(parts.root));
|
|
9
9
|
var getInputEls = (ctx) => {
|
|
10
|
-
|
|
11
|
-
const selector = `input[data-ownedby=${ownerId}]`;
|
|
12
|
-
return queryAll(getRootEl(ctx), selector);
|
|
10
|
+
return ctx.queryAll(ctx.selector(parts.input));
|
|
13
11
|
};
|
|
14
|
-
var getInputEl = (ctx, id) => ctx.getById(getInputId(ctx, id));
|
|
15
12
|
var getInputElAtIndex = (ctx, index) => getInputEls(ctx)[index];
|
|
16
13
|
var getFirstInputEl = (ctx) => getInputEls(ctx)[0];
|
|
17
14
|
var getHiddenInputEl = (ctx) => ctx.getById(getHiddenInputId(ctx));
|
|
@@ -24,7 +21,6 @@ export {
|
|
|
24
21
|
getFirstInputEl,
|
|
25
22
|
getHiddenInputEl,
|
|
26
23
|
getHiddenInputId,
|
|
27
|
-
getInputEl,
|
|
28
24
|
getInputElAtIndex,
|
|
29
25
|
getInputEls,
|
|
30
26
|
getInputId,
|
|
@@ -40,11 +40,12 @@ var dom = __toESM(require("./pin-input.dom.js"));
|
|
|
40
40
|
var { choose, createMachine } = (0, import_core.setup)();
|
|
41
41
|
var machine = createMachine({
|
|
42
42
|
props({ props }) {
|
|
43
|
+
(0, import_utils.ensureProps)(props, ["count"], "pin-input");
|
|
43
44
|
return {
|
|
44
45
|
placeholder: "\u25CB",
|
|
45
46
|
otp: false,
|
|
46
47
|
type: "numeric",
|
|
47
|
-
defaultValue:
|
|
48
|
+
defaultValue: fill([], props.count),
|
|
48
49
|
...props,
|
|
49
50
|
translations: {
|
|
50
51
|
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
@@ -68,15 +69,11 @@ var machine = createMachine({
|
|
|
68
69
|
focusedIndex: bindable(() => ({
|
|
69
70
|
sync: true,
|
|
70
71
|
defaultValue: -1
|
|
71
|
-
})),
|
|
72
|
-
// TODO: Move this to `props` in next major version
|
|
73
|
-
count: bindable(() => ({
|
|
74
|
-
defaultValue: prop("count")
|
|
75
72
|
}))
|
|
76
73
|
};
|
|
77
74
|
},
|
|
78
75
|
computed: {
|
|
79
|
-
_value: ({ context }) => fill(context.get("value"),
|
|
76
|
+
_value: ({ context, prop }) => fill(context.get("value"), prop("count")),
|
|
80
77
|
valueLength: ({ computed }) => computed("_value").length,
|
|
81
78
|
filledValueLength: ({ computed }) => computed("_value").filter((v) => v?.trim() !== "").length,
|
|
82
79
|
isValueComplete: ({ computed }) => computed("valueLength") === computed("filledValueLength"),
|
|
@@ -86,9 +83,8 @@ var machine = createMachine({
|
|
|
86
83
|
entry: choose([
|
|
87
84
|
{
|
|
88
85
|
guard: "autoFocus",
|
|
89
|
-
actions: ["
|
|
90
|
-
}
|
|
91
|
-
{ actions: ["setInputCount"] }
|
|
86
|
+
actions: ["setFocusIndexToFirst"]
|
|
87
|
+
}
|
|
92
88
|
]),
|
|
93
89
|
watch({ action, track, context, computed }) {
|
|
94
90
|
track([() => context.get("focusedIndex")], () => {
|
|
@@ -187,11 +183,6 @@ var machine = createMachine({
|
|
|
187
183
|
const inputEl = dom.getHiddenInputEl(scope);
|
|
188
184
|
(0, import_dom_query.dispatchInputValueEvent)(inputEl, { value: computed("valueAsString") });
|
|
189
185
|
},
|
|
190
|
-
setInputCount({ scope, context, prop }) {
|
|
191
|
-
if (prop("count")) return;
|
|
192
|
-
const inputEls = dom.getInputEls(scope);
|
|
193
|
-
context.set("count", inputEls.length);
|
|
194
|
-
},
|
|
195
186
|
focusInput({ context, scope }) {
|
|
196
187
|
const focusedIndex = context.get("focusedIndex");
|
|
197
188
|
if (focusedIndex === -1) return;
|
|
@@ -226,8 +217,8 @@ var machine = createMachine({
|
|
|
226
217
|
const maxIndex = Math.min(computed("filledValueLength"), computed("valueLength") - 1);
|
|
227
218
|
context.set("focusedIndex", Math.min(event.index, maxIndex));
|
|
228
219
|
},
|
|
229
|
-
setValue({ context, event }) {
|
|
230
|
-
const value = fill(event.value,
|
|
220
|
+
setValue({ context, event, prop }) {
|
|
221
|
+
const value = fill(event.value, prop("count"));
|
|
231
222
|
context.set("value", value);
|
|
232
223
|
},
|
|
233
224
|
setFocusedValue({ context, event, computed, flush }) {
|
|
@@ -273,8 +264,8 @@ var machine = createMachine({
|
|
|
273
264
|
const nextValue = getNextValue(computed("focusedValue"), event.value);
|
|
274
265
|
context.set("value", (0, import_utils.setValueAtIndex)(computed("_value"), event.index, nextValue));
|
|
275
266
|
},
|
|
276
|
-
clearValue({ context }) {
|
|
277
|
-
const nextValue = Array.from({ length:
|
|
267
|
+
clearValue({ context, prop }) {
|
|
268
|
+
const nextValue = Array.from({ length: prop("count") }).fill("");
|
|
278
269
|
queueMicrotask(() => {
|
|
279
270
|
context.set("value", nextValue);
|
|
280
271
|
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
// src/pin-input.machine.ts
|
|
2
2
|
import { setup } from "@zag-js/core";
|
|
3
3
|
import { dispatchInputValueEvent, raf } from "@zag-js/dom-query";
|
|
4
|
-
import { isEqual, setValueAtIndex } from "@zag-js/utils";
|
|
4
|
+
import { ensureProps, isEqual, setValueAtIndex } from "@zag-js/utils";
|
|
5
5
|
import * as dom from "./pin-input.dom.mjs";
|
|
6
6
|
var { choose, createMachine } = setup();
|
|
7
7
|
var machine = createMachine({
|
|
8
8
|
props({ props }) {
|
|
9
|
+
ensureProps(props, ["count"], "pin-input");
|
|
9
10
|
return {
|
|
10
11
|
placeholder: "\u25CB",
|
|
11
12
|
otp: false,
|
|
12
13
|
type: "numeric",
|
|
13
|
-
defaultValue:
|
|
14
|
+
defaultValue: fill([], props.count),
|
|
14
15
|
...props,
|
|
15
16
|
translations: {
|
|
16
17
|
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
@@ -34,15 +35,11 @@ var machine = createMachine({
|
|
|
34
35
|
focusedIndex: bindable(() => ({
|
|
35
36
|
sync: true,
|
|
36
37
|
defaultValue: -1
|
|
37
|
-
})),
|
|
38
|
-
// TODO: Move this to `props` in next major version
|
|
39
|
-
count: bindable(() => ({
|
|
40
|
-
defaultValue: prop("count")
|
|
41
38
|
}))
|
|
42
39
|
};
|
|
43
40
|
},
|
|
44
41
|
computed: {
|
|
45
|
-
_value: ({ context }) => fill(context.get("value"),
|
|
42
|
+
_value: ({ context, prop }) => fill(context.get("value"), prop("count")),
|
|
46
43
|
valueLength: ({ computed }) => computed("_value").length,
|
|
47
44
|
filledValueLength: ({ computed }) => computed("_value").filter((v) => v?.trim() !== "").length,
|
|
48
45
|
isValueComplete: ({ computed }) => computed("valueLength") === computed("filledValueLength"),
|
|
@@ -52,9 +49,8 @@ var machine = createMachine({
|
|
|
52
49
|
entry: choose([
|
|
53
50
|
{
|
|
54
51
|
guard: "autoFocus",
|
|
55
|
-
actions: ["
|
|
56
|
-
}
|
|
57
|
-
{ actions: ["setInputCount"] }
|
|
52
|
+
actions: ["setFocusIndexToFirst"]
|
|
53
|
+
}
|
|
58
54
|
]),
|
|
59
55
|
watch({ action, track, context, computed }) {
|
|
60
56
|
track([() => context.get("focusedIndex")], () => {
|
|
@@ -153,11 +149,6 @@ var machine = createMachine({
|
|
|
153
149
|
const inputEl = dom.getHiddenInputEl(scope);
|
|
154
150
|
dispatchInputValueEvent(inputEl, { value: computed("valueAsString") });
|
|
155
151
|
},
|
|
156
|
-
setInputCount({ scope, context, prop }) {
|
|
157
|
-
if (prop("count")) return;
|
|
158
|
-
const inputEls = dom.getInputEls(scope);
|
|
159
|
-
context.set("count", inputEls.length);
|
|
160
|
-
},
|
|
161
152
|
focusInput({ context, scope }) {
|
|
162
153
|
const focusedIndex = context.get("focusedIndex");
|
|
163
154
|
if (focusedIndex === -1) return;
|
|
@@ -192,8 +183,8 @@ var machine = createMachine({
|
|
|
192
183
|
const maxIndex = Math.min(computed("filledValueLength"), computed("valueLength") - 1);
|
|
193
184
|
context.set("focusedIndex", Math.min(event.index, maxIndex));
|
|
194
185
|
},
|
|
195
|
-
setValue({ context, event }) {
|
|
196
|
-
const value = fill(event.value,
|
|
186
|
+
setValue({ context, event, prop }) {
|
|
187
|
+
const value = fill(event.value, prop("count"));
|
|
197
188
|
context.set("value", value);
|
|
198
189
|
},
|
|
199
190
|
setFocusedValue({ context, event, computed, flush }) {
|
|
@@ -239,8 +230,8 @@ var machine = createMachine({
|
|
|
239
230
|
const nextValue = getNextValue(computed("focusedValue"), event.value);
|
|
240
231
|
context.set("value", setValueAtIndex(computed("_value"), event.index, nextValue));
|
|
241
232
|
},
|
|
242
|
-
clearValue({ context }) {
|
|
243
|
-
const nextValue = Array.from({ length:
|
|
233
|
+
clearValue({ context, prop }) {
|
|
234
|
+
const nextValue = Array.from({ length: prop("count") }).fill("");
|
|
244
235
|
queueMicrotask(() => {
|
|
245
236
|
context.set("value", nextValue);
|
|
246
237
|
});
|
|
@@ -119,19 +119,17 @@ interface PinInputProps extends DirectionProperty, CommonProperties {
|
|
|
119
119
|
*/
|
|
120
120
|
translations?: IntlTranslations | undefined;
|
|
121
121
|
/**
|
|
122
|
-
* The number of
|
|
123
|
-
* This will be required in next major version.
|
|
122
|
+
* The number of pin input fields.
|
|
124
123
|
*/
|
|
125
|
-
count
|
|
124
|
+
count: number;
|
|
126
125
|
}
|
|
127
|
-
type PropsWithDefault = "placeholder" | "otp" | "type" | "defaultValue";
|
|
126
|
+
type PropsWithDefault = "placeholder" | "otp" | "type" | "defaultValue" | "count";
|
|
128
127
|
interface PinInputSchema {
|
|
129
128
|
state: "idle" | "focused";
|
|
130
129
|
props: RequiredBy<PinInputProps, PropsWithDefault>;
|
|
131
130
|
context: {
|
|
132
131
|
value: string[];
|
|
133
132
|
focusedIndex: number;
|
|
134
|
-
count: number;
|
|
135
133
|
};
|
|
136
134
|
computed: {
|
|
137
135
|
_value: string[];
|
|
@@ -119,19 +119,17 @@ interface PinInputProps extends DirectionProperty, CommonProperties {
|
|
|
119
119
|
*/
|
|
120
120
|
translations?: IntlTranslations | undefined;
|
|
121
121
|
/**
|
|
122
|
-
* The number of
|
|
123
|
-
* This will be required in next major version.
|
|
122
|
+
* The number of pin input fields.
|
|
124
123
|
*/
|
|
125
|
-
count
|
|
124
|
+
count: number;
|
|
126
125
|
}
|
|
127
|
-
type PropsWithDefault = "placeholder" | "otp" | "type" | "defaultValue";
|
|
126
|
+
type PropsWithDefault = "placeholder" | "otp" | "type" | "defaultValue" | "count";
|
|
128
127
|
interface PinInputSchema {
|
|
129
128
|
state: "idle" | "focused";
|
|
130
129
|
props: RequiredBy<PinInputProps, PropsWithDefault>;
|
|
131
130
|
context: {
|
|
132
131
|
value: string[];
|
|
133
132
|
focusedIndex: number;
|
|
134
|
-
count: number;
|
|
135
133
|
};
|
|
136
134
|
computed: {
|
|
137
135
|
_value: string[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-next.0",
|
|
4
4
|
"description": "Core logic for the pin-input widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@zag-js/anatomy": "
|
|
30
|
-
"@zag-js/dom-query": "
|
|
31
|
-
"@zag-js/utils": "
|
|
32
|
-
"@zag-js/core": "
|
|
33
|
-
"@zag-js/types": "
|
|
29
|
+
"@zag-js/anatomy": "2.0.0-next.0",
|
|
30
|
+
"@zag-js/dom-query": "2.0.0-next.0",
|
|
31
|
+
"@zag-js/utils": "2.0.0-next.0",
|
|
32
|
+
"@zag-js/core": "2.0.0-next.0",
|
|
33
|
+
"@zag-js/types": "2.0.0-next.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"clean-package": "2.2.0"
|