@datum-cloud/datum-ui 0.5.0 → 0.6.0-alpha.b817c77
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 +75 -40
- package/dist/adapter-context-B7L2ucTr.mjs +25 -0
- package/dist/components/features/form/adapter-context.d.ts +17 -0
- package/dist/components/features/form/adapter-context.d.ts.map +1 -0
- package/dist/components/features/form/adapter-types.d.ts +100 -0
- package/dist/components/features/form/adapter-types.d.ts.map +1 -0
- package/dist/components/features/form/adapters/conform/conform-adapter.d.ts +9 -0
- package/dist/components/features/form/adapters/conform/conform-adapter.d.ts.map +1 -0
- package/dist/components/features/form/adapters/conform/conform-provider.d.ts +22 -0
- package/dist/components/features/form/adapters/conform/conform-provider.d.ts.map +1 -0
- package/dist/components/features/form/adapters/conform/index.d.ts +3 -0
- package/dist/components/features/form/adapters/conform/index.d.ts.map +1 -0
- package/dist/components/features/form/adapters/rhf/index.d.ts +3 -0
- package/dist/components/features/form/adapters/rhf/index.d.ts.map +1 -0
- package/dist/components/features/form/adapters/rhf/rhf-adapter.d.ts +10 -0
- package/dist/components/features/form/adapters/rhf/rhf-adapter.d.ts.map +1 -0
- package/dist/components/features/form/adapters/rhf/rhf-provider.d.ts +22 -0
- package/dist/components/features/form/adapters/rhf/rhf-provider.d.ts.map +1 -0
- package/dist/components/features/form/components/form-autocomplete.d.ts.map +1 -1
- package/dist/components/features/form/components/form-checkbox.d.ts.map +1 -1
- package/dist/components/features/form/components/form-copy-box.d.ts.map +1 -1
- package/dist/components/features/form/components/form-field-array.d.ts +5 -17
- package/dist/components/features/form/components/form-field-array.d.ts.map +1 -1
- package/dist/components/features/form/components/form-field.d.ts +7 -21
- package/dist/components/features/form/components/form-field.d.ts.map +1 -1
- package/dist/components/features/form/components/form-input-group.d.ts +4 -4
- package/dist/components/features/form/components/form-input-group.d.ts.map +1 -1
- package/dist/components/features/form/components/form-input.d.ts.map +1 -1
- package/dist/components/features/form/components/form-radio-group.d.ts.map +1 -1
- package/dist/components/features/form/components/form-root.d.ts +5 -25
- package/dist/components/features/form/components/form-root.d.ts.map +1 -1
- package/dist/components/features/form/components/form-select.d.ts.map +1 -1
- package/dist/components/features/form/components/form-switch.d.ts.map +1 -1
- package/dist/components/features/form/components/form-textarea.d.ts.map +1 -1
- package/dist/components/features/form/components/stepper/form-stepper.d.ts.map +1 -1
- package/dist/components/features/form/context/form-context.d.ts +2 -2
- package/dist/components/features/form/context/form-context.d.ts.map +1 -1
- package/dist/components/features/form/hooks/use-field.d.ts +12 -18
- package/dist/components/features/form/hooks/use-field.d.ts.map +1 -1
- package/dist/components/features/form/hooks/use-watch.d.ts +9 -20
- package/dist/components/features/form/hooks/use-watch.d.ts.map +1 -1
- package/dist/components/features/form/index.d.ts +33 -27
- package/dist/components/features/form/index.d.ts.map +1 -1
- package/dist/components/features/form/types/index.d.ts +32 -32
- package/dist/components/features/form/types/index.d.ts.map +1 -1
- package/dist/components/features/form/utils/get-field-constraints.d.ts +11 -0
- package/dist/components/features/form/utils/get-field-constraints.d.ts.map +1 -0
- package/dist/date-picker/index.mjs +1 -1
- package/dist/form/adapters/conform/index.mjs +237 -0
- package/dist/form/adapters/rhf/index.mjs +181 -0
- package/dist/form/index.mjs +3 -2
- package/dist/{form-Co3fM4B7.mjs → form-BE1xBne4.mjs} +328 -579
- package/dist/get-field-constraints-BPMW8VvY.mjs +48 -0
- package/dist/grid/index.mjs +1 -1
- package/dist/hooks/index.mjs +1 -1
- package/dist/index.mjs +12 -11
- package/dist/input-number/index.mjs +1 -1
- package/dist/map/index.mjs +1 -1
- package/dist/{map-ClxB41Hg.mjs → map-Cw7u8r6E.mjs} +1 -1
- package/dist/more-actions/index.mjs +1 -1
- package/dist/page-title/index.mjs +1 -1
- package/dist/tag-input/index.mjs +1 -1
- package/dist/task-queue/index.mjs +1 -1
- package/package.json +22 -2
- /package/dist/{col-q-J99UHe.mjs → col-YBbQ5wlb.mjs} +0 -0
- /package/dist/{hooks-Cb7YlxN4.mjs → hooks-DYjN7lvC.mjs} +0 -0
- /package/dist/{input-number-mDB-5M5C.mjs → input-number-DEjXG2I6.mjs} +0 -0
- /package/dist/{map-leaflet-imports-CaMm_rdF.mjs → map-leaflet-imports-D6nTEOIh.mjs} +0 -0
- /package/dist/{more-actions-CGagbIDT.mjs → more-actions-BNQ2yfWZ.mjs} +0 -0
- /package/dist/{page-title-R7QbfbWp.mjs → page-title-CNiRNZ7p.mjs} +0 -0
- /package/dist/{tag-input-BVSwNcRd.mjs → tag-input-BKed-cul.mjs} +0 -0
- /package/dist/{task-queue-dropdown-DyM5R8KF.mjs → task-queue-dropdown-Di_Wjspz.mjs} +0 -0
- /package/dist/{to-api-format-BnbRFYQI.mjs → to-api-format-Cq4prffn.mjs} +0 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { t as FormAdapterProvider } from "../../../adapter-context-B7L2ucTr.mjs";
|
|
2
|
+
import { t as getFieldConstraints } from "../../../get-field-constraints-BPMW8VvY.mjs";
|
|
3
|
+
import * as React$1 from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { FormProvider, getFormProps, getInputProps, useForm, useFormMetadata, useInputControl } from "@conform-to/react";
|
|
6
|
+
import { getZodConstraint, parseWithZod } from "@conform-to/zod/v4";
|
|
7
|
+
//#region src/components/features/form/adapters/conform/conform-adapter.tsx
|
|
8
|
+
/**
|
|
9
|
+
* Resolve a Conform field metadata object by dot-notation path.
|
|
10
|
+
*
|
|
11
|
+
* Handles:
|
|
12
|
+
* - Top-level fields: `"email"` -> `fields.email`
|
|
13
|
+
* - Nested objects: `"address.city"` -> `fields.address.getFieldset().city`
|
|
14
|
+
* - Array items: `"items.0.name"` -> `fields.items.getFieldList()[0].getFieldset().name`
|
|
15
|
+
*/
|
|
16
|
+
function resolveConformField(fields, name) {
|
|
17
|
+
const parts = name.split(".");
|
|
18
|
+
let current = fields;
|
|
19
|
+
for (let i = 0; i < parts.length; i++) {
|
|
20
|
+
const part = parts[i];
|
|
21
|
+
if (!current) break;
|
|
22
|
+
if (/^\d+$/.test(part)) {
|
|
23
|
+
const fieldList = current.getFieldList?.();
|
|
24
|
+
if (fieldList) {
|
|
25
|
+
const item = fieldList[Number.parseInt(part, 10)];
|
|
26
|
+
if (i < parts.length - 1 && item?.getFieldset) current = item.getFieldset();
|
|
27
|
+
else current = item;
|
|
28
|
+
} else current = current[part];
|
|
29
|
+
} else if (current[part] !== void 0) current = current[part];
|
|
30
|
+
else if (typeof current.getFieldset === "function") current = current.getFieldset()[part];
|
|
31
|
+
else current = void 0;
|
|
32
|
+
}
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
function convertFromString(value) {
|
|
36
|
+
if (value === void 0) return void 0;
|
|
37
|
+
if (Array.isArray(value)) return value[0];
|
|
38
|
+
if (value === "on" || value === "true") return true;
|
|
39
|
+
if (value === "" || value === "false") return false;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function convertToString(value) {
|
|
43
|
+
if (typeof value === "boolean") return value ? "on" : "";
|
|
44
|
+
if (value === null || value === void 0) return "";
|
|
45
|
+
return String(value);
|
|
46
|
+
}
|
|
47
|
+
/** Create a Conform form instance and normalize it to the adapter interface. */
|
|
48
|
+
function useConformCreateForm(props) {
|
|
49
|
+
const { schema, defaultValues, mode, id, onSubmit, formRef } = props;
|
|
50
|
+
const [form, fields] = useForm({
|
|
51
|
+
id,
|
|
52
|
+
defaultValue: defaultValues,
|
|
53
|
+
constraint: getZodConstraint(schema),
|
|
54
|
+
shouldValidate: {
|
|
55
|
+
onBlur: "onBlur",
|
|
56
|
+
onChange: "onInput",
|
|
57
|
+
onSubmit: "onSubmit"
|
|
58
|
+
}[mode],
|
|
59
|
+
shouldRevalidate: mode !== "onSubmit" ? "onInput" : void 0,
|
|
60
|
+
onValidate: ({ formData }) => parseWithZod(formData, { schema }),
|
|
61
|
+
onSubmit: onSubmit ? (event, context) => {
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
const submission = context.submission;
|
|
64
|
+
if (submission?.status === "success") onSubmit(submission.value);
|
|
65
|
+
} : void 0
|
|
66
|
+
});
|
|
67
|
+
const constraints = React$1.useMemo(() => getFieldConstraints(schema), [schema]);
|
|
68
|
+
const normalizedFields = React$1.useMemo(() => {
|
|
69
|
+
const result = {};
|
|
70
|
+
for (const [key, fieldMeta] of Object.entries(fields)) result[key] = {
|
|
71
|
+
id: fieldMeta.id ?? `${id ?? form.id}-${key}`,
|
|
72
|
+
errors: fieldMeta.errors ?? [],
|
|
73
|
+
required: constraints[key]?.required ?? false
|
|
74
|
+
};
|
|
75
|
+
return result;
|
|
76
|
+
}, [
|
|
77
|
+
fields,
|
|
78
|
+
id,
|
|
79
|
+
form.id,
|
|
80
|
+
constraints
|
|
81
|
+
]);
|
|
82
|
+
const conformFormProps = React$1.useMemo(() => getFormProps(form), [form]);
|
|
83
|
+
return React$1.useMemo(() => ({
|
|
84
|
+
id: form.id,
|
|
85
|
+
fields: normalizedFields,
|
|
86
|
+
formProps: conformFormProps,
|
|
87
|
+
submit: () => formRef?.current?.requestSubmit(),
|
|
88
|
+
reset: () => form.reset(),
|
|
89
|
+
getValues: () => {
|
|
90
|
+
if (!formRef?.current) return {};
|
|
91
|
+
const formData = new FormData(formRef.current);
|
|
92
|
+
const values = {};
|
|
93
|
+
for (const [key, value] of formData.entries()) values[key] = value;
|
|
94
|
+
return values;
|
|
95
|
+
},
|
|
96
|
+
raw: {
|
|
97
|
+
form,
|
|
98
|
+
fields
|
|
99
|
+
}
|
|
100
|
+
}), [
|
|
101
|
+
form,
|
|
102
|
+
fields,
|
|
103
|
+
normalizedFields,
|
|
104
|
+
conformFormProps,
|
|
105
|
+
formRef
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
/** Resolve a field by dot-notation path and return its normalized state. */
|
|
109
|
+
function useConformField(name) {
|
|
110
|
+
const fieldMeta = resolveConformField(useFormMetadata().getFieldset(), name);
|
|
111
|
+
const control = useInputControl(fieldMeta ?? {
|
|
112
|
+
name,
|
|
113
|
+
key: void 0,
|
|
114
|
+
id: name
|
|
115
|
+
});
|
|
116
|
+
if (!fieldMeta) throw new Error(`[Conform Adapter] Field "${name}" not found. Make sure the field name matches your schema.`);
|
|
117
|
+
return React$1.useMemo(() => ({
|
|
118
|
+
name: fieldMeta.name,
|
|
119
|
+
id: fieldMeta.id,
|
|
120
|
+
errors: fieldMeta.errors ?? [],
|
|
121
|
+
required: fieldMeta.required ?? false,
|
|
122
|
+
value: convertFromString(control.value),
|
|
123
|
+
change: (value) => control.change(convertToString(value)),
|
|
124
|
+
blur: () => control.blur(),
|
|
125
|
+
focus: () => control.focus(),
|
|
126
|
+
inputProps: getInputProps(fieldMeta, { type: "text" })
|
|
127
|
+
}), [fieldMeta, control]);
|
|
128
|
+
}
|
|
129
|
+
/** Watch a single field's value reactively. */
|
|
130
|
+
function useConformWatch(name) {
|
|
131
|
+
const fieldMeta = resolveConformField(useFormMetadata().getFieldset(), name);
|
|
132
|
+
if (!fieldMeta) return void 0;
|
|
133
|
+
return convertFromString(fieldMeta.value);
|
|
134
|
+
}
|
|
135
|
+
/** Watch multiple fields' values reactively. */
|
|
136
|
+
function useConformWatchAll(names) {
|
|
137
|
+
const allFields = useFormMetadata().getFieldset();
|
|
138
|
+
return React$1.useMemo(() => {
|
|
139
|
+
const result = {};
|
|
140
|
+
for (const name of names) {
|
|
141
|
+
const fieldMeta = resolveConformField(allFields, name);
|
|
142
|
+
if (fieldMeta) result[name] = convertFromString(fieldMeta.value);
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}, [allFields, names]);
|
|
146
|
+
}
|
|
147
|
+
/** Get field array helpers for a given array field name. */
|
|
148
|
+
function useConformFieldArray(name) {
|
|
149
|
+
const formMeta = useFormMetadata();
|
|
150
|
+
const arrayField = resolveConformField(formMeta.getFieldset(), name);
|
|
151
|
+
const items = React$1.useMemo(() => {
|
|
152
|
+
if (!arrayField?.getFieldList) return [];
|
|
153
|
+
return arrayField.getFieldList().map((item, index) => ({
|
|
154
|
+
id: item.id ?? `${name}-${index}`,
|
|
155
|
+
key: item.key ?? `${name}-${index}`,
|
|
156
|
+
name: `${name}[${index}]`
|
|
157
|
+
}));
|
|
158
|
+
}, [arrayField, name]);
|
|
159
|
+
const arrayFieldName = arrayField?.name;
|
|
160
|
+
return {
|
|
161
|
+
items,
|
|
162
|
+
append: React$1.useCallback((defaultValue) => {
|
|
163
|
+
if (!arrayFieldName) return;
|
|
164
|
+
formMeta.insert({
|
|
165
|
+
name: arrayFieldName,
|
|
166
|
+
defaultValue
|
|
167
|
+
});
|
|
168
|
+
}, [formMeta, arrayFieldName]),
|
|
169
|
+
remove: React$1.useCallback((index) => {
|
|
170
|
+
if (!arrayFieldName) return;
|
|
171
|
+
formMeta.remove({
|
|
172
|
+
name: arrayFieldName,
|
|
173
|
+
index
|
|
174
|
+
});
|
|
175
|
+
}, [formMeta, arrayFieldName]),
|
|
176
|
+
move: React$1.useCallback((from, to) => {
|
|
177
|
+
if (!arrayFieldName) return;
|
|
178
|
+
formMeta.reorder({
|
|
179
|
+
name: arrayFieldName,
|
|
180
|
+
from,
|
|
181
|
+
to
|
|
182
|
+
});
|
|
183
|
+
}, [formMeta, arrayFieldName])
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Wraps children in Conform's FormProvider so that useFormMetadata() and
|
|
188
|
+
* useInputControl() work inside field components.
|
|
189
|
+
*/
|
|
190
|
+
function ConformFormProviderWrapper({ instance, children }) {
|
|
191
|
+
const { form } = instance.raw;
|
|
192
|
+
return /* @__PURE__ */ jsx(FormProvider, {
|
|
193
|
+
context: form.context,
|
|
194
|
+
children
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Conform.js adapter implementing the `FormAdapter` interface.
|
|
199
|
+
*
|
|
200
|
+
* Maps Conform's `useForm` / `useInputControl` / `useFormMetadata` APIs
|
|
201
|
+
* to the normalized form adapter contract.
|
|
202
|
+
*/
|
|
203
|
+
const conformAdapter = {
|
|
204
|
+
name: "Conform",
|
|
205
|
+
useCreateForm: useConformCreateForm,
|
|
206
|
+
useField: useConformField,
|
|
207
|
+
useWatch: useConformWatch,
|
|
208
|
+
useWatchAll: useConformWatchAll,
|
|
209
|
+
useFieldArray: useConformFieldArray,
|
|
210
|
+
FormProvider: ConformFormProviderWrapper
|
|
211
|
+
};
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/components/features/form/adapters/conform/conform-provider.tsx
|
|
214
|
+
/**
|
|
215
|
+
* Wrap your application with this provider to use Conform.js as the form backend.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```tsx
|
|
219
|
+
* import { ConformAdapter } from '@datum-cloud/datum-ui/form/adapters/conform'
|
|
220
|
+
*
|
|
221
|
+
* function App() {
|
|
222
|
+
* return (
|
|
223
|
+
* <ConformAdapter>
|
|
224
|
+
* <MyApp />
|
|
225
|
+
* </ConformAdapter>
|
|
226
|
+
* )
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
function ConformAdapter({ children }) {
|
|
231
|
+
return /* @__PURE__ */ jsx(FormAdapterProvider, {
|
|
232
|
+
adapter: conformAdapter,
|
|
233
|
+
children
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
//#endregion
|
|
237
|
+
export { ConformAdapter, conformAdapter };
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { t as FormAdapterProvider } from "../../../adapter-context-B7L2ucTr.mjs";
|
|
2
|
+
import { t as getFieldConstraints } from "../../../get-field-constraints-BPMW8VvY.mjs";
|
|
3
|
+
import * as React$1 from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
6
|
+
import { FormProvider, useController, useFieldArray, useForm, useFormContext, useWatch } from "react-hook-form";
|
|
7
|
+
//#region src/components/features/form/adapters/rhf/rhf-adapter.tsx
|
|
8
|
+
/** Create a React Hook Form instance and normalize it to the adapter interface. */
|
|
9
|
+
function useRHFCreateForm(props) {
|
|
10
|
+
const { schema, defaultValues, mode, id, onSubmit, formRef } = props;
|
|
11
|
+
const form = useForm({
|
|
12
|
+
resolver: zodResolver(schema),
|
|
13
|
+
defaultValues,
|
|
14
|
+
mode: {
|
|
15
|
+
onBlur: "onBlur",
|
|
16
|
+
onChange: "onChange",
|
|
17
|
+
onSubmit: "onSubmit"
|
|
18
|
+
}[mode]
|
|
19
|
+
});
|
|
20
|
+
const constraints = React$1.useMemo(() => getFieldConstraints(schema), [schema]);
|
|
21
|
+
const onSubmitRef = React$1.useRef(onSubmit);
|
|
22
|
+
onSubmitRef.current = onSubmit;
|
|
23
|
+
const normalizedFields = React$1.useMemo(() => {
|
|
24
|
+
const result = {};
|
|
25
|
+
const errors = form.formState.errors;
|
|
26
|
+
for (const [key, constraint] of Object.entries(constraints)) {
|
|
27
|
+
const fieldError = errors[key];
|
|
28
|
+
result[key] = {
|
|
29
|
+
id: `${id ?? "form"}-${key}`,
|
|
30
|
+
errors: fieldError?.message ? [String(fieldError.message)] : [],
|
|
31
|
+
required: constraint.required
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}, [
|
|
36
|
+
constraints,
|
|
37
|
+
form.formState.errors,
|
|
38
|
+
id
|
|
39
|
+
]);
|
|
40
|
+
const handleSubmit = React$1.useMemo(() => form.handleSubmit((data) => {
|
|
41
|
+
onSubmitRef.current?.(data);
|
|
42
|
+
}), [form]);
|
|
43
|
+
const formProps = React$1.useMemo(() => ({
|
|
44
|
+
id,
|
|
45
|
+
onSubmit: handleSubmit,
|
|
46
|
+
noValidate: true
|
|
47
|
+
}), [id, handleSubmit]);
|
|
48
|
+
return React$1.useMemo(() => ({
|
|
49
|
+
id: id ?? "form",
|
|
50
|
+
fields: normalizedFields,
|
|
51
|
+
formProps,
|
|
52
|
+
submit: () => formRef?.current?.requestSubmit(),
|
|
53
|
+
reset: () => form.reset(),
|
|
54
|
+
getValues: () => form.getValues(),
|
|
55
|
+
raw: form
|
|
56
|
+
}), [
|
|
57
|
+
id,
|
|
58
|
+
normalizedFields,
|
|
59
|
+
formProps,
|
|
60
|
+
form,
|
|
61
|
+
formRef
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
/** Resolve a field by name and return its normalized state. */
|
|
65
|
+
function useRHFField(name) {
|
|
66
|
+
const { field, fieldState } = useController({
|
|
67
|
+
name,
|
|
68
|
+
control: useFormContext().control
|
|
69
|
+
});
|
|
70
|
+
return React$1.useMemo(() => ({
|
|
71
|
+
name: field.name,
|
|
72
|
+
id: name,
|
|
73
|
+
errors: fieldState.error?.message ? [String(fieldState.error.message)] : [],
|
|
74
|
+
required: false,
|
|
75
|
+
value: field.value,
|
|
76
|
+
change: (value) => field.onChange(value),
|
|
77
|
+
blur: () => field.onBlur(),
|
|
78
|
+
focus: () => {}
|
|
79
|
+
}), [
|
|
80
|
+
field,
|
|
81
|
+
fieldState,
|
|
82
|
+
name
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
/** Watch a single field's value reactively. */
|
|
86
|
+
function useRHFWatchHook(name) {
|
|
87
|
+
return useWatch({
|
|
88
|
+
name,
|
|
89
|
+
control: useFormContext().control
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** Watch multiple fields' values reactively. */
|
|
93
|
+
function useRHFWatchAllHook(names) {
|
|
94
|
+
const values = useWatch({
|
|
95
|
+
name: names,
|
|
96
|
+
control: useFormContext().control
|
|
97
|
+
});
|
|
98
|
+
return React$1.useMemo(() => {
|
|
99
|
+
const result = {};
|
|
100
|
+
names.forEach((n, index) => {
|
|
101
|
+
result[n] = values[index];
|
|
102
|
+
});
|
|
103
|
+
return result;
|
|
104
|
+
}, [names, values]);
|
|
105
|
+
}
|
|
106
|
+
/** Get field array helpers for a given array field name. */
|
|
107
|
+
function useRHFFieldArrayHook(name) {
|
|
108
|
+
const { fields, append, remove, move } = useFieldArray({
|
|
109
|
+
name,
|
|
110
|
+
control: useFormContext().control
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
items: React$1.useMemo(() => fields.map((field, index) => ({
|
|
114
|
+
id: field.id,
|
|
115
|
+
key: field.id,
|
|
116
|
+
name: `${name}.${index}`
|
|
117
|
+
})), [fields, name]),
|
|
118
|
+
append: React$1.useCallback((defaultValue) => {
|
|
119
|
+
append(defaultValue ?? {});
|
|
120
|
+
}, [append]),
|
|
121
|
+
remove: React$1.useCallback((index) => {
|
|
122
|
+
remove(index);
|
|
123
|
+
}, [remove]),
|
|
124
|
+
move: React$1.useCallback((from, to) => {
|
|
125
|
+
move(from, to);
|
|
126
|
+
}, [move])
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Wraps children in React Hook Form's FormProvider so that useFormContext()
|
|
131
|
+
* and useController() work inside field components.
|
|
132
|
+
*/
|
|
133
|
+
function RHFFormProviderWrapper({ instance, children }) {
|
|
134
|
+
const form = instance.raw;
|
|
135
|
+
return /* @__PURE__ */ jsx(FormProvider, {
|
|
136
|
+
...form,
|
|
137
|
+
children
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* React Hook Form adapter implementing the `FormAdapter` interface.
|
|
142
|
+
*
|
|
143
|
+
* Maps react-hook-form's `useForm` / `useController` / `useWatch` APIs
|
|
144
|
+
* to the normalized form adapter contract. Uses `@hookform/resolvers/zod`
|
|
145
|
+
* for Zod schema validation.
|
|
146
|
+
*/
|
|
147
|
+
const rhfAdapter = {
|
|
148
|
+
name: "React Hook Form",
|
|
149
|
+
useCreateForm: useRHFCreateForm,
|
|
150
|
+
useField: useRHFField,
|
|
151
|
+
useWatch: useRHFWatchHook,
|
|
152
|
+
useWatchAll: useRHFWatchAllHook,
|
|
153
|
+
useFieldArray: useRHFFieldArrayHook,
|
|
154
|
+
FormProvider: RHFFormProviderWrapper
|
|
155
|
+
};
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/components/features/form/adapters/rhf/rhf-provider.tsx
|
|
158
|
+
/**
|
|
159
|
+
* Wrap your application with this provider to use React Hook Form as the form backend.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```tsx
|
|
163
|
+
* import { RHFAdapter } from '@datum-cloud/datum-ui/form/adapters/rhf'
|
|
164
|
+
*
|
|
165
|
+
* function App() {
|
|
166
|
+
* return (
|
|
167
|
+
* <RHFAdapter>
|
|
168
|
+
* <MyApp />
|
|
169
|
+
* </RHFAdapter>
|
|
170
|
+
* )
|
|
171
|
+
* }
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
function RHFAdapter({ children }) {
|
|
175
|
+
return /* @__PURE__ */ jsx(FormAdapterProvider, {
|
|
176
|
+
adapter: rhfAdapter,
|
|
177
|
+
children
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
181
|
+
export { RHFAdapter, rhfAdapter };
|
package/dist/form/index.mjs
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { A as FormButton, C as FormField, D as FormCustom, E as FormDescription, O as FormCopyBox, S as FormFieldArray, T as FormDialog, _ as FormSelectItem, a as useField, b as FormRadioItem, c as FormStep, d as useWatch, f as useWatchAll, g as FormSelect, h as FormSubmit, i as useFieldContext, j as FormAutocomplete, k as FormCheckbox, l as FormStepper, m as FormSwitch, n as useStepper, o as StepperNavigation, p as FormTextarea, r as useFormContext, s as StepperControls, t as Form, u as FormWhen, v as FormRoot, w as FormError, x as FormInput, y as FormRadioGroup } from "../form-
|
|
2
|
-
|
|
1
|
+
import { A as FormButton, C as FormField, D as FormCustom, E as FormDescription, O as FormCopyBox, S as FormFieldArray, T as FormDialog, _ as FormSelectItem, a as useField, b as FormRadioItem, c as FormStep, d as useWatch, f as useWatchAll, g as FormSelect, h as FormSubmit, i as useFieldContext, j as FormAutocomplete, k as FormCheckbox, l as FormStepper, m as FormSwitch, n as useStepper, o as StepperNavigation, p as FormTextarea, r as useFormContext, s as StepperControls, t as Form, u as FormWhen, v as FormRoot, w as FormError, x as FormInput, y as FormRadioGroup } from "../form-BE1xBne4.mjs";
|
|
2
|
+
import { n as useAdapter, t as FormAdapterProvider } from "../adapter-context-B7L2ucTr.mjs";
|
|
3
|
+
export { Form, FormAdapterProvider, FormAutocomplete, FormButton, FormCheckbox, FormCopyBox, FormCustom, FormDescription, FormDialog, FormError, FormField, FormFieldArray, FormInput, FormRadioGroup, FormRadioItem, FormRoot, FormSelect, FormSelectItem, FormStep, FormStepper, FormSubmit, FormSwitch, FormTextarea, FormWhen, StepperControls, StepperNavigation, useAdapter, useField, useFieldContext, useFormContext, useStepper, useWatch, useWatchAll };
|