@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/cjs/Form.cjs +223 -67
- package/dist/cjs/Form.native.cjs +347 -0
- package/dist/cjs/Form.native.js +326 -68
- package/dist/cjs/Form.native.js.map +1 -1
- package/dist/cjs/index.cjs +10 -11
- package/dist/cjs/index.native.cjs +21 -0
- package/dist/cjs/index.native.js +10 -10
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/esm/Form.mjs +200 -49
- package/dist/esm/Form.mjs.map +1 -1
- package/dist/esm/Form.native.js +303 -51
- package/dist/esm/Form.native.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/index.native.js +1 -2
- package/dist/jsx/Form.mjs +200 -49
- package/dist/jsx/Form.mjs.map +1 -1
- package/dist/jsx/Form.native.cjs +347 -0
- package/dist/jsx/Form.native.js +326 -68
- package/dist/jsx/Form.native.js.map +1 -1
- package/dist/jsx/index.js +1 -2
- package/dist/jsx/index.mjs +1 -2
- package/dist/jsx/index.native.cjs +21 -0
- package/dist/jsx/index.native.js +10 -10
- package/dist/jsx/index.native.js.map +1 -1
- package/package.json +5 -4
- package/src/Form.tsx +318 -47
- package/types/Form.d.ts +63 -16
- package/types/Form.d.ts.map +1 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/index.native.js.map +0 -1
- package/dist/jsx/index.js.map +0 -1
- package/dist/jsx/index.mjs.map +0 -1
package/dist/cjs/Form.cjs
CHANGED
|
@@ -1,87 +1,243 @@
|
|
|
1
|
+
|
|
2
|
+
var __create = Object.create;
|
|
1
3
|
var __defProp = Object.defineProperty;
|
|
2
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
8
|
var __export = (target, all) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
for (var name in all) __defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
10
13
|
};
|
|
11
14
|
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
17
|
+
get: () => from[key],
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
19
22
|
};
|
|
20
|
-
var
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
26
|
+
}) : target, mod));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
28
|
var Form_exports = {};
|
|
24
29
|
__export(Form_exports, {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
Form: () => Form2,
|
|
31
|
+
FormContext: () => FormContext,
|
|
32
|
+
FormFrame: () => FormFrame,
|
|
33
|
+
FormProvider: () => FormProvider,
|
|
34
|
+
FormRegistryContext: () => FormRegistryContext,
|
|
35
|
+
FormTrigger: () => FormTrigger,
|
|
36
|
+
useFormContext: () => useFormContext,
|
|
37
|
+
useFormRegistryContext: () => useFormRegistryContext
|
|
31
38
|
});
|
|
32
39
|
module.exports = __toCommonJS(Form_exports);
|
|
33
40
|
var import_core = require("@tamagui/core");
|
|
41
|
+
var import_focusable = require("@tamagui/focusable");
|
|
34
42
|
var import_helpers = require("@tamagui/helpers");
|
|
43
|
+
var React = __toESM(require("react"), 1);
|
|
35
44
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
36
45
|
const FORM_NAME = "Form";
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
const EMPTY_ERRORS = {};
|
|
47
|
+
const defaultSubmitAttemptedRef = { current: false };
|
|
48
|
+
const defaultFormElementRef = { current: null };
|
|
49
|
+
const FormRegistryContext = React.createContext({
|
|
50
|
+
validationMode: "onSubmit",
|
|
51
|
+
errors: EMPTY_ERRORS,
|
|
52
|
+
formElementRef: defaultFormElementRef,
|
|
53
|
+
submitAttemptedRef: defaultSubmitAttemptedRef,
|
|
54
|
+
clearErrors: () => {},
|
|
55
|
+
getValues: () => ({}),
|
|
56
|
+
registerField: () => () => {}
|
|
43
57
|
});
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
const useFormRegistryContext = () => React.useContext(FormRegistryContext);
|
|
59
|
+
const FormContext = (0, import_core.createStyledContext)({ onSubmit: void 0 });
|
|
60
|
+
const { useStyledContext: useFormContext, Provider: FormProvider } = FormContext;
|
|
61
|
+
const FormFrame = (0, import_core.styled)(import_core.View, {
|
|
62
|
+
displayName: FORM_NAME,
|
|
63
|
+
render: "form"
|
|
50
64
|
});
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
ref: forwardedRef,
|
|
63
|
-
onPress: (0, import_helpers.composeEventHandlers)(onPress, context.onSubmit),
|
|
64
|
-
children
|
|
65
|
-
});
|
|
65
|
+
const FormTriggerFrame = (0, import_core.styled)(import_core.View, { displayName: "FormTrigger" });
|
|
66
|
+
const FormTrigger = (0, import_core.createStyledHOC)(FormTriggerFrame, (props, forwardedRef) => {
|
|
67
|
+
const { scope, children, onPress, ...triggerProps } = props;
|
|
68
|
+
const context = useFormContext(scope);
|
|
69
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormTriggerFrame, {
|
|
70
|
+
role: "button",
|
|
71
|
+
...triggerProps,
|
|
72
|
+
ref: forwardedRef,
|
|
73
|
+
onPress: (0, import_helpers.composeEventHandlers)(onPress, context.onSubmit),
|
|
74
|
+
children
|
|
75
|
+
});
|
|
66
76
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
function hasError(errors, name) {
|
|
78
|
+
if (!name || !Object.hasOwn(errors, name)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const error = errors[name];
|
|
82
|
+
return Array.isArray(error) ? error.length > 0 : Boolean(error);
|
|
83
|
+
}
|
|
84
|
+
const FormComponent = (0, import_core.createStyledHOC)(FormFrame, function Form({ scope, validationMode = "onSubmit", errors: externalErrors, actionsRef, onSubmit, ...props }, forwardedRef) {
|
|
85
|
+
const registryRef = React.useRef(/* @__PURE__ */ new Map());
|
|
86
|
+
const formElementRef = React.useRef(null);
|
|
87
|
+
const submittedRef = React.useRef(false);
|
|
88
|
+
const submitAttemptedRef = React.useRef(false);
|
|
89
|
+
const pendingSubmitDetailsRef = React.useRef(null);
|
|
90
|
+
const onSubmitRef = React.useRef(onSubmit);
|
|
91
|
+
onSubmitRef.current = onSubmit;
|
|
92
|
+
const [errors, setErrors] = React.useState(externalErrors ?? EMPTY_ERRORS);
|
|
93
|
+
const errorsRef = React.useRef(errors);
|
|
94
|
+
errorsRef.current = errors;
|
|
95
|
+
React.useEffect(() => {
|
|
96
|
+
const next = externalErrors ?? EMPTY_ERRORS;
|
|
97
|
+
errorsRef.current = next;
|
|
98
|
+
setErrors(next);
|
|
99
|
+
}, [externalErrors]);
|
|
100
|
+
const registerField = React.useCallback((id, registration) => {
|
|
101
|
+
registryRef.current.set(id, registration);
|
|
102
|
+
return () => {
|
|
103
|
+
if (registryRef.current.get(id) === registration) {
|
|
104
|
+
registryRef.current.delete(id);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}, []);
|
|
108
|
+
const getValues = React.useCallback(() => {
|
|
109
|
+
const values = {};
|
|
110
|
+
for (const field of registryRef.current.values()) {
|
|
111
|
+
if (field.name) {
|
|
112
|
+
values[field.name] = field.getValue();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return values;
|
|
116
|
+
}, []);
|
|
117
|
+
const clearErrors = React.useCallback((name) => {
|
|
118
|
+
if (!name || !hasError(errorsRef.current, name)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const next = { ...errorsRef.current };
|
|
122
|
+
delete next[name];
|
|
123
|
+
errorsRef.current = next;
|
|
124
|
+
setErrors(next);
|
|
125
|
+
}, []);
|
|
126
|
+
const focusFirstInvalid = React.useCallback(() => {
|
|
127
|
+
let hasInvalid = false;
|
|
128
|
+
for (const field of registryRef.current.values()) {
|
|
129
|
+
if (!hasError(errorsRef.current, field.name) && field.validityData.state.valid !== false) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
hasInvalid = true;
|
|
133
|
+
const control = field.controlRef.current;
|
|
134
|
+
if (import_core.isWeb && control?.focus) {
|
|
135
|
+
control.focus();
|
|
136
|
+
if (control.tagName === "INPUT" || control.tagName === "TEXTAREA") {
|
|
137
|
+
control.select?.();
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (!import_core.isWeb && field.controlId) {
|
|
142
|
+
(0, import_focusable.focusFocusable)(field.controlId);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
if (control?.focus) {
|
|
146
|
+
control.focus();
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return hasInvalid;
|
|
151
|
+
}, []);
|
|
152
|
+
const performSubmit = React.useCallback((details) => {
|
|
153
|
+
submitAttemptedRef.current = true;
|
|
154
|
+
for (const field of registryRef.current.values()) {
|
|
155
|
+
field.validate();
|
|
156
|
+
}
|
|
157
|
+
if (focusFirstInvalid()) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
submittedRef.current = true;
|
|
161
|
+
void onSubmitRef.current?.(getValues(), details);
|
|
162
|
+
}, [focusFirstInvalid, getValues]);
|
|
163
|
+
const requestSubmit = React.useCallback((event) => {
|
|
164
|
+
event?.preventDefault?.();
|
|
165
|
+
const details = {
|
|
166
|
+
reason: "trigger-press",
|
|
167
|
+
event: event?.nativeEvent ?? event,
|
|
168
|
+
trigger: event?.currentTarget
|
|
169
|
+
};
|
|
170
|
+
if (import_core.isWeb && formElementRef.current?.requestSubmit) {
|
|
171
|
+
pendingSubmitDetailsRef.current = details;
|
|
172
|
+
formElementRef.current.requestSubmit();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
performSubmit(details);
|
|
176
|
+
}, [performSubmit]);
|
|
177
|
+
React.useEffect(() => {
|
|
178
|
+
if (!submittedRef.current) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
submittedRef.current = false;
|
|
182
|
+
focusFirstInvalid();
|
|
183
|
+
}, [errors, focusFirstInvalid]);
|
|
184
|
+
React.useImperativeHandle(actionsRef, () => ({ validate(fieldName) {
|
|
185
|
+
if (fieldName) {
|
|
186
|
+
for (const field of registryRef.current.values()) {
|
|
187
|
+
if (field.name === fieldName) {
|
|
188
|
+
field.validate();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
for (const field of registryRef.current.values()) {
|
|
195
|
+
field.validate();
|
|
196
|
+
}
|
|
197
|
+
} }), []);
|
|
198
|
+
const registryContext = React.useMemo(() => ({
|
|
199
|
+
validationMode,
|
|
200
|
+
errors,
|
|
201
|
+
formElementRef,
|
|
202
|
+
submitAttemptedRef,
|
|
203
|
+
clearErrors,
|
|
204
|
+
getValues,
|
|
205
|
+
registerField
|
|
206
|
+
}), [
|
|
207
|
+
clearErrors,
|
|
208
|
+
errors,
|
|
209
|
+
getValues,
|
|
210
|
+
registerField,
|
|
211
|
+
validationMode
|
|
212
|
+
]);
|
|
213
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormRegistryContext.Provider, {
|
|
214
|
+
value: registryContext,
|
|
215
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormProvider, {
|
|
216
|
+
scope,
|
|
217
|
+
onSubmit: requestSubmit,
|
|
218
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormFrame, {
|
|
219
|
+
...props,
|
|
220
|
+
ref: (element) => {
|
|
221
|
+
formElementRef.current = element;
|
|
222
|
+
if (typeof forwardedRef === "function") {
|
|
223
|
+
forwardedRef(element);
|
|
224
|
+
} else if (forwardedRef) {
|
|
225
|
+
forwardedRef.current = element;
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
noValidate: true,
|
|
229
|
+
onSubmit: (event) => {
|
|
230
|
+
event.preventDefault();
|
|
231
|
+
const pendingDetails = pendingSubmitDetailsRef.current;
|
|
232
|
+
pendingSubmitDetailsRef.current = null;
|
|
233
|
+
performSubmit(pendingDetails ?? {
|
|
234
|
+
reason: "submit",
|
|
235
|
+
event: event.nativeEvent ?? event,
|
|
236
|
+
trigger: event.nativeEvent?.submitter
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
});
|
|
84
242
|
});
|
|
85
|
-
const Form2 = (0, import_helpers.withStaticProperties)(FormComponent, {
|
|
86
|
-
Trigger: FormTrigger
|
|
87
|
-
});
|
|
243
|
+
const Form2 = (0, import_helpers.withStaticProperties)(FormComponent, { Trigger: FormTrigger });
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
+
get: () => from[key],
|
|
20
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
26
|
+
value: mod,
|
|
27
|
+
enumerable: true
|
|
28
|
+
}) : target, mod));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var Form_exports = {};
|
|
31
|
+
__export(Form_exports, {
|
|
32
|
+
Form: () => Form2,
|
|
33
|
+
FormContext: () => FormContext,
|
|
34
|
+
FormFrame: () => FormFrame,
|
|
35
|
+
FormProvider: () => FormProvider,
|
|
36
|
+
FormRegistryContext: () => FormRegistryContext,
|
|
37
|
+
FormTrigger: () => FormTrigger,
|
|
38
|
+
useFormContext: () => useFormContext,
|
|
39
|
+
useFormRegistryContext: () => useFormRegistryContext
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(Form_exports);
|
|
42
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
43
|
+
var import_core = require("@tamagui/core");
|
|
44
|
+
var import_focusable = require("@tamagui/focusable");
|
|
45
|
+
var import_helpers = require("@tamagui/helpers");
|
|
46
|
+
var React = __toESM(require("react"), 1);
|
|
47
|
+
var FORM_NAME = "Form";
|
|
48
|
+
var EMPTY_ERRORS = {};
|
|
49
|
+
var defaultSubmitAttemptedRef = { current: false };
|
|
50
|
+
var defaultFormElementRef = { current: null };
|
|
51
|
+
var FormRegistryContext = /* @__PURE__ */ React.createContext({
|
|
52
|
+
validationMode: "onSubmit",
|
|
53
|
+
errors: EMPTY_ERRORS,
|
|
54
|
+
formElementRef: defaultFormElementRef,
|
|
55
|
+
submitAttemptedRef: defaultSubmitAttemptedRef,
|
|
56
|
+
clearErrors: function() {},
|
|
57
|
+
getValues: function() {
|
|
58
|
+
return {};
|
|
59
|
+
},
|
|
60
|
+
registerField: function() {
|
|
61
|
+
return function() {};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
var useFormRegistryContext = function() {
|
|
65
|
+
return React.useContext(FormRegistryContext);
|
|
66
|
+
};
|
|
67
|
+
var FormContext = (0, import_core.createStyledContext)({ onSubmit: void 0 });
|
|
68
|
+
var { useStyledContext: useFormContext, Provider: FormProvider } = FormContext;
|
|
69
|
+
var FormFrame = (0, import_core.styled)(import_core.View, {
|
|
70
|
+
displayName: FORM_NAME,
|
|
71
|
+
render: "form"
|
|
72
|
+
});
|
|
73
|
+
var FormTriggerFrame = (0, import_core.styled)(import_core.View, { displayName: "FormTrigger" });
|
|
74
|
+
var FormTrigger = (0, import_core.createStyledHOC)(FormTriggerFrame, function(props, forwardedRef) {
|
|
75
|
+
var { scope, children, onPress, ...triggerProps } = props;
|
|
76
|
+
var context = useFormContext(scope);
|
|
77
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormTriggerFrame, {
|
|
78
|
+
role: "button",
|
|
79
|
+
...triggerProps,
|
|
80
|
+
ref: forwardedRef,
|
|
81
|
+
onPress: (0, import_helpers.composeEventHandlers)(onPress, context.onSubmit),
|
|
82
|
+
children
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
function hasError(errors, name) {
|
|
86
|
+
if (!name || !Object.hasOwn(errors, name)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
var error = errors[name];
|
|
90
|
+
return Array.isArray(error) ? error.length > 0 : Boolean(error);
|
|
91
|
+
}
|
|
92
|
+
var FormComponent = (0, import_core.createStyledHOC)(FormFrame, function Form(param, forwardedRef) {
|
|
93
|
+
var { scope, validationMode = "onSubmit", errors: externalErrors, actionsRef, onSubmit, ...props } = param;
|
|
94
|
+
var registryRef = React.useRef(/* @__PURE__ */ new Map());
|
|
95
|
+
var formElementRef = React.useRef(null);
|
|
96
|
+
var submittedRef = React.useRef(false);
|
|
97
|
+
var submitAttemptedRef = React.useRef(false);
|
|
98
|
+
var pendingSubmitDetailsRef = React.useRef(null);
|
|
99
|
+
var onSubmitRef = React.useRef(onSubmit);
|
|
100
|
+
onSubmitRef.current = onSubmit;
|
|
101
|
+
var [errors, setErrors] = React.useState(externalErrors !== null && externalErrors !== void 0 ? externalErrors : EMPTY_ERRORS);
|
|
102
|
+
var errorsRef = React.useRef(errors);
|
|
103
|
+
errorsRef.current = errors;
|
|
104
|
+
React.useEffect(function() {
|
|
105
|
+
var next = externalErrors !== null && externalErrors !== void 0 ? externalErrors : EMPTY_ERRORS;
|
|
106
|
+
errorsRef.current = next;
|
|
107
|
+
setErrors(next);
|
|
108
|
+
}, [externalErrors]);
|
|
109
|
+
var registerField = React.useCallback(function(id, registration) {
|
|
110
|
+
registryRef.current.set(id, registration);
|
|
111
|
+
return function() {
|
|
112
|
+
if (registryRef.current.get(id) === registration) {
|
|
113
|
+
registryRef.current.delete(id);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}, []);
|
|
117
|
+
var getValues = React.useCallback(function() {
|
|
118
|
+
var values = {};
|
|
119
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
120
|
+
try {
|
|
121
|
+
for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
122
|
+
var field = _step.value;
|
|
123
|
+
if (field.name) {
|
|
124
|
+
values[field.name] = field.getValue();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} catch (err) {
|
|
128
|
+
_didIteratorError = true;
|
|
129
|
+
_iteratorError = err;
|
|
130
|
+
} finally {
|
|
131
|
+
try {
|
|
132
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
133
|
+
_iterator.return();
|
|
134
|
+
}
|
|
135
|
+
} finally {
|
|
136
|
+
if (_didIteratorError) {
|
|
137
|
+
throw _iteratorError;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return values;
|
|
142
|
+
}, []);
|
|
143
|
+
var clearErrors = React.useCallback(function(name) {
|
|
144
|
+
if (!name || !hasError(errorsRef.current, name)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
var next = { ...errorsRef.current };
|
|
148
|
+
delete next[name];
|
|
149
|
+
errorsRef.current = next;
|
|
150
|
+
setErrors(next);
|
|
151
|
+
}, []);
|
|
152
|
+
var focusFirstInvalid = React.useCallback(function() {
|
|
153
|
+
var hasInvalid = false;
|
|
154
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
155
|
+
try {
|
|
156
|
+
for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
157
|
+
var field = _step.value;
|
|
158
|
+
if (!hasError(errorsRef.current, field.name) && field.validityData.state.valid !== false) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
hasInvalid = true;
|
|
162
|
+
var control = field.controlRef.current;
|
|
163
|
+
if (import_core.isWeb && (control === null || control === void 0 ? void 0 : control.focus)) {
|
|
164
|
+
control.focus();
|
|
165
|
+
if (control.tagName === "INPUT" || control.tagName === "TEXTAREA") {
|
|
166
|
+
var _control_select;
|
|
167
|
+
(_control_select = control.select) === null || _control_select === void 0 ? void 0 : _control_select.call(control);
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
if (!import_core.isWeb && field.controlId) {
|
|
172
|
+
(0, import_focusable.focusFocusable)(field.controlId);
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
if (control === null || control === void 0 ? void 0 : control.focus) {
|
|
176
|
+
control.focus();
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} catch (err) {
|
|
181
|
+
_didIteratorError = true;
|
|
182
|
+
_iteratorError = err;
|
|
183
|
+
} finally {
|
|
184
|
+
try {
|
|
185
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
186
|
+
_iterator.return();
|
|
187
|
+
}
|
|
188
|
+
} finally {
|
|
189
|
+
if (_didIteratorError) {
|
|
190
|
+
throw _iteratorError;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return hasInvalid;
|
|
195
|
+
}, []);
|
|
196
|
+
var performSubmit = React.useCallback(function(details) {
|
|
197
|
+
var _onSubmitRef_current;
|
|
198
|
+
submitAttemptedRef.current = true;
|
|
199
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
200
|
+
try {
|
|
201
|
+
for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
202
|
+
var field = _step.value;
|
|
203
|
+
field.validate();
|
|
204
|
+
}
|
|
205
|
+
} catch (err) {
|
|
206
|
+
_didIteratorError = true;
|
|
207
|
+
_iteratorError = err;
|
|
208
|
+
} finally {
|
|
209
|
+
try {
|
|
210
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
211
|
+
_iterator.return();
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
if (_didIteratorError) {
|
|
215
|
+
throw _iteratorError;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (focusFirstInvalid()) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
submittedRef.current = true;
|
|
223
|
+
void ((_onSubmitRef_current = onSubmitRef.current) === null || _onSubmitRef_current === void 0 ? void 0 : _onSubmitRef_current.call(onSubmitRef, getValues(), details));
|
|
224
|
+
}, [focusFirstInvalid, getValues]);
|
|
225
|
+
var requestSubmit = React.useCallback(function(event) {
|
|
226
|
+
var _event_preventDefault, _formElementRef_current;
|
|
227
|
+
event === null || event === void 0 ? void 0 : (_event_preventDefault = event.preventDefault) === null || _event_preventDefault === void 0 ? void 0 : _event_preventDefault.call(event);
|
|
228
|
+
var _event_nativeEvent;
|
|
229
|
+
var details = {
|
|
230
|
+
reason: "trigger-press",
|
|
231
|
+
event: (_event_nativeEvent = event === null || event === void 0 ? void 0 : event.nativeEvent) !== null && _event_nativeEvent !== void 0 ? _event_nativeEvent : event,
|
|
232
|
+
trigger: event === null || event === void 0 ? void 0 : event.currentTarget
|
|
233
|
+
};
|
|
234
|
+
if (import_core.isWeb && ((_formElementRef_current = formElementRef.current) === null || _formElementRef_current === void 0 ? void 0 : _formElementRef_current.requestSubmit)) {
|
|
235
|
+
pendingSubmitDetailsRef.current = details;
|
|
236
|
+
formElementRef.current.requestSubmit();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
performSubmit(details);
|
|
240
|
+
}, [performSubmit]);
|
|
241
|
+
React.useEffect(function() {
|
|
242
|
+
if (!submittedRef.current) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
submittedRef.current = false;
|
|
246
|
+
focusFirstInvalid();
|
|
247
|
+
}, [errors, focusFirstInvalid]);
|
|
248
|
+
React.useImperativeHandle(actionsRef, function() {
|
|
249
|
+
return { validate(fieldName) {
|
|
250
|
+
if (fieldName) {
|
|
251
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
252
|
+
try {
|
|
253
|
+
for (var _iterator = registryRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
254
|
+
var field = _step.value;
|
|
255
|
+
if (field.name === fieldName) {
|
|
256
|
+
field.validate();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
} catch (err) {
|
|
261
|
+
_didIteratorError = true;
|
|
262
|
+
_iteratorError = err;
|
|
263
|
+
} finally {
|
|
264
|
+
try {
|
|
265
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
266
|
+
_iterator.return();
|
|
267
|
+
}
|
|
268
|
+
} finally {
|
|
269
|
+
if (_didIteratorError) {
|
|
270
|
+
throw _iteratorError;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
|
|
277
|
+
try {
|
|
278
|
+
for (var _iterator1 = registryRef.current.values()[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
|
|
279
|
+
var field1 = _step1.value;
|
|
280
|
+
field1.validate();
|
|
281
|
+
}
|
|
282
|
+
} catch (err) {
|
|
283
|
+
_didIteratorError1 = true;
|
|
284
|
+
_iteratorError1 = err;
|
|
285
|
+
} finally {
|
|
286
|
+
try {
|
|
287
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
288
|
+
_iterator1.return();
|
|
289
|
+
}
|
|
290
|
+
} finally {
|
|
291
|
+
if (_didIteratorError1) {
|
|
292
|
+
throw _iteratorError1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
} };
|
|
297
|
+
}, []);
|
|
298
|
+
var registryContext = React.useMemo(function() {
|
|
299
|
+
return {
|
|
300
|
+
validationMode,
|
|
301
|
+
errors,
|
|
302
|
+
formElementRef,
|
|
303
|
+
submitAttemptedRef,
|
|
304
|
+
clearErrors,
|
|
305
|
+
getValues,
|
|
306
|
+
registerField
|
|
307
|
+
};
|
|
308
|
+
}, [
|
|
309
|
+
clearErrors,
|
|
310
|
+
errors,
|
|
311
|
+
getValues,
|
|
312
|
+
registerField,
|
|
313
|
+
validationMode
|
|
314
|
+
]);
|
|
315
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormRegistryContext.Provider, {
|
|
316
|
+
value: registryContext,
|
|
317
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormProvider, {
|
|
318
|
+
scope,
|
|
319
|
+
onSubmit: requestSubmit,
|
|
320
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormFrame, {
|
|
321
|
+
...props,
|
|
322
|
+
ref: function(element) {
|
|
323
|
+
formElementRef.current = element;
|
|
324
|
+
if (typeof forwardedRef === "function") {
|
|
325
|
+
forwardedRef(element);
|
|
326
|
+
} else if (forwardedRef) {
|
|
327
|
+
forwardedRef.current = element;
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
noValidate: true,
|
|
331
|
+
onSubmit: function(event) {
|
|
332
|
+
var _event_nativeEvent;
|
|
333
|
+
event.preventDefault();
|
|
334
|
+
var pendingDetails = pendingSubmitDetailsRef.current;
|
|
335
|
+
pendingSubmitDetailsRef.current = null;
|
|
336
|
+
var _event_nativeEvent1;
|
|
337
|
+
performSubmit(pendingDetails !== null && pendingDetails !== void 0 ? pendingDetails : {
|
|
338
|
+
reason: "submit",
|
|
339
|
+
event: (_event_nativeEvent1 = event.nativeEvent) !== null && _event_nativeEvent1 !== void 0 ? _event_nativeEvent1 : event,
|
|
340
|
+
trigger: (_event_nativeEvent = event.nativeEvent) === null || _event_nativeEvent === void 0 ? void 0 : _event_nativeEvent.submitter
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
})
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
var Form2 = (0, import_helpers.withStaticProperties)(FormComponent, { Trigger: FormTrigger });
|