@wix/site-ui 1.83.0 → 1.84.0

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/8972.js ADDED
@@ -0,0 +1,576 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useBaseUiId } from "./6046.js";
3
+ import { useRefWithInit, EMPTY_OBJECT, mergeProps, useRenderElement } from "./8272.js";
4
+ import { LabelableContext, FieldRootContext, useFieldRootContext, useLabelableContext, fieldValidityMapping, DEFAULT_VALIDITY_STATE } from "./1477.js";
5
+ import { useStableCallback } from "./7541.js";
6
+ import { useFormContext } from "./5085.js";
7
+ import { useTimeout } from "./42.js";
8
+ import { useIsoLayoutEffect } from "./6499.js";
9
+ import { useFieldsetRootContext } from "./353.js";
10
+ import { transitionStatusMapping, useOpenChangeComplete, useTransitionStatus } from "./880.js";
11
+ import * as __rspack_external_react from "react";
12
+ const LabelableProvider_LabelableProvider = function(props) {
13
+ const defaultId = useBaseUiId();
14
+ const initialControlId = void 0 === props.controlId ? defaultId : props.controlId;
15
+ const [controlId, setControlIdState] = __rspack_external_react.useState(initialControlId);
16
+ const [labelId, setLabelId] = __rspack_external_react.useState(props.labelId);
17
+ const [messageIds, setMessageIds] = __rspack_external_react.useState([]);
18
+ const registrationsRef = useRefWithInit(()=>new Map());
19
+ const { messageIds: parentMessageIds } = useLabelableContext();
20
+ const registerControlId = useStableCallback((source, nextId)=>{
21
+ const registrations = registrationsRef.current;
22
+ if (void 0 === nextId) return void registrations.delete(source);
23
+ registrations.set(source, nextId);
24
+ setControlIdState((prev)=>{
25
+ if (0 === registrations.size) return;
26
+ let nextControlId;
27
+ for (const id of registrations.values()){
28
+ if (void 0 !== prev && id === prev) return prev;
29
+ if (void 0 === nextControlId) nextControlId = id;
30
+ }
31
+ return nextControlId;
32
+ });
33
+ });
34
+ const getDescriptionProps = __rspack_external_react.useCallback((externalProps)=>mergeProps({
35
+ 'aria-describedby': parentMessageIds.concat(messageIds).join(' ') || void 0
36
+ }, externalProps), [
37
+ parentMessageIds,
38
+ messageIds
39
+ ]);
40
+ const contextValue = __rspack_external_react.useMemo(()=>({
41
+ controlId,
42
+ registerControlId,
43
+ labelId,
44
+ setLabelId,
45
+ messageIds,
46
+ setMessageIds,
47
+ getDescriptionProps
48
+ }), [
49
+ controlId,
50
+ registerControlId,
51
+ labelId,
52
+ setLabelId,
53
+ messageIds,
54
+ setMessageIds,
55
+ getDescriptionProps
56
+ ]);
57
+ return /*#__PURE__*/ jsx(LabelableContext.Provider, {
58
+ value: contextValue,
59
+ children: props.children
60
+ });
61
+ };
62
+ if ("production" !== process.env.NODE_ENV) LabelableProvider_LabelableProvider.displayName = "LabelableProvider";
63
+ function getCombinedFieldValidityData(validityData, invalid) {
64
+ return {
65
+ ...validityData,
66
+ state: {
67
+ ...validityData.state,
68
+ valid: !invalid && validityData.state.valid
69
+ }
70
+ };
71
+ }
72
+ const validityKeys = Object.keys(DEFAULT_VALIDITY_STATE);
73
+ function isOnlyValueMissing(state) {
74
+ if (!state || state.valid || !state.valueMissing) return false;
75
+ let onlyValueMissing = false;
76
+ for (const key of validityKeys)if ('valid' !== key) {
77
+ if ('valueMissing' === key) onlyValueMissing = state[key];
78
+ if (state[key]) onlyValueMissing = false;
79
+ }
80
+ return onlyValueMissing;
81
+ }
82
+ function useFieldValidation(params) {
83
+ const { formRef, clearErrors } = useFormContext();
84
+ const { setValidityData, validate, validityData, validationDebounceTime, invalid, markedDirtyRef, state, name, shouldValidateOnChange, getRegisteredFieldId } = params;
85
+ const { controlId, getDescriptionProps } = useLabelableContext();
86
+ const timeout = useTimeout();
87
+ const inputRef = __rspack_external_react.useRef(null);
88
+ const commit = useStableCallback(async (value, revalidate = false)=>{
89
+ const element = inputRef.current;
90
+ if (!element) return;
91
+ function updateRegisteredFieldValidity(nextValidityData, externalInvalid = invalid) {
92
+ const fieldId = getRegisteredFieldId() ?? controlId;
93
+ if (null == fieldId) return;
94
+ const currentFieldData = formRef.current.fields.get(fieldId);
95
+ if (!currentFieldData) return;
96
+ const validityDataWithFormErrors = getCombinedFieldValidityData(nextValidityData, externalInvalid);
97
+ formRef.current.fields.set(fieldId, {
98
+ ...currentFieldData,
99
+ validityData: validityDataWithFormErrors
100
+ });
101
+ }
102
+ if (revalidate) {
103
+ if (false !== state.valid) return;
104
+ const currentNativeValidity = element.validity;
105
+ if (!currentNativeValidity.valueMissing) {
106
+ const nextValidityData = {
107
+ value,
108
+ state: {
109
+ ...DEFAULT_VALIDITY_STATE,
110
+ valid: true
111
+ },
112
+ error: '',
113
+ errors: [],
114
+ initialValue: validityData.initialValue
115
+ };
116
+ element.setCustomValidity('');
117
+ updateRegisteredFieldValidity(nextValidityData, false);
118
+ setValidityData(nextValidityData);
119
+ return;
120
+ }
121
+ const currentNativeValidityObject = validityKeys.reduce((acc, key)=>{
122
+ acc[key] = currentNativeValidity[key];
123
+ return acc;
124
+ }, {});
125
+ if (!currentNativeValidityObject.valid && !isOnlyValueMissing(currentNativeValidityObject)) return;
126
+ }
127
+ function getState(el) {
128
+ const computedState = validityKeys.reduce((acc, key)=>{
129
+ acc[key] = el.validity[key];
130
+ return acc;
131
+ }, {});
132
+ let hasOnlyValueMissingError = false;
133
+ for (const key of validityKeys)if ('valid' !== key) {
134
+ if ('valueMissing' === key && computedState[key]) hasOnlyValueMissingError = true;
135
+ else if (computedState[key]) return computedState;
136
+ }
137
+ if (hasOnlyValueMissingError && !markedDirtyRef.current) {
138
+ computedState.valid = true;
139
+ computedState.valueMissing = false;
140
+ }
141
+ return computedState;
142
+ }
143
+ timeout.clear();
144
+ let result = null;
145
+ let validationErrors = [];
146
+ const nextState = getState(element);
147
+ let defaultValidationMessage;
148
+ const validateOnChange = shouldValidateOnChange();
149
+ if (element.validationMessage && !validateOnChange) {
150
+ defaultValidationMessage = element.validationMessage;
151
+ validationErrors = [
152
+ element.validationMessage
153
+ ];
154
+ } else {
155
+ const formValues = Array.from(formRef.current.fields.values()).reduce((acc, field)=>{
156
+ if (field.name) acc[field.name] = field.getValue();
157
+ return acc;
158
+ }, {});
159
+ const resultOrPromise = validate(value, formValues);
160
+ result = 'object' == typeof resultOrPromise && null !== resultOrPromise && 'then' in resultOrPromise ? await resultOrPromise : resultOrPromise;
161
+ if (null !== result) {
162
+ nextState.valid = false;
163
+ nextState.customError = true;
164
+ if (Array.isArray(result)) {
165
+ validationErrors = result;
166
+ element.setCustomValidity(result.join('\n'));
167
+ } else if (result) {
168
+ validationErrors = [
169
+ result
170
+ ];
171
+ element.setCustomValidity(result);
172
+ }
173
+ } else if (validateOnChange) {
174
+ element.setCustomValidity('');
175
+ nextState.customError = false;
176
+ if (element.validationMessage) {
177
+ defaultValidationMessage = element.validationMessage;
178
+ validationErrors = [
179
+ element.validationMessage
180
+ ];
181
+ } else if (element.validity.valid && !nextState.valid) nextState.valid = true;
182
+ }
183
+ }
184
+ const nextValidityData = {
185
+ value,
186
+ state: nextState,
187
+ error: defaultValidationMessage ?? (Array.isArray(result) ? result[0] : result ?? ''),
188
+ errors: validationErrors,
189
+ initialValue: validityData.initialValue
190
+ };
191
+ updateRegisteredFieldValidity(nextValidityData);
192
+ setValidityData(nextValidityData);
193
+ });
194
+ const getValidationProps = __rspack_external_react.useCallback((externalProps = {})=>mergeProps(getDescriptionProps, false === state.valid ? {
195
+ 'aria-invalid': true
196
+ } : EMPTY_OBJECT, externalProps), [
197
+ getDescriptionProps,
198
+ state.valid
199
+ ]);
200
+ const getInputValidationProps = __rspack_external_react.useCallback((externalProps = {})=>mergeProps({
201
+ onChange (event) {
202
+ if (event.nativeEvent.defaultPrevented) return;
203
+ clearErrors(name);
204
+ if (!shouldValidateOnChange()) return void commit(event.currentTarget.value, true);
205
+ const element = event.currentTarget;
206
+ if ('' === element.value) return void commit(element.value);
207
+ timeout.clear();
208
+ if (validationDebounceTime) timeout.start(validationDebounceTime, ()=>{
209
+ commit(element.value);
210
+ });
211
+ else commit(element.value);
212
+ }
213
+ }, getValidationProps(externalProps)), [
214
+ getValidationProps,
215
+ clearErrors,
216
+ name,
217
+ timeout,
218
+ commit,
219
+ validationDebounceTime,
220
+ shouldValidateOnChange
221
+ ]);
222
+ return __rspack_external_react.useMemo(()=>({
223
+ getValidationProps,
224
+ getInputValidationProps,
225
+ inputRef,
226
+ commit
227
+ }), [
228
+ getValidationProps,
229
+ getInputValidationProps,
230
+ commit
231
+ ]);
232
+ }
233
+ function useFieldControlRegistration(params) {
234
+ const { commit, invalid, markedDirtyRef, name, setRegisteredFieldId, setValidityData, validityData } = params;
235
+ const { formRef } = useFormContext();
236
+ const activeFieldControlSourceRef = __rspack_external_react.useRef(null);
237
+ const registrationRef = __rspack_external_react.useRef(null);
238
+ const fallbackControlRef = __rspack_external_react.useRef(null);
239
+ const getValueForForm = useStableCallback(()=>{
240
+ const registration = registrationRef.current;
241
+ if (!registration) return;
242
+ if (registration.getValue) return registration.getValue();
243
+ return registration.value;
244
+ });
245
+ const validate = useStableCallback(()=>{
246
+ const registration = registrationRef.current;
247
+ if (!registration) return;
248
+ let nextValue = registration.value;
249
+ if (void 0 === nextValue) nextValue = getValueForForm();
250
+ markedDirtyRef.current = true;
251
+ commit(nextValue);
252
+ });
253
+ function refreshRegistration() {
254
+ const registration = registrationRef.current;
255
+ if (!registration || !registration.id) return;
256
+ formRef.current.fields.set(registration.id, {
257
+ getValue: getValueForForm,
258
+ name,
259
+ controlRef: registration.controlRef ?? fallbackControlRef,
260
+ validityData: getCombinedFieldValidityData(validityData, invalid),
261
+ validate
262
+ });
263
+ }
264
+ function deleteRegistration(id = registrationRef.current?.id) {
265
+ if (id) formRef.current.fields.delete(id);
266
+ }
267
+ function syncInitialValue() {
268
+ const registration = registrationRef.current;
269
+ if (!registration) return;
270
+ let initialValue = registration.value;
271
+ if (void 0 === initialValue) initialValue = getValueForForm();
272
+ if (null === validityData.initialValue && null !== initialValue) setValidityData((prev)=>({
273
+ ...prev,
274
+ initialValue
275
+ }));
276
+ }
277
+ useIsoLayoutEffect(()=>{
278
+ const registration = registrationRef.current;
279
+ if (!registration || !registration.id) return;
280
+ formRef.current.fields.set(registration.id, {
281
+ getValue: getValueForForm,
282
+ name,
283
+ controlRef: registration.controlRef ?? fallbackControlRef,
284
+ validityData: getCombinedFieldValidityData(validityData, invalid),
285
+ validate
286
+ });
287
+ }, [
288
+ formRef,
289
+ getValueForForm,
290
+ invalid,
291
+ name,
292
+ validate,
293
+ validityData
294
+ ]);
295
+ useIsoLayoutEffect(()=>{
296
+ const fields = formRef.current.fields;
297
+ return ()=>{
298
+ const id = registrationRef.current?.id;
299
+ if (id) fields.delete(id);
300
+ };
301
+ }, [
302
+ formRef
303
+ ]);
304
+ return useStableCallback((source, registration)=>{
305
+ if (!registration) {
306
+ if (activeFieldControlSourceRef.current === source) {
307
+ activeFieldControlSourceRef.current = null;
308
+ deleteRegistration();
309
+ registrationRef.current = null;
310
+ setRegisteredFieldId(void 0);
311
+ }
312
+ return;
313
+ }
314
+ const previousId = registrationRef.current?.id;
315
+ activeFieldControlSourceRef.current = source;
316
+ registrationRef.current = registration;
317
+ setRegisteredFieldId(registration.id);
318
+ if (previousId && previousId !== registration.id) deleteRegistration(previousId);
319
+ syncInitialValue();
320
+ refreshRegistration();
321
+ });
322
+ }
323
+ const FieldRoot_FieldRootInner = /*#__PURE__*/ __rspack_external_react.forwardRef(function(componentProps, forwardedRef) {
324
+ const { errors, validationMode: formValidationMode, submitAttemptedRef } = useFormContext();
325
+ const { render, className, validate: validateProp, validationDebounceTime = 0, validationMode = formValidationMode, name, disabled: disabledProp = false, invalid: invalidProp, dirty: dirtyProp, touched: touchedProp, actionsRef, style, ...elementProps } = componentProps;
326
+ const { disabled: disabledFieldset } = useFieldsetRootContext();
327
+ const validate = useStableCallback(validateProp || (()=>null));
328
+ const disabled = disabledFieldset || disabledProp;
329
+ const [touchedState, setTouchedUnwrapped] = __rspack_external_react.useState(false);
330
+ const [dirtyState, setDirtyUnwrapped] = __rspack_external_react.useState(false);
331
+ const [filled, setFilled] = __rspack_external_react.useState(false);
332
+ const [focused, setFocused] = __rspack_external_react.useState(false);
333
+ const dirty = dirtyProp ?? dirtyState;
334
+ const touched = touchedProp ?? touchedState;
335
+ const markedDirtyRef = __rspack_external_react.useRef(false);
336
+ const registeredFieldIdRef = __rspack_external_react.useRef(void 0);
337
+ const getRegisteredFieldId = __rspack_external_react.useCallback(()=>registeredFieldIdRef.current, []);
338
+ const setRegisteredFieldId = __rspack_external_react.useCallback((id)=>{
339
+ registeredFieldIdRef.current = id;
340
+ }, []);
341
+ const setDirty = useStableCallback((value)=>{
342
+ if (void 0 !== dirtyProp) return;
343
+ if (value) markedDirtyRef.current = true;
344
+ setDirtyUnwrapped(value);
345
+ });
346
+ const setTouched = useStableCallback((value)=>{
347
+ if (void 0 !== touchedProp) return;
348
+ setTouchedUnwrapped(value);
349
+ });
350
+ const shouldValidateOnChange = useStableCallback(()=>'onChange' === validationMode || 'onSubmit' === validationMode && submitAttemptedRef.current);
351
+ const hasFormError = !!name && Object.hasOwn(errors, name) && void 0 !== errors[name];
352
+ const invalid = true === invalidProp || hasFormError;
353
+ const [validityData, setValidityData] = __rspack_external_react.useState({
354
+ state: DEFAULT_VALIDITY_STATE,
355
+ error: '',
356
+ errors: [],
357
+ value: null,
358
+ initialValue: null
359
+ });
360
+ const valid = !invalid && validityData.state.valid;
361
+ const state = __rspack_external_react.useMemo(()=>({
362
+ disabled,
363
+ touched,
364
+ dirty,
365
+ valid,
366
+ filled,
367
+ focused
368
+ }), [
369
+ disabled,
370
+ touched,
371
+ dirty,
372
+ valid,
373
+ filled,
374
+ focused
375
+ ]);
376
+ const validation = useFieldValidation({
377
+ setValidityData,
378
+ validate,
379
+ validityData,
380
+ validationDebounceTime,
381
+ invalid,
382
+ markedDirtyRef,
383
+ state,
384
+ name,
385
+ shouldValidateOnChange,
386
+ getRegisteredFieldId
387
+ });
388
+ const validityValue = validityData.value;
389
+ const handleImperativeValidate = __rspack_external_react.useCallback(()=>{
390
+ markedDirtyRef.current = true;
391
+ validation.commit(validityValue);
392
+ }, [
393
+ validation,
394
+ validityValue
395
+ ]);
396
+ const registerFieldControl = useFieldControlRegistration({
397
+ commit: validation.commit,
398
+ invalid,
399
+ markedDirtyRef,
400
+ name,
401
+ setRegisteredFieldId,
402
+ setValidityData,
403
+ validityData
404
+ });
405
+ __rspack_external_react.useImperativeHandle(actionsRef, ()=>({
406
+ validate: handleImperativeValidate
407
+ }), [
408
+ handleImperativeValidate
409
+ ]);
410
+ const contextValue = __rspack_external_react.useMemo(()=>({
411
+ invalid,
412
+ name,
413
+ validityData,
414
+ setValidityData,
415
+ disabled,
416
+ touched,
417
+ setTouched,
418
+ dirty,
419
+ setDirty,
420
+ filled,
421
+ setFilled,
422
+ focused,
423
+ setFocused,
424
+ validate,
425
+ validationMode,
426
+ validationDebounceTime,
427
+ shouldValidateOnChange,
428
+ state,
429
+ markedDirtyRef,
430
+ registerFieldControl,
431
+ validation
432
+ }), [
433
+ invalid,
434
+ name,
435
+ validityData,
436
+ disabled,
437
+ touched,
438
+ setTouched,
439
+ dirty,
440
+ setDirty,
441
+ filled,
442
+ setFilled,
443
+ focused,
444
+ setFocused,
445
+ validate,
446
+ validationMode,
447
+ validationDebounceTime,
448
+ shouldValidateOnChange,
449
+ state,
450
+ registerFieldControl,
451
+ validation
452
+ ]);
453
+ const element = useRenderElement('div', componentProps, {
454
+ ref: forwardedRef,
455
+ state,
456
+ props: elementProps,
457
+ stateAttributesMapping: fieldValidityMapping
458
+ });
459
+ return /*#__PURE__*/ jsx(FieldRootContext.Provider, {
460
+ value: contextValue,
461
+ children: element
462
+ });
463
+ });
464
+ if ("production" !== process.env.NODE_ENV) FieldRoot_FieldRootInner.displayName = "FieldRootInner";
465
+ const FieldRoot_FieldRoot = /*#__PURE__*/ __rspack_external_react.forwardRef(function(componentProps, forwardedRef) {
466
+ return /*#__PURE__*/ jsx(LabelableProvider_LabelableProvider, {
467
+ children: /*#__PURE__*/ jsx(FieldRoot_FieldRootInner, {
468
+ ...componentProps,
469
+ ref: forwardedRef
470
+ })
471
+ });
472
+ });
473
+ if ("production" !== process.env.NODE_ENV) FieldRoot_FieldRoot.displayName = "FieldRoot";
474
+ const stateAttributesMapping = {
475
+ ...fieldValidityMapping,
476
+ ...transitionStatusMapping
477
+ };
478
+ const FieldError_FieldError = /*#__PURE__*/ __rspack_external_react.forwardRef(function(componentProps, forwardedRef) {
479
+ const { render, id: idProp, className, match, style, ...elementProps } = componentProps;
480
+ const id = useBaseUiId(idProp);
481
+ const { validityData, state: fieldState, name } = useFieldRootContext(false);
482
+ const { setMessageIds } = useLabelableContext();
483
+ const { errors } = useFormContext();
484
+ const formError = name ? errors[name] : null;
485
+ const hasSpecificMatch = 'string' == typeof match;
486
+ let rendered = false;
487
+ rendered = true === match ? true : hasSpecificMatch ? Boolean(validityData.state[match]) : Boolean(formError) || false === validityData.state.valid;
488
+ const { mounted, transitionStatus, setMounted } = useTransitionStatus(rendered);
489
+ useIsoLayoutEffect(()=>{
490
+ if (!rendered || !id) return;
491
+ setMessageIds((v)=>v.concat(id));
492
+ return ()=>{
493
+ setMessageIds((v)=>v.filter((item)=>item !== id));
494
+ };
495
+ }, [
496
+ rendered,
497
+ id,
498
+ setMessageIds
499
+ ]);
500
+ const errorRef = __rspack_external_react.useRef(null);
501
+ const [lastRenderedMessage, setLastRenderedMessage] = __rspack_external_react.useState(null);
502
+ const [lastRenderedMessageKey, setLastRenderedMessageKey] = __rspack_external_react.useState(null);
503
+ const clientErrorMessage = validityData.errors.length > 1 ? /*#__PURE__*/ jsx("ul", {
504
+ children: validityData.errors.map((message)=>/*#__PURE__*/ jsx("li", {
505
+ children: message
506
+ }, message))
507
+ }) : validityData.error;
508
+ const errorMessage = hasSpecificMatch ? clientErrorMessage : formError || clientErrorMessage;
509
+ let errorKey = validityData.error;
510
+ if (null != formError) errorKey = Array.isArray(formError) ? JSON.stringify(formError) : formError;
511
+ else if (validityData.errors.length > 1) errorKey = JSON.stringify(validityData.errors);
512
+ if (rendered && errorKey !== lastRenderedMessageKey) {
513
+ setLastRenderedMessageKey(errorKey);
514
+ setLastRenderedMessage(errorMessage);
515
+ }
516
+ useOpenChangeComplete({
517
+ open: rendered,
518
+ ref: errorRef,
519
+ onComplete () {
520
+ if (!rendered) setMounted(false);
521
+ }
522
+ });
523
+ const state = {
524
+ ...fieldState,
525
+ transitionStatus
526
+ };
527
+ const element = useRenderElement('div', componentProps, {
528
+ ref: [
529
+ forwardedRef,
530
+ errorRef
531
+ ],
532
+ state,
533
+ props: [
534
+ {
535
+ id,
536
+ children: rendered ? errorMessage : lastRenderedMessage
537
+ },
538
+ elementProps
539
+ ],
540
+ stateAttributesMapping: stateAttributesMapping,
541
+ enabled: mounted
542
+ });
543
+ if (!mounted) return null;
544
+ return element;
545
+ });
546
+ if ("production" !== process.env.NODE_ENV) FieldError_FieldError.displayName = "FieldError";
547
+ const FieldDescription_FieldDescription = /*#__PURE__*/ __rspack_external_react.forwardRef(function(componentProps, forwardedRef) {
548
+ const { render, id: idProp, className, style, ...elementProps } = componentProps;
549
+ const id = useBaseUiId(idProp);
550
+ const fieldRootContext = useFieldRootContext(false);
551
+ const { setMessageIds } = useLabelableContext();
552
+ useIsoLayoutEffect(()=>{
553
+ if (!id) return;
554
+ setMessageIds((v)=>v.concat(id));
555
+ return ()=>{
556
+ setMessageIds((v)=>v.filter((item)=>item !== id));
557
+ };
558
+ }, [
559
+ id,
560
+ setMessageIds
561
+ ]);
562
+ const element = useRenderElement('p', componentProps, {
563
+ ref: forwardedRef,
564
+ state: fieldRootContext.state,
565
+ props: [
566
+ {
567
+ id
568
+ },
569
+ elementProps
570
+ ],
571
+ stateAttributesMapping: fieldValidityMapping
572
+ });
573
+ return element;
574
+ });
575
+ if ("production" !== process.env.NODE_ENV) FieldDescription_FieldDescription.displayName = "FieldDescription";
576
+ export { FieldDescription_FieldDescription, FieldError_FieldError, FieldRoot_FieldRoot, LabelableProvider_LabelableProvider, getCombinedFieldValidityData };