@radix-ui/react-form 0.0.1-rc.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/README.md +13 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +575 -0
- package/dist/index.js.map +1 -0
- package/dist/index.module.js +554 -0
- package/dist/index.module.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import $RRrwT$babelruntimehelpersesmextends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import {forwardRef as $RRrwT$forwardRef, useRef as $RRrwT$useRef, useState as $RRrwT$useState, useCallback as $RRrwT$useCallback, createElement as $RRrwT$createElement, useEffect as $RRrwT$useEffect, useMemo as $RRrwT$useMemo, Fragment as $RRrwT$Fragment} from "react";
|
|
3
|
+
import {composeEventHandlers as $RRrwT$composeEventHandlers} from "@radix-ui/primitive";
|
|
4
|
+
import {useComposedRefs as $RRrwT$useComposedRefs} from "@radix-ui/react-compose-refs";
|
|
5
|
+
import {createContextScope as $RRrwT$createContextScope} from "@radix-ui/react-context";
|
|
6
|
+
import {useId as $RRrwT$useId} from "@radix-ui/react-id";
|
|
7
|
+
import {Label as $RRrwT$Label} from "@radix-ui/react-label";
|
|
8
|
+
import {Primitive as $RRrwT$Primitive} from "@radix-ui/react-primitive";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const [$d94698215c4408a7$var$createFormContext, $d94698215c4408a7$export$299997c7551e97cb] = $RRrwT$createContextScope('Form');
|
|
19
|
+
/* -------------------------------------------------------------------------------------------------
|
|
20
|
+
* Form
|
|
21
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$FORM_NAME = 'Form';
|
|
22
|
+
const [$d94698215c4408a7$var$ValidationProvider, $d94698215c4408a7$var$useValidationContext] = $d94698215c4408a7$var$createFormContext($d94698215c4408a7$var$FORM_NAME);
|
|
23
|
+
const [$d94698215c4408a7$var$AriaDescriptionProvider, $d94698215c4408a7$var$useAriaDescriptionContext] = $d94698215c4408a7$var$createFormContext($d94698215c4408a7$var$FORM_NAME);
|
|
24
|
+
const $d94698215c4408a7$export$a7fed597f4b8afd8 = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
25
|
+
const { __scopeForm: __scopeForm , onClearServerErrors: onClearServerErrors = ()=>{} , ...rootProps } = props;
|
|
26
|
+
const formRef = $RRrwT$useRef(null);
|
|
27
|
+
const composedFormRef = $RRrwT$useComposedRefs(forwardedRef, formRef); // native validity per field
|
|
28
|
+
const [validityMap, setValidityMap] = $RRrwT$useState({});
|
|
29
|
+
const getFieldValidity = $RRrwT$useCallback((fieldName)=>validityMap[fieldName]
|
|
30
|
+
, [
|
|
31
|
+
validityMap
|
|
32
|
+
]);
|
|
33
|
+
const handleFieldValidityChange = $RRrwT$useCallback((fieldName, validity)=>setValidityMap((prevValidityMap)=>{
|
|
34
|
+
var _prevValidityMap$fiel;
|
|
35
|
+
return {
|
|
36
|
+
...prevValidityMap,
|
|
37
|
+
[fieldName]: {
|
|
38
|
+
...(_prevValidityMap$fiel = prevValidityMap[fieldName]) !== null && _prevValidityMap$fiel !== void 0 ? _prevValidityMap$fiel : {},
|
|
39
|
+
...validity
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
})
|
|
43
|
+
, []);
|
|
44
|
+
const handleFieldValiditionClear = $RRrwT$useCallback((fieldName)=>{
|
|
45
|
+
setValidityMap((prevValidityMap)=>({
|
|
46
|
+
...prevValidityMap,
|
|
47
|
+
[fieldName]: undefined
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
setCustomErrorsMap((prevCustomErrorsMap)=>({
|
|
51
|
+
...prevCustomErrorsMap,
|
|
52
|
+
[fieldName]: {}
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
}, []); // custom matcher entries per field
|
|
56
|
+
const [customMatcherEntriesMap, setCustomMatcherEntriesMap] = $RRrwT$useState({});
|
|
57
|
+
const getFieldCustomMatcherEntries = $RRrwT$useCallback((fieldName)=>{
|
|
58
|
+
var _customMatcherEntries;
|
|
59
|
+
return (_customMatcherEntries = customMatcherEntriesMap[fieldName]) !== null && _customMatcherEntries !== void 0 ? _customMatcherEntries : [];
|
|
60
|
+
}, [
|
|
61
|
+
customMatcherEntriesMap
|
|
62
|
+
]);
|
|
63
|
+
const handleFieldCustomMatcherAdd = $RRrwT$useCallback((fieldName, matcherEntry)=>{
|
|
64
|
+
setCustomMatcherEntriesMap((prevCustomMatcherEntriesMap)=>{
|
|
65
|
+
var _prevCustomMatcherEnt;
|
|
66
|
+
return {
|
|
67
|
+
...prevCustomMatcherEntriesMap,
|
|
68
|
+
[fieldName]: [
|
|
69
|
+
...(_prevCustomMatcherEnt = prevCustomMatcherEntriesMap[fieldName]) !== null && _prevCustomMatcherEnt !== void 0 ? _prevCustomMatcherEnt : [],
|
|
70
|
+
matcherEntry
|
|
71
|
+
]
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
}, []);
|
|
75
|
+
const handleFieldCustomMatcherRemove = $RRrwT$useCallback((fieldName, matcherEntryId)=>{
|
|
76
|
+
setCustomMatcherEntriesMap((prevCustomMatcherEntriesMap)=>{
|
|
77
|
+
var _prevCustomMatcherEnt2;
|
|
78
|
+
return {
|
|
79
|
+
...prevCustomMatcherEntriesMap,
|
|
80
|
+
[fieldName]: ((_prevCustomMatcherEnt2 = prevCustomMatcherEntriesMap[fieldName]) !== null && _prevCustomMatcherEnt2 !== void 0 ? _prevCustomMatcherEnt2 : []).filter((matcherEntry)=>matcherEntry.id !== matcherEntryId
|
|
81
|
+
)
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
}, []); // custom errors per field
|
|
85
|
+
const [customErrorsMap, setCustomErrorsMap] = $RRrwT$useState({});
|
|
86
|
+
const getFieldCustomErrors = $RRrwT$useCallback((fieldName)=>{
|
|
87
|
+
var _customErrorsMap$fiel;
|
|
88
|
+
return (_customErrorsMap$fiel = customErrorsMap[fieldName]) !== null && _customErrorsMap$fiel !== void 0 ? _customErrorsMap$fiel : {};
|
|
89
|
+
}, [
|
|
90
|
+
customErrorsMap
|
|
91
|
+
]);
|
|
92
|
+
const handleFieldCustomErrorsChange = $RRrwT$useCallback((fieldName, customErrors)=>{
|
|
93
|
+
setCustomErrorsMap((prevCustomErrorsMap)=>{
|
|
94
|
+
var _prevCustomErrorsMap$;
|
|
95
|
+
return {
|
|
96
|
+
...prevCustomErrorsMap,
|
|
97
|
+
[fieldName]: {
|
|
98
|
+
...(_prevCustomErrorsMap$ = prevCustomErrorsMap[fieldName]) !== null && _prevCustomErrorsMap$ !== void 0 ? _prevCustomErrorsMap$ : {},
|
|
99
|
+
...customErrors
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}, []); // messageIds per field
|
|
104
|
+
const [messageIdsMap, setMessageIdsMap] = $RRrwT$useState({});
|
|
105
|
+
const handleFieldMessageIdAdd = $RRrwT$useCallback((fieldName, id)=>{
|
|
106
|
+
setMessageIdsMap((prevMessageIdsMap)=>{
|
|
107
|
+
const fieldDescriptionIds = new Set(prevMessageIdsMap[fieldName]).add(id);
|
|
108
|
+
return {
|
|
109
|
+
...prevMessageIdsMap,
|
|
110
|
+
[fieldName]: fieldDescriptionIds
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}, []);
|
|
114
|
+
const handleFieldMessageIdRemove = $RRrwT$useCallback((fieldName, id)=>{
|
|
115
|
+
setMessageIdsMap((prevMessageIdsMap)=>{
|
|
116
|
+
const fieldDescriptionIds = new Set(prevMessageIdsMap[fieldName]);
|
|
117
|
+
fieldDescriptionIds.delete(id);
|
|
118
|
+
return {
|
|
119
|
+
...prevMessageIdsMap,
|
|
120
|
+
[fieldName]: fieldDescriptionIds
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}, []);
|
|
124
|
+
const getFieldDescription = $RRrwT$useCallback((fieldName)=>{
|
|
125
|
+
var _messageIdsMap$fieldN;
|
|
126
|
+
return Array.from((_messageIdsMap$fieldN = messageIdsMap[fieldName]) !== null && _messageIdsMap$fieldN !== void 0 ? _messageIdsMap$fieldN : []).join(' ') || undefined;
|
|
127
|
+
}, [
|
|
128
|
+
messageIdsMap
|
|
129
|
+
]);
|
|
130
|
+
return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$ValidationProvider, {
|
|
131
|
+
scope: __scopeForm,
|
|
132
|
+
getFieldValidity: getFieldValidity,
|
|
133
|
+
onFieldValidityChange: handleFieldValidityChange,
|
|
134
|
+
getFieldCustomMatcherEntries: getFieldCustomMatcherEntries,
|
|
135
|
+
onFieldCustomMatcherEntryAdd: handleFieldCustomMatcherAdd,
|
|
136
|
+
onFieldCustomMatcherEntryRemove: handleFieldCustomMatcherRemove,
|
|
137
|
+
getFieldCustomErrors: getFieldCustomErrors,
|
|
138
|
+
onFieldCustomErrorsChange: handleFieldCustomErrorsChange,
|
|
139
|
+
onFieldValiditionClear: handleFieldValiditionClear
|
|
140
|
+
}, /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$AriaDescriptionProvider, {
|
|
141
|
+
scope: __scopeForm,
|
|
142
|
+
onFieldMessageIdAdd: handleFieldMessageIdAdd,
|
|
143
|
+
onFieldMessageIdRemove: handleFieldMessageIdRemove,
|
|
144
|
+
getFieldDescription: getFieldDescription
|
|
145
|
+
}, /*#__PURE__*/ $RRrwT$createElement($RRrwT$Primitive.form, $RRrwT$babelruntimehelpersesmextends({}, rootProps, {
|
|
146
|
+
ref: composedFormRef // focus first invalid control when the form is submitted
|
|
147
|
+
,
|
|
148
|
+
onInvalid: $RRrwT$composeEventHandlers(props.onInvalid, (event)=>{
|
|
149
|
+
const firstInvalidControl = $d94698215c4408a7$var$getFirstInvalidControl(event.currentTarget);
|
|
150
|
+
if (firstInvalidControl === event.target) firstInvalidControl.focus(); // prevent default browser UI for form validation
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
}) // clear server errors when the form is re-submitted
|
|
153
|
+
,
|
|
154
|
+
onSubmit: $RRrwT$composeEventHandlers(props.onSubmit, onClearServerErrors, {
|
|
155
|
+
checkForDefaultPrevented: false
|
|
156
|
+
}) // clear server errors when the form is reset
|
|
157
|
+
,
|
|
158
|
+
onReset: $RRrwT$composeEventHandlers(props.onReset, onClearServerErrors)
|
|
159
|
+
}))));
|
|
160
|
+
});
|
|
161
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$a7fed597f4b8afd8, {
|
|
162
|
+
displayName: $d94698215c4408a7$var$FORM_NAME
|
|
163
|
+
});
|
|
164
|
+
/* -------------------------------------------------------------------------------------------------
|
|
165
|
+
* FormField
|
|
166
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$FIELD_NAME = 'FormField';
|
|
167
|
+
const [$d94698215c4408a7$var$FormFieldProvider, $d94698215c4408a7$var$useFormFieldContext] = $d94698215c4408a7$var$createFormContext($d94698215c4408a7$var$FIELD_NAME);
|
|
168
|
+
const $d94698215c4408a7$export$56e87bf42978147a = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
169
|
+
const { __scopeForm: __scopeForm , name: name , serverInvalid: serverInvalid = false , ...fieldProps } = props;
|
|
170
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$FIELD_NAME, __scopeForm);
|
|
171
|
+
const validity = validationContext.getFieldValidity(name);
|
|
172
|
+
const id = $RRrwT$useId();
|
|
173
|
+
return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormFieldProvider, {
|
|
174
|
+
scope: __scopeForm,
|
|
175
|
+
id: id,
|
|
176
|
+
name: name,
|
|
177
|
+
serverInvalid: serverInvalid
|
|
178
|
+
}, /*#__PURE__*/ $RRrwT$createElement($RRrwT$Primitive.div, $RRrwT$babelruntimehelpersesmextends({
|
|
179
|
+
"data-valid": $d94698215c4408a7$var$getValidAttribute(validity, serverInvalid),
|
|
180
|
+
"data-invalid": $d94698215c4408a7$var$getInvalidAttribute(validity, serverInvalid)
|
|
181
|
+
}, fieldProps, {
|
|
182
|
+
ref: forwardedRef
|
|
183
|
+
})));
|
|
184
|
+
});
|
|
185
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$56e87bf42978147a, {
|
|
186
|
+
displayName: $d94698215c4408a7$var$FIELD_NAME
|
|
187
|
+
});
|
|
188
|
+
/* -------------------------------------------------------------------------------------------------
|
|
189
|
+
* FormLabel
|
|
190
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$LABEL_NAME = 'FormLabel';
|
|
191
|
+
const $d94698215c4408a7$export$842aba50ed0ce9d7 = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
192
|
+
const { __scopeForm: __scopeForm , ...labelProps } = props;
|
|
193
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$LABEL_NAME, __scopeForm);
|
|
194
|
+
const fieldContext = $d94698215c4408a7$var$useFormFieldContext($d94698215c4408a7$var$LABEL_NAME, __scopeForm);
|
|
195
|
+
const htmlFor = labelProps.htmlFor || fieldContext.id;
|
|
196
|
+
const validity = validationContext.getFieldValidity(fieldContext.name);
|
|
197
|
+
return /*#__PURE__*/ $RRrwT$createElement($RRrwT$Label, $RRrwT$babelruntimehelpersesmextends({
|
|
198
|
+
"data-valid": $d94698215c4408a7$var$getValidAttribute(validity, fieldContext.serverInvalid),
|
|
199
|
+
"data-invalid": $d94698215c4408a7$var$getInvalidAttribute(validity, fieldContext.serverInvalid)
|
|
200
|
+
}, labelProps, {
|
|
201
|
+
ref: forwardedRef,
|
|
202
|
+
htmlFor: htmlFor
|
|
203
|
+
}));
|
|
204
|
+
});
|
|
205
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$842aba50ed0ce9d7, {
|
|
206
|
+
displayName: $d94698215c4408a7$var$LABEL_NAME
|
|
207
|
+
});
|
|
208
|
+
/* -------------------------------------------------------------------------------------------------
|
|
209
|
+
* FormControl
|
|
210
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$CONTROL_NAME = 'FormControl';
|
|
211
|
+
const $d94698215c4408a7$export$fe5d99d8691b3f62 = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
212
|
+
const { __scopeForm: __scopeForm , ...controlProps } = props;
|
|
213
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$CONTROL_NAME, __scopeForm);
|
|
214
|
+
const fieldContext = $d94698215c4408a7$var$useFormFieldContext($d94698215c4408a7$var$CONTROL_NAME, __scopeForm);
|
|
215
|
+
const ariaDescriptionContext = $d94698215c4408a7$var$useAriaDescriptionContext($d94698215c4408a7$var$CONTROL_NAME, __scopeForm);
|
|
216
|
+
const ref = $RRrwT$useRef(null);
|
|
217
|
+
const composedRef = $RRrwT$useComposedRefs(forwardedRef, ref);
|
|
218
|
+
const name = controlProps.name || fieldContext.name;
|
|
219
|
+
const id1 = controlProps.id || fieldContext.id;
|
|
220
|
+
const customMatcherEntries = validationContext.getFieldCustomMatcherEntries(name);
|
|
221
|
+
const { onFieldValidityChange: onFieldValidityChange , onFieldCustomErrorsChange: onFieldCustomErrorsChange , onFieldValiditionClear: onFieldValiditionClear } = validationContext;
|
|
222
|
+
const updateControlValidity = $RRrwT$useCallback(async (control)=>{
|
|
223
|
+
//------------------------------------------------------------------------------------------
|
|
224
|
+
// 1. first, if we have built-in errors we stop here
|
|
225
|
+
if ($d94698215c4408a7$var$hasBuiltInError(control.validity)) {
|
|
226
|
+
const controlValidity = $d94698215c4408a7$var$validityStateToObject(control.validity);
|
|
227
|
+
onFieldValidityChange(name, controlValidity);
|
|
228
|
+
return;
|
|
229
|
+
} //------------------------------------------------------------------------------------------
|
|
230
|
+
// 2. then gather the form data to give to custom matchers for cross-comparisons
|
|
231
|
+
const formData = control.form ? new FormData(control.form) : new FormData();
|
|
232
|
+
const matcherArgs = [
|
|
233
|
+
control.value,
|
|
234
|
+
formData
|
|
235
|
+
]; //------------------------------------------------------------------------------------------
|
|
236
|
+
// 3. split sync and async custom matcher entries
|
|
237
|
+
const syncCustomMatcherEntries = [];
|
|
238
|
+
const ayncCustomMatcherEntries = [];
|
|
239
|
+
customMatcherEntries.forEach((customMatcherEntry)=>{
|
|
240
|
+
if ($d94698215c4408a7$var$isAsyncCustomMatcherEntry(customMatcherEntry, matcherArgs)) ayncCustomMatcherEntries.push(customMatcherEntry);
|
|
241
|
+
else if ($d94698215c4408a7$var$isSyncCustomMatcherEntry(customMatcherEntry)) syncCustomMatcherEntries.push(customMatcherEntry);
|
|
242
|
+
}); //------------------------------------------------------------------------------------------
|
|
243
|
+
// 4. run sync custom matchers and update control validity / internal validity + errors
|
|
244
|
+
const syncCustomErrors = syncCustomMatcherEntries.map(({ id: id , match: match })=>{
|
|
245
|
+
return [
|
|
246
|
+
id,
|
|
247
|
+
match(...matcherArgs)
|
|
248
|
+
];
|
|
249
|
+
});
|
|
250
|
+
const syncCustomErrorsById = Object.fromEntries(syncCustomErrors);
|
|
251
|
+
const hasSyncCustomErrors = Object.values(syncCustomErrorsById).some(Boolean);
|
|
252
|
+
const hasCustomError = hasSyncCustomErrors;
|
|
253
|
+
control.setCustomValidity(hasCustomError ? $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE : '');
|
|
254
|
+
const controlValidity = $d94698215c4408a7$var$validityStateToObject(control.validity);
|
|
255
|
+
onFieldValidityChange(name, controlValidity);
|
|
256
|
+
onFieldCustomErrorsChange(name, syncCustomErrorsById); //------------------------------------------------------------------------------------------
|
|
257
|
+
// 5. run async custom matchers and update control validity / internal validity + errors
|
|
258
|
+
if (!hasSyncCustomErrors && ayncCustomMatcherEntries.length > 0) {
|
|
259
|
+
const promisedCustomErrors = ayncCustomMatcherEntries.map(({ id: id , match: match })=>match(...matcherArgs).then((matches)=>[
|
|
260
|
+
id,
|
|
261
|
+
matches
|
|
262
|
+
]
|
|
263
|
+
)
|
|
264
|
+
);
|
|
265
|
+
const asyncCustomErrors = await Promise.all(promisedCustomErrors);
|
|
266
|
+
const asyncCustomErrorsById = Object.fromEntries(asyncCustomErrors);
|
|
267
|
+
const hasAsyncCustomErrors = Object.values(asyncCustomErrorsById).some(Boolean);
|
|
268
|
+
const hasCustomError = hasAsyncCustomErrors;
|
|
269
|
+
control.setCustomValidity(hasCustomError ? $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE : '');
|
|
270
|
+
const controlValidity = $d94698215c4408a7$var$validityStateToObject(control.validity);
|
|
271
|
+
onFieldValidityChange(name, controlValidity);
|
|
272
|
+
onFieldCustomErrorsChange(name, asyncCustomErrorsById);
|
|
273
|
+
}
|
|
274
|
+
}, [
|
|
275
|
+
customMatcherEntries,
|
|
276
|
+
name,
|
|
277
|
+
onFieldCustomErrorsChange,
|
|
278
|
+
onFieldValidityChange
|
|
279
|
+
]);
|
|
280
|
+
$RRrwT$useEffect(()=>{
|
|
281
|
+
const control = ref.current;
|
|
282
|
+
if (control) {
|
|
283
|
+
// We only want validate on change (native `change` event, not React's `onChange`). This is primarily
|
|
284
|
+
// a UX decision, we don't want to validate on every keystroke and React's `onChange` is the `input` event.
|
|
285
|
+
const handleChange = ()=>updateControlValidity(control)
|
|
286
|
+
;
|
|
287
|
+
control.addEventListener('change', handleChange);
|
|
288
|
+
return ()=>control.removeEventListener('change', handleChange)
|
|
289
|
+
;
|
|
290
|
+
}
|
|
291
|
+
}, [
|
|
292
|
+
updateControlValidity
|
|
293
|
+
]);
|
|
294
|
+
const resetControlValidity = $RRrwT$useCallback(()=>{
|
|
295
|
+
const control = ref.current;
|
|
296
|
+
if (control) {
|
|
297
|
+
control.setCustomValidity('');
|
|
298
|
+
onFieldValiditionClear(name);
|
|
299
|
+
}
|
|
300
|
+
}, [
|
|
301
|
+
name,
|
|
302
|
+
onFieldValiditionClear
|
|
303
|
+
]); // reset validity and errors when the form is reset
|
|
304
|
+
$RRrwT$useEffect(()=>{
|
|
305
|
+
var _ref$current;
|
|
306
|
+
const form = (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.form;
|
|
307
|
+
if (form) {
|
|
308
|
+
form.addEventListener('reset', resetControlValidity);
|
|
309
|
+
return ()=>form.removeEventListener('reset', resetControlValidity)
|
|
310
|
+
;
|
|
311
|
+
}
|
|
312
|
+
}, [
|
|
313
|
+
resetControlValidity
|
|
314
|
+
]); // focus first invalid control when fields are set as invalid by server
|
|
315
|
+
$RRrwT$useEffect(()=>{
|
|
316
|
+
const control = ref.current;
|
|
317
|
+
const form = control === null || control === void 0 ? void 0 : control.closest('form');
|
|
318
|
+
if (form && fieldContext.serverInvalid) {
|
|
319
|
+
const firstInvalidControl = $d94698215c4408a7$var$getFirstInvalidControl(form);
|
|
320
|
+
if (firstInvalidControl === control) firstInvalidControl.focus();
|
|
321
|
+
}
|
|
322
|
+
}, [
|
|
323
|
+
fieldContext.serverInvalid
|
|
324
|
+
]);
|
|
325
|
+
const validity = validationContext.getFieldValidity(name);
|
|
326
|
+
return /*#__PURE__*/ $RRrwT$createElement($RRrwT$Primitive.input, $RRrwT$babelruntimehelpersesmextends({
|
|
327
|
+
"data-valid": $d94698215c4408a7$var$getValidAttribute(validity, fieldContext.serverInvalid),
|
|
328
|
+
"data-invalid": $d94698215c4408a7$var$getInvalidAttribute(validity, fieldContext.serverInvalid),
|
|
329
|
+
"aria-invalid": fieldContext.serverInvalid ? true : undefined,
|
|
330
|
+
"aria-describedby": ariaDescriptionContext.getFieldDescription(name) // disable default browser behaviour of showing built-in error message on hover
|
|
331
|
+
,
|
|
332
|
+
title: ""
|
|
333
|
+
}, controlProps, {
|
|
334
|
+
ref: composedRef,
|
|
335
|
+
id: id1,
|
|
336
|
+
name: name,
|
|
337
|
+
onInvalid: $RRrwT$composeEventHandlers(props.onInvalid, (event)=>{
|
|
338
|
+
const control = event.currentTarget;
|
|
339
|
+
updateControlValidity(control);
|
|
340
|
+
}),
|
|
341
|
+
onChange: $RRrwT$composeEventHandlers(props.onChange, (event)=>{
|
|
342
|
+
// reset validity when user changes value
|
|
343
|
+
resetControlValidity();
|
|
344
|
+
})
|
|
345
|
+
}));
|
|
346
|
+
});
|
|
347
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$fe5d99d8691b3f62, {
|
|
348
|
+
displayName: $d94698215c4408a7$var$CONTROL_NAME
|
|
349
|
+
});
|
|
350
|
+
/* -------------------------------------------------------------------------------------------------
|
|
351
|
+
* FormMessage
|
|
352
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$validityMatchers = [
|
|
353
|
+
'badInput',
|
|
354
|
+
'patternMismatch',
|
|
355
|
+
'rangeOverflow',
|
|
356
|
+
'rangeUnderflow',
|
|
357
|
+
'stepMismatch',
|
|
358
|
+
'tooLong',
|
|
359
|
+
'tooShort',
|
|
360
|
+
'typeMismatch',
|
|
361
|
+
'valid',
|
|
362
|
+
'valueMissing'
|
|
363
|
+
];
|
|
364
|
+
const $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE = 'This value is not valid';
|
|
365
|
+
const $d94698215c4408a7$var$DEFAULT_BUILT_IN_MESSAGES = {
|
|
366
|
+
badInput: $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE,
|
|
367
|
+
patternMismatch: 'This value does not match the required pattern',
|
|
368
|
+
rangeOverflow: 'This value is too large',
|
|
369
|
+
rangeUnderflow: 'This value is too small',
|
|
370
|
+
stepMismatch: 'This value does not match the required step',
|
|
371
|
+
tooLong: 'This value is too long',
|
|
372
|
+
tooShort: 'This value is too short',
|
|
373
|
+
typeMismatch: 'This value does not match the required type',
|
|
374
|
+
valid: undefined,
|
|
375
|
+
valueMissing: 'This value is missing'
|
|
376
|
+
};
|
|
377
|
+
const $d94698215c4408a7$var$MESSAGE_NAME = 'FormMessage';
|
|
378
|
+
const $d94698215c4408a7$export$2e8ae7a1a126169a = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
379
|
+
const { match: match , name: nameProp , ...messageProps } = props;
|
|
380
|
+
const fieldContext = $d94698215c4408a7$var$useFormFieldContext($d94698215c4408a7$var$MESSAGE_NAME, props.__scopeForm);
|
|
381
|
+
const name = nameProp !== null && nameProp !== void 0 ? nameProp : fieldContext.name;
|
|
382
|
+
if (match === undefined) return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormMessageImpl, $RRrwT$babelruntimehelpersesmextends({}, messageProps, {
|
|
383
|
+
ref: forwardedRef,
|
|
384
|
+
name: name
|
|
385
|
+
}), props.children || $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE);
|
|
386
|
+
else if (typeof match === 'function') return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormCustomMessage, $RRrwT$babelruntimehelpersesmextends({
|
|
387
|
+
match: match
|
|
388
|
+
}, messageProps, {
|
|
389
|
+
ref: forwardedRef,
|
|
390
|
+
name: name
|
|
391
|
+
}));
|
|
392
|
+
else return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormBuiltInMessage, $RRrwT$babelruntimehelpersesmextends({
|
|
393
|
+
match: match
|
|
394
|
+
}, messageProps, {
|
|
395
|
+
ref: forwardedRef,
|
|
396
|
+
name: name
|
|
397
|
+
}));
|
|
398
|
+
});
|
|
399
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$2e8ae7a1a126169a, {
|
|
400
|
+
displayName: $d94698215c4408a7$var$MESSAGE_NAME
|
|
401
|
+
});
|
|
402
|
+
const $d94698215c4408a7$var$FormBuiltInMessage = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
403
|
+
const { match: match , forceMatch: forceMatch = false , name: name , children: children , ...messageProps } = props;
|
|
404
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$MESSAGE_NAME, messageProps.__scopeForm);
|
|
405
|
+
const validity = validationContext.getFieldValidity(name);
|
|
406
|
+
const matches = forceMatch || (validity === null || validity === void 0 ? void 0 : validity[match]);
|
|
407
|
+
if (matches) return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormMessageImpl, $RRrwT$babelruntimehelpersesmextends({
|
|
408
|
+
ref: forwardedRef
|
|
409
|
+
}, messageProps, {
|
|
410
|
+
name: name
|
|
411
|
+
}), children !== null && children !== void 0 ? children : $d94698215c4408a7$var$DEFAULT_BUILT_IN_MESSAGES[match]);
|
|
412
|
+
return null;
|
|
413
|
+
});
|
|
414
|
+
const $d94698215c4408a7$var$FormCustomMessage = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
415
|
+
const { match: match , forceMatch: forceMatch = false , name: name , id: idProp , children: children , ...messageProps } = props;
|
|
416
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$MESSAGE_NAME, messageProps.__scopeForm);
|
|
417
|
+
const ref = $RRrwT$useRef(null);
|
|
418
|
+
const composedRef = $RRrwT$useComposedRefs(forwardedRef, ref);
|
|
419
|
+
const _id = $RRrwT$useId();
|
|
420
|
+
const id = idProp !== null && idProp !== void 0 ? idProp : _id;
|
|
421
|
+
const customMatcherEntry = $RRrwT$useMemo(()=>({
|
|
422
|
+
id: id,
|
|
423
|
+
match: match
|
|
424
|
+
})
|
|
425
|
+
, [
|
|
426
|
+
id,
|
|
427
|
+
match
|
|
428
|
+
]);
|
|
429
|
+
const { onFieldCustomMatcherEntryAdd: onFieldCustomMatcherEntryAdd , onFieldCustomMatcherEntryRemove: onFieldCustomMatcherEntryRemove } = validationContext;
|
|
430
|
+
$RRrwT$useEffect(()=>{
|
|
431
|
+
onFieldCustomMatcherEntryAdd(name, customMatcherEntry);
|
|
432
|
+
return ()=>onFieldCustomMatcherEntryRemove(name, customMatcherEntry.id)
|
|
433
|
+
;
|
|
434
|
+
}, [
|
|
435
|
+
customMatcherEntry,
|
|
436
|
+
name,
|
|
437
|
+
onFieldCustomMatcherEntryAdd,
|
|
438
|
+
onFieldCustomMatcherEntryRemove
|
|
439
|
+
]);
|
|
440
|
+
const validity = validationContext.getFieldValidity(name);
|
|
441
|
+
const customErrors = validationContext.getFieldCustomErrors(name);
|
|
442
|
+
const hasMatchingCustomError = customErrors[id];
|
|
443
|
+
const matches = forceMatch || validity && !$d94698215c4408a7$var$hasBuiltInError(validity) && hasMatchingCustomError;
|
|
444
|
+
if (matches) return /*#__PURE__*/ $RRrwT$createElement($d94698215c4408a7$var$FormMessageImpl, $RRrwT$babelruntimehelpersesmextends({
|
|
445
|
+
id: id,
|
|
446
|
+
ref: composedRef
|
|
447
|
+
}, messageProps, {
|
|
448
|
+
name: name
|
|
449
|
+
}), children !== null && children !== void 0 ? children : $d94698215c4408a7$var$DEFAULT_INVALID_MESSAGE);
|
|
450
|
+
return null;
|
|
451
|
+
});
|
|
452
|
+
const $d94698215c4408a7$var$FormMessageImpl = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
453
|
+
const { __scopeForm: __scopeForm , id: idProp , name: name , ...messageProps } = props;
|
|
454
|
+
const ariaDescriptionContext = $d94698215c4408a7$var$useAriaDescriptionContext($d94698215c4408a7$var$MESSAGE_NAME, __scopeForm);
|
|
455
|
+
const _id = $RRrwT$useId();
|
|
456
|
+
const id = idProp !== null && idProp !== void 0 ? idProp : _id;
|
|
457
|
+
const { onFieldMessageIdAdd: onFieldMessageIdAdd , onFieldMessageIdRemove: onFieldMessageIdRemove } = ariaDescriptionContext;
|
|
458
|
+
$RRrwT$useEffect(()=>{
|
|
459
|
+
onFieldMessageIdAdd(name, id);
|
|
460
|
+
return ()=>onFieldMessageIdRemove(name, id)
|
|
461
|
+
;
|
|
462
|
+
}, [
|
|
463
|
+
name,
|
|
464
|
+
id,
|
|
465
|
+
onFieldMessageIdAdd,
|
|
466
|
+
onFieldMessageIdRemove
|
|
467
|
+
]);
|
|
468
|
+
return /*#__PURE__*/ $RRrwT$createElement($RRrwT$Primitive.span, $RRrwT$babelruntimehelpersesmextends({
|
|
469
|
+
id: id
|
|
470
|
+
}, messageProps, {
|
|
471
|
+
ref: forwardedRef
|
|
472
|
+
}));
|
|
473
|
+
});
|
|
474
|
+
/* -------------------------------------------------------------------------------------------------
|
|
475
|
+
* FormValidityState
|
|
476
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$VALIDITY_STATE_NAME = 'FormValidityState';
|
|
477
|
+
const $d94698215c4408a7$export$7a93102810e06862 = (props)=>{
|
|
478
|
+
const { __scopeForm: __scopeForm , name: nameProp , children: children } = props;
|
|
479
|
+
const validationContext = $d94698215c4408a7$var$useValidationContext($d94698215c4408a7$var$VALIDITY_STATE_NAME, __scopeForm);
|
|
480
|
+
const fieldContext = $d94698215c4408a7$var$useFormFieldContext($d94698215c4408a7$var$VALIDITY_STATE_NAME, __scopeForm);
|
|
481
|
+
const name = nameProp !== null && nameProp !== void 0 ? nameProp : fieldContext.name;
|
|
482
|
+
const validity = validationContext.getFieldValidity(name);
|
|
483
|
+
return /*#__PURE__*/ $RRrwT$createElement($RRrwT$Fragment, null, children(validity));
|
|
484
|
+
};
|
|
485
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$7a93102810e06862, {
|
|
486
|
+
displayName: $d94698215c4408a7$var$VALIDITY_STATE_NAME
|
|
487
|
+
});
|
|
488
|
+
/* -------------------------------------------------------------------------------------------------
|
|
489
|
+
* FormSubmit
|
|
490
|
+
* -----------------------------------------------------------------------------------------------*/ const $d94698215c4408a7$var$SUBMIT_NAME = 'FormSubmit';
|
|
491
|
+
const $d94698215c4408a7$export$d0861e5bd09bd9e4 = /*#__PURE__*/ $RRrwT$forwardRef((props, forwardedRef)=>{
|
|
492
|
+
const { __scopeForm: __scopeForm , ...submitProps } = props;
|
|
493
|
+
return /*#__PURE__*/ $RRrwT$createElement($RRrwT$Primitive.button, $RRrwT$babelruntimehelpersesmextends({
|
|
494
|
+
type: "submit"
|
|
495
|
+
}, submitProps, {
|
|
496
|
+
ref: forwardedRef
|
|
497
|
+
}));
|
|
498
|
+
});
|
|
499
|
+
/*#__PURE__*/ Object.assign($d94698215c4408a7$export$d0861e5bd09bd9e4, {
|
|
500
|
+
displayName: $d94698215c4408a7$var$SUBMIT_NAME
|
|
501
|
+
});
|
|
502
|
+
/* -----------------------------------------------------------------------------------------------*/ function $d94698215c4408a7$var$validityStateToObject(validity) {
|
|
503
|
+
const object = {};
|
|
504
|
+
for(const key in validity)object[key] = validity[key];
|
|
505
|
+
return object;
|
|
506
|
+
}
|
|
507
|
+
function $d94698215c4408a7$var$isHTMLElement(element) {
|
|
508
|
+
return element instanceof HTMLElement;
|
|
509
|
+
}
|
|
510
|
+
function $d94698215c4408a7$var$isFormControl(element) {
|
|
511
|
+
return 'validity' in element;
|
|
512
|
+
}
|
|
513
|
+
function $d94698215c4408a7$var$isInvalid(control) {
|
|
514
|
+
return $d94698215c4408a7$var$isFormControl(control) && (control.validity.valid === false || control.getAttribute('aria-invalid') === 'true');
|
|
515
|
+
}
|
|
516
|
+
function $d94698215c4408a7$var$getFirstInvalidControl(form) {
|
|
517
|
+
const elements = form.elements;
|
|
518
|
+
const [firstInvalidControl] = Array.from(elements).filter($d94698215c4408a7$var$isHTMLElement).filter($d94698215c4408a7$var$isInvalid);
|
|
519
|
+
return firstInvalidControl;
|
|
520
|
+
}
|
|
521
|
+
function $d94698215c4408a7$var$isAsyncCustomMatcherEntry(entry, args) {
|
|
522
|
+
return entry.match.constructor.name === 'AsyncFunction' || $d94698215c4408a7$var$returnsPromise(entry.match, args);
|
|
523
|
+
}
|
|
524
|
+
function $d94698215c4408a7$var$isSyncCustomMatcherEntry(entry) {
|
|
525
|
+
return entry.match.constructor.name === 'Function';
|
|
526
|
+
}
|
|
527
|
+
function $d94698215c4408a7$var$returnsPromise(func, args) {
|
|
528
|
+
return func(...args) instanceof Promise;
|
|
529
|
+
}
|
|
530
|
+
function $d94698215c4408a7$var$hasBuiltInError(validity) {
|
|
531
|
+
let error = false;
|
|
532
|
+
for(const validityKey in validity){
|
|
533
|
+
const key = validityKey;
|
|
534
|
+
if (key !== 'valid' && key !== 'customError' && validity[key]) {
|
|
535
|
+
error = true;
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return error;
|
|
540
|
+
}
|
|
541
|
+
function $d94698215c4408a7$var$getValidAttribute(validity, serverInvalid) {
|
|
542
|
+
if ((validity === null || validity === void 0 ? void 0 : validity.valid) === true && !serverInvalid) return true;
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
function $d94698215c4408a7$var$getInvalidAttribute(validity, serverInvalid) {
|
|
546
|
+
if ((validity === null || validity === void 0 ? void 0 : validity.valid) === false || serverInvalid) return true;
|
|
547
|
+
return undefined;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
export {$d94698215c4408a7$export$299997c7551e97cb as createFormScope, $d94698215c4408a7$export$a7fed597f4b8afd8 as Form, $d94698215c4408a7$export$a7fed597f4b8afd8 as Root, $d94698215c4408a7$export$56e87bf42978147a as FormField, $d94698215c4408a7$export$56e87bf42978147a as Field, $d94698215c4408a7$export$842aba50ed0ce9d7 as FormLabel, $d94698215c4408a7$export$842aba50ed0ce9d7 as Label, $d94698215c4408a7$export$fe5d99d8691b3f62 as FormControl, $d94698215c4408a7$export$fe5d99d8691b3f62 as Control, $d94698215c4408a7$export$2e8ae7a1a126169a as FormMessage, $d94698215c4408a7$export$2e8ae7a1a126169a as Message, $d94698215c4408a7$export$7a93102810e06862 as FormValidityState, $d94698215c4408a7$export$7a93102810e06862 as ValidityState, $d94698215c4408a7$export$d0861e5bd09bd9e4 as FormSubmit, $d94698215c4408a7$export$d0861e5bd09bd9e4 as Submit};
|
|
554
|
+
//# sourceMappingURL=index.module.js.map
|