@radix-ui/react-form 0.0.0-20250116175529
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.mts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +483 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +451 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +59 -0
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
|
3
|
+
import { Scope } from '@radix-ui/react-context';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import { Label as Label$1 } from '@radix-ui/react-label';
|
|
6
|
+
import { Primitive } from '@radix-ui/react-primitive';
|
|
7
|
+
|
|
8
|
+
type ScopedProps<P> = P & {
|
|
9
|
+
__scopeForm?: Scope;
|
|
10
|
+
};
|
|
11
|
+
declare const createFormScope: _radix_ui_react_context.CreateScope;
|
|
12
|
+
type PrimitiveFormProps = React.ComponentPropsWithoutRef<typeof Primitive.form>;
|
|
13
|
+
interface FormProps extends PrimitiveFormProps {
|
|
14
|
+
onClearServerErrors?(): void;
|
|
15
|
+
}
|
|
16
|
+
declare const Form: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<HTMLFormElement>>;
|
|
17
|
+
type PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;
|
|
18
|
+
interface FormFieldProps extends PrimitiveDivProps {
|
|
19
|
+
name: string;
|
|
20
|
+
serverInvalid?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare const FormField: React.ForwardRefExoticComponent<FormFieldProps & React.RefAttributes<HTMLDivElement>>;
|
|
23
|
+
type LabelProps = React.ComponentPropsWithoutRef<typeof Label$1>;
|
|
24
|
+
interface FormLabelProps extends LabelProps {
|
|
25
|
+
}
|
|
26
|
+
declare const FormLabel: React.ForwardRefExoticComponent<FormLabelProps & React.RefAttributes<HTMLLabelElement>>;
|
|
27
|
+
type PrimitiveInputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
|
28
|
+
interface FormControlProps extends PrimitiveInputProps {
|
|
29
|
+
}
|
|
30
|
+
declare const FormControl: React.ForwardRefExoticComponent<FormControlProps & React.RefAttributes<HTMLInputElement>>;
|
|
31
|
+
declare const validityMatchers: readonly ["badInput", "patternMismatch", "rangeOverflow", "rangeUnderflow", "stepMismatch", "tooLong", "tooShort", "typeMismatch", "valid", "valueMissing"];
|
|
32
|
+
type ValidityMatcher = (typeof validityMatchers)[number];
|
|
33
|
+
interface FormMessageProps extends Omit<FormMessageImplProps, 'name'> {
|
|
34
|
+
match?: ValidityMatcher | CustomMatcher;
|
|
35
|
+
forceMatch?: boolean;
|
|
36
|
+
name?: string;
|
|
37
|
+
}
|
|
38
|
+
declare const FormMessage: React.ForwardRefExoticComponent<FormMessageProps & React.RefAttributes<HTMLSpanElement>>;
|
|
39
|
+
type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
40
|
+
interface FormMessageImplProps extends PrimitiveSpanProps {
|
|
41
|
+
name: string;
|
|
42
|
+
}
|
|
43
|
+
interface FormValidityStateProps {
|
|
44
|
+
children(validity: ValidityState | undefined): React.ReactNode;
|
|
45
|
+
name?: string;
|
|
46
|
+
}
|
|
47
|
+
declare const FormValidityState: {
|
|
48
|
+
(props: ScopedProps<FormValidityStateProps>): react_jsx_runtime.JSX.Element;
|
|
49
|
+
displayName: string;
|
|
50
|
+
};
|
|
51
|
+
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
52
|
+
interface FormSubmitProps extends PrimitiveButtonProps {
|
|
53
|
+
}
|
|
54
|
+
declare const FormSubmit: React.ForwardRefExoticComponent<FormSubmitProps & React.RefAttributes<HTMLButtonElement>>;
|
|
55
|
+
type SyncCustomMatcher = (value: string, formData: FormData) => boolean;
|
|
56
|
+
type AsyncCustomMatcher = (value: string, formData: FormData) => Promise<boolean>;
|
|
57
|
+
type CustomMatcher = SyncCustomMatcher | AsyncCustomMatcher;
|
|
58
|
+
declare const Root: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<HTMLFormElement>>;
|
|
59
|
+
declare const Field: React.ForwardRefExoticComponent<FormFieldProps & React.RefAttributes<HTMLDivElement>>;
|
|
60
|
+
declare const Label: React.ForwardRefExoticComponent<FormLabelProps & React.RefAttributes<HTMLLabelElement>>;
|
|
61
|
+
declare const Control: React.ForwardRefExoticComponent<FormControlProps & React.RefAttributes<HTMLInputElement>>;
|
|
62
|
+
declare const Message: React.ForwardRefExoticComponent<FormMessageProps & React.RefAttributes<HTMLSpanElement>>;
|
|
63
|
+
declare const ValidityState: {
|
|
64
|
+
(props: ScopedProps<FormValidityStateProps>): react_jsx_runtime.JSX.Element;
|
|
65
|
+
displayName: string;
|
|
66
|
+
};
|
|
67
|
+
declare const Submit: React.ForwardRefExoticComponent<FormSubmitProps & React.RefAttributes<HTMLButtonElement>>;
|
|
68
|
+
|
|
69
|
+
export { Control, Field, Form, FormControl, type FormControlProps, FormField, type FormFieldProps, FormLabel, type FormLabelProps, FormMessage, type FormMessageProps, type FormProps, FormSubmit, type FormSubmitProps, FormValidityState, type FormValidityStateProps, Label, Message, Root, Submit, ValidityState, createFormScope };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as _radix_ui_react_context from '@radix-ui/react-context';
|
|
3
|
+
import { Scope } from '@radix-ui/react-context';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import { Label as Label$1 } from '@radix-ui/react-label';
|
|
6
|
+
import { Primitive } from '@radix-ui/react-primitive';
|
|
7
|
+
|
|
8
|
+
type ScopedProps<P> = P & {
|
|
9
|
+
__scopeForm?: Scope;
|
|
10
|
+
};
|
|
11
|
+
declare const createFormScope: _radix_ui_react_context.CreateScope;
|
|
12
|
+
type PrimitiveFormProps = React.ComponentPropsWithoutRef<typeof Primitive.form>;
|
|
13
|
+
interface FormProps extends PrimitiveFormProps {
|
|
14
|
+
onClearServerErrors?(): void;
|
|
15
|
+
}
|
|
16
|
+
declare const Form: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<HTMLFormElement>>;
|
|
17
|
+
type PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;
|
|
18
|
+
interface FormFieldProps extends PrimitiveDivProps {
|
|
19
|
+
name: string;
|
|
20
|
+
serverInvalid?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare const FormField: React.ForwardRefExoticComponent<FormFieldProps & React.RefAttributes<HTMLDivElement>>;
|
|
23
|
+
type LabelProps = React.ComponentPropsWithoutRef<typeof Label$1>;
|
|
24
|
+
interface FormLabelProps extends LabelProps {
|
|
25
|
+
}
|
|
26
|
+
declare const FormLabel: React.ForwardRefExoticComponent<FormLabelProps & React.RefAttributes<HTMLLabelElement>>;
|
|
27
|
+
type PrimitiveInputProps = React.ComponentPropsWithoutRef<typeof Primitive.input>;
|
|
28
|
+
interface FormControlProps extends PrimitiveInputProps {
|
|
29
|
+
}
|
|
30
|
+
declare const FormControl: React.ForwardRefExoticComponent<FormControlProps & React.RefAttributes<HTMLInputElement>>;
|
|
31
|
+
declare const validityMatchers: readonly ["badInput", "patternMismatch", "rangeOverflow", "rangeUnderflow", "stepMismatch", "tooLong", "tooShort", "typeMismatch", "valid", "valueMissing"];
|
|
32
|
+
type ValidityMatcher = (typeof validityMatchers)[number];
|
|
33
|
+
interface FormMessageProps extends Omit<FormMessageImplProps, 'name'> {
|
|
34
|
+
match?: ValidityMatcher | CustomMatcher;
|
|
35
|
+
forceMatch?: boolean;
|
|
36
|
+
name?: string;
|
|
37
|
+
}
|
|
38
|
+
declare const FormMessage: React.ForwardRefExoticComponent<FormMessageProps & React.RefAttributes<HTMLSpanElement>>;
|
|
39
|
+
type PrimitiveSpanProps = React.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
40
|
+
interface FormMessageImplProps extends PrimitiveSpanProps {
|
|
41
|
+
name: string;
|
|
42
|
+
}
|
|
43
|
+
interface FormValidityStateProps {
|
|
44
|
+
children(validity: ValidityState | undefined): React.ReactNode;
|
|
45
|
+
name?: string;
|
|
46
|
+
}
|
|
47
|
+
declare const FormValidityState: {
|
|
48
|
+
(props: ScopedProps<FormValidityStateProps>): react_jsx_runtime.JSX.Element;
|
|
49
|
+
displayName: string;
|
|
50
|
+
};
|
|
51
|
+
type PrimitiveButtonProps = React.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
52
|
+
interface FormSubmitProps extends PrimitiveButtonProps {
|
|
53
|
+
}
|
|
54
|
+
declare const FormSubmit: React.ForwardRefExoticComponent<FormSubmitProps & React.RefAttributes<HTMLButtonElement>>;
|
|
55
|
+
type SyncCustomMatcher = (value: string, formData: FormData) => boolean;
|
|
56
|
+
type AsyncCustomMatcher = (value: string, formData: FormData) => Promise<boolean>;
|
|
57
|
+
type CustomMatcher = SyncCustomMatcher | AsyncCustomMatcher;
|
|
58
|
+
declare const Root: React.ForwardRefExoticComponent<FormProps & React.RefAttributes<HTMLFormElement>>;
|
|
59
|
+
declare const Field: React.ForwardRefExoticComponent<FormFieldProps & React.RefAttributes<HTMLDivElement>>;
|
|
60
|
+
declare const Label: React.ForwardRefExoticComponent<FormLabelProps & React.RefAttributes<HTMLLabelElement>>;
|
|
61
|
+
declare const Control: React.ForwardRefExoticComponent<FormControlProps & React.RefAttributes<HTMLInputElement>>;
|
|
62
|
+
declare const Message: React.ForwardRefExoticComponent<FormMessageProps & React.RefAttributes<HTMLSpanElement>>;
|
|
63
|
+
declare const ValidityState: {
|
|
64
|
+
(props: ScopedProps<FormValidityStateProps>): react_jsx_runtime.JSX.Element;
|
|
65
|
+
displayName: string;
|
|
66
|
+
};
|
|
67
|
+
declare const Submit: React.ForwardRefExoticComponent<FormSubmitProps & React.RefAttributes<HTMLButtonElement>>;
|
|
68
|
+
|
|
69
|
+
export { Control, Field, Form, FormControl, type FormControlProps, FormField, type FormFieldProps, FormLabel, type FormLabelProps, FormMessage, type FormMessageProps, type FormProps, FormSubmit, type FormSubmitProps, FormValidityState, type FormValidityStateProps, Label, Message, Root, Submit, ValidityState, createFormScope };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// packages/react/form/src/index.ts
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
Control: () => Control,
|
|
35
|
+
Field: () => Field,
|
|
36
|
+
Form: () => Form,
|
|
37
|
+
FormControl: () => FormControl,
|
|
38
|
+
FormField: () => FormField,
|
|
39
|
+
FormLabel: () => FormLabel,
|
|
40
|
+
FormMessage: () => FormMessage,
|
|
41
|
+
FormSubmit: () => FormSubmit,
|
|
42
|
+
FormValidityState: () => FormValidityState,
|
|
43
|
+
Label: () => Label,
|
|
44
|
+
Message: () => Message,
|
|
45
|
+
Root: () => Root,
|
|
46
|
+
Submit: () => Submit,
|
|
47
|
+
ValidityState: () => ValidityState,
|
|
48
|
+
createFormScope: () => createFormScope
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(src_exports);
|
|
51
|
+
|
|
52
|
+
// packages/react/form/src/Form.tsx
|
|
53
|
+
var React = __toESM(require("react"));
|
|
54
|
+
var import_primitive = require("@radix-ui/primitive");
|
|
55
|
+
var import_react_compose_refs = require("@radix-ui/react-compose-refs");
|
|
56
|
+
var import_react_context = require("@radix-ui/react-context");
|
|
57
|
+
var import_react_id = require("@radix-ui/react-id");
|
|
58
|
+
var import_react_label = require("@radix-ui/react-label");
|
|
59
|
+
var import_react_primitive = require("@radix-ui/react-primitive");
|
|
60
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
61
|
+
var [createFormContext, createFormScope] = (0, import_react_context.createContextScope)("Form");
|
|
62
|
+
var FORM_NAME = "Form";
|
|
63
|
+
var [ValidationProvider, useValidationContext] = createFormContext(FORM_NAME);
|
|
64
|
+
var [AriaDescriptionProvider, useAriaDescriptionContext] = createFormContext(FORM_NAME);
|
|
65
|
+
var Form = React.forwardRef(
|
|
66
|
+
(props, forwardedRef) => {
|
|
67
|
+
const { __scopeForm, onClearServerErrors = () => {
|
|
68
|
+
}, ...rootProps } = props;
|
|
69
|
+
const formRef = React.useRef(null);
|
|
70
|
+
const composedFormRef = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, formRef);
|
|
71
|
+
const [validityMap, setValidityMap] = React.useState({});
|
|
72
|
+
const getFieldValidity = React.useCallback(
|
|
73
|
+
(fieldName) => validityMap[fieldName],
|
|
74
|
+
[validityMap]
|
|
75
|
+
);
|
|
76
|
+
const handleFieldValidityChange = React.useCallback(
|
|
77
|
+
(fieldName, validity) => setValidityMap((prevValidityMap) => ({
|
|
78
|
+
...prevValidityMap,
|
|
79
|
+
[fieldName]: { ...prevValidityMap[fieldName] ?? {}, ...validity }
|
|
80
|
+
})),
|
|
81
|
+
[]
|
|
82
|
+
);
|
|
83
|
+
const handleFieldValiditionClear = React.useCallback((fieldName) => {
|
|
84
|
+
setValidityMap((prevValidityMap) => ({ ...prevValidityMap, [fieldName]: void 0 }));
|
|
85
|
+
setCustomErrorsMap((prevCustomErrorsMap) => ({ ...prevCustomErrorsMap, [fieldName]: {} }));
|
|
86
|
+
}, []);
|
|
87
|
+
const [customMatcherEntriesMap, setCustomMatcherEntriesMap] = React.useState({});
|
|
88
|
+
const getFieldCustomMatcherEntries = React.useCallback(
|
|
89
|
+
(fieldName) => customMatcherEntriesMap[fieldName] ?? [],
|
|
90
|
+
[customMatcherEntriesMap]
|
|
91
|
+
);
|
|
92
|
+
const handleFieldCustomMatcherAdd = React.useCallback((fieldName, matcherEntry) => {
|
|
93
|
+
setCustomMatcherEntriesMap((prevCustomMatcherEntriesMap) => ({
|
|
94
|
+
...prevCustomMatcherEntriesMap,
|
|
95
|
+
[fieldName]: [...prevCustomMatcherEntriesMap[fieldName] ?? [], matcherEntry]
|
|
96
|
+
}));
|
|
97
|
+
}, []);
|
|
98
|
+
const handleFieldCustomMatcherRemove = React.useCallback((fieldName, matcherEntryId) => {
|
|
99
|
+
setCustomMatcherEntriesMap((prevCustomMatcherEntriesMap) => ({
|
|
100
|
+
...prevCustomMatcherEntriesMap,
|
|
101
|
+
[fieldName]: (prevCustomMatcherEntriesMap[fieldName] ?? []).filter(
|
|
102
|
+
(matcherEntry) => matcherEntry.id !== matcherEntryId
|
|
103
|
+
)
|
|
104
|
+
}));
|
|
105
|
+
}, []);
|
|
106
|
+
const [customErrorsMap, setCustomErrorsMap] = React.useState({});
|
|
107
|
+
const getFieldCustomErrors = React.useCallback(
|
|
108
|
+
(fieldName) => customErrorsMap[fieldName] ?? {},
|
|
109
|
+
[customErrorsMap]
|
|
110
|
+
);
|
|
111
|
+
const handleFieldCustomErrorsChange = React.useCallback((fieldName, customErrors) => {
|
|
112
|
+
setCustomErrorsMap((prevCustomErrorsMap) => ({
|
|
113
|
+
...prevCustomErrorsMap,
|
|
114
|
+
[fieldName]: { ...prevCustomErrorsMap[fieldName] ?? {}, ...customErrors }
|
|
115
|
+
}));
|
|
116
|
+
}, []);
|
|
117
|
+
const [messageIdsMap, setMessageIdsMap] = React.useState({});
|
|
118
|
+
const handleFieldMessageIdAdd = React.useCallback((fieldName, id) => {
|
|
119
|
+
setMessageIdsMap((prevMessageIdsMap) => {
|
|
120
|
+
const fieldDescriptionIds = new Set(prevMessageIdsMap[fieldName]).add(id);
|
|
121
|
+
return { ...prevMessageIdsMap, [fieldName]: fieldDescriptionIds };
|
|
122
|
+
});
|
|
123
|
+
}, []);
|
|
124
|
+
const handleFieldMessageIdRemove = React.useCallback((fieldName, id) => {
|
|
125
|
+
setMessageIdsMap((prevMessageIdsMap) => {
|
|
126
|
+
const fieldDescriptionIds = new Set(prevMessageIdsMap[fieldName]);
|
|
127
|
+
fieldDescriptionIds.delete(id);
|
|
128
|
+
return { ...prevMessageIdsMap, [fieldName]: fieldDescriptionIds };
|
|
129
|
+
});
|
|
130
|
+
}, []);
|
|
131
|
+
const getFieldDescription = React.useCallback(
|
|
132
|
+
(fieldName) => Array.from(messageIdsMap[fieldName] ?? []).join(" ") || void 0,
|
|
133
|
+
[messageIdsMap]
|
|
134
|
+
);
|
|
135
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
136
|
+
ValidationProvider,
|
|
137
|
+
{
|
|
138
|
+
scope: __scopeForm,
|
|
139
|
+
getFieldValidity,
|
|
140
|
+
onFieldValidityChange: handleFieldValidityChange,
|
|
141
|
+
getFieldCustomMatcherEntries,
|
|
142
|
+
onFieldCustomMatcherEntryAdd: handleFieldCustomMatcherAdd,
|
|
143
|
+
onFieldCustomMatcherEntryRemove: handleFieldCustomMatcherRemove,
|
|
144
|
+
getFieldCustomErrors,
|
|
145
|
+
onFieldCustomErrorsChange: handleFieldCustomErrorsChange,
|
|
146
|
+
onFieldValiditionClear: handleFieldValiditionClear,
|
|
147
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
148
|
+
AriaDescriptionProvider,
|
|
149
|
+
{
|
|
150
|
+
scope: __scopeForm,
|
|
151
|
+
onFieldMessageIdAdd: handleFieldMessageIdAdd,
|
|
152
|
+
onFieldMessageIdRemove: handleFieldMessageIdRemove,
|
|
153
|
+
getFieldDescription,
|
|
154
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
155
|
+
import_react_primitive.Primitive.form,
|
|
156
|
+
{
|
|
157
|
+
...rootProps,
|
|
158
|
+
ref: composedFormRef,
|
|
159
|
+
onInvalid: (0, import_primitive.composeEventHandlers)(props.onInvalid, (event) => {
|
|
160
|
+
const firstInvalidControl = getFirstInvalidControl(event.currentTarget);
|
|
161
|
+
if (firstInvalidControl === event.target) firstInvalidControl.focus();
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
}),
|
|
164
|
+
onSubmit: (0, import_primitive.composeEventHandlers)(props.onSubmit, onClearServerErrors, {
|
|
165
|
+
checkForDefaultPrevented: false
|
|
166
|
+
}),
|
|
167
|
+
onReset: (0, import_primitive.composeEventHandlers)(props.onReset, onClearServerErrors)
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
Form.displayName = FORM_NAME;
|
|
177
|
+
var FIELD_NAME = "FormField";
|
|
178
|
+
var [FormFieldProvider, useFormFieldContext] = createFormContext(FIELD_NAME);
|
|
179
|
+
var FormField = React.forwardRef(
|
|
180
|
+
(props, forwardedRef) => {
|
|
181
|
+
const { __scopeForm, name, serverInvalid = false, ...fieldProps } = props;
|
|
182
|
+
const validationContext = useValidationContext(FIELD_NAME, __scopeForm);
|
|
183
|
+
const validity = validationContext.getFieldValidity(name);
|
|
184
|
+
const id = (0, import_react_id.useId)();
|
|
185
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormFieldProvider, { scope: __scopeForm, id, name, serverInvalid, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
186
|
+
import_react_primitive.Primitive.div,
|
|
187
|
+
{
|
|
188
|
+
"data-valid": getValidAttribute(validity, serverInvalid),
|
|
189
|
+
"data-invalid": getInvalidAttribute(validity, serverInvalid),
|
|
190
|
+
...fieldProps,
|
|
191
|
+
ref: forwardedRef
|
|
192
|
+
}
|
|
193
|
+
) });
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
FormField.displayName = FIELD_NAME;
|
|
197
|
+
var LABEL_NAME = "FormLabel";
|
|
198
|
+
var FormLabel = React.forwardRef(
|
|
199
|
+
(props, forwardedRef) => {
|
|
200
|
+
const { __scopeForm, ...labelProps } = props;
|
|
201
|
+
const validationContext = useValidationContext(LABEL_NAME, __scopeForm);
|
|
202
|
+
const fieldContext = useFormFieldContext(LABEL_NAME, __scopeForm);
|
|
203
|
+
const htmlFor = labelProps.htmlFor || fieldContext.id;
|
|
204
|
+
const validity = validationContext.getFieldValidity(fieldContext.name);
|
|
205
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
206
|
+
import_react_label.Label,
|
|
207
|
+
{
|
|
208
|
+
"data-valid": getValidAttribute(validity, fieldContext.serverInvalid),
|
|
209
|
+
"data-invalid": getInvalidAttribute(validity, fieldContext.serverInvalid),
|
|
210
|
+
...labelProps,
|
|
211
|
+
ref: forwardedRef,
|
|
212
|
+
htmlFor
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
FormLabel.displayName = LABEL_NAME;
|
|
218
|
+
var CONTROL_NAME = "FormControl";
|
|
219
|
+
var FormControl = React.forwardRef(
|
|
220
|
+
(props, forwardedRef) => {
|
|
221
|
+
const { __scopeForm, ...controlProps } = props;
|
|
222
|
+
const validationContext = useValidationContext(CONTROL_NAME, __scopeForm);
|
|
223
|
+
const fieldContext = useFormFieldContext(CONTROL_NAME, __scopeForm);
|
|
224
|
+
const ariaDescriptionContext = useAriaDescriptionContext(CONTROL_NAME, __scopeForm);
|
|
225
|
+
const ref = React.useRef(null);
|
|
226
|
+
const composedRef = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, ref);
|
|
227
|
+
const name = controlProps.name || fieldContext.name;
|
|
228
|
+
const id = controlProps.id || fieldContext.id;
|
|
229
|
+
const customMatcherEntries = validationContext.getFieldCustomMatcherEntries(name);
|
|
230
|
+
const { onFieldValidityChange, onFieldCustomErrorsChange, onFieldValiditionClear } = validationContext;
|
|
231
|
+
const updateControlValidity = React.useCallback(
|
|
232
|
+
async (control) => {
|
|
233
|
+
if (hasBuiltInError(control.validity)) {
|
|
234
|
+
const controlValidity2 = validityStateToObject(control.validity);
|
|
235
|
+
onFieldValidityChange(name, controlValidity2);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const formData = control.form ? new FormData(control.form) : new FormData();
|
|
239
|
+
const matcherArgs = [control.value, formData];
|
|
240
|
+
const syncCustomMatcherEntries = [];
|
|
241
|
+
const ayncCustomMatcherEntries = [];
|
|
242
|
+
customMatcherEntries.forEach((customMatcherEntry) => {
|
|
243
|
+
if (isAsyncCustomMatcherEntry(customMatcherEntry, matcherArgs)) {
|
|
244
|
+
ayncCustomMatcherEntries.push(customMatcherEntry);
|
|
245
|
+
} else if (isSyncCustomMatcherEntry(customMatcherEntry)) {
|
|
246
|
+
syncCustomMatcherEntries.push(customMatcherEntry);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
const syncCustomErrors = syncCustomMatcherEntries.map(({ id: id2, match }) => {
|
|
250
|
+
return [id2, match(...matcherArgs)];
|
|
251
|
+
});
|
|
252
|
+
const syncCustomErrorsById = Object.fromEntries(syncCustomErrors);
|
|
253
|
+
const hasSyncCustomErrors = Object.values(syncCustomErrorsById).some(Boolean);
|
|
254
|
+
const hasCustomError = hasSyncCustomErrors;
|
|
255
|
+
control.setCustomValidity(hasCustomError ? DEFAULT_INVALID_MESSAGE : "");
|
|
256
|
+
const controlValidity = validityStateToObject(control.validity);
|
|
257
|
+
onFieldValidityChange(name, controlValidity);
|
|
258
|
+
onFieldCustomErrorsChange(name, syncCustomErrorsById);
|
|
259
|
+
if (!hasSyncCustomErrors && ayncCustomMatcherEntries.length > 0) {
|
|
260
|
+
const promisedCustomErrors = ayncCustomMatcherEntries.map(
|
|
261
|
+
({ id: id2, match }) => match(...matcherArgs).then((matches) => [id2, matches])
|
|
262
|
+
);
|
|
263
|
+
const asyncCustomErrors = await Promise.all(promisedCustomErrors);
|
|
264
|
+
const asyncCustomErrorsById = Object.fromEntries(asyncCustomErrors);
|
|
265
|
+
const hasAsyncCustomErrors = Object.values(asyncCustomErrorsById).some(Boolean);
|
|
266
|
+
const hasCustomError2 = hasAsyncCustomErrors;
|
|
267
|
+
control.setCustomValidity(hasCustomError2 ? DEFAULT_INVALID_MESSAGE : "");
|
|
268
|
+
const controlValidity2 = validityStateToObject(control.validity);
|
|
269
|
+
onFieldValidityChange(name, controlValidity2);
|
|
270
|
+
onFieldCustomErrorsChange(name, asyncCustomErrorsById);
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
[customMatcherEntries, name, onFieldCustomErrorsChange, onFieldValidityChange]
|
|
274
|
+
);
|
|
275
|
+
React.useEffect(() => {
|
|
276
|
+
const control = ref.current;
|
|
277
|
+
if (control) {
|
|
278
|
+
const handleChange = () => updateControlValidity(control);
|
|
279
|
+
control.addEventListener("change", handleChange);
|
|
280
|
+
return () => control.removeEventListener("change", handleChange);
|
|
281
|
+
}
|
|
282
|
+
}, [updateControlValidity]);
|
|
283
|
+
const resetControlValidity = React.useCallback(() => {
|
|
284
|
+
const control = ref.current;
|
|
285
|
+
if (control) {
|
|
286
|
+
control.setCustomValidity("");
|
|
287
|
+
onFieldValiditionClear(name);
|
|
288
|
+
}
|
|
289
|
+
}, [name, onFieldValiditionClear]);
|
|
290
|
+
React.useEffect(() => {
|
|
291
|
+
const form = ref.current?.form;
|
|
292
|
+
if (form) {
|
|
293
|
+
form.addEventListener("reset", resetControlValidity);
|
|
294
|
+
return () => form.removeEventListener("reset", resetControlValidity);
|
|
295
|
+
}
|
|
296
|
+
}, [resetControlValidity]);
|
|
297
|
+
React.useEffect(() => {
|
|
298
|
+
const control = ref.current;
|
|
299
|
+
const form = control?.closest("form");
|
|
300
|
+
if (form && fieldContext.serverInvalid) {
|
|
301
|
+
const firstInvalidControl = getFirstInvalidControl(form);
|
|
302
|
+
if (firstInvalidControl === control) firstInvalidControl.focus();
|
|
303
|
+
}
|
|
304
|
+
}, [fieldContext.serverInvalid]);
|
|
305
|
+
const validity = validationContext.getFieldValidity(name);
|
|
306
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
307
|
+
import_react_primitive.Primitive.input,
|
|
308
|
+
{
|
|
309
|
+
"data-valid": getValidAttribute(validity, fieldContext.serverInvalid),
|
|
310
|
+
"data-invalid": getInvalidAttribute(validity, fieldContext.serverInvalid),
|
|
311
|
+
"aria-invalid": fieldContext.serverInvalid ? true : void 0,
|
|
312
|
+
"aria-describedby": ariaDescriptionContext.getFieldDescription(name),
|
|
313
|
+
title: "",
|
|
314
|
+
...controlProps,
|
|
315
|
+
ref: composedRef,
|
|
316
|
+
id,
|
|
317
|
+
name,
|
|
318
|
+
onInvalid: (0, import_primitive.composeEventHandlers)(props.onInvalid, (event) => {
|
|
319
|
+
const control = event.currentTarget;
|
|
320
|
+
updateControlValidity(control);
|
|
321
|
+
}),
|
|
322
|
+
onChange: (0, import_primitive.composeEventHandlers)(props.onChange, (event) => {
|
|
323
|
+
resetControlValidity();
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
FormControl.displayName = CONTROL_NAME;
|
|
330
|
+
var DEFAULT_INVALID_MESSAGE = "This value is not valid";
|
|
331
|
+
var DEFAULT_BUILT_IN_MESSAGES = {
|
|
332
|
+
badInput: DEFAULT_INVALID_MESSAGE,
|
|
333
|
+
patternMismatch: "This value does not match the required pattern",
|
|
334
|
+
rangeOverflow: "This value is too large",
|
|
335
|
+
rangeUnderflow: "This value is too small",
|
|
336
|
+
stepMismatch: "This value does not match the required step",
|
|
337
|
+
tooLong: "This value is too long",
|
|
338
|
+
tooShort: "This value is too short",
|
|
339
|
+
typeMismatch: "This value does not match the required type",
|
|
340
|
+
valid: void 0,
|
|
341
|
+
valueMissing: "This value is missing"
|
|
342
|
+
};
|
|
343
|
+
var MESSAGE_NAME = "FormMessage";
|
|
344
|
+
var FormMessage = React.forwardRef(
|
|
345
|
+
(props, forwardedRef) => {
|
|
346
|
+
const { match, name: nameProp, ...messageProps } = props;
|
|
347
|
+
const fieldContext = useFormFieldContext(MESSAGE_NAME, props.__scopeForm);
|
|
348
|
+
const name = nameProp ?? fieldContext.name;
|
|
349
|
+
if (match === void 0) {
|
|
350
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormMessageImpl, { ...messageProps, ref: forwardedRef, name, children: props.children || DEFAULT_INVALID_MESSAGE });
|
|
351
|
+
} else if (typeof match === "function") {
|
|
352
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormCustomMessage, { match, ...messageProps, ref: forwardedRef, name });
|
|
353
|
+
} else {
|
|
354
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormBuiltInMessage, { match, ...messageProps, ref: forwardedRef, name });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
);
|
|
358
|
+
FormMessage.displayName = MESSAGE_NAME;
|
|
359
|
+
var FormBuiltInMessage = React.forwardRef(
|
|
360
|
+
(props, forwardedRef) => {
|
|
361
|
+
const { match, forceMatch = false, name, children, ...messageProps } = props;
|
|
362
|
+
const validationContext = useValidationContext(MESSAGE_NAME, messageProps.__scopeForm);
|
|
363
|
+
const validity = validationContext.getFieldValidity(name);
|
|
364
|
+
const matches = forceMatch || validity?.[match];
|
|
365
|
+
if (matches) {
|
|
366
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormMessageImpl, { ref: forwardedRef, ...messageProps, name, children: children ?? DEFAULT_BUILT_IN_MESSAGES[match] });
|
|
367
|
+
}
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
);
|
|
371
|
+
var FormCustomMessage = React.forwardRef(
|
|
372
|
+
(props, forwardedRef) => {
|
|
373
|
+
const { match, forceMatch = false, name, id: idProp, children, ...messageProps } = props;
|
|
374
|
+
const validationContext = useValidationContext(MESSAGE_NAME, messageProps.__scopeForm);
|
|
375
|
+
const ref = React.useRef(null);
|
|
376
|
+
const composedRef = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, ref);
|
|
377
|
+
const _id = (0, import_react_id.useId)();
|
|
378
|
+
const id = idProp ?? _id;
|
|
379
|
+
const customMatcherEntry = React.useMemo(() => ({ id, match }), [id, match]);
|
|
380
|
+
const { onFieldCustomMatcherEntryAdd, onFieldCustomMatcherEntryRemove } = validationContext;
|
|
381
|
+
React.useEffect(() => {
|
|
382
|
+
onFieldCustomMatcherEntryAdd(name, customMatcherEntry);
|
|
383
|
+
return () => onFieldCustomMatcherEntryRemove(name, customMatcherEntry.id);
|
|
384
|
+
}, [customMatcherEntry, name, onFieldCustomMatcherEntryAdd, onFieldCustomMatcherEntryRemove]);
|
|
385
|
+
const validity = validationContext.getFieldValidity(name);
|
|
386
|
+
const customErrors = validationContext.getFieldCustomErrors(name);
|
|
387
|
+
const hasMatchingCustomError = customErrors[id];
|
|
388
|
+
const matches = forceMatch || validity && !hasBuiltInError(validity) && hasMatchingCustomError;
|
|
389
|
+
if (matches) {
|
|
390
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormMessageImpl, { id, ref: composedRef, ...messageProps, name, children: children ?? DEFAULT_INVALID_MESSAGE });
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
var FormMessageImpl = React.forwardRef(
|
|
396
|
+
(props, forwardedRef) => {
|
|
397
|
+
const { __scopeForm, id: idProp, name, ...messageProps } = props;
|
|
398
|
+
const ariaDescriptionContext = useAriaDescriptionContext(MESSAGE_NAME, __scopeForm);
|
|
399
|
+
const _id = (0, import_react_id.useId)();
|
|
400
|
+
const id = idProp ?? _id;
|
|
401
|
+
const { onFieldMessageIdAdd, onFieldMessageIdRemove } = ariaDescriptionContext;
|
|
402
|
+
React.useEffect(() => {
|
|
403
|
+
onFieldMessageIdAdd(name, id);
|
|
404
|
+
return () => onFieldMessageIdRemove(name, id);
|
|
405
|
+
}, [name, id, onFieldMessageIdAdd, onFieldMessageIdRemove]);
|
|
406
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.span, { id, ...messageProps, ref: forwardedRef });
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
var VALIDITY_STATE_NAME = "FormValidityState";
|
|
410
|
+
var FormValidityState = (props) => {
|
|
411
|
+
const { __scopeForm, name: nameProp, children } = props;
|
|
412
|
+
const validationContext = useValidationContext(VALIDITY_STATE_NAME, __scopeForm);
|
|
413
|
+
const fieldContext = useFormFieldContext(VALIDITY_STATE_NAME, __scopeForm);
|
|
414
|
+
const name = nameProp ?? fieldContext.name;
|
|
415
|
+
const validity = validationContext.getFieldValidity(name);
|
|
416
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: children(validity) });
|
|
417
|
+
};
|
|
418
|
+
FormValidityState.displayName = VALIDITY_STATE_NAME;
|
|
419
|
+
var SUBMIT_NAME = "FormSubmit";
|
|
420
|
+
var FormSubmit = React.forwardRef(
|
|
421
|
+
(props, forwardedRef) => {
|
|
422
|
+
const { __scopeForm, ...submitProps } = props;
|
|
423
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.button, { type: "submit", ...submitProps, ref: forwardedRef });
|
|
424
|
+
}
|
|
425
|
+
);
|
|
426
|
+
FormSubmit.displayName = SUBMIT_NAME;
|
|
427
|
+
function validityStateToObject(validity) {
|
|
428
|
+
const object = {};
|
|
429
|
+
for (const key in validity) {
|
|
430
|
+
object[key] = validity[key];
|
|
431
|
+
}
|
|
432
|
+
return object;
|
|
433
|
+
}
|
|
434
|
+
function isHTMLElement(element) {
|
|
435
|
+
return element instanceof HTMLElement;
|
|
436
|
+
}
|
|
437
|
+
function isFormControl(element) {
|
|
438
|
+
return "validity" in element;
|
|
439
|
+
}
|
|
440
|
+
function isInvalid(control) {
|
|
441
|
+
return isFormControl(control) && (control.validity.valid === false || control.getAttribute("aria-invalid") === "true");
|
|
442
|
+
}
|
|
443
|
+
function getFirstInvalidControl(form) {
|
|
444
|
+
const elements = form.elements;
|
|
445
|
+
const [firstInvalidControl] = Array.from(elements).filter(isHTMLElement).filter(isInvalid);
|
|
446
|
+
return firstInvalidControl;
|
|
447
|
+
}
|
|
448
|
+
function isAsyncCustomMatcherEntry(entry, args) {
|
|
449
|
+
return entry.match.constructor.name === "AsyncFunction" || returnsPromise(entry.match, args);
|
|
450
|
+
}
|
|
451
|
+
function isSyncCustomMatcherEntry(entry) {
|
|
452
|
+
return entry.match.constructor.name === "Function";
|
|
453
|
+
}
|
|
454
|
+
function returnsPromise(func, args) {
|
|
455
|
+
return func(...args) instanceof Promise;
|
|
456
|
+
}
|
|
457
|
+
function hasBuiltInError(validity) {
|
|
458
|
+
let error = false;
|
|
459
|
+
for (const validityKey in validity) {
|
|
460
|
+
const key = validityKey;
|
|
461
|
+
if (key !== "valid" && key !== "customError" && validity[key]) {
|
|
462
|
+
error = true;
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return error;
|
|
467
|
+
}
|
|
468
|
+
function getValidAttribute(validity, serverInvalid) {
|
|
469
|
+
if (validity?.valid === true && !serverInvalid) return true;
|
|
470
|
+
return void 0;
|
|
471
|
+
}
|
|
472
|
+
function getInvalidAttribute(validity, serverInvalid) {
|
|
473
|
+
if (validity?.valid === false || serverInvalid) return true;
|
|
474
|
+
return void 0;
|
|
475
|
+
}
|
|
476
|
+
var Root = Form;
|
|
477
|
+
var Field = FormField;
|
|
478
|
+
var Label = FormLabel;
|
|
479
|
+
var Control = FormControl;
|
|
480
|
+
var Message = FormMessage;
|
|
481
|
+
var ValidityState = FormValidityState;
|
|
482
|
+
var Submit = FormSubmit;
|
|
483
|
+
//# sourceMappingURL=index.js.map
|