@tamagui/field 0.0.0-bootstrap.0 → 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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/Field.cjs +857 -0
  3. package/dist/cjs/Field.native.cjs +1018 -0
  4. package/dist/cjs/Field.native.js +1020 -0
  5. package/dist/cjs/Field.native.js.map +1 -0
  6. package/dist/cjs/FieldContext.cjs +86 -0
  7. package/dist/cjs/FieldContext.native.cjs +90 -0
  8. package/dist/cjs/FieldContext.native.js +92 -0
  9. package/dist/cjs/FieldContext.native.js.map +1 -0
  10. package/dist/cjs/index.cjs +21 -0
  11. package/dist/cjs/index.native.cjs +23 -0
  12. package/dist/cjs/index.native.js +25 -0
  13. package/dist/cjs/index.native.js.map +1 -0
  14. package/dist/cjs/types.cjs +17 -0
  15. package/dist/cjs/types.native.cjs +19 -0
  16. package/dist/cjs/types.native.js +21 -0
  17. package/dist/cjs/types.native.js.map +1 -0
  18. package/dist/cjs/validation.cjs +114 -0
  19. package/dist/cjs/validation.native.cjs +126 -0
  20. package/dist/cjs/validation.native.js +128 -0
  21. package/dist/cjs/validation.native.js.map +1 -0
  22. package/dist/esm/Field.mjs +824 -0
  23. package/dist/esm/Field.mjs.map +1 -0
  24. package/dist/esm/Field.native.js +983 -0
  25. package/dist/esm/Field.native.js.map +1 -0
  26. package/dist/esm/FieldContext.mjs +51 -0
  27. package/dist/esm/FieldContext.mjs.map +1 -0
  28. package/dist/esm/FieldContext.native.js +53 -0
  29. package/dist/esm/FieldContext.native.js.map +1 -0
  30. package/dist/esm/index.js +5 -0
  31. package/dist/esm/index.mjs +5 -0
  32. package/dist/esm/index.native.js +5 -0
  33. package/dist/esm/types.mjs +0 -0
  34. package/dist/esm/types.native.js +0 -0
  35. package/dist/esm/validation.mjs +86 -0
  36. package/dist/esm/validation.mjs.map +1 -0
  37. package/dist/esm/validation.native.js +96 -0
  38. package/dist/esm/validation.native.js.map +1 -0
  39. package/dist/jsx/Field.mjs +824 -0
  40. package/dist/jsx/Field.mjs.map +1 -0
  41. package/dist/jsx/Field.native.cjs +1018 -0
  42. package/dist/jsx/Field.native.js +1020 -0
  43. package/dist/jsx/Field.native.js.map +1 -0
  44. package/dist/jsx/FieldContext.mjs +51 -0
  45. package/dist/jsx/FieldContext.mjs.map +1 -0
  46. package/dist/jsx/FieldContext.native.cjs +90 -0
  47. package/dist/jsx/FieldContext.native.js +92 -0
  48. package/dist/jsx/FieldContext.native.js.map +1 -0
  49. package/dist/jsx/index.js +5 -0
  50. package/dist/jsx/index.mjs +5 -0
  51. package/dist/jsx/index.native.cjs +23 -0
  52. package/dist/jsx/index.native.js +25 -0
  53. package/dist/jsx/index.native.js.map +1 -0
  54. package/dist/jsx/types.mjs +0 -0
  55. package/dist/jsx/types.native.cjs +19 -0
  56. package/dist/jsx/types.native.js +21 -0
  57. package/dist/jsx/types.native.js.map +1 -0
  58. package/dist/jsx/validation.mjs +86 -0
  59. package/dist/jsx/validation.mjs.map +1 -0
  60. package/dist/jsx/validation.native.cjs +126 -0
  61. package/dist/jsx/validation.native.js +128 -0
  62. package/dist/jsx/validation.native.js.map +1 -0
  63. package/package.json +50 -5
  64. package/src/Field.tsx +1129 -0
  65. package/src/FieldContext.tsx +61 -0
  66. package/src/index.ts +3 -0
  67. package/src/types.ts +125 -0
  68. package/src/validation.ts +136 -0
  69. package/types/Field.d.ts +401 -0
  70. package/types/Field.d.ts.map +1 -0
  71. package/types/FieldContext.d.ts +11 -0
  72. package/types/FieldContext.d.ts.map +1 -0
  73. package/types/index.d.ts +4 -0
  74. package/types/index.d.ts.map +1 -0
  75. package/types/types.d.ts +112 -0
  76. package/types/types.d.ts.map +1 -0
  77. package/types/validation.d.ts +24 -0
  78. package/types/validation.d.ts.map +1 -0
  79. package/README.md +0 -1
