@zag-js/pin-input 0.53.0 → 0.55.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/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +514 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +485 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
- package/src/pin-input.connect.ts +48 -40
- package/src/pin-input.types.ts +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -169,10 +169,10 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
169
169
|
* Function to focus the pin-input. This will focus the first input.
|
|
170
170
|
*/
|
|
171
171
|
focus: () => void;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
172
|
+
getRootProps(): T["element"];
|
|
173
|
+
getLabelProps(): T["label"];
|
|
174
|
+
getHiddenInputProps(): T["input"];
|
|
175
|
+
getControlProps(): T["element"];
|
|
176
176
|
getInputProps(props: InputProps): T["input"];
|
|
177
177
|
}
|
|
178
178
|
|
package/dist/index.d.ts
CHANGED
|
@@ -169,10 +169,10 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
169
169
|
* Function to focus the pin-input. This will focus the first input.
|
|
170
170
|
*/
|
|
171
171
|
focus: () => void;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
172
|
+
getRootProps(): T["element"];
|
|
173
|
+
getLabelProps(): T["label"];
|
|
174
|
+
getHiddenInputProps(): T["input"];
|
|
175
|
+
getControlProps(): T["element"];
|
|
176
176
|
getInputProps(props: InputProps): T["input"];
|
|
177
177
|
}
|
|
178
178
|
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,515 @@
|
|
|
1
|
-
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var src_exports={};__export(src_exports,{anatomy:()=>anatomy,connect:()=>connect,machine:()=>machine});module.exports=__toCommonJS(src_exports);var import_anatomy=require("@zag-js/anatomy");var anatomy=(0,import_anatomy.createAnatomy)("pinInput").parts("root","label","input","control");var parts=anatomy.build();var import_dom_event=require("@zag-js/dom-event");var import_dom_query2=require("@zag-js/dom-query");var import_utils=require("@zag-js/utils");var import_dom_query=require("@zag-js/dom-query");var dom=(0,import_dom_query.createScope)({getRootId:ctx=>ctx.ids?.root??`pin-input:${ctx.id}`,getInputId:(ctx,id)=>ctx.ids?.input?.(id)??`pin-input:${ctx.id}:${id}`,getHiddenInputId:ctx=>ctx.ids?.hiddenInput??`pin-input:${ctx.id}:hidden`,getLabelId:ctx=>ctx.ids?.label??`pin-input:${ctx.id}:label`,getControlId:ctx=>ctx.ids?.control??`pin-input:${ctx.id}:control`,getRootEl:ctx=>dom.getById(ctx,dom.getRootId(ctx)),getInputEls:ctx=>{const ownerId=CSS.escape(dom.getRootId(ctx));const selector=`input[data-ownedby=${ownerId}]`;return(0,import_dom_query.queryAll)(dom.getRootEl(ctx),selector)},getInputEl:(ctx,id)=>dom.getById(ctx,dom.getInputId(ctx,id)),getFocusedInputEl:ctx=>dom.getInputEls(ctx)[ctx.focusedIndex],getFirstInputEl:ctx=>dom.getInputEls(ctx)[0],getHiddenInputEl:ctx=>dom.getById(ctx,dom.getHiddenInputId(ctx))});var REGEX={numeric:/^[0-9]+$/,alphabetic:/^[A-Za-z]+$/,alphanumeric:/^[a-zA-Z0-9]+$/i};function isValidType(ctx,value){if(!ctx.type)return true;return!!REGEX[ctx.type]?.test(value)}function isValidValue(ctx,value){if(!ctx.pattern)return isValidType(ctx,value);const regex=new RegExp(ctx.pattern,"g");return regex.test(value)}function connect(state,send,normalize){const complete=state.context.isValueComplete;const invalid=state.context.invalid;const focusedIndex=state.context.focusedIndex;const translations=state.context.translations;function focus(){dom.getFirstInputEl(state.context)?.focus()}return{focus,value:state.context.value,valueAsString:state.context.valueAsString,complete,setValue(value){if(!Array.isArray(value)){(0,import_utils.invariant)("[pin-input/setValue] value must be an array")}send({type:"VALUE.SET",value})},clearValue(){send({type:"VALUE.CLEAR"})},setValueAtIndex(index,value){send({type:"VALUE.SET",value,index})},rootProps:normalize.element({dir:state.context.dir,...parts.root.attrs,id:dom.getRootId(state.context),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),"data-disabled":(0,import_dom_query2.dataAttr)(state.context.disabled),"data-complete":(0,import_dom_query2.dataAttr)(complete)}),labelProps:normalize.label({...parts.label.attrs,dir:state.context.dir,htmlFor:dom.getHiddenInputId(state.context),id:dom.getLabelId(state.context),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),"data-disabled":(0,import_dom_query2.dataAttr)(state.context.disabled),"data-complete":(0,import_dom_query2.dataAttr)(complete),onClick(event){event.preventDefault();focus()}}),hiddenInputProps:normalize.input({"aria-hidden":true,type:"text",tabIndex:-1,id:dom.getHiddenInputId(state.context),name:state.context.name,form:state.context.form,style:import_dom_query2.visuallyHiddenStyle,maxLength:state.context.valueLength,defaultValue:state.context.valueAsString}),controlProps:normalize.element({...parts.control.attrs,dir:state.context.dir,id:dom.getControlId(state.context)}),getInputProps(props){const{index}=props;const inputType=state.context.type==="numeric"?"tel":"text";return normalize.input({...parts.input.attrs,dir:state.context.dir,disabled:state.context.disabled,"data-disabled":(0,import_dom_query2.dataAttr)(state.context.disabled),"data-complete":(0,import_dom_query2.dataAttr)(complete),id:dom.getInputId(state.context,index.toString()),"data-ownedby":dom.getRootId(state.context),"aria-label":translations.inputLabel(index,state.context.valueLength),inputMode:state.context.otp||state.context.type==="numeric"?"numeric":"text","aria-invalid":(0,import_dom_query2.ariaAttr)(invalid),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),type:state.context.mask?"password":inputType,defaultValue:state.context.value[index]||"",autoCapitalize:"none",autoComplete:state.context.otp?"one-time-code":"off",placeholder:focusedIndex===index?"":state.context.placeholder,onBeforeInput(event){try{const value=(0,import_dom_query2.getBeforeInputValue)(event);const isValid=isValidValue(state.context,value);if(!isValid){send({type:"VALUE.INVALID",value});event.preventDefault()}if(value.length>2){event.currentTarget.setSelectionRange(0,1,"forward")}}catch{}},onChange(event){const evt=(0,import_dom_event.getNativeEvent)(event);const{value}=event.currentTarget;if(evt.inputType==="insertFromPaste"||value.length>2){send({type:"INPUT.PASTE",value});event.target.value=value[0];event.preventDefault();return}if(evt.inputType==="deleteContentBackward"){send("INPUT.BACKSPACE");return}send({type:"INPUT.CHANGE",value,index})},onKeyDown(event){if(event.defaultPrevented)return;if((0,import_dom_query2.isComposingEvent)(event))return;if((0,import_dom_event.isModifierKey)(event))return;const keyMap={Backspace(){send("INPUT.BACKSPACE")},Delete(){send("INPUT.DELETE")},ArrowLeft(){send("INPUT.ARROW_LEFT")},ArrowRight(){send("INPUT.ARROW_RIGHT")},Enter(){send("INPUT.ENTER")}};const exec=keyMap[(0,import_dom_event.getEventKey)(event,state.context)];if(exec){exec(event);event.preventDefault()}},onFocus(){send({type:"INPUT.FOCUS",index})},onBlur(){send({type:"INPUT.BLUR",index})}})}}}var import_core=require("@zag-js/core");var import_dom_query3=require("@zag-js/dom-query");var import_form_utils=require("@zag-js/form-utils");var import_utils2=require("@zag-js/utils");function machine(userContext){const ctx=(0,import_utils2.compact)(userContext);return(0,import_core.createMachine)({id:"pin-input",initial:"idle",context:{value:[],placeholder:"\u25CB",otp:false,type:"numeric",...ctx,focusedIndex:-1,translations:{inputLabel:(index,length)=>`pin code ${index+1} of ${length}`,...ctx.translations}},computed:{valueLength:ctx2=>ctx2.value.length,filledValueLength:ctx2=>ctx2.value.filter(v=>v?.trim()!=="").length,isValueComplete:ctx2=>ctx2.valueLength===ctx2.filledValueLength,valueAsString:ctx2=>ctx2.value.join(""),focusedValue:ctx2=>ctx2.value[ctx2.focusedIndex]||""},entry:(0,import_core.choose)([{guard:"autoFocus",actions:["setupValue","setFocusIndexToFirst"]},{actions:["setupValue"]}]),watch:{focusedIndex:["focusInput","selectInputIfNeeded"],value:["syncInputElements"],isValueComplete:["invokeOnComplete","blurFocusedInputIfNeeded"]},on:{"VALUE.SET":[{guard:"hasIndex",actions:["setValueAtIndex"]},{actions:["setValue"]}],"VALUE.CLEAR":{actions:["clearValue","setFocusIndexToFirst"]}},states:{idle:{on:{"INPUT.FOCUS":{target:"focused",actions:"setFocusedIndex"}}},focused:{on:{"INPUT.CHANGE":[{guard:"isFinalValue",actions:["setFocusedValue","syncInputValue"]},{actions:["setFocusedValue","setNextFocusedIndex","syncInputValue"]}],"INPUT.PASTE":{actions:["setPastedValue","setLastValueFocusIndex"]},"INPUT.BLUR":{target:"idle",actions:"clearFocusedIndex"},"INPUT.DELETE":{guard:"hasValue",actions:"clearFocusedValue"},"INPUT.ARROW_LEFT":{actions:"setPrevFocusedIndex"},"INPUT.ARROW_RIGHT":{actions:"setNextFocusedIndex"},"INPUT.BACKSPACE":[{guard:"hasValue",actions:["clearFocusedValue"]},{actions:["setPrevFocusedIndex","clearFocusedValue"]}],"INPUT.ENTER":{guard:"isValueComplete",actions:"requestFormSubmit"},"VALUE.INVALID":{actions:"invokeOnInvalid"}}}}},{guards:{autoFocus:ctx2=>!!ctx2.autoFocus,isValueEmpty:(_ctx,evt)=>evt.value==="",hasValue:ctx2=>ctx2.value[ctx2.focusedIndex]!=="",isValueComplete:ctx2=>ctx2.isValueComplete,isFinalValue:ctx2=>ctx2.filledValueLength+1===ctx2.valueLength&&ctx2.value.findIndex(v=>v.trim()==="")===ctx2.focusedIndex,hasIndex:(_ctx,evt)=>evt.index!==void 0,isDisabled:ctx2=>!!ctx2.disabled},actions:{setupValue(ctx2){if(ctx2.value.length)return;const inputEls=dom.getInputEls(ctx2);const emptyValues=Array.from({length:inputEls.length}).fill("");assignValue(ctx2,emptyValues)},focusInput(ctx2){if(ctx2.focusedIndex===-1)return;dom.getFocusedInputEl(ctx2)?.focus({preventScroll:true})},selectInputIfNeeded(ctx2){if(!ctx2.selectOnFocus||ctx2.focusedIndex===-1)return;(0,import_dom_query3.raf)(()=>{dom.getFocusedInputEl(ctx2)?.select()})},invokeOnComplete(ctx2){if(!ctx2.isValueComplete)return;ctx2.onValueComplete?.({value:Array.from(ctx2.value),valueAsString:ctx2.valueAsString})},invokeOnInvalid(ctx2,evt){ctx2.onValueInvalid?.({value:evt.value,index:ctx2.focusedIndex})},clearFocusedIndex(ctx2){ctx2.focusedIndex=-1},setFocusedIndex(ctx2,evt){ctx2.focusedIndex=evt.index},setValue(ctx2,evt){set.value(ctx2,evt.value)},setFocusedValue(ctx2,evt){const nextValue=getNextValue(ctx2.focusedValue,evt.value);set.valueAtIndex(ctx2,ctx2.focusedIndex,nextValue)},revertInputValue(ctx2){const inputEl=dom.getFocusedInputEl(ctx2);dom.setValue(inputEl,ctx2.focusedValue)},syncInputValue(ctx2,evt){const inputEl=dom.getInputEl(ctx2,evt.index.toString());dom.setValue(inputEl,ctx2.value[evt.index])},syncInputElements(ctx2){const inputEls=dom.getInputEls(ctx2);inputEls.forEach((inputEl,index)=>{dom.setValue(inputEl,ctx2.value[index])})},setPastedValue(ctx2,evt){(0,import_dom_query3.raf)(()=>{const startIndex=Math.min(ctx2.focusedIndex,ctx2.filledValueLength);const left=startIndex>0?ctx2.valueAsString.substring(0,ctx2.focusedIndex):"";const right=evt.value.substring(0,ctx2.valueLength-startIndex);const value=left+right;set.value(ctx2,value.split(""))})},setValueAtIndex(ctx2,evt){const nextValue=getNextValue(ctx2.focusedValue,evt.value);set.valueAtIndex(ctx2,evt.index,nextValue)},clearValue(ctx2){const nextValue=Array.from({length:ctx2.valueLength}).fill("");set.value(ctx2,nextValue)},clearFocusedValue(ctx2){set.valueAtIndex(ctx2,ctx2.focusedIndex,"")},setFocusIndexToFirst(ctx2){ctx2.focusedIndex=0},setNextFocusedIndex(ctx2){ctx2.focusedIndex=Math.min(ctx2.focusedIndex+1,ctx2.valueLength-1)},setPrevFocusedIndex(ctx2){ctx2.focusedIndex=Math.max(ctx2.focusedIndex-1,0)},setLastValueFocusIndex(ctx2){(0,import_dom_query3.raf)(()=>{ctx2.focusedIndex=Math.min(ctx2.filledValueLength,ctx2.valueLength-1)})},blurFocusedInputIfNeeded(ctx2){if(!ctx2.blurOnComplete)return;(0,import_dom_query3.raf)(()=>{dom.getFocusedInputEl(ctx2)?.blur()})},requestFormSubmit(ctx2){if(!ctx2.name||!ctx2.isValueComplete)return;const inputEl=dom.getHiddenInputEl(ctx2);inputEl?.form?.requestSubmit()}}})}function assignValue(ctx,value){const arr=Array.isArray(value)?value:value.split("").filter(Boolean);arr.forEach((value2,index)=>{ctx.value[index]=value2})}function getNextValue(current,next){let nextValue=next;if(current[0]===next[0])nextValue=next[1];else if(current[0]===next[1])nextValue=next[0];return nextValue.split("")[nextValue.length-1]}var invoke={change(ctx){ctx.onValueChange?.({value:Array.from(ctx.value),valueAsString:ctx.valueAsString});const inputEl=dom.getHiddenInputEl(ctx);(0,import_form_utils.dispatchInputValueEvent)(inputEl,{value:ctx.valueAsString})}};var set={value(ctx,value){if((0,import_utils2.isEqual)(ctx.value,value))return;assignValue(ctx,value);invoke.change(ctx)},valueAtIndex(ctx,index,value){if((0,import_utils2.isEqual)(ctx.value[index],value))return;ctx.value[index]=value;invoke.change(ctx)}};0&&(module.exports={anatomy,connect,machine});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
connect: () => connect,
|
|
25
|
+
machine: () => machine
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/pin-input.anatomy.ts
|
|
30
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
31
|
+
var anatomy = (0, import_anatomy.createAnatomy)("pinInput").parts("root", "label", "input", "control");
|
|
32
|
+
var parts = anatomy.build();
|
|
33
|
+
|
|
34
|
+
// src/pin-input.connect.ts
|
|
35
|
+
var import_dom_event = require("@zag-js/dom-event");
|
|
36
|
+
var import_dom_query2 = require("@zag-js/dom-query");
|
|
37
|
+
var import_utils = require("@zag-js/utils");
|
|
38
|
+
|
|
39
|
+
// src/pin-input.dom.ts
|
|
40
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
41
|
+
var dom = (0, import_dom_query.createScope)({
|
|
42
|
+
getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
|
|
43
|
+
getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
|
|
44
|
+
getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
|
|
45
|
+
getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
|
|
46
|
+
getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
|
|
47
|
+
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
48
|
+
getInputEls: (ctx) => {
|
|
49
|
+
const ownerId = CSS.escape(dom.getRootId(ctx));
|
|
50
|
+
const selector = `input[data-ownedby=${ownerId}]`;
|
|
51
|
+
return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), selector);
|
|
52
|
+
},
|
|
53
|
+
getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
|
|
54
|
+
getFocusedInputEl: (ctx) => dom.getInputEls(ctx)[ctx.focusedIndex],
|
|
55
|
+
getFirstInputEl: (ctx) => dom.getInputEls(ctx)[0],
|
|
56
|
+
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// src/pin-input.utils.ts
|
|
60
|
+
var REGEX = {
|
|
61
|
+
numeric: /^[0-9]+$/,
|
|
62
|
+
alphabetic: /^[A-Za-z]+$/,
|
|
63
|
+
alphanumeric: /^[a-zA-Z0-9]+$/i
|
|
64
|
+
};
|
|
65
|
+
function isValidType(ctx, value) {
|
|
66
|
+
if (!ctx.type)
|
|
67
|
+
return true;
|
|
68
|
+
return !!REGEX[ctx.type]?.test(value);
|
|
69
|
+
}
|
|
70
|
+
function isValidValue(ctx, value) {
|
|
71
|
+
if (!ctx.pattern)
|
|
72
|
+
return isValidType(ctx, value);
|
|
73
|
+
const regex = new RegExp(ctx.pattern, "g");
|
|
74
|
+
return regex.test(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/pin-input.connect.ts
|
|
78
|
+
function connect(state, send, normalize) {
|
|
79
|
+
const complete = state.context.isValueComplete;
|
|
80
|
+
const invalid = state.context.invalid;
|
|
81
|
+
const focusedIndex = state.context.focusedIndex;
|
|
82
|
+
const translations = state.context.translations;
|
|
83
|
+
function focus() {
|
|
84
|
+
dom.getFirstInputEl(state.context)?.focus();
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
focus,
|
|
88
|
+
value: state.context.value,
|
|
89
|
+
valueAsString: state.context.valueAsString,
|
|
90
|
+
complete,
|
|
91
|
+
setValue(value) {
|
|
92
|
+
if (!Array.isArray(value)) {
|
|
93
|
+
(0, import_utils.invariant)("[pin-input/setValue] value must be an array");
|
|
94
|
+
}
|
|
95
|
+
send({ type: "VALUE.SET", value });
|
|
96
|
+
},
|
|
97
|
+
clearValue() {
|
|
98
|
+
send({ type: "VALUE.CLEAR" });
|
|
99
|
+
},
|
|
100
|
+
setValueAtIndex(index, value) {
|
|
101
|
+
send({ type: "VALUE.SET", value, index });
|
|
102
|
+
},
|
|
103
|
+
getRootProps() {
|
|
104
|
+
return normalize.element({
|
|
105
|
+
dir: state.context.dir,
|
|
106
|
+
...parts.root.attrs,
|
|
107
|
+
id: dom.getRootId(state.context),
|
|
108
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
109
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
110
|
+
"data-complete": (0, import_dom_query2.dataAttr)(complete)
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
getLabelProps() {
|
|
114
|
+
return normalize.label({
|
|
115
|
+
...parts.label.attrs,
|
|
116
|
+
dir: state.context.dir,
|
|
117
|
+
htmlFor: dom.getHiddenInputId(state.context),
|
|
118
|
+
id: dom.getLabelId(state.context),
|
|
119
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
120
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
121
|
+
"data-complete": (0, import_dom_query2.dataAttr)(complete),
|
|
122
|
+
onClick(event) {
|
|
123
|
+
event.preventDefault();
|
|
124
|
+
focus();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
getHiddenInputProps() {
|
|
129
|
+
return normalize.input({
|
|
130
|
+
"aria-hidden": true,
|
|
131
|
+
type: "text",
|
|
132
|
+
tabIndex: -1,
|
|
133
|
+
id: dom.getHiddenInputId(state.context),
|
|
134
|
+
name: state.context.name,
|
|
135
|
+
form: state.context.form,
|
|
136
|
+
style: import_dom_query2.visuallyHiddenStyle,
|
|
137
|
+
maxLength: state.context.valueLength,
|
|
138
|
+
defaultValue: state.context.valueAsString
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
getControlProps() {
|
|
142
|
+
return normalize.element({
|
|
143
|
+
...parts.control.attrs,
|
|
144
|
+
dir: state.context.dir,
|
|
145
|
+
id: dom.getControlId(state.context)
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
getInputProps(props) {
|
|
149
|
+
const { index } = props;
|
|
150
|
+
const inputType = state.context.type === "numeric" ? "tel" : "text";
|
|
151
|
+
return normalize.input({
|
|
152
|
+
...parts.input.attrs,
|
|
153
|
+
dir: state.context.dir,
|
|
154
|
+
disabled: state.context.disabled,
|
|
155
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(state.context.disabled),
|
|
156
|
+
"data-complete": (0, import_dom_query2.dataAttr)(complete),
|
|
157
|
+
id: dom.getInputId(state.context, index.toString()),
|
|
158
|
+
"data-ownedby": dom.getRootId(state.context),
|
|
159
|
+
"aria-label": translations.inputLabel(index, state.context.valueLength),
|
|
160
|
+
inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
|
|
161
|
+
"aria-invalid": (0, import_dom_query2.ariaAttr)(invalid),
|
|
162
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
163
|
+
type: state.context.mask ? "password" : inputType,
|
|
164
|
+
defaultValue: state.context.value[index] || "",
|
|
165
|
+
autoCapitalize: "none",
|
|
166
|
+
autoComplete: state.context.otp ? "one-time-code" : "off",
|
|
167
|
+
placeholder: focusedIndex === index ? "" : state.context.placeholder,
|
|
168
|
+
onBeforeInput(event) {
|
|
169
|
+
try {
|
|
170
|
+
const value = (0, import_dom_query2.getBeforeInputValue)(event);
|
|
171
|
+
const isValid = isValidValue(state.context, value);
|
|
172
|
+
if (!isValid) {
|
|
173
|
+
send({ type: "VALUE.INVALID", value });
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
}
|
|
176
|
+
if (value.length > 2) {
|
|
177
|
+
event.currentTarget.setSelectionRange(0, 1, "forward");
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
onChange(event) {
|
|
183
|
+
const evt = (0, import_dom_event.getNativeEvent)(event);
|
|
184
|
+
const { value } = event.currentTarget;
|
|
185
|
+
if (evt.inputType === "insertFromPaste" || value.length > 2) {
|
|
186
|
+
send({ type: "INPUT.PASTE", value });
|
|
187
|
+
event.target.value = value[0];
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (evt.inputType === "deleteContentBackward") {
|
|
192
|
+
send("INPUT.BACKSPACE");
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
send({ type: "INPUT.CHANGE", value, index });
|
|
196
|
+
},
|
|
197
|
+
onKeyDown(event) {
|
|
198
|
+
if (event.defaultPrevented)
|
|
199
|
+
return;
|
|
200
|
+
if ((0, import_dom_query2.isComposingEvent)(event))
|
|
201
|
+
return;
|
|
202
|
+
if ((0, import_dom_event.isModifierKey)(event))
|
|
203
|
+
return;
|
|
204
|
+
const keyMap = {
|
|
205
|
+
Backspace() {
|
|
206
|
+
send("INPUT.BACKSPACE");
|
|
207
|
+
},
|
|
208
|
+
Delete() {
|
|
209
|
+
send("INPUT.DELETE");
|
|
210
|
+
},
|
|
211
|
+
ArrowLeft() {
|
|
212
|
+
send("INPUT.ARROW_LEFT");
|
|
213
|
+
},
|
|
214
|
+
ArrowRight() {
|
|
215
|
+
send("INPUT.ARROW_RIGHT");
|
|
216
|
+
},
|
|
217
|
+
Enter() {
|
|
218
|
+
send("INPUT.ENTER");
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const exec = keyMap[(0, import_dom_event.getEventKey)(event, state.context)];
|
|
222
|
+
if (exec) {
|
|
223
|
+
exec(event);
|
|
224
|
+
event.preventDefault();
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
onFocus() {
|
|
228
|
+
send({ type: "INPUT.FOCUS", index });
|
|
229
|
+
},
|
|
230
|
+
onBlur() {
|
|
231
|
+
send({ type: "INPUT.BLUR", index });
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/pin-input.machine.ts
|
|
239
|
+
var import_core = require("@zag-js/core");
|
|
240
|
+
var import_dom_query3 = require("@zag-js/dom-query");
|
|
241
|
+
var import_form_utils = require("@zag-js/form-utils");
|
|
242
|
+
var import_utils2 = require("@zag-js/utils");
|
|
243
|
+
function machine(userContext) {
|
|
244
|
+
const ctx = (0, import_utils2.compact)(userContext);
|
|
245
|
+
return (0, import_core.createMachine)(
|
|
246
|
+
{
|
|
247
|
+
id: "pin-input",
|
|
248
|
+
initial: "idle",
|
|
249
|
+
context: {
|
|
250
|
+
value: [],
|
|
251
|
+
placeholder: "\u25CB",
|
|
252
|
+
otp: false,
|
|
253
|
+
type: "numeric",
|
|
254
|
+
...ctx,
|
|
255
|
+
focusedIndex: -1,
|
|
256
|
+
translations: {
|
|
257
|
+
inputLabel: (index, length) => `pin code ${index + 1} of ${length}`,
|
|
258
|
+
...ctx.translations
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
computed: {
|
|
262
|
+
valueLength: (ctx2) => ctx2.value.length,
|
|
263
|
+
filledValueLength: (ctx2) => ctx2.value.filter((v) => v?.trim() !== "").length,
|
|
264
|
+
isValueComplete: (ctx2) => ctx2.valueLength === ctx2.filledValueLength,
|
|
265
|
+
valueAsString: (ctx2) => ctx2.value.join(""),
|
|
266
|
+
focusedValue: (ctx2) => ctx2.value[ctx2.focusedIndex] || ""
|
|
267
|
+
},
|
|
268
|
+
entry: (0, import_core.choose)([
|
|
269
|
+
{
|
|
270
|
+
guard: "autoFocus",
|
|
271
|
+
actions: ["setupValue", "setFocusIndexToFirst"]
|
|
272
|
+
},
|
|
273
|
+
{ actions: ["setupValue"] }
|
|
274
|
+
]),
|
|
275
|
+
watch: {
|
|
276
|
+
focusedIndex: ["focusInput", "selectInputIfNeeded"],
|
|
277
|
+
value: ["syncInputElements"],
|
|
278
|
+
isValueComplete: ["invokeOnComplete", "blurFocusedInputIfNeeded"]
|
|
279
|
+
},
|
|
280
|
+
on: {
|
|
281
|
+
"VALUE.SET": [
|
|
282
|
+
{
|
|
283
|
+
guard: "hasIndex",
|
|
284
|
+
actions: ["setValueAtIndex"]
|
|
285
|
+
},
|
|
286
|
+
{ actions: ["setValue"] }
|
|
287
|
+
],
|
|
288
|
+
"VALUE.CLEAR": {
|
|
289
|
+
actions: ["clearValue", "setFocusIndexToFirst"]
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
states: {
|
|
293
|
+
idle: {
|
|
294
|
+
on: {
|
|
295
|
+
"INPUT.FOCUS": {
|
|
296
|
+
target: "focused",
|
|
297
|
+
actions: "setFocusedIndex"
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
focused: {
|
|
302
|
+
on: {
|
|
303
|
+
"INPUT.CHANGE": [
|
|
304
|
+
{
|
|
305
|
+
guard: "isFinalValue",
|
|
306
|
+
actions: ["setFocusedValue", "syncInputValue"]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
actions: ["setFocusedValue", "setNextFocusedIndex", "syncInputValue"]
|
|
310
|
+
}
|
|
311
|
+
],
|
|
312
|
+
"INPUT.PASTE": {
|
|
313
|
+
actions: ["setPastedValue", "setLastValueFocusIndex"]
|
|
314
|
+
},
|
|
315
|
+
"INPUT.BLUR": {
|
|
316
|
+
target: "idle",
|
|
317
|
+
actions: "clearFocusedIndex"
|
|
318
|
+
},
|
|
319
|
+
"INPUT.DELETE": {
|
|
320
|
+
guard: "hasValue",
|
|
321
|
+
actions: "clearFocusedValue"
|
|
322
|
+
},
|
|
323
|
+
"INPUT.ARROW_LEFT": {
|
|
324
|
+
actions: "setPrevFocusedIndex"
|
|
325
|
+
},
|
|
326
|
+
"INPUT.ARROW_RIGHT": {
|
|
327
|
+
actions: "setNextFocusedIndex"
|
|
328
|
+
},
|
|
329
|
+
"INPUT.BACKSPACE": [
|
|
330
|
+
{
|
|
331
|
+
guard: "hasValue",
|
|
332
|
+
actions: ["clearFocusedValue"]
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
actions: ["setPrevFocusedIndex", "clearFocusedValue"]
|
|
336
|
+
}
|
|
337
|
+
],
|
|
338
|
+
"INPUT.ENTER": {
|
|
339
|
+
guard: "isValueComplete",
|
|
340
|
+
actions: "requestFormSubmit"
|
|
341
|
+
},
|
|
342
|
+
"VALUE.INVALID": {
|
|
343
|
+
actions: "invokeOnInvalid"
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
guards: {
|
|
351
|
+
autoFocus: (ctx2) => !!ctx2.autoFocus,
|
|
352
|
+
isValueEmpty: (_ctx, evt) => evt.value === "",
|
|
353
|
+
hasValue: (ctx2) => ctx2.value[ctx2.focusedIndex] !== "",
|
|
354
|
+
isValueComplete: (ctx2) => ctx2.isValueComplete,
|
|
355
|
+
isFinalValue: (ctx2) => ctx2.filledValueLength + 1 === ctx2.valueLength && ctx2.value.findIndex((v) => v.trim() === "") === ctx2.focusedIndex,
|
|
356
|
+
hasIndex: (_ctx, evt) => evt.index !== void 0,
|
|
357
|
+
isDisabled: (ctx2) => !!ctx2.disabled
|
|
358
|
+
},
|
|
359
|
+
actions: {
|
|
360
|
+
setupValue(ctx2) {
|
|
361
|
+
if (ctx2.value.length)
|
|
362
|
+
return;
|
|
363
|
+
const inputEls = dom.getInputEls(ctx2);
|
|
364
|
+
const emptyValues = Array.from({ length: inputEls.length }).fill("");
|
|
365
|
+
assignValue(ctx2, emptyValues);
|
|
366
|
+
},
|
|
367
|
+
focusInput(ctx2) {
|
|
368
|
+
if (ctx2.focusedIndex === -1)
|
|
369
|
+
return;
|
|
370
|
+
dom.getFocusedInputEl(ctx2)?.focus({ preventScroll: true });
|
|
371
|
+
},
|
|
372
|
+
selectInputIfNeeded(ctx2) {
|
|
373
|
+
if (!ctx2.selectOnFocus || ctx2.focusedIndex === -1)
|
|
374
|
+
return;
|
|
375
|
+
(0, import_dom_query3.raf)(() => {
|
|
376
|
+
dom.getFocusedInputEl(ctx2)?.select();
|
|
377
|
+
});
|
|
378
|
+
},
|
|
379
|
+
invokeOnComplete(ctx2) {
|
|
380
|
+
if (!ctx2.isValueComplete)
|
|
381
|
+
return;
|
|
382
|
+
ctx2.onValueComplete?.({
|
|
383
|
+
value: Array.from(ctx2.value),
|
|
384
|
+
valueAsString: ctx2.valueAsString
|
|
385
|
+
});
|
|
386
|
+
},
|
|
387
|
+
invokeOnInvalid(ctx2, evt) {
|
|
388
|
+
ctx2.onValueInvalid?.({
|
|
389
|
+
value: evt.value,
|
|
390
|
+
index: ctx2.focusedIndex
|
|
391
|
+
});
|
|
392
|
+
},
|
|
393
|
+
clearFocusedIndex(ctx2) {
|
|
394
|
+
ctx2.focusedIndex = -1;
|
|
395
|
+
},
|
|
396
|
+
setFocusedIndex(ctx2, evt) {
|
|
397
|
+
ctx2.focusedIndex = evt.index;
|
|
398
|
+
},
|
|
399
|
+
setValue(ctx2, evt) {
|
|
400
|
+
set.value(ctx2, evt.value);
|
|
401
|
+
},
|
|
402
|
+
setFocusedValue(ctx2, evt) {
|
|
403
|
+
const nextValue = getNextValue(ctx2.focusedValue, evt.value);
|
|
404
|
+
set.valueAtIndex(ctx2, ctx2.focusedIndex, nextValue);
|
|
405
|
+
},
|
|
406
|
+
revertInputValue(ctx2) {
|
|
407
|
+
const inputEl = dom.getFocusedInputEl(ctx2);
|
|
408
|
+
dom.setValue(inputEl, ctx2.focusedValue);
|
|
409
|
+
},
|
|
410
|
+
syncInputValue(ctx2, evt) {
|
|
411
|
+
const inputEl = dom.getInputEl(ctx2, evt.index.toString());
|
|
412
|
+
dom.setValue(inputEl, ctx2.value[evt.index]);
|
|
413
|
+
},
|
|
414
|
+
syncInputElements(ctx2) {
|
|
415
|
+
const inputEls = dom.getInputEls(ctx2);
|
|
416
|
+
inputEls.forEach((inputEl, index) => {
|
|
417
|
+
dom.setValue(inputEl, ctx2.value[index]);
|
|
418
|
+
});
|
|
419
|
+
},
|
|
420
|
+
setPastedValue(ctx2, evt) {
|
|
421
|
+
(0, import_dom_query3.raf)(() => {
|
|
422
|
+
const startIndex = Math.min(ctx2.focusedIndex, ctx2.filledValueLength);
|
|
423
|
+
const left = startIndex > 0 ? ctx2.valueAsString.substring(0, ctx2.focusedIndex) : "";
|
|
424
|
+
const right = evt.value.substring(0, ctx2.valueLength - startIndex);
|
|
425
|
+
const value = left + right;
|
|
426
|
+
set.value(ctx2, value.split(""));
|
|
427
|
+
});
|
|
428
|
+
},
|
|
429
|
+
setValueAtIndex(ctx2, evt) {
|
|
430
|
+
const nextValue = getNextValue(ctx2.focusedValue, evt.value);
|
|
431
|
+
set.valueAtIndex(ctx2, evt.index, nextValue);
|
|
432
|
+
},
|
|
433
|
+
clearValue(ctx2) {
|
|
434
|
+
const nextValue = Array.from({ length: ctx2.valueLength }).fill("");
|
|
435
|
+
set.value(ctx2, nextValue);
|
|
436
|
+
},
|
|
437
|
+
clearFocusedValue(ctx2) {
|
|
438
|
+
set.valueAtIndex(ctx2, ctx2.focusedIndex, "");
|
|
439
|
+
},
|
|
440
|
+
setFocusIndexToFirst(ctx2) {
|
|
441
|
+
ctx2.focusedIndex = 0;
|
|
442
|
+
},
|
|
443
|
+
setNextFocusedIndex(ctx2) {
|
|
444
|
+
ctx2.focusedIndex = Math.min(ctx2.focusedIndex + 1, ctx2.valueLength - 1);
|
|
445
|
+
},
|
|
446
|
+
setPrevFocusedIndex(ctx2) {
|
|
447
|
+
ctx2.focusedIndex = Math.max(ctx2.focusedIndex - 1, 0);
|
|
448
|
+
},
|
|
449
|
+
setLastValueFocusIndex(ctx2) {
|
|
450
|
+
(0, import_dom_query3.raf)(() => {
|
|
451
|
+
ctx2.focusedIndex = Math.min(ctx2.filledValueLength, ctx2.valueLength - 1);
|
|
452
|
+
});
|
|
453
|
+
},
|
|
454
|
+
blurFocusedInputIfNeeded(ctx2) {
|
|
455
|
+
if (!ctx2.blurOnComplete)
|
|
456
|
+
return;
|
|
457
|
+
(0, import_dom_query3.raf)(() => {
|
|
458
|
+
dom.getFocusedInputEl(ctx2)?.blur();
|
|
459
|
+
});
|
|
460
|
+
},
|
|
461
|
+
requestFormSubmit(ctx2) {
|
|
462
|
+
if (!ctx2.name || !ctx2.isValueComplete)
|
|
463
|
+
return;
|
|
464
|
+
const inputEl = dom.getHiddenInputEl(ctx2);
|
|
465
|
+
inputEl?.form?.requestSubmit();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
function assignValue(ctx, value) {
|
|
472
|
+
const arr = Array.isArray(value) ? value : value.split("").filter(Boolean);
|
|
473
|
+
arr.forEach((value2, index) => {
|
|
474
|
+
ctx.value[index] = value2;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
function getNextValue(current, next) {
|
|
478
|
+
let nextValue = next;
|
|
479
|
+
if (current[0] === next[0])
|
|
480
|
+
nextValue = next[1];
|
|
481
|
+
else if (current[0] === next[1])
|
|
482
|
+
nextValue = next[0];
|
|
483
|
+
return nextValue.split("")[nextValue.length - 1];
|
|
484
|
+
}
|
|
485
|
+
var invoke = {
|
|
486
|
+
change(ctx) {
|
|
487
|
+
ctx.onValueChange?.({
|
|
488
|
+
value: Array.from(ctx.value),
|
|
489
|
+
valueAsString: ctx.valueAsString
|
|
490
|
+
});
|
|
491
|
+
const inputEl = dom.getHiddenInputEl(ctx);
|
|
492
|
+
(0, import_form_utils.dispatchInputValueEvent)(inputEl, { value: ctx.valueAsString });
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
var set = {
|
|
496
|
+
value(ctx, value) {
|
|
497
|
+
if ((0, import_utils2.isEqual)(ctx.value, value))
|
|
498
|
+
return;
|
|
499
|
+
assignValue(ctx, value);
|
|
500
|
+
invoke.change(ctx);
|
|
501
|
+
},
|
|
502
|
+
valueAtIndex(ctx, index, value) {
|
|
503
|
+
if ((0, import_utils2.isEqual)(ctx.value[index], value))
|
|
504
|
+
return;
|
|
505
|
+
ctx.value[index] = value;
|
|
506
|
+
invoke.change(ctx);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
510
|
+
0 && (module.exports = {
|
|
511
|
+
anatomy,
|
|
512
|
+
connect,
|
|
513
|
+
machine
|
|
514
|
+
});
|
|
2
515
|
//# sourceMappingURL=index.js.map
|