@datum-cloud/datum-ui 0.5.0 → 0.6.0-alpha.3e04d8c
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 +78 -40
- package/dist/adapter-context-rWveHhDd.mjs +25 -0
- package/dist/autocomplete/index.mjs +1 -1
- package/dist/{autocomplete-V5-qslzS.mjs → autocomplete-CkYJueBL.mjs} +2 -2
- package/dist/autosearch/index.mjs +195 -0
- package/dist/{calendar-date-picker-DWK94_DC.mjs → calendar-date-picker-CDT-8Ha8.mjs} +2 -1
- package/dist/combobox/index.mjs +2 -0
- package/dist/combobox-B-C9lJeD.mjs +97 -0
- package/dist/components/features/autocomplete/autocomplete.d.ts +1 -1
- package/dist/components/features/autocomplete/autocomplete.d.ts.map +1 -1
- package/dist/components/features/autocomplete/autocomplete.types.d.ts +2 -0
- package/dist/components/features/autocomplete/autocomplete.types.d.ts.map +1 -1
- package/dist/components/features/autosearch/autosearch.d.ts +35 -0
- package/dist/components/features/autosearch/autosearch.d.ts.map +1 -0
- package/dist/components/features/autosearch/autosearch.types.d.ts +51 -0
- package/dist/components/features/autosearch/autosearch.types.d.ts.map +1 -0
- package/dist/components/features/autosearch/index.d.ts +3 -0
- package/dist/components/features/autosearch/index.d.ts.map +1 -0
- package/dist/components/features/calendar-date-picker/calendar-date-picker.d.ts +2 -1
- package/dist/components/features/calendar-date-picker/calendar-date-picker.d.ts.map +1 -1
- package/dist/components/features/combobox/combobox.d.ts +27 -0
- package/dist/components/features/combobox/combobox.d.ts.map +1 -0
- package/dist/components/features/combobox/index.d.ts +3 -0
- package/dist/components/features/combobox/index.d.ts.map +1 -0
- package/dist/components/features/combobox/types.d.ts +84 -0
- package/dist/components/features/combobox/types.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/date-time-picker.d.ts +9 -0
- package/dist/components/features/date-time-picker/date-time-picker.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/index.d.ts +3 -0
- package/dist/components/features/date-time-picker/index.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/types.d.ts +59 -0
- package/dist/components/features/date-time-picker/types.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/utils/format.d.ts +13 -0
- package/dist/components/features/date-time-picker/utils/format.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/utils/index.d.ts +3 -0
- package/dist/components/features/date-time-picker/utils/index.d.ts.map +1 -0
- package/dist/components/features/date-time-picker/utils/timezone.d.ts +23 -0
- package/dist/components/features/date-time-picker/utils/timezone.d.ts.map +1 -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 +120 -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-autosearch.d.ts +37 -0
- package/dist/components/features/form/components/form-autosearch.d.ts.map +1 -0
- package/dist/components/features/form/components/form-checkbox.d.ts.map +1 -1
- package/dist/components/features/form/components/form-combobox.d.ts +80 -0
- package/dist/components/features/form/components/form-combobox.d.ts.map +1 -0
- package/dist/components/features/form/components/form-copy-box.d.ts +3 -0
- package/dist/components/features/form/components/form-copy-box.d.ts.map +1 -1
- package/dist/components/features/form/components/form-custom.d.ts.map +1 -1
- package/dist/components/features/form/components/form-date-picker.d.ts +40 -0
- package/dist/components/features/form/components/form-date-picker.d.ts.map +1 -0
- package/dist/components/features/form/components/form-date-time-picker.d.ts +39 -0
- package/dist/components/features/form/components/form-date-time-picker.d.ts.map +1 -0
- package/dist/components/features/form/components/form-dialog.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/form-time-picker.d.ts +21 -0
- package/dist/components/features/form/components/form-time-picker.d.ts.map +1 -0
- package/dist/components/features/form/components/form-transfer.d.ts +37 -0
- package/dist/components/features/form/components/form-transfer.d.ts.map +1 -0
- package/dist/components/features/form/components/index.d.ts +7 -1
- package/dist/components/features/form/components/index.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/index.d.ts +1 -1
- package/dist/components/features/form/hooks/index.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-form-state.d.ts +36 -0
- package/dist/components/features/form/hooks/use-form-state.d.ts.map +1 -0
- 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 +69 -45
- package/dist/components/features/form/index.d.ts.map +1 -1
- package/dist/components/features/form/stepper/index.d.ts +17 -0
- package/dist/components/features/form/stepper/index.d.ts.map +1 -0
- package/dist/components/features/form/types/index.d.ts +68 -32
- package/dist/components/features/form/types/index.d.ts.map +1 -1
- package/dist/components/features/form/utils/get-field-constraints.d.ts +33 -0
- package/dist/components/features/form/utils/get-field-constraints.d.ts.map +1 -0
- package/dist/components/features/form/utils/get-schema-defaults.d.ts +24 -0
- package/dist/components/features/form/utils/get-schema-defaults.d.ts.map +1 -0
- package/dist/components/features/form/utils/zod-helpers.d.ts +12 -0
- package/dist/components/features/form/utils/zod-helpers.d.ts.map +1 -0
- package/dist/components/features/time-picker/index.d.ts +3 -0
- package/dist/components/features/time-picker/index.d.ts.map +1 -0
- package/dist/components/features/time-picker/time-picker.d.ts +22 -0
- package/dist/components/features/time-picker/time-picker.d.ts.map +1 -0
- package/dist/components/features/time-picker/types.d.ts +31 -0
- package/dist/components/features/time-picker/types.d.ts.map +1 -0
- package/dist/components/features/transfer/components/index.d.ts +9 -0
- package/dist/components/features/transfer/components/index.d.ts.map +1 -0
- package/dist/components/features/transfer/components/transfer-group.d.ts +7 -0
- package/dist/components/features/transfer/components/transfer-group.d.ts.map +1 -0
- package/dist/components/features/transfer/components/transfer-item.d.ts +10 -0
- package/dist/components/features/transfer/components/transfer-item.d.ts.map +1 -0
- package/dist/components/features/transfer/components/transfer-panel.d.ts +18 -0
- package/dist/components/features/transfer/components/transfer-panel.d.ts.map +1 -0
- package/dist/components/features/transfer/components/transfer-search.d.ts +9 -0
- package/dist/components/features/transfer/components/transfer-search.d.ts.map +1 -0
- package/dist/components/features/transfer/hooks/use-transfer-dnd.d.ts +26 -0
- package/dist/components/features/transfer/hooks/use-transfer-dnd.d.ts.map +1 -0
- package/dist/components/features/transfer/hooks/use-transfer-state.d.ts +20 -0
- package/dist/components/features/transfer/hooks/use-transfer-state.d.ts.map +1 -0
- package/dist/components/features/transfer/index.d.ts +3 -0
- package/dist/components/features/transfer/index.d.ts.map +1 -0
- package/dist/components/features/transfer/transfer.d.ts +6 -0
- package/dist/components/features/transfer/transfer.d.ts.map +1 -0
- package/dist/components/features/transfer/types.d.ts +69 -0
- package/dist/components/features/transfer/types.d.ts.map +1 -0
- package/dist/data-table/index.mjs +1 -1
- package/dist/date-picker/index.mjs +2 -2
- package/dist/date-time-picker/index.mjs +2 -0
- package/dist/date-time-picker-BomrW07W.mjs +178 -0
- package/dist/form/adapters/conform/index.mjs +327 -0
- package/dist/form/adapters/rhf/index.mjs +267 -0
- package/dist/form/index.mjs +3 -2
- package/dist/form/stepper/index.mjs +541 -0
- package/dist/form-D8OnRHdd.mjs +1653 -0
- package/dist/form-context-Ccxm-wqL.mjs +17 -0
- package/dist/get-field-constraints-BicgDkfH.mjs +51 -0
- package/dist/grid/index.mjs +1 -1
- package/dist/hooks/index.mjs +2 -2
- package/dist/index.mjs +16 -15
- package/dist/input-number/index.mjs +1 -1
- package/dist/map/index.mjs +1 -1
- package/dist/{map-ClxB41Hg.mjs → map-CWIQ-eql.mjs} +1 -1
- package/dist/more-actions/index.mjs +1 -1
- package/dist/page-title/index.mjs +1 -1
- package/dist/stepper/index.mjs +1 -320
- package/dist/stepper-DvIOp0hh.mjs +321 -0
- package/dist/tag-input/index.mjs +1 -1
- package/dist/task-queue/index.mjs +1 -1
- package/dist/time-picker/index.mjs +2 -0
- package/dist/time-picker-BoF7pZZ2.mjs +43 -0
- package/dist/transfer/index.mjs +2 -0
- package/dist/transfer-B2n8pgEQ.mjs +260 -0
- package/package.json +63 -2
- package/dist/form-Co3fM4B7.mjs +0 -2114
- /package/dist/{col-q-J99UHe.mjs → col-1T0Q3SlH.mjs} +0 -0
- /package/dist/{hooks-Cb7YlxN4.mjs → hooks-D8r2M2U6.mjs} +0 -0
- /package/dist/{input-number-mDB-5M5C.mjs → input-number-a7uydAsw.mjs} +0 -0
- /package/dist/{map-leaflet-imports-CaMm_rdF.mjs → map-leaflet-imports-CRSKA79m.mjs} +0 -0
- /package/dist/{more-actions-CGagbIDT.mjs → more-actions-ILnEZq_E.mjs} +0 -0
- /package/dist/{page-title-R7QbfbWp.mjs → page-title-ChsnpBiH.mjs} +0 -0
- /package/dist/{tag-input-BVSwNcRd.mjs → tag-input-T9cUX9-G.mjs} +0 -0
- /package/dist/{task-queue-dropdown-DyM5R8KF.mjs → task-queue-dropdown-Wcbj-f0V.mjs} +0 -0
- /package/dist/{to-api-format-BnbRFYQI.mjs → to-api-format-Bh3c01gr.mjs} +0 -0
- /package/dist/{use-copy-to-clipboard-BGdTmkFV.mjs → use-copy-to-clipboard-uNeeVHC4.mjs} +0 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { t as FormAdapterProvider } from "../../../adapter-context-rWveHhDd.mjs";
|
|
2
|
+
import { t as getFieldConstraints } from "../../../get-field-constraints-BicgDkfH.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 { isDirty, useFormData } from "@conform-to/react/future";
|
|
7
|
+
import { getZodConstraint, parseWithZod } from "@conform-to/zod/v4";
|
|
8
|
+
//#region src/components/features/form/adapters/conform/conform-adapter.tsx
|
|
9
|
+
/**
|
|
10
|
+
* Shared context for the touched fields set.
|
|
11
|
+
* Created in useConformCreateForm and consumed by useConformField.
|
|
12
|
+
*/
|
|
13
|
+
const TouchedFieldsContext = React$1.createContext({ current: /* @__PURE__ */ new Set() });
|
|
14
|
+
/**
|
|
15
|
+
* Resolve a Conform field metadata object by dot-notation path.
|
|
16
|
+
*
|
|
17
|
+
* Handles:
|
|
18
|
+
* - Top-level fields: `"email"` -> `fields.email`
|
|
19
|
+
* - Nested objects: `"address.city"` -> `fields.address.getFieldset().city`
|
|
20
|
+
* - Array items: `"items.0.name"` -> `fields.items.getFieldList()[0].getFieldset().name`
|
|
21
|
+
*/
|
|
22
|
+
function resolveConformField(fields, name) {
|
|
23
|
+
const parts = name.split(".");
|
|
24
|
+
let current = fields;
|
|
25
|
+
for (let i = 0; i < parts.length; i++) {
|
|
26
|
+
const part = parts[i];
|
|
27
|
+
if (!current) break;
|
|
28
|
+
if (/^\d+$/.test(part)) {
|
|
29
|
+
const fieldList = current.getFieldList?.();
|
|
30
|
+
if (fieldList) {
|
|
31
|
+
const item = fieldList[Number.parseInt(part, 10)];
|
|
32
|
+
if (i < parts.length - 1 && item?.getFieldset) current = item.getFieldset();
|
|
33
|
+
else current = item;
|
|
34
|
+
} else current = current[part];
|
|
35
|
+
} else if (current[part] !== void 0) current = current[part];
|
|
36
|
+
else if (typeof current.getFieldset === "function") current = current.getFieldset()[part];
|
|
37
|
+
else current = void 0;
|
|
38
|
+
}
|
|
39
|
+
return current;
|
|
40
|
+
}
|
|
41
|
+
function convertFromString(value) {
|
|
42
|
+
if (value === void 0) return void 0;
|
|
43
|
+
if (Array.isArray(value)) return value[0];
|
|
44
|
+
if (value === "on") return true;
|
|
45
|
+
if (value.startsWith("[") && value.endsWith("]")) try {
|
|
46
|
+
return JSON.parse(value);
|
|
47
|
+
} catch {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function convertToString(value) {
|
|
53
|
+
if (typeof value === "boolean") return value ? "on" : "";
|
|
54
|
+
if (value === null || value === void 0) return "";
|
|
55
|
+
if (Array.isArray(value)) return JSON.stringify(value);
|
|
56
|
+
return String(value);
|
|
57
|
+
}
|
|
58
|
+
/** Create a Conform form instance and normalize it to the adapter interface. */
|
|
59
|
+
function useConformCreateForm(props) {
|
|
60
|
+
const { schema, defaultValues, mode, id, onSubmit, formRef } = props;
|
|
61
|
+
const [form, fields] = useForm({
|
|
62
|
+
id,
|
|
63
|
+
defaultValue: defaultValues,
|
|
64
|
+
constraint: getZodConstraint(schema),
|
|
65
|
+
shouldValidate: {
|
|
66
|
+
onBlur: "onBlur",
|
|
67
|
+
onChange: "onInput",
|
|
68
|
+
onSubmit: "onSubmit"
|
|
69
|
+
}[mode],
|
|
70
|
+
shouldRevalidate: mode !== "onSubmit" ? "onInput" : void 0,
|
|
71
|
+
onValidate: ({ formData }) => parseWithZod(formData, { schema }),
|
|
72
|
+
onSubmit: onSubmit ? (event, context) => {
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
const submission = context.submission;
|
|
75
|
+
if (submission?.status === "success") onSubmit(submission.value);
|
|
76
|
+
} : void 0
|
|
77
|
+
});
|
|
78
|
+
const constraints = React$1.useMemo(() => getFieldConstraints(schema), [schema]);
|
|
79
|
+
const formIsDirty = useFormData(formRef, (formData) => isDirty(formData, { defaultValue: defaultValues }) ?? false) ?? false;
|
|
80
|
+
const dirtyFields = useFormData(formRef, (formData) => {
|
|
81
|
+
const result = {};
|
|
82
|
+
const defaults = defaultValues ?? {};
|
|
83
|
+
for (const key of Object.keys(defaults)) {
|
|
84
|
+
const current = formData.get(key);
|
|
85
|
+
const defaultVal = defaults[key];
|
|
86
|
+
result[key] = current !== (defaultVal === true ? "on" : defaultVal === false || defaultVal === null || defaultVal === void 0 ? "" : String(defaultVal));
|
|
87
|
+
}
|
|
88
|
+
for (const key of formData.keys()) if (!(key in result) && !key.startsWith("$")) {
|
|
89
|
+
const current = formData.get(key);
|
|
90
|
+
result[key] = current !== "" && current !== null;
|
|
91
|
+
}
|
|
92
|
+
const dirty = {};
|
|
93
|
+
for (const [key, value] of Object.entries(result)) if (value) dirty[key] = true;
|
|
94
|
+
return dirty;
|
|
95
|
+
}) ?? {};
|
|
96
|
+
const isValid = useFormData(formRef, (formData) => {
|
|
97
|
+
return parseWithZod(formData, { schema })?.status === "success";
|
|
98
|
+
}) ?? false;
|
|
99
|
+
const touchedFieldsRef = React$1.useRef(/* @__PURE__ */ new Set());
|
|
100
|
+
const [touchedFields, setTouchedFields] = React$1.useState({});
|
|
101
|
+
React$1.useEffect(() => {
|
|
102
|
+
const formEl = formRef?.current;
|
|
103
|
+
if (!formEl) return;
|
|
104
|
+
const handleFocusOut = (event) => {
|
|
105
|
+
const target = event.target;
|
|
106
|
+
if (target instanceof HTMLInputElement || target instanceof HTMLSelectElement || target instanceof HTMLTextAreaElement) {
|
|
107
|
+
const name = target.name;
|
|
108
|
+
if (name && !touchedFieldsRef.current.has(name)) {
|
|
109
|
+
touchedFieldsRef.current.add(name);
|
|
110
|
+
setTouchedFields((prev) => ({
|
|
111
|
+
...prev,
|
|
112
|
+
[name]: true
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
formEl.addEventListener("focusout", handleFocusOut);
|
|
118
|
+
return () => formEl.removeEventListener("focusout", handleFocusOut);
|
|
119
|
+
}, [formRef]);
|
|
120
|
+
const formState = React$1.useMemo(() => ({
|
|
121
|
+
isDirty: formIsDirty,
|
|
122
|
+
isValid,
|
|
123
|
+
isSubmitted: false,
|
|
124
|
+
submitCount: 0,
|
|
125
|
+
dirtyFields,
|
|
126
|
+
touchedFields
|
|
127
|
+
}), [
|
|
128
|
+
formIsDirty,
|
|
129
|
+
isValid,
|
|
130
|
+
dirtyFields,
|
|
131
|
+
touchedFields
|
|
132
|
+
]);
|
|
133
|
+
const normalizedFields = React$1.useMemo(() => {
|
|
134
|
+
const result = {};
|
|
135
|
+
for (const [key, fieldMeta] of Object.entries(fields)) result[key] = {
|
|
136
|
+
id: fieldMeta.id ?? `${id ?? form.id}-${key}`,
|
|
137
|
+
errors: fieldMeta.errors ?? [],
|
|
138
|
+
required: constraints[key]?.required ?? false,
|
|
139
|
+
isDirty: dirtyFields[key] ?? false,
|
|
140
|
+
isTouched: touchedFields[key] ?? false
|
|
141
|
+
};
|
|
142
|
+
return result;
|
|
143
|
+
}, [
|
|
144
|
+
fields,
|
|
145
|
+
id,
|
|
146
|
+
form.id,
|
|
147
|
+
constraints,
|
|
148
|
+
dirtyFields,
|
|
149
|
+
touchedFields
|
|
150
|
+
]);
|
|
151
|
+
const conformFormProps = React$1.useMemo(() => getFormProps(form), [form]);
|
|
152
|
+
return React$1.useMemo(() => ({
|
|
153
|
+
id: form.id,
|
|
154
|
+
fields: normalizedFields,
|
|
155
|
+
formProps: conformFormProps,
|
|
156
|
+
formState,
|
|
157
|
+
submit: () => formRef?.current?.requestSubmit(),
|
|
158
|
+
reset: () => form.reset(),
|
|
159
|
+
getValues: () => {
|
|
160
|
+
if (!formRef?.current) return {};
|
|
161
|
+
const formData = new FormData(formRef.current);
|
|
162
|
+
const values = {};
|
|
163
|
+
for (const [key, value] of formData.entries()) values[key] = value;
|
|
164
|
+
return values;
|
|
165
|
+
},
|
|
166
|
+
raw: {
|
|
167
|
+
form,
|
|
168
|
+
fields,
|
|
169
|
+
touchedFieldsRef
|
|
170
|
+
}
|
|
171
|
+
}), [
|
|
172
|
+
form,
|
|
173
|
+
fields,
|
|
174
|
+
normalizedFields,
|
|
175
|
+
conformFormProps,
|
|
176
|
+
formState,
|
|
177
|
+
formRef
|
|
178
|
+
]);
|
|
179
|
+
}
|
|
180
|
+
/** Resolve a field by dot-notation path and return its normalized state. */
|
|
181
|
+
function useConformField(name) {
|
|
182
|
+
const fieldMeta = resolveConformField(useFormMetadata().getFieldset(), name);
|
|
183
|
+
const touchedFieldsRef = React$1.use(TouchedFieldsContext);
|
|
184
|
+
const control = useInputControl(fieldMeta ?? {
|
|
185
|
+
name,
|
|
186
|
+
key: void 0,
|
|
187
|
+
id: name
|
|
188
|
+
});
|
|
189
|
+
if (!fieldMeta) throw new Error(`[Conform Adapter] Field "${name}" not found. Make sure the field name matches your schema.`);
|
|
190
|
+
const currentValue = convertFromString(control.value);
|
|
191
|
+
const defaultValue = convertFromString(fieldMeta.initialValue);
|
|
192
|
+
const fieldIsDirty = currentValue !== (defaultValue === void 0 ? "" : defaultValue);
|
|
193
|
+
const fieldIsTouched = touchedFieldsRef.current.has(name);
|
|
194
|
+
return React$1.useMemo(() => ({
|
|
195
|
+
name: fieldMeta.name,
|
|
196
|
+
id: fieldMeta.id,
|
|
197
|
+
errors: fieldMeta.errors ?? [],
|
|
198
|
+
required: fieldMeta.required ?? false,
|
|
199
|
+
isDirty: fieldIsDirty,
|
|
200
|
+
isTouched: fieldIsTouched,
|
|
201
|
+
value: currentValue,
|
|
202
|
+
change: (value) => control.change(convertToString(value)),
|
|
203
|
+
blur: () => control.blur(),
|
|
204
|
+
focus: () => control.focus(),
|
|
205
|
+
inputProps: getInputProps(fieldMeta, { type: "text" })
|
|
206
|
+
}), [
|
|
207
|
+
fieldMeta,
|
|
208
|
+
control,
|
|
209
|
+
currentValue,
|
|
210
|
+
fieldIsDirty,
|
|
211
|
+
fieldIsTouched
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
/** Watch a single field's value reactively. */
|
|
215
|
+
function useConformWatch(name) {
|
|
216
|
+
const fieldMeta = resolveConformField(useFormMetadata().getFieldset(), name);
|
|
217
|
+
if (!fieldMeta) return void 0;
|
|
218
|
+
return convertFromString(fieldMeta.value);
|
|
219
|
+
}
|
|
220
|
+
/** Watch multiple fields' values reactively. */
|
|
221
|
+
function useConformWatchAll(names) {
|
|
222
|
+
const allFields = useFormMetadata().getFieldset();
|
|
223
|
+
const namesKey = names.join(",");
|
|
224
|
+
return React$1.useMemo(() => {
|
|
225
|
+
const result = {};
|
|
226
|
+
for (const name of names) {
|
|
227
|
+
const fieldMeta = resolveConformField(allFields, name);
|
|
228
|
+
if (fieldMeta) result[name] = convertFromString(fieldMeta.value);
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}, [allFields, namesKey]);
|
|
232
|
+
}
|
|
233
|
+
/** Get field array helpers for a given array field name. */
|
|
234
|
+
function useConformFieldArray(name) {
|
|
235
|
+
const formMeta = useFormMetadata();
|
|
236
|
+
const arrayField = resolveConformField(formMeta.getFieldset(), name);
|
|
237
|
+
const items = React$1.useMemo(() => {
|
|
238
|
+
if (!arrayField?.getFieldList) return [];
|
|
239
|
+
return arrayField.getFieldList().map((item, index) => ({
|
|
240
|
+
id: item.id ?? `${name}-${index}`,
|
|
241
|
+
key: item.key ?? `${name}-${index}`,
|
|
242
|
+
name: `${name}[${index}]`
|
|
243
|
+
}));
|
|
244
|
+
}, [arrayField, name]);
|
|
245
|
+
const arrayFieldName = arrayField?.name;
|
|
246
|
+
return {
|
|
247
|
+
items,
|
|
248
|
+
append: React$1.useCallback((defaultValue) => {
|
|
249
|
+
if (!arrayFieldName) return;
|
|
250
|
+
formMeta.insert({
|
|
251
|
+
name: arrayFieldName,
|
|
252
|
+
defaultValue
|
|
253
|
+
});
|
|
254
|
+
}, [formMeta, arrayFieldName]),
|
|
255
|
+
remove: React$1.useCallback((index) => {
|
|
256
|
+
if (!arrayFieldName) return;
|
|
257
|
+
formMeta.remove({
|
|
258
|
+
name: arrayFieldName,
|
|
259
|
+
index
|
|
260
|
+
});
|
|
261
|
+
}, [formMeta, arrayFieldName]),
|
|
262
|
+
move: React$1.useCallback((from, to) => {
|
|
263
|
+
if (!arrayFieldName) return;
|
|
264
|
+
formMeta.reorder({
|
|
265
|
+
name: arrayFieldName,
|
|
266
|
+
from,
|
|
267
|
+
to
|
|
268
|
+
});
|
|
269
|
+
}, [formMeta, arrayFieldName])
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Wraps children in Conform's FormProvider so that useFormMetadata() and
|
|
274
|
+
* useInputControl() work inside field components.
|
|
275
|
+
* Also provides the touched fields ref via context for useConformField.
|
|
276
|
+
*/
|
|
277
|
+
function ConformFormProviderWrapper({ instance, children }) {
|
|
278
|
+
const { form, touchedFieldsRef } = instance.raw;
|
|
279
|
+
return /* @__PURE__ */ jsx(FormProvider, {
|
|
280
|
+
context: form.context,
|
|
281
|
+
children: /* @__PURE__ */ jsx(TouchedFieldsContext, {
|
|
282
|
+
value: touchedFieldsRef,
|
|
283
|
+
children
|
|
284
|
+
})
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Conform.js adapter implementing the `FormAdapter` interface.
|
|
289
|
+
*
|
|
290
|
+
* Maps Conform's `useForm` / `useInputControl` / `useFormMetadata` APIs
|
|
291
|
+
* to the normalized form adapter contract.
|
|
292
|
+
*/
|
|
293
|
+
const conformAdapter = {
|
|
294
|
+
name: "Conform",
|
|
295
|
+
useCreateForm: useConformCreateForm,
|
|
296
|
+
useField: useConformField,
|
|
297
|
+
useWatch: useConformWatch,
|
|
298
|
+
useWatchAll: useConformWatchAll,
|
|
299
|
+
useFieldArray: useConformFieldArray,
|
|
300
|
+
FormProvider: ConformFormProviderWrapper
|
|
301
|
+
};
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/components/features/form/adapters/conform/conform-provider.tsx
|
|
304
|
+
/**
|
|
305
|
+
* Wrap your application with this provider to use Conform.js as the form backend.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```tsx
|
|
309
|
+
* import { ConformAdapter } from '@datum-cloud/datum-ui/form/adapters/conform'
|
|
310
|
+
*
|
|
311
|
+
* function App() {
|
|
312
|
+
* return (
|
|
313
|
+
* <ConformAdapter>
|
|
314
|
+
* <MyApp />
|
|
315
|
+
* </ConformAdapter>
|
|
316
|
+
* )
|
|
317
|
+
* }
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
function ConformAdapter({ children }) {
|
|
321
|
+
return /* @__PURE__ */ jsx(FormAdapterProvider, {
|
|
322
|
+
adapter: conformAdapter,
|
|
323
|
+
children
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
//#endregion
|
|
327
|
+
export { ConformAdapter, conformAdapter };
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { t as FormAdapterProvider } from "../../../adapter-context-rWveHhDd.mjs";
|
|
2
|
+
import { n as getObjectShape, t as getFieldConstraints } from "../../../get-field-constraints-BicgDkfH.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/utils/get-schema-defaults.ts
|
|
8
|
+
/**
|
|
9
|
+
* Derive default values from a Zod schema that match what React Hook Form's
|
|
10
|
+
* `useController` will produce when fields register.
|
|
11
|
+
*
|
|
12
|
+
* This is critical for RHF's `isDirty` tracking: RHF compares current values
|
|
13
|
+
* against `_defaultValues`. Without explicit defaults, RHF uses `{}` as the
|
|
14
|
+
* baseline, so when `useController` registers fields (e.g. `username: ''`),
|
|
15
|
+
* the form is immediately considered dirty.
|
|
16
|
+
*
|
|
17
|
+
* By passing these schema-derived defaults to `useForm({ defaultValues })`,
|
|
18
|
+
* the baseline matches the registered values and `isDirty` starts as `false`.
|
|
19
|
+
*
|
|
20
|
+
* Maps Zod field types to their `useController` registration values:
|
|
21
|
+
* - string -> `''`
|
|
22
|
+
* - number -> `undefined`
|
|
23
|
+
* - boolean -> `false`
|
|
24
|
+
* - array -> `[]`
|
|
25
|
+
* - object -> `{}` (recursive)
|
|
26
|
+
* - optional/nullable wrappers -> unwrap and derive inner default
|
|
27
|
+
* - `.default()` -> use the provided default value
|
|
28
|
+
*/
|
|
29
|
+
function getSchemaDefaults(schema) {
|
|
30
|
+
const shape = getObjectShape(schema);
|
|
31
|
+
if (!shape) return {};
|
|
32
|
+
const defaults = {};
|
|
33
|
+
for (const [key, fieldSchema] of Object.entries(shape)) defaults[key] = getFieldDefault(fieldSchema);
|
|
34
|
+
return defaults;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the default value for a Zod field type that matches what RHF's
|
|
38
|
+
* `useController` will produce when registering the field.
|
|
39
|
+
*
|
|
40
|
+
* Unwraps optional/nullable/default wrappers to find the inner type,
|
|
41
|
+
* then returns the natural "empty" value for that type.
|
|
42
|
+
*/
|
|
43
|
+
function getFieldDefault(schema) {
|
|
44
|
+
const { type } = schema.def;
|
|
45
|
+
if (type === "default") return schema.def.defaultValue;
|
|
46
|
+
if (type === "optional" || type === "nullable") {
|
|
47
|
+
const innerDef = schema.def;
|
|
48
|
+
return getFieldDefault(innerDef.innerType);
|
|
49
|
+
}
|
|
50
|
+
if (type === "pipe") {
|
|
51
|
+
const pipeDef = schema.def;
|
|
52
|
+
return getFieldDefault(pipeDef.in);
|
|
53
|
+
}
|
|
54
|
+
if (type === "string") return "";
|
|
55
|
+
if (type === "number" || type === "bigint" || type === "nan") return void 0;
|
|
56
|
+
if (type === "boolean") return false;
|
|
57
|
+
if (type === "array") return [];
|
|
58
|
+
if (type === "object") return getSchemaDefaults(schema);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/components/features/form/adapters/rhf/rhf-adapter.tsx
|
|
62
|
+
const RHFFormIdContext = React$1.createContext("form");
|
|
63
|
+
/** Create a React Hook Form instance and normalize it to the adapter interface. */
|
|
64
|
+
function useRHFCreateForm(props) {
|
|
65
|
+
const { schema, defaultValues, mode, id, onSubmit, formRef } = props;
|
|
66
|
+
const modeMap = {
|
|
67
|
+
onBlur: "onBlur",
|
|
68
|
+
onChange: "onChange",
|
|
69
|
+
onSubmit: "onSubmit"
|
|
70
|
+
};
|
|
71
|
+
const schemaDefaults = React$1.useMemo(() => getSchemaDefaults(schema), [schema]);
|
|
72
|
+
const mergedDefaults = React$1.useMemo(() => ({
|
|
73
|
+
...schemaDefaults,
|
|
74
|
+
...defaultValues
|
|
75
|
+
}), [schemaDefaults, defaultValues]);
|
|
76
|
+
const form = useForm({
|
|
77
|
+
resolver: zodResolver(schema),
|
|
78
|
+
defaultValues: mergedDefaults,
|
|
79
|
+
mode: modeMap[mode]
|
|
80
|
+
});
|
|
81
|
+
const { errors, isDirty, isValid, dirtyFields, touchedFields } = form.formState;
|
|
82
|
+
const constraints = React$1.useMemo(() => getFieldConstraints(schema), [schema]);
|
|
83
|
+
const onSubmitRef = React$1.useRef(onSubmit);
|
|
84
|
+
onSubmitRef.current = onSubmit;
|
|
85
|
+
const normalizedFields = React$1.useMemo(() => {
|
|
86
|
+
const result = {};
|
|
87
|
+
for (const [key, constraint] of Object.entries(constraints)) {
|
|
88
|
+
const fieldError = errors[key];
|
|
89
|
+
result[key] = {
|
|
90
|
+
id: `${id ?? "form"}-${key}`,
|
|
91
|
+
errors: fieldError?.message ? [String(fieldError.message)] : [],
|
|
92
|
+
required: constraint.required,
|
|
93
|
+
isDirty: Boolean(dirtyFields[key]),
|
|
94
|
+
isTouched: Boolean(touchedFields[key])
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}, [
|
|
99
|
+
constraints,
|
|
100
|
+
errors,
|
|
101
|
+
id,
|
|
102
|
+
dirtyFields,
|
|
103
|
+
touchedFields
|
|
104
|
+
]);
|
|
105
|
+
const formState = React$1.useMemo(() => ({
|
|
106
|
+
isDirty,
|
|
107
|
+
isValid,
|
|
108
|
+
isSubmitted: false,
|
|
109
|
+
submitCount: 0,
|
|
110
|
+
dirtyFields: Object.fromEntries(Object.entries(dirtyFields).map(([k, v]) => [k, Boolean(v)])),
|
|
111
|
+
touchedFields: Object.fromEntries(Object.entries(touchedFields).map(([k, v]) => [k, Boolean(v)]))
|
|
112
|
+
}), [
|
|
113
|
+
isDirty,
|
|
114
|
+
isValid,
|
|
115
|
+
dirtyFields,
|
|
116
|
+
touchedFields
|
|
117
|
+
]);
|
|
118
|
+
const handleSubmit = React$1.useMemo(() => form.handleSubmit((data) => {
|
|
119
|
+
onSubmitRef.current?.(data);
|
|
120
|
+
}), [form]);
|
|
121
|
+
const formProps = React$1.useMemo(() => ({
|
|
122
|
+
id,
|
|
123
|
+
onSubmit: handleSubmit,
|
|
124
|
+
noValidate: true
|
|
125
|
+
}), [id, handleSubmit]);
|
|
126
|
+
return React$1.useMemo(() => ({
|
|
127
|
+
id: id ?? "form",
|
|
128
|
+
fields: normalizedFields,
|
|
129
|
+
formProps,
|
|
130
|
+
formState,
|
|
131
|
+
submit: () => formRef?.current?.requestSubmit(),
|
|
132
|
+
reset: () => form.reset(),
|
|
133
|
+
getValues: () => form.getValues(),
|
|
134
|
+
raw: form
|
|
135
|
+
}), [
|
|
136
|
+
id,
|
|
137
|
+
normalizedFields,
|
|
138
|
+
formProps,
|
|
139
|
+
formState,
|
|
140
|
+
form,
|
|
141
|
+
formRef
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
/** Resolve a field by name and return its normalized state. */
|
|
145
|
+
function useRHFField(name) {
|
|
146
|
+
const form = useFormContext();
|
|
147
|
+
const formId = React$1.use(RHFFormIdContext);
|
|
148
|
+
const { field, fieldState } = useController({
|
|
149
|
+
name,
|
|
150
|
+
control: form.control
|
|
151
|
+
});
|
|
152
|
+
return React$1.useMemo(() => ({
|
|
153
|
+
name: field.name,
|
|
154
|
+
id: `${formId}-${name}`,
|
|
155
|
+
errors: fieldState.error?.message ? [String(fieldState.error.message)] : [],
|
|
156
|
+
required: false,
|
|
157
|
+
isDirty: fieldState.isDirty,
|
|
158
|
+
isTouched: fieldState.isTouched,
|
|
159
|
+
value: field.value,
|
|
160
|
+
change: (value) => field.onChange(value),
|
|
161
|
+
blur: () => field.onBlur(),
|
|
162
|
+
focus: () => form.setFocus(name)
|
|
163
|
+
}), [
|
|
164
|
+
field,
|
|
165
|
+
fieldState,
|
|
166
|
+
name,
|
|
167
|
+
formId,
|
|
168
|
+
form
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
/** Watch a single field's value reactively. */
|
|
172
|
+
function useRHFWatchHook(name) {
|
|
173
|
+
return useWatch({
|
|
174
|
+
name,
|
|
175
|
+
control: useFormContext().control
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/** Watch multiple fields' values reactively. */
|
|
179
|
+
function useRHFWatchAllHook(names) {
|
|
180
|
+
const values = useWatch({
|
|
181
|
+
name: names,
|
|
182
|
+
control: useFormContext().control
|
|
183
|
+
});
|
|
184
|
+
const namesKey = names.join(",");
|
|
185
|
+
return React$1.useMemo(() => {
|
|
186
|
+
const result = {};
|
|
187
|
+
names.forEach((n, index) => {
|
|
188
|
+
result[n] = values[index];
|
|
189
|
+
});
|
|
190
|
+
return result;
|
|
191
|
+
}, [namesKey, values]);
|
|
192
|
+
}
|
|
193
|
+
/** Get field array helpers for a given array field name. */
|
|
194
|
+
function useRHFFieldArrayHook(name) {
|
|
195
|
+
const { fields, append, remove, move } = useFieldArray({
|
|
196
|
+
name,
|
|
197
|
+
control: useFormContext().control
|
|
198
|
+
});
|
|
199
|
+
return {
|
|
200
|
+
items: React$1.useMemo(() => fields.map((field, index) => ({
|
|
201
|
+
id: field.id,
|
|
202
|
+
key: field.id,
|
|
203
|
+
name: `${name}.${index}`
|
|
204
|
+
})), [fields, name]),
|
|
205
|
+
append: React$1.useCallback((defaultValue) => {
|
|
206
|
+
append(defaultValue ?? {});
|
|
207
|
+
}, [append]),
|
|
208
|
+
remove,
|
|
209
|
+
move
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Wraps children in React Hook Form's FormProvider so that useFormContext()
|
|
214
|
+
* and useController() work inside field components.
|
|
215
|
+
*/
|
|
216
|
+
function RHFFormProviderWrapper({ instance, children }) {
|
|
217
|
+
const form = instance.raw;
|
|
218
|
+
return /* @__PURE__ */ jsx(RHFFormIdContext, {
|
|
219
|
+
value: instance.id,
|
|
220
|
+
children: /* @__PURE__ */ jsx(FormProvider, {
|
|
221
|
+
...form,
|
|
222
|
+
children
|
|
223
|
+
})
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* React Hook Form adapter implementing the `FormAdapter` interface.
|
|
228
|
+
*
|
|
229
|
+
* Maps react-hook-form's `useForm` / `useController` / `useWatch` APIs
|
|
230
|
+
* to the normalized form adapter contract. Uses `@hookform/resolvers/zod`
|
|
231
|
+
* for Zod schema validation.
|
|
232
|
+
*/
|
|
233
|
+
const rhfAdapter = {
|
|
234
|
+
name: "React Hook Form",
|
|
235
|
+
useCreateForm: useRHFCreateForm,
|
|
236
|
+
useField: useRHFField,
|
|
237
|
+
useWatch: useRHFWatchHook,
|
|
238
|
+
useWatchAll: useRHFWatchAllHook,
|
|
239
|
+
useFieldArray: useRHFFieldArrayHook,
|
|
240
|
+
FormProvider: RHFFormProviderWrapper
|
|
241
|
+
};
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/components/features/form/adapters/rhf/rhf-provider.tsx
|
|
244
|
+
/**
|
|
245
|
+
* Wrap your application with this provider to use React Hook Form as the form backend.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```tsx
|
|
249
|
+
* import { RHFAdapter } from '@datum-cloud/datum-ui/form/adapters/rhf'
|
|
250
|
+
*
|
|
251
|
+
* function App() {
|
|
252
|
+
* return (
|
|
253
|
+
* <RHFAdapter>
|
|
254
|
+
* <MyApp />
|
|
255
|
+
* </RHFAdapter>
|
|
256
|
+
* )
|
|
257
|
+
* }
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
function RHFAdapter({ children }) {
|
|
261
|
+
return /* @__PURE__ */ jsx(FormAdapterProvider, {
|
|
262
|
+
adapter: rhfAdapter,
|
|
263
|
+
children
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
export { RHFAdapter, rhfAdapter };
|
package/dist/form/index.mjs
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { A as
|
|
2
|
-
|
|
1
|
+
import { A as FormCheckbox, C as FormDialog, D as FormCustom, E as FormDatePicker, M as FormAutosearch, N as FormAutocomplete, O as FormCopyBox, S as FormError, T as FormDateTimePicker, _ as FormRadioGroup, a as useField, b as FormFieldArray, c as useWatchAll, d as FormTextarea, f as FormSwitch, g as FormRoot, h as FormSelectItem, i as useFieldContext, j as FormButton, k as FormCombobox, l as FormTransfer, m as FormSelect, n as useFormState, o as FormWhen, p as FormSubmit, r as useFormContext, s as useWatch, t as Form, u as FormTimePicker, v as FormRadioItem, w as FormDescription, x as FormField, y as FormInput } from "../form-D8OnRHdd.mjs";
|
|
2
|
+
import { n as useAdapter, t as FormAdapterProvider } from "../adapter-context-rWveHhDd.mjs";
|
|
3
|
+
export { Form, FormAdapterProvider, FormAutocomplete, FormAutosearch, FormButton, FormCheckbox, FormCombobox, FormCopyBox, FormCustom, FormDatePicker, FormDateTimePicker, FormDescription, FormDialog, FormError, FormField, FormFieldArray, FormInput, FormRadioGroup, FormRadioItem, FormRoot, FormSelect, FormSelectItem, FormSubmit, FormSwitch, FormTextarea, FormTimePicker, FormTransfer, FormWhen, useAdapter, useField, useFieldContext, useFormContext, useFormState, useWatch, useWatchAll };
|