@@ -0,0 +1,983 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { Text, View, createStyledHOC, isWeb, styled, useIsomorphicLayoutEffect, withStaticProperties } from "@tamagui/core";
3
+ import { useFormRegistryContext } from "@tamagui/form";
4
+ import { focusFocusable } from "@tamagui/focusable";
5
+ import * as React from "react";
6
+ import { FieldControlContext, FieldStateContext, FieldStyledContext, useFieldControl, useFieldState } from "./FieldContext.native.js";
7
+ import { createDefaultValidityState, createValidationCommitter, getValidationResult, isFilled, normalizeNativeValidity, shouldValidateOnChange } from "./validation.native.js";
8
+
9
+ function _instanceof(left, right) {
10
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
11
+ return !!right[Symbol.hasInstance](left);
12
+ } else {
13
+ return left instanceof right;
14
+ }
15
+ }
16
+ var fieldStateVariants = {
17
+ valid: { true: {} },
18
+ invalid: { true: {} },
19
+ touched: { true: {} },
20
+ dirty: { true: {} },
21
+ filled: { true: {} },
22
+ focused: { true: {} },
23
+ disabled: { true: {} }
24
+ };
25
+ var FieldFrame = styled(View, {
26
+ displayName: "Field",
27
+ context: FieldStyledContext,
28
+ variants: fieldStateVariants
29
+ });
30
+ var FieldLabelFrame = styled(Text, {
31
+ displayName: "FieldLabel",
32
+ render: "label",
33
+ context: FieldStyledContext,
34
+ variants: fieldStateVariants
35
+ });
36
+ var FieldDescriptionFrame = styled(Text, {
37
+ displayName: "FieldDescription",
38
+ render: "p",
39
+ context: FieldStyledContext,
40
+ variants: fieldStateVariants
41
+ });
42
+ var FieldErrorFrame = styled(Text, {
43
+ displayName: "FieldError",
44
+ render: "span",
45
+ context: FieldStyledContext,
46
+ variants: fieldStateVariants
47
+ });
48
+ var FieldItemFrame = styled(View, {
49
+ displayName: "FieldItem",
50
+ context: FieldStyledContext,
51
+ variants: fieldStateVariants
52
+ });
53
+ var validityKeys = [
54
+ "badInput",
55
+ "customError",
56
+ "patternMismatch",
57
+ "rangeOverflow",
58
+ "rangeUnderflow",
59
+ "stepMismatch",
60
+ "tooLong",
61
+ "tooShort",
62
+ "typeMismatch",
63
+ "valueMissing"
64
+ ];
65
+ var AssociationContext = /* @__PURE__ */ React.createContext(null);
66
+ var FieldRootContext = /* @__PURE__ */ React.createContext(null);
67
+ var FieldItemContext = /* @__PURE__ */ React.createContext(false);
68
+ function getText(children) {
69
+ var text = [];
70
+ React.Children.forEach(children, function(child) {
71
+ if (typeof child === "string" || typeof child === "number") {
72
+ text.push(String(child));
73
+ return;
74
+ }
75
+ if (/* @__PURE__ */ React.isValidElement(child)) {
76
+ var nested = getText(child.props.children);
77
+ if (nested) {
78
+ text.push(nested);
79
+ }
80
+ }
81
+ });
82
+ return text.join(" ").trim() || void 0;
83
+ }
84
+ function useAssociation(prefix, parent) {
85
+ var defaultControlId = `${prefix}-control`;
86
+ var controlIdsRef = React.useRef(/* @__PURE__ */ new Map());
87
+ var labelsRef = React.useRef(/* @__PURE__ */ new Map());
88
+ var messagesRef = React.useRef(/* @__PURE__ */ new Map());
89
+ var [, update] = React.useReducer(function(value) {
90
+ return value + 1;
91
+ }, 0);
92
+ var registerControlId = React.useCallback(function(id) {
93
+ var source = /* @__PURE__ */ Symbol();
94
+ controlIdsRef.current.set(source, id);
95
+ update();
96
+ return function() {
97
+ controlIdsRef.current.delete(source);
98
+ update();
99
+ };
100
+ }, []);
101
+ var registerLabel = React.useCallback(function(id, text) {
102
+ var source = /* @__PURE__ */ Symbol();
103
+ labelsRef.current.set(source, {
104
+ id,
105
+ text
106
+ });
107
+ update();
108
+ return function() {
109
+ labelsRef.current.delete(source);
110
+ update();
111
+ };
112
+ }, []);
113
+ var registerMessage = React.useCallback(function(id, text) {
114
+ var source = /* @__PURE__ */ Symbol();
115
+ messagesRef.current.set(source, {
116
+ id,
117
+ text
118
+ });
119
+ update();
120
+ return function() {
121
+ messagesRef.current.delete(source);
122
+ update();
123
+ };
124
+ }, []);
125
+ var _Array_from_at;
126
+ var controlId = (_Array_from_at = Array.from(controlIdsRef.current.values()).at(-1)) !== null && _Array_from_at !== void 0 ? _Array_from_at : defaultControlId;
127
+ var label = Array.from(labelsRef.current.values()).at(-1);
128
+ var messages = Array.from(messagesRef.current.values());
129
+ return React.useMemo(function() {
130
+ var _parent_messageIds, _parent_messageTexts;
131
+ return {
132
+ controlId,
133
+ labelId: [parent === null || parent === void 0 ? void 0 : parent.labelId, label === null || label === void 0 ? void 0 : label.id].filter(Boolean).join(" ") || void 0,
134
+ labelText: [parent === null || parent === void 0 ? void 0 : parent.labelText, label === null || label === void 0 ? void 0 : label.text].filter(Boolean).join(" ") || void 0,
135
+ messageIds: Array.from(/* @__PURE__ */ new Set([...(_parent_messageIds = parent === null || parent === void 0 ? void 0 : parent.messageIds) !== null && _parent_messageIds !== void 0 ? _parent_messageIds : [], ...messages.map(function(message) {
136
+ return message.id;
137
+ })])),
138
+ messageTexts: [...(_parent_messageTexts = parent === null || parent === void 0 ? void 0 : parent.messageTexts) !== null && _parent_messageTexts !== void 0 ? _parent_messageTexts : [], ...messages.flatMap(function(message) {
139
+ return message.text ? [message.text] : [];
140
+ })],
141
+ registerControlId,
142
+ registerLabel,
143
+ registerMessage
144
+ };
145
+ }, [
146
+ controlId,
147
+ label === null || label === void 0 ? void 0 : label.id,
148
+ label === null || label === void 0 ? void 0 : label.text,
149
+ messages,
150
+ parent === null || parent === void 0 ? void 0 : parent.labelId,
151
+ parent === null || parent === void 0 ? void 0 : parent.labelText,
152
+ parent === null || parent === void 0 ? void 0 : parent.messageIds,
153
+ parent === null || parent === void 0 ? void 0 : parent.messageTexts,
154
+ registerControlId,
155
+ registerLabel,
156
+ registerMessage
157
+ ]);
158
+ }
159
+ function getStyleState(state) {
160
+ var disabled = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : state.disabled;
161
+ return {
162
+ valid: state.valid === true,
163
+ invalid: state.valid === false,
164
+ touched: state.touched,
165
+ dirty: state.dirty,
166
+ filled: state.filled,
167
+ focused: state.focused,
168
+ disabled
169
+ };
170
+ }
171
+ function getDataProps(state) {
172
+ var disabled = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : state.disabled;
173
+ if (!isWeb) {
174
+ return {};
175
+ }
176
+ return {
177
+ "data-valid": state.valid === true ? "" : void 0,
178
+ "data-invalid": state.valid === false ? "" : void 0,
179
+ "data-touched": state.touched ? "" : void 0,
180
+ "data-dirty": state.dirty ? "" : void 0,
181
+ "data-filled": state.filled ? "" : void 0,
182
+ "data-focused": state.focused ? "" : void 0,
183
+ "data-disabled": disabled ? "" : void 0
184
+ };
185
+ }
186
+ function useControlContext(root, association) {
187
+ var itemDisabled = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false, isItem = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : false;
188
+ var disabled = root.state.disabled || itemDisabled;
189
+ var controlIdRef = React.useRef(association.controlId);
190
+ var registrationDisabledRef = React.useRef(root.rootDisabled || itemDisabled);
191
+ controlIdRef.current = association.controlId;
192
+ registrationDisabledRef.current = root.rootDisabled || itemDisabled;
193
+ var registerControl = React.useCallback(function(registration) {
194
+ var _registration_id;
195
+ var id = (_registration_id = registration.id) !== null && _registration_id !== void 0 ? _registration_id : controlIdRef.current;
196
+ var unregisterId = association.registerControlId(id);
197
+ var nextRegistration = {
198
+ ...registration,
199
+ id,
200
+ get disabled() {
201
+ return registrationDisabledRef.current || registration.disabled;
202
+ }
203
+ };
204
+ var unregisterControl = root.registerControl(nextRegistration, !isItem);
205
+ return function() {
206
+ unregisterControl();
207
+ unregisterId();
208
+ };
209
+ }, [
210
+ association.registerControlId,
211
+ isItem,
212
+ root.registerControl
213
+ ]);
214
+ return React.useMemo(function() {
215
+ return {
216
+ name: root.name,
217
+ disabled,
218
+ ariaProps: {
219
+ id: association.controlId,
220
+ ...isWeb ? {
221
+ "aria-labelledby": association.labelId,
222
+ "aria-describedby": association.messageIds.join(" ") || void 0,
223
+ "aria-invalid": root.state.valid === false ? true : void 0
224
+ } : {
225
+ accessibilityLabel: association.labelText,
226
+ accessibilityHint: association.messageTexts.join(" ") || void 0,
227
+ accessibilityState: disabled ? { disabled: true } : void 0
228
+ }
229
+ },
230
+ dataProps: getDataProps(root.state, disabled),
231
+ onFocus: root.onFocus,
232
+ onBlur: root.onBlur,
233
+ onChange: root.onChange,
234
+ onDisabledChange: isItem ? function() {} : root.onDisabledChange,
235
+ registerControl
236
+ };
237
+ }, [
238
+ association.controlId,
239
+ association.labelId,
240
+ association.labelText,
241
+ association.messageIds,
242
+ association.messageTexts,
243
+ disabled,
244
+ isItem,
245
+ registerControl,
246
+ root.name,
247
+ root.onBlur,
248
+ root.onChange,
249
+ root.onDisabledChange,
250
+ root.onFocus,
251
+ root.state
252
+ ]);
253
+ }
254
+ function getRegistrationValue(registration) {
255
+ return (registration === null || registration === void 0 ? void 0 : registration.getValue) ? registration.getValue() : registration === null || registration === void 0 ? void 0 : registration.value;
256
+ }
257
+ function getInput(registration) {
258
+ var _registration_inputRef;
259
+ return (_registration_inputRef = registration.inputRef) === null || _registration_inputRef === void 0 ? void 0 : _registration_inputRef.current;
260
+ }
261
+ function canValidateInput(input) {
262
+ return Boolean(input && input.validity && typeof input.setCustomValidity === "function" && !input.disabled);
263
+ }
264
+ function isEligibleInput(input, formElement) {
265
+ var _input_matches, _input_hasAttribute;
266
+ if ((_input_matches = input.matches) === null || _input_matches === void 0 ? void 0 : _input_matches.call(input, ":disabled")) {
267
+ return false;
268
+ }
269
+ if (!formElement || input.form === formElement) {
270
+ return true;
271
+ }
272
+ return input.form === null && !((_input_hasAttribute = input.hasAttribute) === null || _input_hasAttribute === void 0 ? void 0 : _input_hasAttribute.call(input, "form"));
273
+ }
274
+ function isEligibleRegistration(registration, formElement) {
275
+ if (registration.disabled) {
276
+ return false;
277
+ }
278
+ var input = getInput(registration);
279
+ return !canValidateInput(input) || isEligibleInput(input, formElement);
280
+ }
281
+ function getNativeValidity(input, showValueMissing) {
282
+ var state = createDefaultValidityState(true);
283
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
284
+ try {
285
+ for (var _iterator = validityKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
286
+ var key = _step.value;
287
+ state[key] = Boolean(input.validity[key]);
288
+ }
289
+ } catch (err) {
290
+ _didIteratorError = true;
291
+ _iteratorError = err;
292
+ } finally {
293
+ try {
294
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
295
+ _iterator.return();
296
+ }
297
+ } finally {
298
+ if (_didIteratorError) {
299
+ throw _iteratorError;
300
+ }
301
+ }
302
+ }
303
+ state.valid = Boolean(input.validity.valid);
304
+ return normalizeNativeValidity(state, showValueMissing);
305
+ }
306
+ function hasValidityError(state) {
307
+ return state.valid === false;
308
+ }
309
+ function hasError(errors) {
310
+ return Array.isArray(errors) ? errors.length > 0 : Boolean(errors);
311
+ }
312
+ var FieldComponent = createStyledHOC(FieldFrame, function Field(param, forwardedRef) {
313
+ var { name: nameProp, disabled: disabledProp = false, invalid = false, validate, validationMode: validationModeProp, validationDebounceTime = 0, children, ...fieldProps } = param;
314
+ var form = useFormRegistryContext();
315
+ var { clearErrors: clearFormErrors, errors: formErrors, formElementRef, getValues: getFormValues, registerField: registerFormField, submitAttemptedRef, validationMode: formValidationMode } = form;
316
+ var reactId = React.useId().replace(/:/g, "");
317
+ var association = useAssociation(`field-${reactId}`);
318
+ var fieldId = `field-${reactId}`;
319
+ var controlsRef = React.useRef(/* @__PURE__ */ new Map());
320
+ var reportsDisabledRef = React.useRef(/* @__PURE__ */ new WeakMap());
321
+ var activeControlRef = React.useRef(void 0);
322
+ var activeValidationControlRef = React.useRef(void 0);
323
+ var formControlRef = React.useRef(null);
324
+ var formValidityDataRef = React.useRef({ state: { valid: null } });
325
+ var initialValueRef = React.useRef(void 0);
326
+ var hasInitialValueRef = React.useRef(false);
327
+ var valueRef = React.useRef(void 0);
328
+ var dirtyRef = React.useRef(false);
329
+ var debounceRef = React.useRef(void 0);
330
+ var [registeredName, setRegisteredName] = React.useState();
331
+ var [controlDisabled, setControlDisabled] = React.useState(false);
332
+ var [touched, setTouched] = React.useState(false);
333
+ var [dirty, setDirty] = React.useState(false);
334
+ var [filled, setFilled] = React.useState(false);
335
+ var [focused, setFocused] = React.useState(false);
336
+ var [currentValue, setCurrentValue] = React.useState(void 0);
337
+ var [validityData, setValidityData] = React.useState({
338
+ state: createDefaultValidityState(),
339
+ error: "",
340
+ errors: [],
341
+ value: void 0,
342
+ initialValue: void 0
343
+ });
344
+ var validityDataRef = React.useRef(validityData);
345
+ validityDataRef.current = validityData;
346
+ var effectiveName = nameProp !== null && nameProp !== void 0 ? nameProp : registeredName;
347
+ var effectiveNameRef = React.useRef(effectiveName);
348
+ effectiveNameRef.current = effectiveName;
349
+ var disabled = disabledProp || controlDisabled;
350
+ var disabledRef = React.useRef(disabled);
351
+ disabledRef.current = disabled;
352
+ var validationMode = validationModeProp !== null && validationModeProp !== void 0 ? validationModeProp : formValidationMode;
353
+ var externalError = effectiveName && Object.hasOwn(formErrors, effectiveName) ? formErrors[effectiveName] : void 0;
354
+ var externalErrors = Array.isArray(externalError) ? externalError : externalError ? [externalError] : [];
355
+ var externallyInvalid = invalid || hasError(externalError);
356
+ var externallyInvalidRef = React.useRef(externallyInvalid);
357
+ externallyInvalidRef.current = externallyInvalid;
358
+ var valid = externallyInvalid ? false : disabled ? null : validityData.state.valid;
359
+ formValidityDataRef.current.state.valid = disabled ? null : externallyInvalid ? false : validityData.state.valid;
360
+ var errors = externalErrors.length ? externalErrors : validityData.errors;
361
+ var _errors_;
362
+ var error = (_errors_ = errors[0]) !== null && _errors_ !== void 0 ? _errors_ : "";
363
+ var state = React.useMemo(function() {
364
+ return {
365
+ name: effectiveName,
366
+ value: currentValue,
367
+ error,
368
+ errors,
369
+ validity: validityData.state,
370
+ valid,
371
+ touched,
372
+ dirty,
373
+ filled,
374
+ focused,
375
+ disabled
376
+ };
377
+ }, [
378
+ dirty,
379
+ disabled,
380
+ currentValue,
381
+ effectiveName,
382
+ error,
383
+ errors,
384
+ filled,
385
+ focused,
386
+ touched,
387
+ valid,
388
+ validityData.state
389
+ ]);
390
+ var stateRef = React.useRef(state);
391
+ stateRef.current = state;
392
+ var publishValidityRef = React.useRef(function() {});
393
+ var validationCommitterRef = React.useRef(void 0);
394
+ if (!validationCommitterRef.current) {
395
+ validationCommitterRef.current = createValidationCommitter(function(data) {
396
+ publishValidityRef.current(data);
397
+ });
398
+ }
399
+ var getActiveControl = React.useCallback(function() {
400
+ var active;
401
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
402
+ try {
403
+ for (var _iterator = controlsRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
404
+ var registration = _step.value;
405
+ if (!isEligibleRegistration(registration, formElementRef.current)) {
406
+ continue;
407
+ }
408
+ active = registration;
409
+ }
410
+ } catch (err) {
411
+ _didIteratorError = true;
412
+ _iteratorError = err;
413
+ } finally {
414
+ try {
415
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
416
+ _iterator.return();
417
+ }
418
+ } finally {
419
+ if (_didIteratorError) {
420
+ throw _iteratorError;
421
+ }
422
+ }
423
+ }
424
+ return active;
425
+ }, [formElementRef]);
426
+ var syncControlDisabled = React.useCallback(function() {
427
+ var nextDisabled = false;
428
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
429
+ try {
430
+ for (var _iterator = controlsRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
431
+ var registration = _step.value;
432
+ if (reportsDisabledRef.current.get(registration)) {
433
+ nextDisabled = Boolean(registration.disabled);
434
+ }
435
+ }
436
+ } catch (err) {
437
+ _didIteratorError = true;
438
+ _iteratorError = err;
439
+ } finally {
440
+ try {
441
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
442
+ _iterator.return();
443
+ }
444
+ } finally {
445
+ if (_didIteratorError) {
446
+ throw _iteratorError;
447
+ }
448
+ }
449
+ }
450
+ setControlDisabled(nextDisabled);
451
+ }, []);
452
+ var getRepresentativeControl = React.useCallback(function() {
453
+ var fallback;
454
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
455
+ try {
456
+ for (var _iterator = controlsRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
457
+ var registration = _step.value;
458
+ var input = getInput(registration);
459
+ if (!isEligibleRegistration(registration, formElementRef.current)) {
460
+ continue;
461
+ }
462
+ fallback !== null && fallback !== void 0 ? fallback : fallback = registration;
463
+ if (canValidateInput(input) && !input.validity.valid) {
464
+ return registration;
465
+ }
466
+ }
467
+ } catch (err) {
468
+ _didIteratorError = true;
469
+ _iteratorError = err;
470
+ } finally {
471
+ try {
472
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
473
+ _iterator.return();
474
+ }
475
+ } finally {
476
+ if (_didIteratorError) {
477
+ throw _iteratorError;
478
+ }
479
+ }
480
+ }
481
+ return fallback !== null && fallback !== void 0 ? fallback : getActiveControl();
482
+ }, [formElementRef, getActiveControl]);
483
+ var clearCustomValidity = React.useCallback(function() {
484
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
485
+ try {
486
+ for (var _iterator = controlsRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
487
+ var registration = _step.value;
488
+ var input = getInput(registration);
489
+ if (canValidateInput(input)) {
490
+ input.setCustomValidity("");
491
+ }
492
+ }
493
+ } catch (err) {
494
+ _didIteratorError = true;
495
+ _iteratorError = err;
496
+ } finally {
497
+ try {
498
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
499
+ _iterator.return();
500
+ }
501
+ } finally {
502
+ if (_didIteratorError) {
503
+ throw _iteratorError;
504
+ }
505
+ }
506
+ }
507
+ }, []);
508
+ var setCustomValidity = React.useCallback(function(message) {
509
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
510
+ try {
511
+ for (var _iterator = controlsRef.current.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
512
+ var registration = _step.value;
513
+ if (!isEligibleRegistration(registration, formElementRef.current)) {
514
+ continue;
515
+ }
516
+ var input = getInput(registration);
517
+ if (canValidateInput(input)) {
518
+ input.setCustomValidity(message);
519
+ }
520
+ }
521
+ } catch (err) {
522
+ _didIteratorError = true;
523
+ _iteratorError = err;
524
+ } finally {
525
+ try {
526
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
527
+ _iterator.return();
528
+ }
529
+ } finally {
530
+ if (_didIteratorError) {
531
+ throw _iteratorError;
532
+ }
533
+ }
534
+ }
535
+ }, [formElementRef]);
536
+ var publishValidity = React.useCallback(function(data) {
537
+ if (data.state.customError) {
538
+ setCustomValidity(data.errors.join("\n"));
539
+ } else {
540
+ clearCustomValidity();
541
+ }
542
+ formValidityDataRef.current.state.valid = disabledRef.current ? null : externallyInvalidRef.current ? false : data.state.valid;
543
+ validityDataRef.current = data;
544
+ setCurrentValue(data.value);
545
+ setValidityData(data);
546
+ }, [clearCustomValidity, setCustomValidity]);
547
+ publishValidityRef.current = publishValidity;
548
+ var validateValue = React.useCallback(function(value) {
549
+ var submit = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false, fromChange = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : false;
550
+ if (debounceRef.current) {
551
+ clearTimeout(debounceRef.current);
552
+ debounceRef.current = void 0;
553
+ }
554
+ var showValueMissing = dirtyRef.current || submit || submitAttemptedRef.current;
555
+ if (fromChange) {
556
+ clearCustomValidity();
557
+ }
558
+ var representative = getRepresentativeControl();
559
+ activeValidationControlRef.current = representative;
560
+ var _representative_controlRef_current;
561
+ formControlRef.current = (_representative_controlRef_current = representative === null || representative === void 0 ? void 0 : representative.controlRef.current) !== null && _representative_controlRef_current !== void 0 ? _representative_controlRef_current : null;
562
+ var input = representative && getInput(representative);
563
+ var nativeState = createDefaultValidityState(true);
564
+ var nativeMessage = "";
565
+ if (canValidateInput(input)) {
566
+ nativeState = getNativeValidity(input, showValueMissing);
567
+ nativeMessage = nativeState.valid === false ? input.validationMessage : "";
568
+ } else {
569
+ var required = Array.from(controlsRef.current.values()).some(function(registration) {
570
+ return registration.required && isEligibleRegistration(registration, formElementRef.current);
571
+ });
572
+ if (required && !isFilled(value) && showValueMissing) {
573
+ nativeState = {
574
+ ...createDefaultValidityState(false),
575
+ valueMissing: true
576
+ };
577
+ nativeMessage = "Please fill out this field.";
578
+ }
579
+ }
580
+ var initialValue = initialValueRef.current;
581
+ var makeValidityData = function(customErrors) {
582
+ if (customErrors.length) {
583
+ var _customErrors_;
584
+ return {
585
+ state: {
586
+ ...nativeState,
587
+ valid: false,
588
+ customError: true
589
+ },
590
+ error: (_customErrors_ = customErrors[0]) !== null && _customErrors_ !== void 0 ? _customErrors_ : "",
591
+ errors: customErrors,
592
+ value,
593
+ initialValue
594
+ };
595
+ }
596
+ if (hasValidityError(nativeState)) {
597
+ return {
598
+ state: nativeState,
599
+ error: nativeMessage,
600
+ errors: nativeMessage ? [nativeMessage] : [],
601
+ value,
602
+ initialValue
603
+ };
604
+ }
605
+ return {
606
+ state: createDefaultValidityState(true),
607
+ error: "",
608
+ errors: [],
609
+ value,
610
+ initialValue
611
+ };
612
+ };
613
+ if (nativeMessage && !fromChange) {
614
+ validationCommitterRef.current.run(makeValidityData([]));
615
+ return;
616
+ }
617
+ var result = validate ? getValidationResult(validate, value, getFormValues()) : [];
618
+ if (_instanceof(result, Promise)) {
619
+ void validationCommitterRef.current.run(result.then(function(customErrors) {
620
+ return makeValidityData(customErrors);
621
+ }));
622
+ } else {
623
+ validationCommitterRef.current.run(makeValidityData(result));
624
+ }
625
+ }, [
626
+ clearCustomValidity,
627
+ formElementRef,
628
+ getFormValues,
629
+ getRepresentativeControl,
630
+ submitAttemptedRef,
631
+ validate
632
+ ]);
633
+ var registerControl = React.useCallback(function(registration) {
634
+ var reportDisabled = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : true;
635
+ var _activeControlRef_current;
636
+ var source = /* @__PURE__ */ Symbol();
637
+ controlsRef.current.set(source, registration);
638
+ reportsDisabledRef.current.set(registration, reportDisabled);
639
+ activeControlRef.current = getActiveControl();
640
+ var _activeControlRef_current_controlRef_current;
641
+ formControlRef.current = (_activeControlRef_current_controlRef_current = (_activeControlRef_current = activeControlRef.current) === null || _activeControlRef_current === void 0 ? void 0 : _activeControlRef_current.controlRef.current) !== null && _activeControlRef_current_controlRef_current !== void 0 ? _activeControlRef_current_controlRef_current : null;
642
+ var value = getRegistrationValue(registration);
643
+ valueRef.current = value;
644
+ setCurrentValue(value);
645
+ if (!hasInitialValueRef.current) {
646
+ hasInitialValueRef.current = true;
647
+ initialValueRef.current = value;
648
+ validityDataRef.current = {
649
+ ...validityDataRef.current,
650
+ initialValue: value
651
+ };
652
+ setValidityData(validityDataRef.current);
653
+ }
654
+ if (!nameProp && registration.name) {
655
+ setRegisteredName(registration.name);
656
+ }
657
+ setFilled(isFilled(value));
658
+ syncControlDisabled();
659
+ return function() {
660
+ var _activeControlRef_current2;
661
+ controlsRef.current.delete(source);
662
+ activeControlRef.current = getActiveControl();
663
+ activeValidationControlRef.current = void 0;
664
+ var _activeControlRef_current_controlRef_current2;
665
+ formControlRef.current = (_activeControlRef_current_controlRef_current2 = (_activeControlRef_current2 = activeControlRef.current) === null || _activeControlRef_current2 === void 0 ? void 0 : _activeControlRef_current2.controlRef.current) !== null && _activeControlRef_current_controlRef_current2 !== void 0 ? _activeControlRef_current_controlRef_current2 : null;
666
+ var next = activeControlRef.current;
667
+ var nextValue = getRegistrationValue(next);
668
+ valueRef.current = nextValue;
669
+ setCurrentValue(nextValue);
670
+ setFilled(isFilled(nextValue));
671
+ if (!nameProp) {
672
+ setRegisteredName(next === null || next === void 0 ? void 0 : next.name);
673
+ }
674
+ syncControlDisabled();
675
+ };
676
+ }, [
677
+ getActiveControl,
678
+ nameProp,
679
+ syncControlDisabled
680
+ ]);
681
+ var onFocus = React.useCallback(function() {
682
+ setFocused(true);
683
+ }, []);
684
+ var onBlur = React.useCallback(function() {
685
+ var value = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : valueRef.current;
686
+ valueRef.current = value;
687
+ setCurrentValue(value);
688
+ setTouched(true);
689
+ setFocused(false);
690
+ if (validationMode === "onBlur") {
691
+ validateValue(value);
692
+ }
693
+ }, [validateValue, validationMode]);
694
+ var onChange = React.useCallback(function(value) {
695
+ valueRef.current = value;
696
+ setCurrentValue(value);
697
+ validationCommitterRef.current.invalidate();
698
+ if (debounceRef.current) {
699
+ clearTimeout(debounceRef.current);
700
+ debounceRef.current = void 0;
701
+ }
702
+ var nextDirty = !Object.is(value, initialValueRef.current);
703
+ dirtyRef.current = nextDirty;
704
+ setDirty(nextDirty);
705
+ setFilled(isFilled(value));
706
+ clearFormErrors(effectiveNameRef.current);
707
+ var validateOnChange = shouldValidateOnChange(validationMode, submitAttemptedRef.current);
708
+ if (validateOnChange) {
709
+ if (validationMode === "onChange" && validationDebounceTime > 0 && value !== "") {
710
+ debounceRef.current = setTimeout(function() {
711
+ validateValue(value, false, true);
712
+ }, validationDebounceTime);
713
+ } else {
714
+ validateValue(value, false, true);
715
+ }
716
+ return;
717
+ }
718
+ if (stateRef.current.valid === false) {
719
+ var required = Array.from(controlsRef.current.values()).some(function(registration) {
720
+ return registration.required && isEligibleRegistration(registration, formElementRef.current);
721
+ });
722
+ if (required && !isFilled(value) && dirtyRef.current) {
723
+ validateValue(value);
724
+ return;
725
+ }
726
+ clearCustomValidity();
727
+ publishValidity({
728
+ state: createDefaultValidityState(true),
729
+ error: "",
730
+ errors: [],
731
+ value,
732
+ initialValue: initialValueRef.current
733
+ });
734
+ }
735
+ }, [
736
+ clearFormErrors,
737
+ clearCustomValidity,
738
+ formElementRef,
739
+ publishValidity,
740
+ submitAttemptedRef,
741
+ validateValue,
742
+ validationDebounceTime,
743
+ validationMode
744
+ ]);
745
+ var onDisabledChange = React.useCallback(function(nextDisabled) {
746
+ setControlDisabled(nextDisabled);
747
+ }, []);
748
+ React.useEffect(function() {
749
+ return function() {
750
+ var _validationCommitterRef_current;
751
+ (_validationCommitterRef_current = validationCommitterRef.current) === null || _validationCommitterRef_current === void 0 ? void 0 : _validationCommitterRef_current.invalidate();
752
+ if (debounceRef.current) {
753
+ clearTimeout(debounceRef.current);
754
+ }
755
+ };
756
+ }, []);
757
+ var formRegistration = React.useMemo(function() {
758
+ return {
759
+ controlRef: formControlRef,
760
+ validityData: formValidityDataRef.current,
761
+ get name() {
762
+ return stateRef.current.disabled ? void 0 : effectiveNameRef.current;
763
+ },
764
+ get controlId() {
765
+ var _activeValidationControlRef_current, _getRepresentativeControl;
766
+ var _activeValidationControlRef_current_id, _ref;
767
+ return (_ref = (_activeValidationControlRef_current_id = (_activeValidationControlRef_current = activeValidationControlRef.current) === null || _activeValidationControlRef_current === void 0 ? void 0 : _activeValidationControlRef_current.id) !== null && _activeValidationControlRef_current_id !== void 0 ? _activeValidationControlRef_current_id : (_getRepresentativeControl = getRepresentativeControl()) === null || _getRepresentativeControl === void 0 ? void 0 : _getRepresentativeControl.id) !== null && _ref !== void 0 ? _ref : association.controlId;
768
+ },
769
+ getValue() {
770
+ var _getRegistrationValue;
771
+ return (_getRegistrationValue = getRegistrationValue(getActiveControl())) !== null && _getRegistrationValue !== void 0 ? _getRegistrationValue : valueRef.current;
772
+ },
773
+ validate() {
774
+ var _getRegistrationValue;
775
+ validateValue((_getRegistrationValue = getRegistrationValue(getActiveControl())) !== null && _getRegistrationValue !== void 0 ? _getRegistrationValue : valueRef.current, true);
776
+ }
777
+ };
778
+ }, [
779
+ association.controlId,
780
+ getActiveControl,
781
+ getRepresentativeControl,
782
+ validateValue
783
+ ]);
784
+ React.useEffect(function() {
785
+ return registerFormField(fieldId, formRegistration);
786
+ }, [
787
+ fieldId,
788
+ formRegistration,
789
+ registerFormField
790
+ ]);
791
+ var rootContext = React.useMemo(function() {
792
+ return {
793
+ state,
794
+ validityData,
795
+ name: effectiveName,
796
+ rootDisabled: disabledProp,
797
+ registerControl,
798
+ onFocus,
799
+ onBlur,
800
+ onChange,
801
+ onDisabledChange
802
+ };
803
+ }, [
804
+ disabledProp,
805
+ effectiveName,
806
+ onBlur,
807
+ onChange,
808
+ onDisabledChange,
809
+ onFocus,
810
+ registerControl,
811
+ state,
812
+ validityData
813
+ ]);
814
+ var controlContext = useControlContext(rootContext, association);
815
+ var styleState = getStyleState(state);
816
+ return /* @__PURE__ */ jsx(FieldStateContext.Provider, {
817
+ value: state,
818
+ children: /* @__PURE__ */ jsx(FieldRootContext.Provider, {
819
+ value: rootContext,
820
+ children: /* @__PURE__ */ jsx(AssociationContext.Provider, {
821
+ value: association,
822
+ children: /* @__PURE__ */ jsx(FieldControlContext.Provider, {
823
+ value: controlContext,
824
+ children: /* @__PURE__ */ jsx(FieldStyledContext.Provider, {
825
+ ...styleState,
826
+ children: /* @__PURE__ */ jsx(FieldFrame, {
827
+ ...fieldProps,
828
+ ...getDataProps(state),
829
+ disabled,
830
+ ref: forwardedRef,
831
+ children
832
+ })
833
+ })
834
+ })
835
+ })
836
+ })
837
+ });
838
+ });
839
+ function usePartContexts() {
840
+ var root = React.useContext(FieldRootContext);
841
+ var association = React.useContext(AssociationContext);
842
+ var itemDisabled = React.useContext(FieldItemContext);
843
+ if (!root || !association) {
844
+ throw new Error("Field parts must be rendered inside <Field>.");
845
+ }
846
+ return {
847
+ root,
848
+ association,
849
+ itemDisabled
850
+ };
851
+ }
852
+ var FieldLabel = createStyledHOC(FieldLabelFrame, function FieldLabel2(param, forwardedRef) {
853
+ var { id: idProp, children, onPress, onMouseDown, ...labelProps } = param;
854
+ var { root, association, itemDisabled } = usePartContexts();
855
+ var generatedId = React.useId().replace(/:/g, "");
856
+ var id = idProp !== null && idProp !== void 0 ? idProp : `field-label-${generatedId}`;
857
+ var text = getText(children);
858
+ var disabled = root.state.disabled || itemDisabled;
859
+ useIsomorphicLayoutEffect(function() {
860
+ return association.registerLabel(id, text);
861
+ }, [
862
+ association.registerLabel,
863
+ id,
864
+ text
865
+ ]);
866
+ return /* @__PURE__ */ jsx(FieldLabelFrame, {
867
+ ...labelProps,
868
+ ...getDataProps(root.state, disabled),
869
+ id,
870
+ ref: forwardedRef,
871
+ ...isWeb ? { htmlFor: association.controlId } : void 0,
872
+ ...!isWeb && disabled ? { accessibilityState: { disabled: true } } : void 0,
873
+ onMouseDown: function(event) {
874
+ onMouseDown === null || onMouseDown === void 0 ? void 0 : onMouseDown(event);
875
+ if (!event.defaultPrevented && event.detail > 1) {
876
+ event.preventDefault();
877
+ }
878
+ },
879
+ onPress: function(event) {
880
+ onPress === null || onPress === void 0 ? void 0 : onPress(event);
881
+ if (!isWeb && !(event === null || event === void 0 ? void 0 : event.defaultPrevented)) {
882
+ focusFocusable(association.controlId);
883
+ }
884
+ },
885
+ children
886
+ });
887
+ });
888
+ var FieldDescription = createStyledHOC(FieldDescriptionFrame, function FieldDescription2(param, forwardedRef) {
889
+ var { id: idProp, children, ...descriptionProps } = param;
890
+ var { root, association, itemDisabled } = usePartContexts();
891
+ var generatedId = React.useId().replace(/:/g, "");
892
+ var id = idProp !== null && idProp !== void 0 ? idProp : `field-description-${generatedId}`;
893
+ var text = getText(children);
894
+ var disabled = root.state.disabled || itemDisabled;
895
+ useIsomorphicLayoutEffect(function() {
896
+ return association.registerMessage(id, text);
897
+ }, [
898
+ association.registerMessage,
899
+ id,
900
+ text
901
+ ]);
902
+ return /* @__PURE__ */ jsx(FieldDescriptionFrame, {
903
+ ...descriptionProps,
904
+ ...getDataProps(root.state, disabled),
905
+ id,
906
+ ref: forwardedRef,
907
+ children
908
+ });
909
+ });
910
+ var FieldError = createStyledHOC(FieldErrorFrame, function FieldError2(param, forwardedRef) {
911
+ var { id: idProp, children, match, ...errorProps } = param;
912
+ var { root, association, itemDisabled } = usePartContexts();
913
+ var generatedId = React.useId().replace(/:/g, "");
914
+ var id = idProp !== null && idProp !== void 0 ? idProp : `field-error-${generatedId}`;
915
+ var disabled = root.state.disabled || itemDisabled;
916
+ var rendered = match === true || !disabled && (typeof match === "string" ? Boolean(root.validityData.state[match]) : root.state.valid === false);
917
+ var message = children !== null && children !== void 0 ? children : root.state.errors.join("\n");
918
+ var text = getText(message);
919
+ useIsomorphicLayoutEffect(function() {
920
+ if (!rendered) {
921
+ return;
922
+ }
923
+ return association.registerMessage(id, text);
924
+ }, [
925
+ association.registerMessage,
926
+ id,
927
+ rendered,
928
+ text
929
+ ]);
930
+ if (!rendered) {
931
+ return null;
932
+ }
933
+ return /* @__PURE__ */ jsx(FieldErrorFrame, {
934
+ ...errorProps,
935
+ ...getDataProps(root.state, disabled),
936
+ id,
937
+ ref: forwardedRef,
938
+ children: message
939
+ });
940
+ });
941
+ var FieldItem = createStyledHOC(FieldItemFrame, function FieldItem2(param, forwardedRef) {
942
+ var { disabled: disabledProp = false, children, ...itemProps } = param;
943
+ var root = React.useContext(FieldRootContext);
944
+ var parentAssociation = React.useContext(AssociationContext);
945
+ var reactId = React.useId().replace(/:/g, "");
946
+ var association = useAssociation(`field-item-${reactId}`, parentAssociation);
947
+ if (!root || !parentAssociation) {
948
+ throw new Error("Field.Item must be rendered inside <Field>.");
949
+ }
950
+ var disabled = root.state.disabled || disabledProp;
951
+ var controlContext = useControlContext(root, association, disabledProp, true);
952
+ var styleState = getStyleState(root.state, disabled);
953
+ return /* @__PURE__ */ jsx(FieldItemContext.Provider, {
954
+ value: disabledProp,
955
+ children: /* @__PURE__ */ jsx(AssociationContext.Provider, {
956
+ value: association,
957
+ children: /* @__PURE__ */ jsx(FieldControlContext.Provider, {
958
+ value: controlContext,
959
+ children: /* @__PURE__ */ jsx(FieldStyledContext.Provider, {
960
+ ...styleState,
961
+ children: /* @__PURE__ */ jsx(FieldItemFrame, {
962
+ ...itemProps,
963
+ ...getDataProps(root.state, disabled),
964
+ disabled,
965
+ ref: forwardedRef,
966
+ children
967
+ })
968
+ })
969
+ })
970
+ })
971
+ });
972
+ });
973
+ var Field2 = withStaticProperties(FieldComponent, {
974
+ Label: FieldLabel,
975
+ Description: FieldDescription,
976
+ Error: FieldError,
977
+ Item: FieldItem,
978
+ useFieldState,
979
+ useFieldControl
980
+ });
981
+
982
+ export { Field2 as Field, FieldDescriptionFrame, FieldErrorFrame, FieldFrame, FieldItemFrame, FieldLabelFrame };
983
+ //# sourceMappingURL=Field.native.js.map