@zag-js/pin-input 0.11.1 → 0.11.2
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 +40 -39
- package/dist/index.d.ts +40 -39
- package/dist/index.js +0 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +0 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +1 -1
- package/src/pin-input.connect.ts +7 -24
- package/src/pin-input.types.ts +37 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import { RequiredBy, DirectionProperty, CommonProperties, Context,
|
|
2
|
+
import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
4
|
import { StateMachine } from '@zag-js/core';
|
|
5
5
|
|
|
@@ -98,43 +98,7 @@ type PublicContext = DirectionProperty & CommonProperties & {
|
|
|
98
98
|
*/
|
|
99
99
|
translations: IntlTranslations;
|
|
100
100
|
};
|
|
101
|
-
type
|
|
102
|
-
type ComputedContext = Readonly<{
|
|
103
|
-
/**
|
|
104
|
-
* @computed
|
|
105
|
-
* The number of inputs
|
|
106
|
-
*/
|
|
107
|
-
valueLength: number;
|
|
108
|
-
/**
|
|
109
|
-
* @computed
|
|
110
|
-
* The number of inputs that are not empty
|
|
111
|
-
*/
|
|
112
|
-
filledValueLength: number;
|
|
113
|
-
/**
|
|
114
|
-
* @computed
|
|
115
|
-
* Whether all input values are valid
|
|
116
|
-
*/
|
|
117
|
-
isValueComplete: boolean;
|
|
118
|
-
/**
|
|
119
|
-
* @computed
|
|
120
|
-
* The string representation of the input values
|
|
121
|
-
*/
|
|
122
|
-
valueAsString: string;
|
|
123
|
-
/**
|
|
124
|
-
* @computed
|
|
125
|
-
* The value at focused index
|
|
126
|
-
*/
|
|
127
|
-
focusedValue: string;
|
|
128
|
-
}>;
|
|
129
|
-
type PrivateContext = Context<{}>;
|
|
130
|
-
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
131
|
-
type MachineState = {
|
|
132
|
-
value: "idle" | "focused";
|
|
133
|
-
};
|
|
134
|
-
type State = StateMachine.State<MachineContext, MachineState>;
|
|
135
|
-
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
136
|
-
|
|
137
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
101
|
+
type PublicApi<T extends PropTypes = PropTypes> = {
|
|
138
102
|
/**
|
|
139
103
|
* The value of the input as an array of strings.
|
|
140
104
|
*/
|
|
@@ -171,7 +135,44 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
|
|
|
171
135
|
index: number;
|
|
172
136
|
}): T["input"];
|
|
173
137
|
};
|
|
138
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
139
|
+
type ComputedContext = Readonly<{
|
|
140
|
+
/**
|
|
141
|
+
* @computed
|
|
142
|
+
* The number of inputs
|
|
143
|
+
*/
|
|
144
|
+
valueLength: number;
|
|
145
|
+
/**
|
|
146
|
+
* @computed
|
|
147
|
+
* The number of inputs that are not empty
|
|
148
|
+
*/
|
|
149
|
+
filledValueLength: number;
|
|
150
|
+
/**
|
|
151
|
+
* @computed
|
|
152
|
+
* Whether all input values are valid
|
|
153
|
+
*/
|
|
154
|
+
isValueComplete: boolean;
|
|
155
|
+
/**
|
|
156
|
+
* @computed
|
|
157
|
+
* The string representation of the input values
|
|
158
|
+
*/
|
|
159
|
+
valueAsString: string;
|
|
160
|
+
/**
|
|
161
|
+
* @computed
|
|
162
|
+
* The value at focused index
|
|
163
|
+
*/
|
|
164
|
+
focusedValue: string;
|
|
165
|
+
}>;
|
|
166
|
+
type PrivateContext = Context<{}>;
|
|
167
|
+
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
168
|
+
type MachineState = {
|
|
169
|
+
value: "idle" | "focused";
|
|
170
|
+
};
|
|
171
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
172
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
173
|
+
|
|
174
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
|
|
174
175
|
|
|
175
176
|
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
176
177
|
|
|
177
|
-
export { UserDefinedContext as Context, anatomy, connect, machine };
|
|
178
|
+
export { UserDefinedContext as Context, PublicApi, anatomy, connect, machine };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
|
-
import { RequiredBy, DirectionProperty, CommonProperties, Context,
|
|
2
|
+
import { PropTypes, RequiredBy, DirectionProperty, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
|
|
3
3
|
import * as _zag_js_core from '@zag-js/core';
|
|
4
4
|
import { StateMachine } from '@zag-js/core';
|
|
5
5
|
|
|
@@ -98,43 +98,7 @@ type PublicContext = DirectionProperty & CommonProperties & {
|
|
|
98
98
|
*/
|
|
99
99
|
translations: IntlTranslations;
|
|
100
100
|
};
|
|
101
|
-
type
|
|
102
|
-
type ComputedContext = Readonly<{
|
|
103
|
-
/**
|
|
104
|
-
* @computed
|
|
105
|
-
* The number of inputs
|
|
106
|
-
*/
|
|
107
|
-
valueLength: number;
|
|
108
|
-
/**
|
|
109
|
-
* @computed
|
|
110
|
-
* The number of inputs that are not empty
|
|
111
|
-
*/
|
|
112
|
-
filledValueLength: number;
|
|
113
|
-
/**
|
|
114
|
-
* @computed
|
|
115
|
-
* Whether all input values are valid
|
|
116
|
-
*/
|
|
117
|
-
isValueComplete: boolean;
|
|
118
|
-
/**
|
|
119
|
-
* @computed
|
|
120
|
-
* The string representation of the input values
|
|
121
|
-
*/
|
|
122
|
-
valueAsString: string;
|
|
123
|
-
/**
|
|
124
|
-
* @computed
|
|
125
|
-
* The value at focused index
|
|
126
|
-
*/
|
|
127
|
-
focusedValue: string;
|
|
128
|
-
}>;
|
|
129
|
-
type PrivateContext = Context<{}>;
|
|
130
|
-
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
131
|
-
type MachineState = {
|
|
132
|
-
value: "idle" | "focused";
|
|
133
|
-
};
|
|
134
|
-
type State = StateMachine.State<MachineContext, MachineState>;
|
|
135
|
-
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
136
|
-
|
|
137
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
|
|
101
|
+
type PublicApi<T extends PropTypes = PropTypes> = {
|
|
138
102
|
/**
|
|
139
103
|
* The value of the input as an array of strings.
|
|
140
104
|
*/
|
|
@@ -171,7 +135,44 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
|
|
|
171
135
|
index: number;
|
|
172
136
|
}): T["input"];
|
|
173
137
|
};
|
|
138
|
+
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
139
|
+
type ComputedContext = Readonly<{
|
|
140
|
+
/**
|
|
141
|
+
* @computed
|
|
142
|
+
* The number of inputs
|
|
143
|
+
*/
|
|
144
|
+
valueLength: number;
|
|
145
|
+
/**
|
|
146
|
+
* @computed
|
|
147
|
+
* The number of inputs that are not empty
|
|
148
|
+
*/
|
|
149
|
+
filledValueLength: number;
|
|
150
|
+
/**
|
|
151
|
+
* @computed
|
|
152
|
+
* Whether all input values are valid
|
|
153
|
+
*/
|
|
154
|
+
isValueComplete: boolean;
|
|
155
|
+
/**
|
|
156
|
+
* @computed
|
|
157
|
+
* The string representation of the input values
|
|
158
|
+
*/
|
|
159
|
+
valueAsString: string;
|
|
160
|
+
/**
|
|
161
|
+
* @computed
|
|
162
|
+
* The value at focused index
|
|
163
|
+
*/
|
|
164
|
+
focusedValue: string;
|
|
165
|
+
}>;
|
|
166
|
+
type PrivateContext = Context<{}>;
|
|
167
|
+
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
168
|
+
type MachineState = {
|
|
169
|
+
value: "idle" | "focused";
|
|
170
|
+
};
|
|
171
|
+
type State = StateMachine.State<MachineContext, MachineState>;
|
|
172
|
+
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
173
|
+
|
|
174
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T>;
|
|
174
175
|
|
|
175
176
|
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
176
177
|
|
|
177
|
-
export { UserDefinedContext as Context, anatomy, connect, machine };
|
|
178
|
+
export { UserDefinedContext as Context, PublicApi, anatomy, connect, machine };
|
package/dist/index.js
CHANGED
|
@@ -67,42 +67,21 @@ function connect(state, send, normalize) {
|
|
|
67
67
|
dom.getFirstInputEl(state.context)?.focus();
|
|
68
68
|
}
|
|
69
69
|
return {
|
|
70
|
-
/**
|
|
71
|
-
* The value of the input as an array of strings.
|
|
72
|
-
*/
|
|
73
70
|
value: state.context.value,
|
|
74
|
-
/**
|
|
75
|
-
* The value of the input as a string.
|
|
76
|
-
*/
|
|
77
71
|
valueAsString: state.context.valueAsString,
|
|
78
|
-
/**
|
|
79
|
-
* Whether all inputs are filled.
|
|
80
|
-
*/
|
|
81
72
|
isValueComplete,
|
|
82
|
-
/**
|
|
83
|
-
* Function to set the value of the inputs.
|
|
84
|
-
*/
|
|
85
73
|
setValue(value) {
|
|
86
74
|
if (!Array.isArray(value)) {
|
|
87
75
|
(0, import_utils.invariant)("[pin-input/setValue] value must be an array");
|
|
88
76
|
}
|
|
89
77
|
send({ type: "SET_VALUE", value });
|
|
90
78
|
},
|
|
91
|
-
/**
|
|
92
|
-
* Function to clear the value of the inputs.
|
|
93
|
-
*/
|
|
94
79
|
clearValue() {
|
|
95
80
|
send({ type: "CLEAR_VALUE" });
|
|
96
81
|
},
|
|
97
|
-
/**
|
|
98
|
-
* Function to set the value of the input at a specific index.
|
|
99
|
-
*/
|
|
100
82
|
setValueAtIndex(index, value) {
|
|
101
83
|
send({ type: "SET_VALUE", value, index });
|
|
102
84
|
},
|
|
103
|
-
/**
|
|
104
|
-
* Function to focus the pin-input. This will focus the first input.
|
|
105
|
-
*/
|
|
106
85
|
focus,
|
|
107
86
|
rootProps: normalize.element({
|
|
108
87
|
dir: state.context.dir,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/pin-input.anatomy.ts","../src/pin-input.connect.ts","../src/pin-input.dom.ts","../src/pin-input.machine.ts"],"sourcesContent":["export { anatomy } from \"./pin-input.anatomy\"\nexport { connect } from \"./pin-input.connect\"\nexport { machine } from \"./pin-input.machine\"\nexport type { UserDefinedContext as Context } from \"./pin-input.types\"\n","import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"pinInput\").parts(\"root\", \"label\", \"hiddenInput\", \"input\", \"control\")\nexport const parts = anatomy.build()\n","import { EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from \"@zag-js/dom-event\"\nimport { ariaAttr, dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { invariant } from \"@zag-js/utils\"\nimport { visuallyHiddenStyle } from \"@zag-js/visually-hidden\"\nimport { parts } from \"./pin-input.anatomy\"\nimport { dom } from \"./pin-input.dom\"\nimport type { Send, State } from \"./pin-input.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>) {\n const isValueComplete = state.context.isValueComplete\n const isInvalid = state.context.invalid\n const focusedIndex = state.context.focusedIndex\n const translations = state.context.translations\n\n function focus() {\n dom.getFirstInputEl(state.context)?.focus()\n }\n\n return {\n /**\n * The value of the input as an array of strings.\n */\n value: state.context.value,\n /**\n * The value of the input as a string.\n */\n valueAsString: state.context.valueAsString,\n /**\n * Whether all inputs are filled.\n */\n isValueComplete: isValueComplete,\n /**\n * Function to set the value of the inputs.\n */\n setValue(value: string[]) {\n if (!Array.isArray(value)) {\n invariant(\"[pin-input/setValue] value must be an array\")\n }\n send({ type: \"SET_VALUE\", value })\n },\n /**\n * Function to clear the value of the inputs.\n */\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n /**\n * Function to set the value of the input at a specific index.\n */\n setValueAtIndex(index: number, value: string) {\n send({ type: \"SET_VALUE\", value, index })\n },\n /**\n * Function to focus the pin-input. This will focus the first input.\n */\n focus,\n\n rootProps: normalize.element({\n dir: state.context.dir,\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n }),\n\n labelProps: normalize.label({\n ...parts.label.attrs,\n htmlFor: dom.getHiddenInputId(state.context),\n id: dom.getLabelId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n onClick: (event) => {\n event.preventDefault()\n focus()\n },\n }),\n\n hiddenInputProps: normalize.input({\n ...parts.hiddenInput.attrs,\n \"aria-hidden\": true,\n type: \"text\",\n tabIndex: -1,\n id: dom.getHiddenInputId(state.context),\n name: state.context.name,\n form: state.context.form,\n style: visuallyHiddenStyle,\n maxLength: state.context.valueLength,\n defaultValue: state.context.valueAsString,\n }),\n\n controlProps: normalize.element({\n ...parts.control.attrs,\n id: dom.getControlId(state.context),\n }),\n\n getInputProps({ index }: { index: number }) {\n const inputType = state.context.type === \"numeric\" ? \"tel\" : \"text\"\n return normalize.input({\n ...parts.input.attrs,\n disabled: state.context.disabled,\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n id: dom.getInputId(state.context, index.toString()),\n \"data-ownedby\": dom.getRootId(state.context),\n \"aria-label\": translations.inputLabel(index, state.context.valueLength),\n inputMode: state.context.otp || state.context.type === \"numeric\" ? \"numeric\" : \"text\",\n \"aria-invalid\": ariaAttr(isInvalid),\n \"data-invalid\": dataAttr(isInvalid),\n type: state.context.mask ? \"password\" : inputType,\n defaultValue: state.context.value[index] || \"\",\n autoCapitalize: \"none\",\n autoComplete: state.context.otp ? \"one-time-code\" : \"off\",\n placeholder: focusedIndex === index ? \"\" : state.context.placeholder,\n onChange(event) {\n const evt = getNativeEvent(event)\n const { value } = event.currentTarget\n if (evt.inputType === \"insertFromPaste\" || value.length > 2) {\n send({ type: \"PASTE\", value })\n event.preventDefault()\n return\n }\n\n if (evt.inputType === \"deleteContentBackward\") {\n send(\"BACKSPACE\")\n return\n }\n send({ type: \"INPUT\", value, index })\n },\n onKeyDown(event) {\n const evt = getNativeEvent(event)\n if (evt.isComposing || isModifiedEvent(evt)) return\n\n const keyMap: EventKeyMap = {\n Backspace() {\n send(\"BACKSPACE\")\n },\n Delete() {\n send(\"DELETE\")\n },\n ArrowLeft() {\n send(\"ARROW_LEFT\")\n },\n ArrowRight() {\n send(\"ARROW_RIGHT\")\n },\n Enter() {\n send(\"ENTER\")\n },\n }\n\n const key = getEventKey(event, { dir: state.context.dir })\n const exec = keyMap[key]\n\n if (exec) {\n exec(event)\n event.preventDefault()\n } else {\n if (key === \"Tab\") return\n send({ type: \"KEY_DOWN\", value: key, preventDefault: () => event.preventDefault() })\n }\n },\n onFocus() {\n send({ type: \"FOCUS\", index })\n },\n onBlur() {\n send({ type: \"BLUR\", index })\n },\n })\n },\n }\n}\n","import { createScope, queryAll } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./pin-input.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,\n getInputId: (ctx: Ctx, id: string) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,\n getHiddenInputId: (ctx: Ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,\n getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,\n getControlId: (ctx: Ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,\n\n getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getRootId(ctx))\n const selector = `input[data-ownedby=${ownerId}]`\n return queryAll<HTMLInputElement>(dom.getRootEl(ctx), selector)\n },\n getInputEl: (ctx: Ctx, id: string) => dom.getById<HTMLInputElement>(ctx, dom.getInputId(ctx, id)),\n getFocusedInputEl: (ctx: Ctx) => dom.getElements(ctx)[ctx.focusedIndex],\n getFirstInputEl: (ctx: Ctx) => dom.getElements(ctx)[0],\n getHiddenInputEl: (ctx: Ctx) => dom.getById<HTMLInputElement>(ctx, dom.getHiddenInputId(ctx)),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { raf } from \"@zag-js/dom-query\"\nimport { dispatchInputValueEvent } from \"@zag-js/form-utils\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./pin-input.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./pin-input.types\"\n\nconst { and, not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"pin-input\",\n initial: ctx.autoFocus ? \"focused\" : \"idle\",\n context: {\n value: [],\n focusedIndex: -1,\n placeholder: \"○\",\n otp: false,\n type: \"numeric\",\n ...ctx,\n translations: {\n inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,\n ...ctx.translations,\n },\n },\n\n computed: {\n valueLength: (ctx) => ctx.value.length,\n filledValueLength: (ctx) => ctx.value.filter((v) => v?.trim() !== \"\").length,\n isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,\n valueAsString: (ctx) => ctx.value.join(\"\"),\n focusedValue: (ctx) => ctx.value[ctx.focusedIndex],\n },\n\n watch: {\n focusedIndex: [\"focusInput\", \"setInputSelection\"],\n value: [\"dispatchInputEvent\", \"syncInputElements\"],\n isValueComplete: [\"invokeOnComplete\", \"blurFocusedInputIfNeeded\"],\n },\n\n entry: ctx.autoFocus ? [\"setupValue\", \"setFocusIndexToFirst\"] : [\"setupValue\"],\n\n on: {\n SET_VALUE: [\n {\n guard: \"hasIndex\",\n actions: [\"setValueAtIndex\", \"invokeOnChange\"],\n },\n { actions: [\"setValue\", \"invokeOnChange\"] },\n ],\n CLEAR_VALUE: [\n {\n guard: \"isDisabled\",\n actions: [\"clearValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"clearValue\", \"invokeOnChange\", \"setFocusIndexToFirst\"],\n },\n ],\n },\n\n states: {\n idle: {\n on: {\n FOCUS: {\n target: \"focused\",\n actions: \"setFocusedIndex\",\n },\n },\n },\n focused: {\n on: {\n INPUT: [\n {\n guard: and(\"isFinalValue\", \"isValidValue\"),\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"syncInputValue\"],\n },\n {\n guard: \"isValidValue\",\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"setNextFocusedIndex\", \"syncInputValue\"],\n },\n ],\n PASTE: [\n {\n guard: \"isValidValue\",\n actions: [\"setPastedValue\", \"invokeOnChange\", \"setLastValueFocusIndex\"],\n },\n { actions: [\"resetFocusedValue\", \"invokeOnChange\"] },\n ],\n BLUR: {\n target: \"idle\",\n actions: \"clearFocusedIndex\",\n },\n DELETE: {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n ARROW_LEFT: {\n actions: \"setPrevFocusedIndex\",\n },\n ARROW_RIGHT: {\n actions: \"setNextFocusedIndex\",\n },\n BACKSPACE: [\n {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"setPrevFocusedIndex\", \"clearFocusedValue\", \"invokeOnChange\"],\n },\n ],\n ENTER: {\n guard: \"isValueComplete\",\n actions: \"requestFormSubmit\",\n },\n KEY_DOWN: {\n guard: not(\"isValidValue\"),\n actions: [\"preventDefault\", \"invokeOnInvalid\"],\n },\n },\n },\n },\n },\n {\n guards: {\n autoFocus: (ctx) => !!ctx.autoFocus,\n isValueEmpty: (_ctx, evt) => evt.value === \"\",\n hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== \"\",\n isValueComplete: (ctx) => ctx.isValueComplete,\n isValidValue: (ctx, evt) => {\n if (!ctx.pattern) return isValidType(evt.value, ctx.type)\n const regex = new RegExp(ctx.pattern, \"g\")\n return regex.test(evt.value)\n },\n isFinalValue: (ctx) => {\n return (\n ctx.filledValueLength + 1 === ctx.valueLength &&\n ctx.value.findIndex((v) => v.trim() === \"\") === ctx.focusedIndex\n )\n },\n isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,\n hasIndex: (_ctx, evt) => evt.index !== undefined,\n isDisabled: (ctx) => !!ctx.disabled,\n },\n actions: {\n setupValue: (ctx) => {\n if (ctx.value.length) return\n const inputs = dom.getElements(ctx)\n const emptyValues = Array.from<string>({ length: inputs.length }).fill(\"\")\n assign(ctx, emptyValues)\n },\n focusInput: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n dom.getFocusedInputEl(ctx)?.focus()\n })\n },\n setInputSelection: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n const input = dom.getFocusedInputEl(ctx)\n const length = input.value.length\n input.selectionStart = ctx.selectOnFocus ? 0 : length\n input.selectionEnd = length\n })\n },\n invokeOnComplete: (ctx) => {\n if (!ctx.isValueComplete) return\n ctx.onComplete?.({ value: Array.from(ctx.value), valueAsString: ctx.valueAsString })\n },\n invokeOnChange: (ctx) => {\n ctx.onChange?.({ value: Array.from(ctx.value) })\n },\n dispatchInputEvent: (ctx) => {\n const inputEl = dom.getHiddenInputEl(ctx)\n dispatchInputValueEvent(inputEl, { value: ctx.valueAsString })\n },\n invokeOnInvalid: (ctx, evt) => {\n ctx.onInvalid?.({ value: evt.value, index: ctx.focusedIndex })\n },\n clearFocusedIndex: (ctx) => {\n ctx.focusedIndex = -1\n },\n setValue: (ctx, evt) => {\n assign(ctx, evt.value)\n },\n setFocusedIndex: (ctx, evt) => {\n ctx.focusedIndex = evt.index\n },\n setFocusedValue: (ctx, evt) => {\n ctx.value[ctx.focusedIndex] = getNextValue(ctx.focusedValue, evt.value)\n },\n syncInputValue(ctx, evt) {\n const input = dom.getInputEl(ctx, evt.index.toString())\n if (!input) return\n input.value = ctx.value[evt.index]\n },\n syncInputElements(ctx) {\n const inputs = dom.getElements(ctx)\n inputs.forEach((input, index) => {\n input.value = ctx.value[index]\n })\n },\n setPastedValue(ctx, evt) {\n raf(() => {\n const startIndex = ctx.focusedValue ? 1 : 0\n const value = evt.value.substring(startIndex, startIndex + ctx.valueLength)\n assign(ctx, value)\n })\n },\n setValueAtIndex: (ctx, evt) => {\n ctx.value[evt.index] = getNextValue(ctx.focusedValue, evt.value)\n },\n clearValue: (ctx) => {\n const nextValue = Array.from<string>({ length: ctx.valueLength }).fill(\"\")\n assign(ctx, nextValue)\n },\n clearFocusedValue: (ctx) => {\n ctx.value[ctx.focusedIndex] = \"\"\n },\n resetFocusedValue: (ctx) => {\n const input = dom.getFocusedInputEl(ctx)\n input.value = ctx.focusedValue\n },\n setFocusIndexToFirst: (ctx) => {\n ctx.focusedIndex = 0\n },\n setNextFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1)\n },\n setPrevFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0)\n },\n setLastValueFocusIndex: (ctx) => {\n raf(() => {\n ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1)\n })\n },\n preventDefault(_, evt) {\n evt.preventDefault()\n },\n blurFocusedInputIfNeeded(ctx) {\n if (!ctx.blurOnComplete) return\n raf(() => {\n dom.getFocusedInputEl(ctx)?.blur()\n })\n },\n requestFormSubmit(ctx) {\n if (!ctx.name || !ctx.isValueComplete) return\n const input = dom.getHiddenInputEl(ctx)\n input?.form?.requestSubmit()\n },\n },\n },\n )\n}\n\nconst REGEX = {\n numeric: /^[0-9]+$/,\n alphabetic: /^[A-Za-z]+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/i,\n}\n\nfunction isValidType(value: string, type: MachineContext[\"type\"]) {\n if (!type) return true\n return !!REGEX[type]?.test(value)\n}\n\nfunction assign(ctx: MachineContext, value: string | string[]) {\n const arr = Array.isArray(value) ? value : value.split(\"\").filter(Boolean)\n arr.forEach((value, index) => {\n ctx.value[index] = value\n })\n}\n\nfunction getNextValue(current: string, next: string) {\n let nextValue = next\n if (current[0] === next[0]) nextValue = next[1]\n else if (current[0] === next[1]) nextValue = next[0]\n return nextValue\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAA8B;AAEvB,IAAM,cAAU,8BAAc,UAAU,EAAE,MAAM,QAAQ,SAAS,eAAe,SAAS,SAAS;AAClG,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,uBAA0E;AAC1E,IAAAA,oBAAmC;AAEnC,mBAA0B;AAC1B,6BAAoC;;;ACJpC,uBAAsC;AAG/B,IAAM,UAAM,8BAAY;AAAA,EAC7B,WAAW,CAAC,QAAa,IAAI,KAAK,QAAQ,aAAa,IAAI;AAAA,EAC3D,YAAY,CAAC,KAAU,OAAe,IAAI,KAAK,QAAQ,EAAE,KAAK,aAAa,IAAI,MAAM;AAAA,EACrF,kBAAkB,CAAC,QAAa,IAAI,KAAK,eAAe,aAAa,IAAI;AAAA,EACzE,YAAY,CAAC,QAAa,IAAI,KAAK,SAAS,aAAa,IAAI;AAAA,EAC7D,cAAc,CAAC,QAAa,IAAI,KAAK,WAAW,aAAa,IAAI;AAAA,EAEjE,WAAW,CAAC,QAAa,IAAI,QAAQ,KAAK,IAAI,UA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AiBA,IAAG;AACtC,iBAAO,MAAM,cAAc;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,QAAQ;AAAA,EACZ,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,cAAc;AAChB;AAEA,SAAS,YAAY,OAAe,MAA8B;AAChE,MAAI,CAAC;AAAM,WAAO;AAClB,SAAO,CAAC,CAAC,MAAM,IAAI,GAAG,KAAK,KAAK;AAClC;AAEA,SAAS,OAAO,KAAqB,OAA0B;AAC7D,QAAM,MAAM,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO;AACzE,MAAI,QAAQ,CAACC,QAAO,UAAU;AAC5B,QAAI,MAAM,KAAK,IAAIA;AAAA,EACrB,CAAC;AACH;AAEA,SAAS,aAAa,SAAiB,MAAc;AACnD,MAAI,YAAY;AAChB,MAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AAAA,WACrC,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AACnD,SAAO;AACT;","names":["import_dom_query","import_dom_query","import_utils","ctx","value"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/pin-input.anatomy.ts","../src/pin-input.connect.ts","../src/pin-input.dom.ts","../src/pin-input.machine.ts"],"sourcesContent":["export { anatomy } from \"./pin-input.anatomy\"\nexport { connect } from \"./pin-input.connect\"\nexport { machine } from \"./pin-input.machine\"\nexport type { UserDefinedContext as Context, PublicApi } from \"./pin-input.types\"\n","import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"pinInput\").parts(\"root\", \"label\", \"hiddenInput\", \"input\", \"control\")\nexport const parts = anatomy.build()\n","import { type EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from \"@zag-js/dom-event\"\nimport { ariaAttr, dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { invariant } from \"@zag-js/utils\"\nimport { visuallyHiddenStyle } from \"@zag-js/visually-hidden\"\nimport { parts } from \"./pin-input.anatomy\"\nimport { dom } from \"./pin-input.dom\"\nimport type { PublicApi, Send, State } from \"./pin-input.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T> {\n const isValueComplete = state.context.isValueComplete\n const isInvalid = state.context.invalid\n const focusedIndex = state.context.focusedIndex\n const translations = state.context.translations\n\n function focus() {\n dom.getFirstInputEl(state.context)?.focus()\n }\n\n return {\n value: state.context.value,\n valueAsString: state.context.valueAsString,\n isValueComplete: isValueComplete,\n\n setValue(value: string[]) {\n if (!Array.isArray(value)) {\n invariant(\"[pin-input/setValue] value must be an array\")\n }\n send({ type: \"SET_VALUE\", value })\n },\n\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n\n setValueAtIndex(index: number, value: string) {\n send({ type: \"SET_VALUE\", value, index })\n },\n\n focus,\n\n rootProps: normalize.element({\n dir: state.context.dir,\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n }),\n\n labelProps: normalize.label({\n ...parts.label.attrs,\n htmlFor: dom.getHiddenInputId(state.context),\n id: dom.getLabelId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n onClick: (event) => {\n event.preventDefault()\n focus()\n },\n }),\n\n hiddenInputProps: normalize.input({\n ...parts.hiddenInput.attrs,\n \"aria-hidden\": true,\n type: \"text\",\n tabIndex: -1,\n id: dom.getHiddenInputId(state.context),\n name: state.context.name,\n form: state.context.form,\n style: visuallyHiddenStyle,\n maxLength: state.context.valueLength,\n defaultValue: state.context.valueAsString,\n }),\n\n controlProps: normalize.element({\n ...parts.control.attrs,\n id: dom.getControlId(state.context),\n }),\n\n getInputProps({ index }: { index: number }) {\n const inputType = state.context.type === \"numeric\" ? \"tel\" : \"text\"\n return normalize.input({\n ...parts.input.attrs,\n disabled: state.context.disabled,\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n id: dom.getInputId(state.context, index.toString()),\n \"data-ownedby\": dom.getRootId(state.context),\n \"aria-label\": translations.inputLabel(index, state.context.valueLength),\n inputMode: state.context.otp || state.context.type === \"numeric\" ? \"numeric\" : \"text\",\n \"aria-invalid\": ariaAttr(isInvalid),\n \"data-invalid\": dataAttr(isInvalid),\n type: state.context.mask ? \"password\" : inputType,\n defaultValue: state.context.value[index] || \"\",\n autoCapitalize: \"none\",\n autoComplete: state.context.otp ? \"one-time-code\" : \"off\",\n placeholder: focusedIndex === index ? \"\" : state.context.placeholder,\n onChange(event) {\n const evt = getNativeEvent(event)\n const { value } = event.currentTarget\n if (evt.inputType === \"insertFromPaste\" || value.length > 2) {\n send({ type: \"PASTE\", value })\n event.preventDefault()\n return\n }\n\n if (evt.inputType === \"deleteContentBackward\") {\n send(\"BACKSPACE\")\n return\n }\n send({ type: \"INPUT\", value, index })\n },\n onKeyDown(event) {\n const evt = getNativeEvent(event)\n if (evt.isComposing || isModifiedEvent(evt)) return\n\n const keyMap: EventKeyMap = {\n Backspace() {\n send(\"BACKSPACE\")\n },\n Delete() {\n send(\"DELETE\")\n },\n ArrowLeft() {\n send(\"ARROW_LEFT\")\n },\n ArrowRight() {\n send(\"ARROW_RIGHT\")\n },\n Enter() {\n send(\"ENTER\")\n },\n }\n\n const key = getEventKey(event, { dir: state.context.dir })\n const exec = keyMap[key]\n\n if (exec) {\n exec(event)\n event.preventDefault()\n } else {\n if (key === \"Tab\") return\n send({ type: \"KEY_DOWN\", value: key, preventDefault: () => event.preventDefault() })\n }\n },\n onFocus() {\n send({ type: \"FOCUS\", index })\n },\n onBlur() {\n send({ type: \"BLUR\", index })\n },\n })\n },\n }\n}\n","import { createScope, queryAll } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./pin-input.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,\n getInputId: (ctx: Ctx, id: string) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,\n getHiddenInputId: (ctx: Ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,\n getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,\n getControlId: (ctx: Ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,\n\n getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getRootId(ctx))\n const selector = `input[data-ownedby=${ownerId}]`\n return queryAll<HTMLInputElement>(dom.getRootEl(ctx), selector)\n },\n getInputEl: (ctx: Ctx, id: string) => dom.getById<HTMLInputElement>(ctx, dom.getInputId(ctx, id)),\n getFocusedInputEl: (ctx: Ctx) => dom.getElements(ctx)[ctx.focusedIndex],\n getFirstInputEl: (ctx: Ctx) => dom.getElements(ctx)[0],\n getHiddenInputEl: (ctx: Ctx) => dom.getById<HTMLInputElement>(ctx, dom.getHiddenInputId(ctx)),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { raf } from \"@zag-js/dom-query\"\nimport { dispatchInputValueEvent } from \"@zag-js/form-utils\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./pin-input.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./pin-input.types\"\n\nconst { and, not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"pin-input\",\n initial: ctx.autoFocus ? \"focused\" : \"idle\",\n context: {\n value: [],\n focusedIndex: -1,\n placeholder: \"○\",\n otp: false,\n type: \"numeric\",\n ...ctx,\n translations: {\n inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,\n ...ctx.translations,\n },\n },\n\n computed: {\n valueLength: (ctx) => ctx.value.length,\n filledValueLength: (ctx) => ctx.value.filter((v) => v?.trim() !== \"\").length,\n isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,\n valueAsString: (ctx) => ctx.value.join(\"\"),\n focusedValue: (ctx) => ctx.value[ctx.focusedIndex],\n },\n\n watch: {\n focusedIndex: [\"focusInput\", \"setInputSelection\"],\n value: [\"dispatchInputEvent\", \"syncInputElements\"],\n isValueComplete: [\"invokeOnComplete\", \"blurFocusedInputIfNeeded\"],\n },\n\n entry: ctx.autoFocus ? [\"setupValue\", \"setFocusIndexToFirst\"] : [\"setupValue\"],\n\n on: {\n SET_VALUE: [\n {\n guard: \"hasIndex\",\n actions: [\"setValueAtIndex\", \"invokeOnChange\"],\n },\n { actions: [\"setValue\", \"invokeOnChange\"] },\n ],\n CLEAR_VALUE: [\n {\n guard: \"isDisabled\",\n actions: [\"clearValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"clearValue\", \"invokeOnChange\", \"setFocusIndexToFirst\"],\n },\n ],\n },\n\n states: {\n idle: {\n on: {\n FOCUS: {\n target: \"focused\",\n actions: \"setFocusedIndex\",\n },\n },\n },\n focused: {\n on: {\n INPUT: [\n {\n guard: and(\"isFinalValue\", \"isValidValue\"),\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"syncInputValue\"],\n },\n {\n guard: \"isValidValue\",\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"setNextFocusedIndex\", \"syncInputValue\"],\n },\n ],\n PASTE: [\n {\n guard: \"isValidValue\",\n actions: [\"setPastedValue\", \"invokeOnChange\", \"setLastValueFocusIndex\"],\n },\n { actions: [\"resetFocusedValue\", \"invokeOnChange\"] },\n ],\n BLUR: {\n target: \"idle\",\n actions: \"clearFocusedIndex\",\n },\n DELETE: {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n ARROW_LEFT: {\n actions: \"setPrevFocusedIndex\",\n },\n ARROW_RIGHT: {\n actions: \"setNextFocusedIndex\",\n },\n BACKSPACE: [\n {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"setPrevFocusedIndex\", \"clearFocusedValue\", \"invokeOnChange\"],\n },\n ],\n ENTER: {\n guard: \"isValueComplete\",\n actions: \"requestFormSubmit\",\n },\n KEY_DOWN: {\n guard: not(\"isValidValue\"),\n actions: [\"preventDefault\", \"invokeOnInvalid\"],\n },\n },\n },\n },\n },\n {\n guards: {\n autoFocus: (ctx) => !!ctx.autoFocus,\n isValueEmpty: (_ctx, evt) => evt.value === \"\",\n hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== \"\",\n isValueComplete: (ctx) => ctx.isValueComplete,\n isValidValue: (ctx, evt) => {\n if (!ctx.pattern) return isValidType(evt.value, ctx.type)\n const regex = new RegExp(ctx.pattern, \"g\")\n return regex.test(evt.value)\n },\n isFinalValue: (ctx) => {\n return (\n ctx.filledValueLength + 1 === ctx.valueLength &&\n ctx.value.findIndex((v) => v.trim() === \"\") === ctx.focusedIndex\n )\n },\n isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,\n hasIndex: (_ctx, evt) => evt.index !== undefined,\n isDisabled: (ctx) => !!ctx.disabled,\n },\n actions: {\n setupValue: (ctx) => {\n if (ctx.value.length) return\n const inputs = dom.getElements(ctx)\n const emptyValues = Array.from<string>({ length: inputs.length }).fill(\"\")\n assign(ctx, emptyValues)\n },\n focusInput: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n dom.getFocusedInputEl(ctx)?.focus()\n })\n },\n setInputSelection: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n const input = dom.getFocusedInputEl(ctx)\n const length = input.value.length\n input.selectionStart = ctx.selectOnFocus ? 0 : length\n input.selectionEnd = length\n })\n },\n invokeOnComplete: (ctx) => {\n if (!ctx.isValueComplete) return\n ctx.onComplete?.({ value: Array.from(ctx.value), valueAsString: ctx.valueAsString })\n },\n invokeOnChange: (ctx) => {\n ctx.onChange?.({ value: Array.from(ctx.value) })\n },\n dispatchInputEvent: (ctx) => {\n const inputEl = dom.getHiddenInputEl(ctx)\n dispatchInputValueEvent(inputEl, { value: ctx.valueAsString })\n },\n invokeOnInvalid: (ctx, evt) => {\n ctx.onInvalid?.({ value: evt.value, index: ctx.focusedIndex })\n },\n clearFocusedIndex: (ctx) => {\n ctx.focusedIndex = -1\n },\n setValue: (ctx, evt) => {\n assign(ctx, evt.value)\n },\n setFocusedIndex: (ctx, evt) => {\n ctx.focusedIndex = evt.index\n },\n setFocusedValue: (ctx, evt) => {\n ctx.value[ctx.focusedIndex] = getNextValue(ctx.focusedValue, evt.value)\n },\n syncInputValue(ctx, evt) {\n const input = dom.getInputEl(ctx, evt.index.toString())\n if (!input) return\n input.value = ctx.value[evt.index]\n },\n syncInputElements(ctx) {\n const inputs = dom.getElements(ctx)\n inputs.forEach((input, index) => {\n input.value = ctx.value[index]\n })\n },\n setPastedValue(ctx, evt) {\n raf(() => {\n const startIndex = ctx.focusedValue ? 1 : 0\n const value = evt.value.substring(startIndex, startIndex + ctx.valueLength)\n assign(ctx, value)\n })\n },\n setValueAtIndex: (ctx, evt) => {\n ctx.value[evt.index] = getNextValue(ctx.focusedValue, evt.value)\n },\n clearValue: (ctx) => {\n const nextValue = Array.from<string>({ length: ctx.valueLength }).fill(\"\")\n assign(ctx, nextValue)\n },\n clearFocusedValue: (ctx) => {\n ctx.value[ctx.focusedIndex] = \"\"\n },\n resetFocusedValue: (ctx) => {\n const input = dom.getFocusedInputEl(ctx)\n input.value = ctx.focusedValue\n },\n setFocusIndexToFirst: (ctx) => {\n ctx.focusedIndex = 0\n },\n setNextFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1)\n },\n setPrevFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0)\n },\n setLastValueFocusIndex: (ctx) => {\n raf(() => {\n ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1)\n })\n },\n preventDefault(_, evt) {\n evt.preventDefault()\n },\n blurFocusedInputIfNeeded(ctx) {\n if (!ctx.blurOnComplete) return\n raf(() => {\n dom.getFocusedInputEl(ctx)?.blur()\n })\n },\n requestFormSubmit(ctx) {\n if (!ctx.name || !ctx.isValueComplete) return\n const input = dom.getHiddenInputEl(ctx)\n input?.form?.requestSubmit()\n },\n },\n },\n )\n}\n\nconst REGEX = {\n numeric: /^[0-9]+$/,\n alphabetic: /^[A-Za-z]+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/i,\n}\n\nfunction isValidType(value: string, type: MachineContext[\"type\"]) {\n if (!type) return true\n return !!REGEX[type]?.test(value)\n}\n\nfunction assign(ctx: MachineContext, value: string | string[]) {\n const arr = Array.isArray(value) ? value : value.split(\"\").filter(Boolean)\n arr.forEach((value, index) => {\n ctx.value[index] = value\n })\n}\n\nfunction getNextValue(current: string, next: string) {\n let nextValue = next\n if (current[0] === next[0]) nextValue = next[1]\n else if (current[0] === next[1]) nextValue = next[0]\n return nextValue\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,qBAA8B;AAEvB,IAAM,cAAU,8BAAc,UAAU,EAAE,MAAM,QAAQ,SAAS,eAAe,SAAS,SAAS;AAClG,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,uBAA+E;AAC/E,IAAAA,oBAAmC;AAEnC,mBAA0B;AAC1B,6BAAoC;;;ACJpC,uBAAsC;AAG/B,IAAM,UAAM,8BAAY;AAAA,EAC7B,WAAW,CAAC,QAAa,IAAI,KAAK,QAAQ,aAAa,IAAI;AAAA,EAC3D,YAAY,CAAC,KAAU,OAAe,IAAI,KAAK,QAAQ,EAAE,KAAK,aAAa,IAAI,MAAM;AAAA,EACrF,kBAAkB,CAA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BA,MAAK;AACrB,cAAI,CAACA,KAAI,QAAQ,CAACA,KAAI;AAAiB;AACvC,gBAAM,QAAQ,IAAI,iBAAiBA,IAAG;AACtC,iBAAO,MAAM,cAAc;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,QAAQ;AAAA,EACZ,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,cAAc;AAChB;AAEA,SAAS,YAAY,OAAe,MAA8B;AAChE,MAAI,CAAC;AAAM,WAAO;AAClB,SAAO,CAAC,CAAC,MAAM,IAAI,GAAG,KAAK,KAAK;AAClC;AAEA,SAAS,OAAO,KAAqB,OAA0B;AAC7D,QAAM,MAAM,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO;AACzE,MAAI,QAAQ,CAACC,QAAO,UAAU;AAC5B,QAAI,MAAM,KAAK,IAAIA;AAAA,EACrB,CAAC;AACH;AAEA,SAAS,aAAa,SAAiB,MAAc;AACnD,MAAI,YAAY;AAChB,MAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AAAA,WACrC,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AACnD,SAAO;AACT;","names":["import_dom_query","import_dom_query","import_utils","ctx","value"]}
|
package/dist/index.mjs
CHANGED
|
@@ -39,42 +39,21 @@ function connect(state, send, normalize) {
|
|
|
39
39
|
dom.getFirstInputEl(state.context)?.focus();
|
|
40
40
|
}
|
|
41
41
|
return {
|
|
42
|
-
/**
|
|
43
|
-
* The value of the input as an array of strings.
|
|
44
|
-
*/
|
|
45
42
|
value: state.context.value,
|
|
46
|
-
/**
|
|
47
|
-
* The value of the input as a string.
|
|
48
|
-
*/
|
|
49
43
|
valueAsString: state.context.valueAsString,
|
|
50
|
-
/**
|
|
51
|
-
* Whether all inputs are filled.
|
|
52
|
-
*/
|
|
53
44
|
isValueComplete,
|
|
54
|
-
/**
|
|
55
|
-
* Function to set the value of the inputs.
|
|
56
|
-
*/
|
|
57
45
|
setValue(value) {
|
|
58
46
|
if (!Array.isArray(value)) {
|
|
59
47
|
invariant("[pin-input/setValue] value must be an array");
|
|
60
48
|
}
|
|
61
49
|
send({ type: "SET_VALUE", value });
|
|
62
50
|
},
|
|
63
|
-
/**
|
|
64
|
-
* Function to clear the value of the inputs.
|
|
65
|
-
*/
|
|
66
51
|
clearValue() {
|
|
67
52
|
send({ type: "CLEAR_VALUE" });
|
|
68
53
|
},
|
|
69
|
-
/**
|
|
70
|
-
* Function to set the value of the input at a specific index.
|
|
71
|
-
*/
|
|
72
54
|
setValueAtIndex(index, value) {
|
|
73
55
|
send({ type: "SET_VALUE", value, index });
|
|
74
56
|
},
|
|
75
|
-
/**
|
|
76
|
-
* Function to focus the pin-input. This will focus the first input.
|
|
77
|
-
*/
|
|
78
57
|
focus,
|
|
79
58
|
rootProps: normalize.element({
|
|
80
59
|
dir: state.context.dir,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pin-input.anatomy.ts","../src/pin-input.connect.ts","../src/pin-input.dom.ts","../src/pin-input.machine.ts"],"sourcesContent":["import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"pinInput\").parts(\"root\", \"label\", \"hiddenInput\", \"input\", \"control\")\nexport const parts = anatomy.build()\n","import { EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from \"@zag-js/dom-event\"\nimport { ariaAttr, dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { invariant } from \"@zag-js/utils\"\nimport { visuallyHiddenStyle } from \"@zag-js/visually-hidden\"\nimport { parts } from \"./pin-input.anatomy\"\nimport { dom } from \"./pin-input.dom\"\nimport type { Send, State } from \"./pin-input.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>) {\n const isValueComplete = state.context.isValueComplete\n const isInvalid = state.context.invalid\n const focusedIndex = state.context.focusedIndex\n const translations = state.context.translations\n\n function focus() {\n dom.getFirstInputEl(state.context)?.focus()\n }\n\n return {\n /**\n * The value of the input as an array of strings.\n */\n value: state.context.value,\n /**\n * The value of the input as a string.\n */\n valueAsString: state.context.valueAsString,\n /**\n * Whether all inputs are filled.\n */\n isValueComplete: isValueComplete,\n /**\n * Function to set the value of the inputs.\n */\n setValue(value: string[]) {\n if (!Array.isArray(value)) {\n invariant(\"[pin-input/setValue] value must be an array\")\n }\n send({ type: \"SET_VALUE\", value })\n },\n /**\n * Function to clear the value of the inputs.\n */\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n /**\n * Function to set the value of the input at a specific index.\n */\n setValueAtIndex(index: number, value: string) {\n send({ type: \"SET_VALUE\", value, index })\n },\n /**\n * Function to focus the pin-input. This will focus the first input.\n */\n focus,\n\n rootProps: normalize.element({\n dir: state.context.dir,\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n }),\n\n labelProps: normalize.label({\n ...parts.label.attrs,\n htmlFor: dom.getHiddenInputId(state.context),\n id: dom.getLabelId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n onClick: (event) => {\n event.preventDefault()\n focus()\n },\n }),\n\n hiddenInputProps: normalize.input({\n ...parts.hiddenInput.attrs,\n \"aria-hidden\": true,\n type: \"text\",\n tabIndex: -1,\n id: dom.getHiddenInputId(state.context),\n name: state.context.name,\n form: state.context.form,\n style: visuallyHiddenStyle,\n maxLength: state.context.valueLength,\n defaultValue: state.context.valueAsString,\n }),\n\n controlProps: normalize.element({\n ...parts.control.attrs,\n id: dom.getControlId(state.context),\n }),\n\n getInputProps({ index }: { index: number }) {\n const inputType = state.context.type === \"numeric\" ? \"tel\" : \"text\"\n return normalize.input({\n ...parts.input.attrs,\n disabled: state.context.disabled,\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n id: dom.getInputId(state.context, index.toString()),\n \"data-ownedby\": dom.getRootId(state.context),\n \"aria-label\": translations.inputLabel(index, state.context.valueLength),\n inputMode: state.context.otp || state.context.type === \"numeric\" ? \"numeric\" : \"text\",\n \"aria-invalid\": ariaAttr(isInvalid),\n \"data-invalid\": dataAttr(isInvalid),\n type: state.context.mask ? \"password\" : inputType,\n defaultValue: state.context.value[index] || \"\",\n autoCapitalize: \"none\",\n autoComplete: state.context.otp ? \"one-time-code\" : \"off\",\n placeholder: focusedIndex === index ? \"\" : state.context.placeholder,\n onChange(event) {\n const evt = getNativeEvent(event)\n const { value } = event.currentTarget\n if (evt.inputType === \"insertFromPaste\" || value.length > 2) {\n send({ type: \"PASTE\", value })\n event.preventDefault()\n return\n }\n\n if (evt.inputType === \"deleteContentBackward\") {\n send(\"BACKSPACE\")\n return\n }\n send({ type: \"INPUT\", value, index })\n },\n onKeyDown(event) {\n const evt = getNativeEvent(event)\n if (evt.isComposing || isModifiedEvent(evt)) return\n\n const keyMap: EventKeyMap = {\n Backspace() {\n send(\"BACKSPACE\")\n },\n Delete() {\n send(\"DELETE\")\n },\n ArrowLeft() {\n send(\"ARROW_LEFT\")\n },\n ArrowRight() {\n send(\"ARROW_RIGHT\")\n },\n Enter() {\n send(\"ENTER\")\n },\n }\n\n const key = getEventKey(event, { dir: state.context.dir })\n const exec = keyMap[key]\n\n if (exec) {\n exec(event)\n event.preventDefault()\n } else {\n if (key === \"Tab\") return\n send({ type: \"KEY_DOWN\", value: key, preventDefault: () => event.preventDefault() })\n }\n },\n onFocus() {\n send({ type: \"FOCUS\", index })\n },\n onBlur() {\n send({ type: \"BLUR\", index })\n },\n })\n },\n }\n}\n","import { createScope, queryAll } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./pin-input.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,\n getInputId: (ctx: Ctx, id: string) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,\n getHiddenInputId: (ctx: Ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,\n getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,\n getControlId: (ctx: Ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,\n\n getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getRootId(ctx))\n const selector = `input[data-ownedby=${ownerId}]`\n return queryAll<HTMLInputElement>(dom.getRootEl(ctx), selector)\n },\n getInputEl: (ctx: Ctx, id: string) => dom.getById<HTMLInputElement>(ctx, dom.getInputId(ctx, id)),\n getFocusedInputEl: (ctx: Ctx) => dom.getElements(ctx)[ctx.focusedIndex],\n getFirstInputEl: (ctx: Ctx) => dom.getElements(ctx)[0],\n getHiddenInputEl: (ctx: Ctx) => dom.getById<HTMLInputElement>(ctx, dom.getHiddenInputId(ctx)),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { raf } from \"@zag-js/dom-query\"\nimport { dispatchInputValueEvent } from \"@zag-js/form-utils\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./pin-input.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./pin-input.types\"\n\nconst { and, not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"pin-input\",\n initial: ctx.autoFocus ? \"focused\" : \"idle\",\n context: {\n value: [],\n focusedIndex: -1,\n placeholder: \"○\",\n otp: false,\n type: \"numeric\",\n ...ctx,\n translations: {\n inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,\n ...ctx.translations,\n },\n },\n\n computed: {\n valueLength: (ctx) => ctx.value.length,\n filledValueLength: (ctx) => ctx.value.filter((v) => v?.trim() !== \"\").length,\n isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,\n valueAsString: (ctx) => ctx.value.join(\"\"),\n focusedValue: (ctx) => ctx.value[ctx.focusedIndex],\n },\n\n watch: {\n focusedIndex: [\"focusInput\", \"setInputSelection\"],\n value: [\"dispatchInputEvent\", \"syncInputElements\"],\n isValueComplete: [\"invokeOnComplete\", \"blurFocusedInputIfNeeded\"],\n },\n\n entry: ctx.autoFocus ? [\"setupValue\", \"setFocusIndexToFirst\"] : [\"setupValue\"],\n\n on: {\n SET_VALUE: [\n {\n guard: \"hasIndex\",\n actions: [\"setValueAtIndex\", \"invokeOnChange\"],\n },\n { actions: [\"setValue\", \"invokeOnChange\"] },\n ],\n CLEAR_VALUE: [\n {\n guard: \"isDisabled\",\n actions: [\"clearValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"clearValue\", \"invokeOnChange\", \"setFocusIndexToFirst\"],\n },\n ],\n },\n\n states: {\n idle: {\n on: {\n FOCUS: {\n target: \"focused\",\n actions: \"setFocusedIndex\",\n },\n },\n },\n focused: {\n on: {\n INPUT: [\n {\n guard: and(\"isFinalValue\", \"isValidValue\"),\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"syncInputValue\"],\n },\n {\n guard: \"isValidValue\",\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"setNextFocusedIndex\", \"syncInputValue\"],\n },\n ],\n PASTE: [\n {\n guard: \"isValidValue\",\n actions: [\"setPastedValue\", \"invokeOnChange\", \"setLastValueFocusIndex\"],\n },\n { actions: [\"resetFocusedValue\", \"invokeOnChange\"] },\n ],\n BLUR: {\n target: \"idle\",\n actions: \"clearFocusedIndex\",\n },\n DELETE: {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n ARROW_LEFT: {\n actions: \"setPrevFocusedIndex\",\n },\n ARROW_RIGHT: {\n actions: \"setNextFocusedIndex\",\n },\n BACKSPACE: [\n {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"setPrevFocusedIndex\", \"clearFocusedValue\", \"invokeOnChange\"],\n },\n ],\n ENTER: {\n guard: \"isValueComplete\",\n actions: \"requestFormSubmit\",\n },\n KEY_DOWN: {\n guard: not(\"isValidValue\"),\n actions: [\"preventDefault\", \"invokeOnInvalid\"],\n },\n },\n },\n },\n },\n {\n guards: {\n autoFocus: (ctx) => !!ctx.autoFocus,\n isValueEmpty: (_ctx, evt) => evt.value === \"\",\n hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== \"\",\n isValueComplete: (ctx) => ctx.isValueComplete,\n isValidValue: (ctx, evt) => {\n if (!ctx.pattern) return isValidType(evt.value, ctx.type)\n const regex = new RegExp(ctx.pattern, \"g\")\n return regex.test(evt.value)\n },\n isFinalValue: (ctx) => {\n return (\n ctx.filledValueLength + 1 === ctx.valueLength &&\n ctx.value.findIndex((v) => v.trim() === \"\") === ctx.focusedIndex\n )\n },\n isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,\n hasIndex: (_ctx, evt) => evt.index !== undefined,\n isDisabled: (ctx) => !!ctx.disabled,\n },\n actions: {\n setupValue: (ctx) => {\n if (ctx.value.length) return\n const inputs = dom.getElements(ctx)\n const emptyValues = Array.from<string>({ length: inputs.length }).fill(\"\")\n assign(ctx, emptyValues)\n },\n focusInput: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n dom.getFocusedInputEl(ctx)?.focus()\n })\n },\n setInputSelection: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n const input = dom.getFocusedInputEl(ctx)\n const length = input.value.length\n input.selectionStart = ctx.selectOnFocus ? 0 : length\n input.selectionEnd = length\n })\n },\n invokeOnComplete: (ctx) => {\n if (!ctx.isValueComplete) return\n ctx.onComplete?.({ value: Array.from(ctx.value), valueAsString: ctx.valueAsString })\n },\n invokeOnChange: (ctx) => {\n ctx.onChange?.({ value: Array.from(ctx.value) })\n },\n dispatchInputEvent: (ctx) => {\n const inputEl = dom.getHiddenInputEl(ctx)\n dispatchInputValueEvent(inputEl, { value: ctx.valueAsString })\n },\n invokeOnInvalid: (ctx, evt) => {\n ctx.onInvalid?.({ value: evt.value, index: ctx.focusedIndex })\n },\n clearFocusedIndex: (ctx) => {\n ctx.focusedIndex = -1\n },\n setValue: (ctx, evt) => {\n assign(ctx, evt.value)\n },\n setFocusedIndex: (ctx, evt) => {\n ctx.focusedIndex = evt.index\n },\n setFocusedValue: (ctx, evt) => {\n ctx.value[ctx.focusedIndex] = getNextValue(ctx.focusedValue, evt.value)\n },\n syncInputValue(ctx, evt) {\n const input = dom.getInputEl(ctx, evt.index.toString())\n if (!input) return\n input.value = ctx.value[evt.index]\n },\n syncInputElements(ctx) {\n const inputs = dom.getElements(ctx)\n inputs.forEach((input, index) => {\n input.value = ctx.value[index]\n })\n },\n setPastedValue(ctx, evt) {\n raf(() => {\n const startIndex = ctx.focusedValue ? 1 : 0\n const value = evt.value.substring(startIndex, startIndex + ctx.valueLength)\n assign(ctx, value)\n })\n },\n setValueAtIndex: (ctx, evt) => {\n ctx.value[evt.index] = getNextValue(ctx.focusedValue, evt.value)\n },\n clearValue: (ctx) => {\n const nextValue = Array.from<string>({ length: ctx.valueLength }).fill(\"\")\n assign(ctx, nextValue)\n },\n clearFocusedValue: (ctx) => {\n ctx.value[ctx.focusedIndex] = \"\"\n },\n resetFocusedValue: (ctx) => {\n const input = dom.getFocusedInputEl(ctx)\n input.value = ctx.focusedValue\n },\n setFocusIndexToFirst: (ctx) => {\n ctx.focusedIndex = 0\n },\n setNextFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1)\n },\n setPrevFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0)\n },\n setLastValueFocusIndex: (ctx) => {\n raf(() => {\n ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1)\n })\n },\n preventDefault(_, evt) {\n evt.preventDefault()\n },\n blurFocusedInputIfNeeded(ctx) {\n if (!ctx.blurOnComplete) return\n raf(() => {\n dom.getFocusedInputEl(ctx)?.blur()\n })\n },\n requestFormSubmit(ctx) {\n if (!ctx.name || !ctx.isValueComplete) return\n const input = dom.getHiddenInputEl(ctx)\n input?.form?.requestSubmit()\n },\n },\n },\n )\n}\n\nconst REGEX = {\n numeric: /^[0-9]+$/,\n alphabetic: /^[A-Za-z]+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/i,\n}\n\nfunction isValidType(value: string, type: MachineContext[\"type\"]) {\n if (!type) return true\n return !!REGEX[type]?.test(value)\n}\n\nfunction assign(ctx: MachineContext, value: string | string[]) {\n const arr = Array.isArray(value) ? value : value.split(\"\").filter(Boolean)\n arr.forEach((value, index) => {\n ctx.value[index] = value\n })\n}\n\nfunction getNextValue(current: string, next: string) {\n let nextValue = next\n if (current[0] === next[0]) nextValue = next[1]\n else if (current[0] === next[1]) nextValue = next[0]\n return nextValue\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,UAAU,EAAE,MAAM,QAAQ,SAAS,eAAe,SAAS,SAAS;AAClG,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,SAAsB,aAAa,gBAAgB,uBAAuB;AAC1E,SAAS,UAAU,gBAAgB;AAEnC,SAAS,iBAAiB;AAC1B,SAAS,2BAA2B;;;ACJpC,SAAS,aAAa,gBAAgB;AAG/B,IAAM,MAAM,YAAY;AAAA,EAC7B,WAAW,CAAC,QAAa,IAAI,KAAK,QAAQ,aAAa,IAAI;AAAA,EAC3D,YAAY,CAAC,KAAU,OAAe,IAAI,KAAK,QAAQ,EAAE,KAAK,aAAa,IAAI,MAAM;AAAA,EACrF,kBAAkB,CAAC,QAAa,IAAI,KAAK,eAAe,aAAa,IAAI;AAAA,EACzE,YAAY,CAAC,QAAa,IA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,UACnC,CAAC;AAAA,QACH;AAAA,QACA,kBAAkBA,MAAK;AACrB,cAAI,CAACA,KAAI,QAAQ,CAACA,KAAI;AAAiB;AACvC,gBAAM,QAAQ,IAAI,iBAAiBA,IAAG;AACtC,iBAAO,MAAM,cAAc;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,QAAQ;AAAA,EACZ,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,cAAc;AAChB;AAEA,SAAS,YAAY,OAAe,MAA8B;AAChE,MAAI,CAAC;AAAM,WAAO;AAClB,SAAO,CAAC,CAAC,MAAM,IAAI,GAAG,KAAK,KAAK;AAClC;AAEA,SAAS,OAAO,KAAqB,OAA0B;AAC7D,QAAM,MAAM,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO;AACzE,MAAI,QAAQ,CAACC,QAAO,UAAU;AAC5B,QAAI,MAAM,KAAK,IAAIA;AAAA,EACrB,CAAC;AACH;AAEA,SAAS,aAAa,SAAiB,MAAc;AACnD,MAAI,YAAY;AAChB,MAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AAAA,WACrC,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AACnD,SAAO;AACT;","names":["ctx","value"]}
|
|
1
|
+
{"version":3,"sources":["../src/pin-input.anatomy.ts","../src/pin-input.connect.ts","../src/pin-input.dom.ts","../src/pin-input.machine.ts"],"sourcesContent":["import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"pinInput\").parts(\"root\", \"label\", \"hiddenInput\", \"input\", \"control\")\nexport const parts = anatomy.build()\n","import { type EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from \"@zag-js/dom-event\"\nimport { ariaAttr, dataAttr } from \"@zag-js/dom-query\"\nimport type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport { invariant } from \"@zag-js/utils\"\nimport { visuallyHiddenStyle } from \"@zag-js/visually-hidden\"\nimport { parts } from \"./pin-input.anatomy\"\nimport { dom } from \"./pin-input.dom\"\nimport type { PublicApi, Send, State } from \"./pin-input.types\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T> {\n const isValueComplete = state.context.isValueComplete\n const isInvalid = state.context.invalid\n const focusedIndex = state.context.focusedIndex\n const translations = state.context.translations\n\n function focus() {\n dom.getFirstInputEl(state.context)?.focus()\n }\n\n return {\n value: state.context.value,\n valueAsString: state.context.valueAsString,\n isValueComplete: isValueComplete,\n\n setValue(value: string[]) {\n if (!Array.isArray(value)) {\n invariant(\"[pin-input/setValue] value must be an array\")\n }\n send({ type: \"SET_VALUE\", value })\n },\n\n clearValue() {\n send({ type: \"CLEAR_VALUE\" })\n },\n\n setValueAtIndex(index: number, value: string) {\n send({ type: \"SET_VALUE\", value, index })\n },\n\n focus,\n\n rootProps: normalize.element({\n dir: state.context.dir,\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n }),\n\n labelProps: normalize.label({\n ...parts.label.attrs,\n htmlFor: dom.getHiddenInputId(state.context),\n id: dom.getLabelId(state.context),\n \"data-invalid\": dataAttr(isInvalid),\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n onClick: (event) => {\n event.preventDefault()\n focus()\n },\n }),\n\n hiddenInputProps: normalize.input({\n ...parts.hiddenInput.attrs,\n \"aria-hidden\": true,\n type: \"text\",\n tabIndex: -1,\n id: dom.getHiddenInputId(state.context),\n name: state.context.name,\n form: state.context.form,\n style: visuallyHiddenStyle,\n maxLength: state.context.valueLength,\n defaultValue: state.context.valueAsString,\n }),\n\n controlProps: normalize.element({\n ...parts.control.attrs,\n id: dom.getControlId(state.context),\n }),\n\n getInputProps({ index }: { index: number }) {\n const inputType = state.context.type === \"numeric\" ? \"tel\" : \"text\"\n return normalize.input({\n ...parts.input.attrs,\n disabled: state.context.disabled,\n \"data-disabled\": dataAttr(state.context.disabled),\n \"data-complete\": dataAttr(isValueComplete),\n id: dom.getInputId(state.context, index.toString()),\n \"data-ownedby\": dom.getRootId(state.context),\n \"aria-label\": translations.inputLabel(index, state.context.valueLength),\n inputMode: state.context.otp || state.context.type === \"numeric\" ? \"numeric\" : \"text\",\n \"aria-invalid\": ariaAttr(isInvalid),\n \"data-invalid\": dataAttr(isInvalid),\n type: state.context.mask ? \"password\" : inputType,\n defaultValue: state.context.value[index] || \"\",\n autoCapitalize: \"none\",\n autoComplete: state.context.otp ? \"one-time-code\" : \"off\",\n placeholder: focusedIndex === index ? \"\" : state.context.placeholder,\n onChange(event) {\n const evt = getNativeEvent(event)\n const { value } = event.currentTarget\n if (evt.inputType === \"insertFromPaste\" || value.length > 2) {\n send({ type: \"PASTE\", value })\n event.preventDefault()\n return\n }\n\n if (evt.inputType === \"deleteContentBackward\") {\n send(\"BACKSPACE\")\n return\n }\n send({ type: \"INPUT\", value, index })\n },\n onKeyDown(event) {\n const evt = getNativeEvent(event)\n if (evt.isComposing || isModifiedEvent(evt)) return\n\n const keyMap: EventKeyMap = {\n Backspace() {\n send(\"BACKSPACE\")\n },\n Delete() {\n send(\"DELETE\")\n },\n ArrowLeft() {\n send(\"ARROW_LEFT\")\n },\n ArrowRight() {\n send(\"ARROW_RIGHT\")\n },\n Enter() {\n send(\"ENTER\")\n },\n }\n\n const key = getEventKey(event, { dir: state.context.dir })\n const exec = keyMap[key]\n\n if (exec) {\n exec(event)\n event.preventDefault()\n } else {\n if (key === \"Tab\") return\n send({ type: \"KEY_DOWN\", value: key, preventDefault: () => event.preventDefault() })\n }\n },\n onFocus() {\n send({ type: \"FOCUS\", index })\n },\n onBlur() {\n send({ type: \"BLUR\", index })\n },\n })\n },\n }\n}\n","import { createScope, queryAll } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./pin-input.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,\n getInputId: (ctx: Ctx, id: string) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,\n getHiddenInputId: (ctx: Ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,\n getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,\n getControlId: (ctx: Ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,\n\n getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),\n getElements: (ctx: Ctx) => {\n const ownerId = CSS.escape(dom.getRootId(ctx))\n const selector = `input[data-ownedby=${ownerId}]`\n return queryAll<HTMLInputElement>(dom.getRootEl(ctx), selector)\n },\n getInputEl: (ctx: Ctx, id: string) => dom.getById<HTMLInputElement>(ctx, dom.getInputId(ctx, id)),\n getFocusedInputEl: (ctx: Ctx) => dom.getElements(ctx)[ctx.focusedIndex],\n getFirstInputEl: (ctx: Ctx) => dom.getElements(ctx)[0],\n getHiddenInputEl: (ctx: Ctx) => dom.getById<HTMLInputElement>(ctx, dom.getHiddenInputId(ctx)),\n})\n","import { createMachine, guards } from \"@zag-js/core\"\nimport { raf } from \"@zag-js/dom-query\"\nimport { dispatchInputValueEvent } from \"@zag-js/form-utils\"\nimport { compact } from \"@zag-js/utils\"\nimport { dom } from \"./pin-input.dom\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./pin-input.types\"\n\nconst { and, not } = guards\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"pin-input\",\n initial: ctx.autoFocus ? \"focused\" : \"idle\",\n context: {\n value: [],\n focusedIndex: -1,\n placeholder: \"○\",\n otp: false,\n type: \"numeric\",\n ...ctx,\n translations: {\n inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,\n ...ctx.translations,\n },\n },\n\n computed: {\n valueLength: (ctx) => ctx.value.length,\n filledValueLength: (ctx) => ctx.value.filter((v) => v?.trim() !== \"\").length,\n isValueComplete: (ctx) => ctx.valueLength === ctx.filledValueLength,\n valueAsString: (ctx) => ctx.value.join(\"\"),\n focusedValue: (ctx) => ctx.value[ctx.focusedIndex],\n },\n\n watch: {\n focusedIndex: [\"focusInput\", \"setInputSelection\"],\n value: [\"dispatchInputEvent\", \"syncInputElements\"],\n isValueComplete: [\"invokeOnComplete\", \"blurFocusedInputIfNeeded\"],\n },\n\n entry: ctx.autoFocus ? [\"setupValue\", \"setFocusIndexToFirst\"] : [\"setupValue\"],\n\n on: {\n SET_VALUE: [\n {\n guard: \"hasIndex\",\n actions: [\"setValueAtIndex\", \"invokeOnChange\"],\n },\n { actions: [\"setValue\", \"invokeOnChange\"] },\n ],\n CLEAR_VALUE: [\n {\n guard: \"isDisabled\",\n actions: [\"clearValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"clearValue\", \"invokeOnChange\", \"setFocusIndexToFirst\"],\n },\n ],\n },\n\n states: {\n idle: {\n on: {\n FOCUS: {\n target: \"focused\",\n actions: \"setFocusedIndex\",\n },\n },\n },\n focused: {\n on: {\n INPUT: [\n {\n guard: and(\"isFinalValue\", \"isValidValue\"),\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"syncInputValue\"],\n },\n {\n guard: \"isValidValue\",\n actions: [\"setFocusedValue\", \"invokeOnChange\", \"setNextFocusedIndex\", \"syncInputValue\"],\n },\n ],\n PASTE: [\n {\n guard: \"isValidValue\",\n actions: [\"setPastedValue\", \"invokeOnChange\", \"setLastValueFocusIndex\"],\n },\n { actions: [\"resetFocusedValue\", \"invokeOnChange\"] },\n ],\n BLUR: {\n target: \"idle\",\n actions: \"clearFocusedIndex\",\n },\n DELETE: {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n ARROW_LEFT: {\n actions: \"setPrevFocusedIndex\",\n },\n ARROW_RIGHT: {\n actions: \"setNextFocusedIndex\",\n },\n BACKSPACE: [\n {\n guard: \"hasValue\",\n actions: [\"clearFocusedValue\", \"invokeOnChange\"],\n },\n {\n actions: [\"setPrevFocusedIndex\", \"clearFocusedValue\", \"invokeOnChange\"],\n },\n ],\n ENTER: {\n guard: \"isValueComplete\",\n actions: \"requestFormSubmit\",\n },\n KEY_DOWN: {\n guard: not(\"isValidValue\"),\n actions: [\"preventDefault\", \"invokeOnInvalid\"],\n },\n },\n },\n },\n },\n {\n guards: {\n autoFocus: (ctx) => !!ctx.autoFocus,\n isValueEmpty: (_ctx, evt) => evt.value === \"\",\n hasValue: (ctx) => ctx.value[ctx.focusedIndex] !== \"\",\n isValueComplete: (ctx) => ctx.isValueComplete,\n isValidValue: (ctx, evt) => {\n if (!ctx.pattern) return isValidType(evt.value, ctx.type)\n const regex = new RegExp(ctx.pattern, \"g\")\n return regex.test(evt.value)\n },\n isFinalValue: (ctx) => {\n return (\n ctx.filledValueLength + 1 === ctx.valueLength &&\n ctx.value.findIndex((v) => v.trim() === \"\") === ctx.focusedIndex\n )\n },\n isLastInputFocused: (ctx) => ctx.focusedIndex === ctx.valueLength - 1,\n hasIndex: (_ctx, evt) => evt.index !== undefined,\n isDisabled: (ctx) => !!ctx.disabled,\n },\n actions: {\n setupValue: (ctx) => {\n if (ctx.value.length) return\n const inputs = dom.getElements(ctx)\n const emptyValues = Array.from<string>({ length: inputs.length }).fill(\"\")\n assign(ctx, emptyValues)\n },\n focusInput: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n dom.getFocusedInputEl(ctx)?.focus()\n })\n },\n setInputSelection: (ctx) => {\n raf(() => {\n if (ctx.focusedIndex === -1) return\n const input = dom.getFocusedInputEl(ctx)\n const length = input.value.length\n input.selectionStart = ctx.selectOnFocus ? 0 : length\n input.selectionEnd = length\n })\n },\n invokeOnComplete: (ctx) => {\n if (!ctx.isValueComplete) return\n ctx.onComplete?.({ value: Array.from(ctx.value), valueAsString: ctx.valueAsString })\n },\n invokeOnChange: (ctx) => {\n ctx.onChange?.({ value: Array.from(ctx.value) })\n },\n dispatchInputEvent: (ctx) => {\n const inputEl = dom.getHiddenInputEl(ctx)\n dispatchInputValueEvent(inputEl, { value: ctx.valueAsString })\n },\n invokeOnInvalid: (ctx, evt) => {\n ctx.onInvalid?.({ value: evt.value, index: ctx.focusedIndex })\n },\n clearFocusedIndex: (ctx) => {\n ctx.focusedIndex = -1\n },\n setValue: (ctx, evt) => {\n assign(ctx, evt.value)\n },\n setFocusedIndex: (ctx, evt) => {\n ctx.focusedIndex = evt.index\n },\n setFocusedValue: (ctx, evt) => {\n ctx.value[ctx.focusedIndex] = getNextValue(ctx.focusedValue, evt.value)\n },\n syncInputValue(ctx, evt) {\n const input = dom.getInputEl(ctx, evt.index.toString())\n if (!input) return\n input.value = ctx.value[evt.index]\n },\n syncInputElements(ctx) {\n const inputs = dom.getElements(ctx)\n inputs.forEach((input, index) => {\n input.value = ctx.value[index]\n })\n },\n setPastedValue(ctx, evt) {\n raf(() => {\n const startIndex = ctx.focusedValue ? 1 : 0\n const value = evt.value.substring(startIndex, startIndex + ctx.valueLength)\n assign(ctx, value)\n })\n },\n setValueAtIndex: (ctx, evt) => {\n ctx.value[evt.index] = getNextValue(ctx.focusedValue, evt.value)\n },\n clearValue: (ctx) => {\n const nextValue = Array.from<string>({ length: ctx.valueLength }).fill(\"\")\n assign(ctx, nextValue)\n },\n clearFocusedValue: (ctx) => {\n ctx.value[ctx.focusedIndex] = \"\"\n },\n resetFocusedValue: (ctx) => {\n const input = dom.getFocusedInputEl(ctx)\n input.value = ctx.focusedValue\n },\n setFocusIndexToFirst: (ctx) => {\n ctx.focusedIndex = 0\n },\n setNextFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.min(ctx.focusedIndex + 1, ctx.valueLength - 1)\n },\n setPrevFocusedIndex: (ctx) => {\n ctx.focusedIndex = Math.max(ctx.focusedIndex - 1, 0)\n },\n setLastValueFocusIndex: (ctx) => {\n raf(() => {\n ctx.focusedIndex = Math.min(ctx.filledValueLength, ctx.valueLength - 1)\n })\n },\n preventDefault(_, evt) {\n evt.preventDefault()\n },\n blurFocusedInputIfNeeded(ctx) {\n if (!ctx.blurOnComplete) return\n raf(() => {\n dom.getFocusedInputEl(ctx)?.blur()\n })\n },\n requestFormSubmit(ctx) {\n if (!ctx.name || !ctx.isValueComplete) return\n const input = dom.getHiddenInputEl(ctx)\n input?.form?.requestSubmit()\n },\n },\n },\n )\n}\n\nconst REGEX = {\n numeric: /^[0-9]+$/,\n alphabetic: /^[A-Za-z]+$/,\n alphanumeric: /^[a-zA-Z0-9]+$/i,\n}\n\nfunction isValidType(value: string, type: MachineContext[\"type\"]) {\n if (!type) return true\n return !!REGEX[type]?.test(value)\n}\n\nfunction assign(ctx: MachineContext, value: string | string[]) {\n const arr = Array.isArray(value) ? value : value.split(\"\").filter(Boolean)\n arr.forEach((value, index) => {\n ctx.value[index] = value\n })\n}\n\nfunction getNextValue(current: string, next: string) {\n let nextValue = next\n if (current[0] === next[0]) nextValue = next[1]\n else if (current[0] === next[1]) nextValue = next[0]\n return nextValue\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,UAAU,EAAE,MAAM,QAAQ,SAAS,eAAe,SAAS,SAAS;AAClG,IAAM,QAAQ,QAAQ,MAAM;;;ACHnC,SAA2B,aAAa,gBAAgB,uBAAuB;AAC/E,SAAS,UAAU,gBAAgB;AAEnC,SAAS,iBAAiB;AAC1B,SAAS,2BAA2B;;;ACJpC,SAAS,aAAa,gBAAgB;AAG/B,IAAM,MAAM,YAAY;AAAA,EAC7B,WAAW,CAAC,QAAa,IAAI,KAAK,QAAQ,aAAa,IAAI;AAAA,EAC3D,YAAY,CAAC,KAAU,OAAe,IAAI,KAAK,QAAQ,EAAE,KAAK,aAAa,IAAI,MAAM;AAAA,EACrF,kBAAkB,CAAC,QAAa,IAAI,KAAK,eAAe,aAAa,IAAI;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AiB;AACvC,gBAAM,QAAQ,IAAI,iBAAiBA,IAAG;AACtC,iBAAO,MAAM,cAAc;AAAA,QAC7B;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,QAAQ;AAAA,EACZ,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,cAAc;AAChB;AAEA,SAAS,YAAY,OAAe,MAA8B;AAChE,MAAI,CAAC;AAAM,WAAO;AAClB,SAAO,CAAC,CAAC,MAAM,IAAI,GAAG,KAAK,KAAK;AAClC;AAEA,SAAS,OAAO,KAAqB,OAA0B;AAC7D,QAAM,MAAM,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,EAAE,EAAE,OAAO,OAAO;AACzE,MAAI,QAAQ,CAACC,QAAO,UAAU;AAC5B,QAAI,MAAM,KAAK,IAAIA;AAAA,EACrB,CAAC;AACH;AAEA,SAAS,aAAa,SAAiB,MAAc;AACnD,MAAI,YAAY;AAChB,MAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AAAA,WACrC,QAAQ,CAAC,MAAM,KAAK,CAAC;AAAG,gBAAY,KAAK,CAAC;AACnD,SAAO;AACT;","names":["ctx","value"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/pin-input",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
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.11.
|
|
31
|
-
"@zag-js/dom-query": "0.11.
|
|
32
|
-
"@zag-js/dom-event": "0.11.
|
|
33
|
-
"@zag-js/form-utils": "0.11.
|
|
34
|
-
"@zag-js/visually-hidden": "0.11.
|
|
35
|
-
"@zag-js/utils": "0.11.
|
|
36
|
-
"@zag-js/core": "0.11.
|
|
37
|
-
"@zag-js/types": "0.11.
|
|
30
|
+
"@zag-js/anatomy": "0.11.2",
|
|
31
|
+
"@zag-js/dom-query": "0.11.2",
|
|
32
|
+
"@zag-js/dom-event": "0.11.2",
|
|
33
|
+
"@zag-js/form-utils": "0.11.2",
|
|
34
|
+
"@zag-js/visually-hidden": "0.11.2",
|
|
35
|
+
"@zag-js/utils": "0.11.2",
|
|
36
|
+
"@zag-js/core": "0.11.2",
|
|
37
|
+
"@zag-js/types": "0.11.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"clean-package": "2.2.0"
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { anatomy } from "./pin-input.anatomy"
|
|
2
2
|
export { connect } from "./pin-input.connect"
|
|
3
3
|
export { machine } from "./pin-input.machine"
|
|
4
|
-
export type { UserDefinedContext as Context } from "./pin-input.types"
|
|
4
|
+
export type { UserDefinedContext as Context, PublicApi } from "./pin-input.types"
|
package/src/pin-input.connect.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from "@zag-js/dom-event"
|
|
1
|
+
import { type EventKeyMap, getEventKey, getNativeEvent, isModifiedEvent } from "@zag-js/dom-event"
|
|
2
2
|
import { ariaAttr, dataAttr } from "@zag-js/dom-query"
|
|
3
3
|
import type { NormalizeProps, PropTypes } from "@zag-js/types"
|
|
4
4
|
import { invariant } from "@zag-js/utils"
|
|
5
5
|
import { visuallyHiddenStyle } from "@zag-js/visually-hidden"
|
|
6
6
|
import { parts } from "./pin-input.anatomy"
|
|
7
7
|
import { dom } from "./pin-input.dom"
|
|
8
|
-
import type { Send, State } from "./pin-input.types"
|
|
8
|
+
import type { PublicApi, Send, State } from "./pin-input.types"
|
|
9
9
|
|
|
10
|
-
export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>) {
|
|
10
|
+
export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): PublicApi<T> {
|
|
11
11
|
const isValueComplete = state.context.isValueComplete
|
|
12
12
|
const isInvalid = state.context.invalid
|
|
13
13
|
const focusedIndex = state.context.focusedIndex
|
|
@@ -18,42 +18,25 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
return {
|
|
21
|
-
/**
|
|
22
|
-
* The value of the input as an array of strings.
|
|
23
|
-
*/
|
|
24
21
|
value: state.context.value,
|
|
25
|
-
/**
|
|
26
|
-
* The value of the input as a string.
|
|
27
|
-
*/
|
|
28
22
|
valueAsString: state.context.valueAsString,
|
|
29
|
-
/**
|
|
30
|
-
* Whether all inputs are filled.
|
|
31
|
-
*/
|
|
32
23
|
isValueComplete: isValueComplete,
|
|
33
|
-
|
|
34
|
-
* Function to set the value of the inputs.
|
|
35
|
-
*/
|
|
24
|
+
|
|
36
25
|
setValue(value: string[]) {
|
|
37
26
|
if (!Array.isArray(value)) {
|
|
38
27
|
invariant("[pin-input/setValue] value must be an array")
|
|
39
28
|
}
|
|
40
29
|
send({ type: "SET_VALUE", value })
|
|
41
30
|
},
|
|
42
|
-
|
|
43
|
-
* Function to clear the value of the inputs.
|
|
44
|
-
*/
|
|
31
|
+
|
|
45
32
|
clearValue() {
|
|
46
33
|
send({ type: "CLEAR_VALUE" })
|
|
47
34
|
},
|
|
48
|
-
|
|
49
|
-
* Function to set the value of the input at a specific index.
|
|
50
|
-
*/
|
|
35
|
+
|
|
51
36
|
setValueAtIndex(index: number, value: string) {
|
|
52
37
|
send({ type: "SET_VALUE", value, index })
|
|
53
38
|
},
|
|
54
|
-
|
|
55
|
-
* Function to focus the pin-input. This will focus the first input.
|
|
56
|
-
*/
|
|
39
|
+
|
|
57
40
|
focus,
|
|
58
41
|
|
|
59
42
|
rootProps: normalize.element({
|
package/src/pin-input.types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StateMachine as S } from "@zag-js/core"
|
|
2
|
-
import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types"
|
|
2
|
+
import type { CommonProperties, Context, DirectionProperty, PropTypes, RequiredBy } from "@zag-js/types"
|
|
3
3
|
|
|
4
4
|
type IntlTranslations = {
|
|
5
5
|
inputLabel: (index: number, length: number) => string
|
|
@@ -90,6 +90,42 @@ type PublicContext = DirectionProperty &
|
|
|
90
90
|
translations: IntlTranslations
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
export type PublicApi<T extends PropTypes = PropTypes> = {
|
|
94
|
+
/**
|
|
95
|
+
* The value of the input as an array of strings.
|
|
96
|
+
*/
|
|
97
|
+
value: string[]
|
|
98
|
+
/**
|
|
99
|
+
* The value of the input as a string.
|
|
100
|
+
*/
|
|
101
|
+
valueAsString: string
|
|
102
|
+
/**
|
|
103
|
+
* Whether all inputs are filled.
|
|
104
|
+
*/
|
|
105
|
+
isValueComplete: boolean
|
|
106
|
+
/**
|
|
107
|
+
* Function to set the value of the inputs.
|
|
108
|
+
*/
|
|
109
|
+
setValue(value: string[]): void
|
|
110
|
+
/**
|
|
111
|
+
* Function to clear the value of the inputs.
|
|
112
|
+
*/
|
|
113
|
+
clearValue(): void
|
|
114
|
+
/**
|
|
115
|
+
* Function to set the value of the input at a specific index.
|
|
116
|
+
*/
|
|
117
|
+
setValueAtIndex(index: number, value: string): void
|
|
118
|
+
/**
|
|
119
|
+
* Function to focus the pin-input. This will focus the first input.
|
|
120
|
+
*/
|
|
121
|
+
focus: () => void
|
|
122
|
+
rootProps: T["element"]
|
|
123
|
+
labelProps: T["label"]
|
|
124
|
+
hiddenInputProps: T["input"]
|
|
125
|
+
controlProps: T["element"]
|
|
126
|
+
getInputProps({ index }: { index: number }): T["input"]
|
|
127
|
+
}
|
|
128
|
+
|
|
93
129
|
export type UserDefinedContext = RequiredBy<PublicContext, "id">
|
|
94
130
|
|
|
95
131
|
type ComputedContext = Readonly<{
|