@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.
@@ -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 });