@tamagui/form 2.7.7 → 3.0.0-beta.643.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/Form.mjs CHANGED
@@ -1,57 +1,208 @@
1
- import { View, createStyledContext, styled } from "@tamagui/core";
1
+ import { View, createStyledContext, createStyledHOC, isWeb, styled } from "@tamagui/core";
2
+ import { focusFocusable } from "@tamagui/focusable";
2
3
  import { composeEventHandlers, withStaticProperties } from "@tamagui/helpers";
4
+ import * as React from "react";
3
5
  import { jsx } from "react/jsx-runtime";
6
+
4
7
  const FORM_NAME = "Form";
5
- const FormFrame = styled(View, {
6
- name: FORM_NAME,
7
- render: "form"
8
- });
9
- const FormContext = createStyledContext({
10
- onSubmit: void 0
8
+ const EMPTY_ERRORS = {};
9
+ const defaultSubmitAttemptedRef = { current: false };
10
+ const defaultFormElementRef = { current: null };
11
+ const FormRegistryContext = React.createContext({
12
+ validationMode: "onSubmit",
13
+ errors: EMPTY_ERRORS,
14
+ formElementRef: defaultFormElementRef,
15
+ submitAttemptedRef: defaultSubmitAttemptedRef,
16
+ clearErrors: () => {},
17
+ getValues: () => ({}),
18
+ registerField: () => () => {}
11
19
  });
12
- const {
13
- useStyledContext: useFormContext,
14
- Provider: FormProvider
15
- } = FormContext;
16
- const FormTriggerFrame = styled(View, {
17
- name: "FormTrigger"
18
- });
19
- const FormTrigger = FormTriggerFrame.styleable((props, forwardedRef) => {
20
- const {
21
- scope,
22
- children,
23
- onPress,
24
- ...triggerProps
25
- } = props;
26
- const context = useFormContext(scope);
27
- return /* @__PURE__ */jsx(FormTriggerFrame, {
28
- role: "button",
29
- ...triggerProps,
30
- ref: forwardedRef,
31
- onPress: composeEventHandlers(onPress, context.onSubmit),
32
- children
33
- });
20
+ const useFormRegistryContext = () => React.useContext(FormRegistryContext);
21
+ const FormContext = createStyledContext({ onSubmit: void 0 });
22
+ const { useStyledContext: useFormContext, Provider: FormProvider } = FormContext;
23
+ const FormFrame = styled(View, {
24
+ displayName: FORM_NAME,
25
+ render: "form"
34
26
  });
35
- const FormComponent = FormFrame.styleable(function Form({
36
- scope,
37
- onSubmit,
38
- ...props
39
- }, ref) {
40
- return /* @__PURE__ */jsx(FormProvider, {
41
- scope,
42
- onSubmit,
43
- children: /* @__PURE__ */jsx(FormFrame, {
44
- ref,
45
- ...props,
46
- onSubmit: e => {
47
- e.preventDefault();
48
- onSubmit?.();
49
- }
50
- })
51
- });
27
+ const FormTriggerFrame = styled(View, { displayName: "FormTrigger" });
28
+ const FormTrigger = createStyledHOC(FormTriggerFrame, (props, forwardedRef) => {
29
+ const { scope, children, onPress, ...triggerProps } = props;
30
+ const context = useFormContext(scope);
31
+ return /* @__PURE__ */ jsx(FormTriggerFrame, {
32
+ role: "button",
33
+ ...triggerProps,
34
+ ref: forwardedRef,
35
+ onPress: composeEventHandlers(onPress, context.onSubmit),
36
+ children
37
+ });
52
38
  });
53
- const Form2 = withStaticProperties(FormComponent, {
54
- Trigger: FormTrigger
39
+ function hasError(errors, name) {
40
+ if (!name || !Object.hasOwn(errors, name)) {
41
+ return false;
42
+ }
43
+ const error = errors[name];
44
+ return Array.isArray(error) ? error.length > 0 : Boolean(error);
45
+ }
46
+ const FormComponent = createStyledHOC(FormFrame, function Form({ scope, validationMode = "onSubmit", errors: externalErrors, actionsRef, onSubmit, ...props }, forwardedRef) {
47
+ const registryRef = React.useRef(/* @__PURE__ */ new Map());
48
+ const formElementRef = React.useRef(null);
49
+ const submittedRef = React.useRef(false);
50
+ const submitAttemptedRef = React.useRef(false);
51
+ const pendingSubmitDetailsRef = React.useRef(null);
52
+ const onSubmitRef = React.useRef(onSubmit);
53
+ onSubmitRef.current = onSubmit;
54
+ const [errors, setErrors] = React.useState(externalErrors ?? EMPTY_ERRORS);
55
+ const errorsRef = React.useRef(errors);
56
+ errorsRef.current = errors;
57
+ React.useEffect(() => {
58
+ const next = externalErrors ?? EMPTY_ERRORS;
59
+ errorsRef.current = next;
60
+ setErrors(next);
61
+ }, [externalErrors]);
62
+ const registerField = React.useCallback((id, registration) => {
63
+ registryRef.current.set(id, registration);
64
+ return () => {
65
+ if (registryRef.current.get(id) === registration) {
66
+ registryRef.current.delete(id);
67
+ }
68
+ };
69
+ }, []);
70
+ const getValues = React.useCallback(() => {
71
+ const values = {};
72
+ for (const field of registryRef.current.values()) {
73
+ if (field.name) {
74
+ values[field.name] = field.getValue();
75
+ }
76
+ }
77
+ return values;
78
+ }, []);
79
+ const clearErrors = React.useCallback((name) => {
80
+ if (!name || !hasError(errorsRef.current, name)) {
81
+ return;
82
+ }
83
+ const next = { ...errorsRef.current };
84
+ delete next[name];
85
+ errorsRef.current = next;
86
+ setErrors(next);
87
+ }, []);
88
+ const focusFirstInvalid = React.useCallback(() => {
89
+ let hasInvalid = false;
90
+ for (const field of registryRef.current.values()) {
91
+ if (!hasError(errorsRef.current, field.name) && field.validityData.state.valid !== false) {
92
+ continue;
93
+ }
94
+ hasInvalid = true;
95
+ const control = field.controlRef.current;
96
+ if (isWeb && control?.focus) {
97
+ control.focus();
98
+ if (control.tagName === "INPUT" || control.tagName === "TEXTAREA") {
99
+ control.select?.();
100
+ }
101
+ return true;
102
+ }
103
+ if (!isWeb && field.controlId) {
104
+ focusFocusable(field.controlId);
105
+ return true;
106
+ }
107
+ if (control?.focus) {
108
+ control.focus();
109
+ return true;
110
+ }
111
+ }
112
+ return hasInvalid;
113
+ }, []);
114
+ const performSubmit = React.useCallback((details) => {
115
+ submitAttemptedRef.current = true;
116
+ for (const field of registryRef.current.values()) {
117
+ field.validate();
118
+ }
119
+ if (focusFirstInvalid()) {
120
+ return;
121
+ }
122
+ submittedRef.current = true;
123
+ void onSubmitRef.current?.(getValues(), details);
124
+ }, [focusFirstInvalid, getValues]);
125
+ const requestSubmit = React.useCallback((event) => {
126
+ event?.preventDefault?.();
127
+ const details = {
128
+ reason: "trigger-press",
129
+ event: event?.nativeEvent ?? event,
130
+ trigger: event?.currentTarget
131
+ };
132
+ if (isWeb && formElementRef.current?.requestSubmit) {
133
+ pendingSubmitDetailsRef.current = details;
134
+ formElementRef.current.requestSubmit();
135
+ return;
136
+ }
137
+ performSubmit(details);
138
+ }, [performSubmit]);
139
+ React.useEffect(() => {
140
+ if (!submittedRef.current) {
141
+ return;
142
+ }
143
+ submittedRef.current = false;
144
+ focusFirstInvalid();
145
+ }, [errors, focusFirstInvalid]);
146
+ React.useImperativeHandle(actionsRef, () => ({ validate(fieldName) {
147
+ if (fieldName) {
148
+ for (const field of registryRef.current.values()) {
149
+ if (field.name === fieldName) {
150
+ field.validate();
151
+ return;
152
+ }
153
+ }
154
+ return;
155
+ }
156
+ for (const field of registryRef.current.values()) {
157
+ field.validate();
158
+ }
159
+ } }), []);
160
+ const registryContext = React.useMemo(() => ({
161
+ validationMode,
162
+ errors,
163
+ formElementRef,
164
+ submitAttemptedRef,
165
+ clearErrors,
166
+ getValues,
167
+ registerField
168
+ }), [
169
+ clearErrors,
170
+ errors,
171
+ getValues,
172
+ registerField,
173
+ validationMode
174
+ ]);
175
+ return /* @__PURE__ */ jsx(FormRegistryContext.Provider, {
176
+ value: registryContext,
177
+ children: /* @__PURE__ */ jsx(FormProvider, {
178
+ scope,
179
+ onSubmit: requestSubmit,
180
+ children: /* @__PURE__ */ jsx(FormFrame, {
181
+ ...props,
182
+ ref: (element) => {
183
+ formElementRef.current = element;
184
+ if (typeof forwardedRef === "function") {
185
+ forwardedRef(element);
186
+ } else if (forwardedRef) {
187
+ forwardedRef.current = element;
188
+ }
189
+ },
190
+ noValidate: true,
191
+ onSubmit: (event) => {
192
+ event.preventDefault();
193
+ const pendingDetails = pendingSubmitDetailsRef.current;
194
+ pendingSubmitDetailsRef.current = null;
195
+ performSubmit(pendingDetails ?? {
196
+ reason: "submit",
197
+ event: event.nativeEvent ?? event,
198
+ trigger: event.nativeEvent?.submitter
199
+ });
200
+ }
201
+ })
202
+ })
203
+ });
55
204
  });
56
- export { Form2 as Form, FormContext, FormFrame, FormProvider, FormTrigger, useFormContext };
205
+ const Form2 = withStaticProperties(FormComponent, { Trigger: FormTrigger });
206
+
207
+ export { Form2 as Form, FormContext, FormFrame, FormProvider, FormRegistryContext, FormTrigger, useFormContext, useFormRegistryContext };
57
208
  //# sourceMappingURL=Form.mjs.map
@@ -1 +1 @@
1
- {"version":3,"names":["View","createStyledContext","styled","composeEventHandlers","withStaticProperties","jsx","FORM_NAME","FormFrame","name","render","FormContext","onSubmit","useStyledContext","useFormContext","Provider","FormProvider","FormTriggerFrame","FormTrigger","styleable","props","forwardedRef","scope","children","onPress","triggerProps","context","role","ref","FormComponent","Form","e","preventDefault","Form2","Trigger"],"sources":["../../src/Form.tsx"],"sourcesContent":[null],"mappings":"AACA,SAASA,IAAA,EAAMC,mBAAA,EAAqBC,MAAA,QAAc;AAClD,SAASC,oBAAA,EAAsBC,oBAAA,QAA4B;AA+CrD,SAAAC,GAAA;AA7CN,MAAMC,SAAA,GAAY;AAEX,MAAMC,SAAA,GAAYL,MAAA,CAAOF,IAAA,EAAM;EACpCQ,IAAA,EAAMF,SAAA;EACNG,MAAA,EAAQ;AACV,CAAC;AAUM,MAAMC,WAAA,GAAcT,mBAAA,CAAsC;EAC/DU,QAAA,EAAU;AACZ,CAAqB;AAEd,MAAM;EAAEC,gBAAA,EAAkBC,cAAA;EAAgBC,QAAA,EAAUC;AAAa,IAAIL,WAAA;AAY5E,MAAMM,gBAAA,GAAmBd,MAAA,CAAOF,IAAA,EAAM;EACpCQ,IAAA,EAAM;AACR,CAAC;AAMM,MAAMS,WAAA,GAAcD,gBAAA,CAAiBE,SAAA,CAC1C,CAACC,KAAA,EAAOC,YAAA,KAAiB;EACvB,MAAM;IAAEC,KAAA;IAAOC,QAAA;IAAUC,OAAA;IAAS,GAAGC;EAAa,IAAIL,KAAA;EACtD,MAAMM,OAAA,GAAUZ,cAAA,CAAeQ,KAAK;EAEpC,OACE,eAAAhB,GAAA,CAACW,gBAAA;IACCU,IAAA,EAAK;IACJ,GAAGF,YAAA;IACJG,GAAA,EAAKP,YAAA;IACLG,OAAA,EAASpB,oBAAA,CAAqBoB,OAAA,EAASE,OAAA,CAAQd,QAAQ;IAEtDW;EAAA,CACH;AAEJ,CACF;AAMA,MAAMM,aAAA,GAAgBrB,SAAA,CAAUW,SAAA,CAA0B,SAASW,KACjE;EAAER,KAAA;EAAOV,QAAA;EAAU,GAAGQ;AAAM,GAC5BQ,GAAA,EACA;EACA,OACE,eAAAtB,GAAA,CAACU,YAAA;IAAaM,KAAA;IAAcV,QAAA;IAC1BW,QAAA,iBAAAjB,GAAA,CAACE,SAAA;MACCoB,GAAA;MACC,GAAIR,KAAA;MACLR,QAAA,EAAWmB,CAAA,IAAW;QACpBA,CAAA,CAAEC,cAAA,CAAe;QACjBpB,QAAA,GAAW;MACb;IAAA,CACF;EAAA,CACF;AAEJ,CAAC;AAEM,MAAMqB,KAAA,GAAO5B,oBAAA,CAAqBwB,aAAA,EAAe;EACtDK,OAAA,EAAShB;AACX,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"Form.js","names":[],"sources":["esm/Form.js"],"sourcesContent":["import { createStyledContext, createStyledHOC, isWeb, styled, View } from \"@tamagui/core\";\nimport { focusFocusable } from \"@tamagui/focusable\";\nimport { composeEventHandlers, withStaticProperties } from \"@tamagui/helpers\";\nimport * as React from \"react\";\nimport { jsx } from \"react/jsx-runtime\";\nconst FORM_NAME = \"Form\";\nconst EMPTY_ERRORS = {};\nconst defaultSubmitAttemptedRef = { current: false };\nconst defaultFormElementRef = { current: null };\nconst FormRegistryContext = React.createContext({\n validationMode: \"onSubmit\",\n errors: EMPTY_ERRORS,\n formElementRef: defaultFormElementRef,\n submitAttemptedRef: defaultSubmitAttemptedRef,\n clearErrors: () => {\n },\n getValues: () => ({}),\n registerField: () => () => {\n }\n});\nconst useFormRegistryContext = () => React.useContext(FormRegistryContext);\nconst FormContext = createStyledContext({\n onSubmit: void 0\n});\nconst { useStyledContext: useFormContext, Provider: FormProvider } = FormContext;\nconst FormFrame = styled(View, {\n displayName: FORM_NAME,\n render: \"form\"\n});\nconst FormTriggerFrame = styled(View, {\n displayName: \"FormTrigger\"\n});\nconst FormTrigger = createStyledHOC(\n FormTriggerFrame,\n (props, forwardedRef) => {\n const { scope, children, onPress, ...triggerProps } = props;\n const context = useFormContext(scope);\n return /* @__PURE__ */ jsx(\n FormTriggerFrame,\n {\n role: \"button\",\n ...triggerProps,\n ref: forwardedRef,\n onPress: composeEventHandlers(onPress, context.onSubmit),\n children\n }\n );\n }\n);\nfunction hasError(errors, name) {\n if (!name || !Object.hasOwn(errors, name)) {\n return false;\n }\n const error = errors[name];\n return Array.isArray(error) ? error.length > 0 : Boolean(error);\n}\nconst FormComponent = createStyledHOC(\n FormFrame,\n function Form({\n scope,\n validationMode = \"onSubmit\",\n errors: externalErrors,\n actionsRef,\n onSubmit,\n ...props\n }, forwardedRef) {\n const registryRef = React.useRef(/* @__PURE__ */ new Map());\n const formElementRef = React.useRef(null);\n const submittedRef = React.useRef(false);\n const submitAttemptedRef = React.useRef(false);\n const pendingSubmitDetailsRef = React.useRef(null);\n const onSubmitRef = React.useRef(onSubmit);\n onSubmitRef.current = onSubmit;\n const [errors, setErrors] = React.useState(externalErrors ?? EMPTY_ERRORS);\n const errorsRef = React.useRef(errors);\n errorsRef.current = errors;\n React.useEffect(() => {\n const next = externalErrors ?? EMPTY_ERRORS;\n errorsRef.current = next;\n setErrors(next);\n }, [externalErrors]);\n const registerField = React.useCallback(\n (id, registration) => {\n registryRef.current.set(id, registration);\n return () => {\n if (registryRef.current.get(id) === registration) {\n registryRef.current.delete(id);\n }\n };\n },\n []\n );\n const getValues = React.useCallback(() => {\n const values = {};\n for (const field of registryRef.current.values()) {\n if (field.name) {\n values[field.name] = field.getValue();\n }\n }\n return values;\n }, []);\n const clearErrors = React.useCallback((name) => {\n if (!name || !hasError(errorsRef.current, name)) {\n return;\n }\n const next = { ...errorsRef.current };\n delete next[name];\n errorsRef.current = next;\n setErrors(next);\n }, []);\n const focusFirstInvalid = React.useCallback(() => {\n let hasInvalid = false;\n for (const field of registryRef.current.values()) {\n if (!hasError(errorsRef.current, field.name) && field.validityData.state.valid !== false) {\n continue;\n }\n hasInvalid = true;\n const control = field.controlRef.current;\n if (isWeb && control?.focus) {\n control.focus();\n if (control.tagName === \"INPUT\" || control.tagName === \"TEXTAREA\") {\n control.select?.();\n }\n return true;\n }\n if (!isWeb && field.controlId) {\n focusFocusable(field.controlId);\n return true;\n }\n if (control?.focus) {\n control.focus();\n return true;\n }\n }\n return hasInvalid;\n }, []);\n const performSubmit = React.useCallback(\n (details) => {\n submitAttemptedRef.current = true;\n for (const field of registryRef.current.values()) {\n field.validate();\n }\n if (focusFirstInvalid()) {\n return;\n }\n submittedRef.current = true;\n void onSubmitRef.current?.(getValues(), details);\n },\n [focusFirstInvalid, getValues]\n );\n const requestSubmit = React.useCallback(\n (event) => {\n event?.preventDefault?.();\n const details = {\n reason: \"trigger-press\",\n event: event?.nativeEvent ?? event,\n trigger: event?.currentTarget\n };\n if (isWeb && formElementRef.current?.requestSubmit) {\n pendingSubmitDetailsRef.current = details;\n formElementRef.current.requestSubmit();\n return;\n }\n performSubmit(details);\n },\n [performSubmit]\n );\n React.useEffect(() => {\n if (!submittedRef.current) {\n return;\n }\n submittedRef.current = false;\n focusFirstInvalid();\n }, [errors, focusFirstInvalid]);\n React.useImperativeHandle(\n actionsRef,\n () => ({\n validate(fieldName) {\n if (fieldName) {\n for (const field of registryRef.current.values()) {\n if (field.name === fieldName) {\n field.validate();\n return;\n }\n }\n return;\n }\n for (const field of registryRef.current.values()) {\n field.validate();\n }\n }\n }),\n []\n );\n const registryContext = React.useMemo(\n () => ({\n validationMode,\n errors,\n formElementRef,\n submitAttemptedRef,\n clearErrors,\n getValues,\n registerField\n }),\n [clearErrors, errors, getValues, registerField, validationMode]\n );\n return /* @__PURE__ */ jsx(FormRegistryContext.Provider, { value: registryContext, children: /* @__PURE__ */ jsx(FormProvider, { scope, onSubmit: requestSubmit, children: /* @__PURE__ */ jsx(\n FormFrame,\n {\n ...props,\n ref: (element) => {\n formElementRef.current = element;\n if (typeof forwardedRef === \"function\") {\n forwardedRef(element);\n } else if (forwardedRef) {\n forwardedRef.current = element;\n }\n },\n noValidate: true,\n onSubmit: (event) => {\n event.preventDefault();\n const pendingDetails = pendingSubmitDetailsRef.current;\n pendingSubmitDetailsRef.current = null;\n performSubmit(\n pendingDetails ?? {\n reason: \"submit\",\n event: event.nativeEvent ?? event,\n trigger: event.nativeEvent?.submitter\n }\n );\n }\n }\n ) }) });\n }\n);\nconst Form2 = withStaticProperties(FormComponent, {\n Trigger: FormTrigger\n});\nexport {\n Form2 as Form,\n FormContext,\n FormFrame,\n FormProvider,\n FormRegistryContext,\n FormTrigger,\n useFormContext,\n useFormRegistryContext\n};\n//# sourceMappingURL=Form.js.map\n"],"mappings":";;;;;;;AAKA,MAAM,YAAY;AAClB,MAAM,eAAe,CAAC;AACtB,MAAM,4BAA4B,EAAE,SAAS,MAAM;AACnD,MAAM,wBAAwB,EAAE,SAAS,KAAK;AAC9C,MAAM,sBAAsB,MAAM,cAAc;CAC9C,gBAAgB;CAChB,QAAQ;CACR,gBAAgB;CAChB,oBAAoB;CACpB,mBAAmB,CACnB;CACA,kBAAkB,CAAC;CACnB,2BAA2B,CAC3B;AACF,CAAC;AACD,MAAM,+BAA+B,MAAM,WAAW,mBAAmB;AACzE,MAAM,cAAc,oBAAoB,EACtC,UAAU,KAAK,EACjB,CAAC;AACD,MAAM,EAAE,kBAAkB,gBAAgB,UAAU,iBAAiB;AACrE,MAAM,YAAY,OAAO,MAAM;CAC7B,aAAa;CACb,QAAQ;AACV,CAAC;AACD,MAAM,mBAAmB,OAAO,MAAM,EACpC,aAAa,cACf,CAAC;AACD,MAAM,cAAc,gBAClB,mBACC,OAAO,iBAAiB;CACvB,MAAM,EAAE,OAAO,UAAU,SAAS,GAAG,iBAAiB;CACtD,MAAM,UAAU,eAAe,KAAK;CACpC,OAAuB,oBACrB,kBACA;EACE,MAAM;EACN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S,YACX,CAAC"}
@@ -1,58 +1,310 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { View, createStyledContext, styled } from "@tamagui/core";
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { View, createStyledContext, createStyledHOC, isWeb, styled } from "@tamagui/core";
3
+ import { focusFocusable } from "@tamagui/focusable";
3
4
  import { composeEventHandlers, withStaticProperties } from "@tamagui/helpers";
5
+ import * as React from "react";
6
+
4
7
  var FORM_NAME = "Form";
5
- var FormFrame = styled(View, {
6
- name: FORM_NAME,
7
- render: "form"
8
- });
9
- var FormContext = createStyledContext({
10
- onSubmit: void 0
8
+ var EMPTY_ERRORS = {};
9
+ var defaultSubmitAttemptedRef = { current: false };
10
+ var defaultFormElementRef = { current: null };
11
+ var FormRegistryContext = /* @__PURE__ */ React.createContext({
12
+ validationMode: "onSubmit",
13
+ errors: EMPTY_ERRORS,
14
+ formElementRef: defaultFormElementRef,
15
+ submitAttemptedRef: defaultSubmitAttemptedRef,
16
+ clearErrors: function() {},
17
+ getValues: function() {
18
+ return {};
19
+ },
20
+ registerField: function() {
21
+ return function() {};
22
+ }
11
23
  });
12
- var {
13
- useStyledContext: useFormContext,
14
- Provider: FormProvider
15
- } = FormContext;
16
- var FormTriggerFrame = styled(View, {
17
- name: "FormTrigger"
18
- });
19
- var FormTrigger = FormTriggerFrame.styleable(function (props, forwardedRef) {
20
- var {
21
- scope,
22
- children,
23
- onPress,
24
- ...triggerProps
25
- } = props;
26
- var context = useFormContext(scope);
27
- return /* @__PURE__ */_jsx(FormTriggerFrame, {
28
- role: "button",
29
- ...triggerProps,
30
- ref: forwardedRef,
31
- onPress: composeEventHandlers(onPress, context.onSubmit),
32
- children
33
- });
24
+ var useFormRegistryContext = function() {
25
+ return React.useContext(FormRegistryContext);
26
+ };
27
+ var FormContext = createStyledContext({ onSubmit: void 0 });
28
+ var { useStyledContext: useFormContext, Provider: FormProvider } = FormContext;
29
+ var FormFrame = styled(View, {
30
+ displayName: FORM_NAME,
31
+ render: "form"
34
32
  });
35
- var FormComponent = FormFrame.styleable(function Form(param, ref) {
36
- var {
37
- scope,
38
- onSubmit,
39
- ...props
40
- } = param;
41
- return /* @__PURE__ */_jsx(FormProvider, {
42
- scope,
43
- onSubmit,
44
- children: /* @__PURE__ */_jsx(FormFrame, {
45
- ref,
46
- ...props,
47
- onSubmit: function (e) {
48
- e.preventDefault();
49
- onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit();
50
- }
51
- })
52
- });
33
+ var FormTriggerFrame = styled(View, { displayName: "FormTrigger" });
34
+ var FormTrigger = createStyledHOC(FormTriggerFrame, function(props, forwardedRef) {
35
+ var { scope, children, onPress, ...triggerProps } = props;
36
+ var context = useFormContext(scope);
37
+ return /* @__PURE__ */ jsx(FormTriggerFrame, {
38
+ role: "button",
39
+ ...triggerProps,
40
+ ref: forwardedRef,
41
+ onPress: composeEventHandlers(onPress, context.onSubmit),
42
+ children
43
+ });
53
44
  });
54
- var Form2 = withStaticProperties(FormComponent, {
55
- Trigger: FormTrigger
45
+ function hasError(errors, name) {
46
+ if (!name || !Object.hasOwn(errors, name)) {
47
+ return false;
48
+ }
49
+ var error = errors[name];
50
+ return Array.isArray(error) ? error.length > 0 : Boolean(error);
51
+ }
52
+ var FormComponent = createStyledHOC(FormFrame, function Form(param, forwardedRef) {
53
+ var { scope, validationMode = "onSubmit", errors: externalErrors, actionsRef, onSubmit, ...props } = param;
54
+ var registryRef = React.useRef(/* @__PURE__ */ new Map());
55
+ var formElementRef = React.useRef(null);
56
+ var submittedRef = React.useRef(false);
57
+ var submitAttemptedRef = React.useRef(false);
58
+ var pendingSubmitDetailsRef = React.useRef(null);
59
+ var onSubmitRef = React.useRef(onSubmit);
60
+ onSubmitRef.current = onSubmit;
61
+ var [errors, setErrors] = React.useState(externalErrors !== null && externalErrors !== void 0 ? externalErrors : EMPTY_ERRORS);
62
+ var errorsRef = React.useRef(errors);
63
+ errorsRef.current = errors;
64
+ React.useEffect(function() {
65
+ var next = externalErrors !== null && externalErrors !== void 0 ? externalErrors : EMPTY_ERRORS;
66
+ errorsRef.current = next;
67
+ setErrors(next);
68
+ }, [externalErrors]);
69
+ var registerField = React.useCallback(function(id, registration) {
70
+ registryRef.current.set(id, registration);
71
+ return function() {
72
+ if (registryRef.current.get(id) === registration) {
73
+ registryRef.current.delete(id);
74
+ }
75
+ };
76
+ }, []);
77
+ var getValues = React.useCallback(function() {
78
+ var values = {};
79
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
80
+ try {
81
+ for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
82
+ var field = _step.value;
83
+ if (field.name) {
84
+ values[field.name] = field.getValue();
85
+ }
86
+ }
87
+ } catch (err) {
88
+ _didIteratorError = true;
89
+ _iteratorError = err;
90
+ } finally {
91
+ try {
92
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
93
+ _iterator.return();
94
+ }
95
+ } finally {
96
+ if (_didIteratorError) {
97
+ throw _iteratorError;
98
+ }
99
+ }
100
+ }
101
+ return values;
102
+ }, []);
103
+ var clearErrors = React.useCallback(function(name) {
104
+ if (!name || !hasError(errorsRef.current, name)) {
105
+ return;
106
+ }
107
+ var next = { ...errorsRef.current };
108
+ delete next[name];
109
+ errorsRef.current = next;
110
+ setErrors(next);
111
+ }, []);
112
+ var focusFirstInvalid = React.useCallback(function() {
113
+ var hasInvalid = false;
114
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
115
+ try {
116
+ for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
117
+ var field = _step.value;
118
+ if (!hasError(errorsRef.current, field.name) && field.validityData.state.valid !== false) {
119
+ continue;
120
+ }
121
+ hasInvalid = true;
122
+ var control = field.controlRef.current;
123
+ if (isWeb && (control === null || control === void 0 ? void 0 : control.focus)) {
124
+ control.focus();
125
+ if (control.tagName === "INPUT" || control.tagName === "TEXTAREA") {
126
+ var _control_select;
127
+ (_control_select = control.select) === null || _control_select === void 0 ? void 0 : _control_select.call(control);
128
+ }
129
+ return true;
130
+ }
131
+ if (!isWeb && field.controlId) {
132
+ focusFocusable(field.controlId);
133
+ return true;
134
+ }
135
+ if (control === null || control === void 0 ? void 0 : control.focus) {
136
+ control.focus();
137
+ return true;
138
+ }
139
+ }
140
+ } catch (err) {
141
+ _didIteratorError = true;
142
+ _iteratorError = err;
143
+ } finally {
144
+ try {
145
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
146
+ _iterator.return();
147
+ }
148
+ } finally {
149
+ if (_didIteratorError) {
150
+ throw _iteratorError;
151
+ }
152
+ }
153
+ }
154
+ return hasInvalid;
155
+ }, []);
156
+ var performSubmit = React.useCallback(function(details) {
157
+ var _onSubmitRef_current;
158
+ submitAttemptedRef.current = true;
159
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
160
+ try {
161
+ for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
162
+ var field = _step.value;
163
+ field.validate();
164
+ }
165
+ } catch (err) {
166
+ _didIteratorError = true;
167
+ _iteratorError = err;
168
+ } finally {
169
+ try {
170
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
171
+ _iterator.return();
172
+ }
173
+ } finally {
174
+ if (_didIteratorError) {
175
+ throw _iteratorError;
176
+ }
177
+ }
178
+ }
179
+ if (focusFirstInvalid()) {
180
+ return;
181
+ }
182
+ submittedRef.current = true;
183
+ void ((_onSubmitRef_current = onSubmitRef.current) === null || _onSubmitRef_current === void 0 ? void 0 : _onSubmitRef_current.call(onSubmitRef, getValues(), details));
184
+ }, [focusFirstInvalid, getValues]);
185
+ var requestSubmit = React.useCallback(function(event) {
186
+ var _event_preventDefault, _formElementRef_current;
187
+ event === null || event === void 0 ? void 0 : (_event_preventDefault = event.preventDefault) === null || _event_preventDefault === void 0 ? void 0 : _event_preventDefault.call(event);
188
+ var _event_nativeEvent;
189
+ var details = {
190
+ reason: "trigger-press",
191
+ event: (_event_nativeEvent = event === null || event === void 0 ? void 0 : event.nativeEvent) !== null && _event_nativeEvent !== void 0 ? _event_nativeEvent : event,
192
+ trigger: event === null || event === void 0 ? void 0 : event.currentTarget
193
+ };
194
+ if (isWeb && ((_formElementRef_current = formElementRef.current) === null || _formElementRef_current === void 0 ? void 0 : _formElementRef_current.requestSubmit)) {
195
+ pendingSubmitDetailsRef.current = details;
196
+ formElementRef.current.requestSubmit();
197
+ return;
198
+ }
199
+ performSubmit(details);
200
+ }, [performSubmit]);
201
+ React.useEffect(function() {
202
+ if (!submittedRef.current) {
203
+ return;
204
+ }
205
+ submittedRef.current = false;
206
+ focusFirstInvalid();
207
+ }, [errors, focusFirstInvalid]);
208
+ React.useImperativeHandle(actionsRef, function() {
209
+ return { validate(fieldName) {
210
+ if (fieldName) {
211
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
212
+ try {
213
+ for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
214
+ var field = _step.value;
215
+ if (field.name === fieldName) {
216
+ field.validate();
217
+ return;
218
+ }
219
+ }
220
+ } catch (err) {
221
+ _didIteratorError = true;
222
+ _iteratorError = err;
223
+ } finally {
224
+ try {
225
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
226
+ _iterator.return();
227
+ }
228
+ } finally {
229
+ if (_didIteratorError) {
230
+ throw _iteratorError;
231
+ }
232
+ }
233
+ }
234
+ return;
235
+ }
236
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
237
+ try {
238
+ for (var _iterator1 = registryRef.current.values()[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
239
+ var field1 = _step1.value;
240
+ field1.validate();
241
+ }
242
+ } catch (err) {
243
+ _didIteratorError1 = true;
244
+ _iteratorError1 = err;
245
+ } finally {
246
+ try {
247
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
248
+ _iterator1.return();
249
+ }
250
+ } finally {
251
+ if (_didIteratorError1) {
252
+ throw _iteratorError1;
253
+ }
254
+ }
255
+ }
256
+ } };
257
+ }, []);
258
+ var registryContext = React.useMemo(function() {
259
+ return {
260
+ validationMode,
261
+ errors,
262
+ formElementRef,
263
+ submitAttemptedRef,
264
+ clearErrors,
265
+ getValues,
266
+ registerField
267
+ };
268
+ }, [
269
+ clearErrors,
270
+ errors,
271
+ getValues,
272
+ registerField,
273
+ validationMode
274
+ ]);
275
+ return /* @__PURE__ */ jsx(FormRegistryContext.Provider, {
276
+ value: registryContext,
277
+ children: /* @__PURE__ */ jsx(FormProvider, {
278
+ scope,
279
+ onSubmit: requestSubmit,
280
+ children: /* @__PURE__ */ jsx(FormFrame, {
281
+ ...props,
282
+ ref: function(element) {
283
+ formElementRef.current = element;
284
+ if (typeof forwardedRef === "function") {
285
+ forwardedRef(element);
286
+ } else if (forwardedRef) {
287
+ forwardedRef.current = element;
288
+ }
289
+ },
290
+ noValidate: true,
291
+ onSubmit: function(event) {
292
+ var _event_nativeEvent;
293
+ event.preventDefault();
294
+ var pendingDetails = pendingSubmitDetailsRef.current;
295
+ pendingSubmitDetailsRef.current = null;
296
+ var _event_nativeEvent1;
297
+ performSubmit(pendingDetails !== null && pendingDetails !== void 0 ? pendingDetails : {
298
+ reason: "submit",
299
+ event: (_event_nativeEvent1 = event.nativeEvent) !== null && _event_nativeEvent1 !== void 0 ? _event_nativeEvent1 : event,
300
+ trigger: (_event_nativeEvent = event.nativeEvent) === null || _event_nativeEvent === void 0 ? void 0 : _event_nativeEvent.submitter
301
+ });
302
+ }
303
+ })
304
+ })
305
+ });
56
306
  });
57
- export { Form2 as Form, FormContext, FormFrame, FormProvider, FormTrigger, useFormContext };
307
+ var Form2 = withStaticProperties(FormComponent, { Trigger: FormTrigger });
308
+
309
+ export { Form2 as Form, FormContext, FormFrame, FormProvider, FormRegistryContext, FormTrigger, useFormContext, useFormRegistryContext };
58
310
  //# sourceMappingURL=Form.native.js.map