@omer-x/tailwind-ui-kit 1.0.0-alpha.12 → 1.0.0-alpha.14
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.
|
@@ -7,7 +7,8 @@ type BaseFormProps = Omit<ComponentProps<"form">, "action"> & {
|
|
|
7
7
|
action: (initialState: unknown, formData: FormData) => Promise<Record<string, string>>;
|
|
8
8
|
messages?: Partial<Record<string, string>>;
|
|
9
9
|
renderMessages?: ComponentProps<typeof JustDiv>["render"];
|
|
10
|
+
onlyDirty?: boolean;
|
|
10
11
|
};
|
|
11
|
-
declare const BaseForm: ({ action, messages: messageTranslations, renderMessages, className, children, }: BaseFormProps) => react.JSX.Element;
|
|
12
|
+
declare const BaseForm: ({ action, messages: messageTranslations, renderMessages, onlyDirty, className, children, }: BaseFormProps) => react.JSX.Element;
|
|
12
13
|
|
|
13
14
|
export { BaseForm as default };
|
|
@@ -2,17 +2,20 @@
|
|
|
2
2
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { Form } from "@base-ui/react/form";
|
|
4
4
|
import { useActionState, useRef } from "react";
|
|
5
|
+
import { getDirtyFormData } from "../../utils/dirty-form.js";
|
|
5
6
|
import JustDiv from "../elements/JustDiv.js";
|
|
6
7
|
import { FormContext } from "./FormContext.js";
|
|
7
8
|
const BaseForm = ({
|
|
8
9
|
action,
|
|
9
10
|
messages: messageTranslations = {},
|
|
10
11
|
renderMessages,
|
|
12
|
+
onlyDirty = false,
|
|
11
13
|
className,
|
|
12
14
|
children
|
|
13
15
|
}) => {
|
|
14
16
|
const [state, formAction, _loading] = useActionState(action, {});
|
|
15
17
|
const fieldsRef = useRef(/* @__PURE__ */ new Set());
|
|
18
|
+
const formRef = useRef(null);
|
|
16
19
|
const getFieldMessage = (fieldName) => {
|
|
17
20
|
if (fieldName in state) {
|
|
18
21
|
return state[fieldName];
|
|
@@ -34,7 +37,13 @@ const BaseForm = ({
|
|
|
34
37
|
formMessages: {},
|
|
35
38
|
fieldMessages: {}
|
|
36
39
|
});
|
|
37
|
-
|
|
40
|
+
const alteredAction = async (fd) => {
|
|
41
|
+
if (!onlyDirty) return formAction(fd);
|
|
42
|
+
const form = formRef.current;
|
|
43
|
+
if (!form) return formAction(fd);
|
|
44
|
+
return formAction(getDirtyFormData(form));
|
|
45
|
+
};
|
|
46
|
+
return /* @__PURE__ */ jsxs(Form, { ref: formRef, action: alteredAction, className, errors: fieldMessages, children: [
|
|
38
47
|
Object.entries(formMessages).map(([messageKey, message]) => /* @__PURE__ */ jsx(
|
|
39
48
|
JustDiv,
|
|
40
49
|
{
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function isDirty(el) {
|
|
2
|
+
if (el instanceof HTMLSelectElement) {
|
|
3
|
+
return Array.from(el.options).filter((o) => o.selected).map((o) => o.value).join(",") !== Array.from(el.options).filter((o) => o.defaultSelected).map((o) => o.value).join(",");
|
|
4
|
+
}
|
|
5
|
+
if (el.type === "checkbox" || el.type === "radio") {
|
|
6
|
+
const baseline = el.dataset.defaultChecked !== void 0 ? el.dataset.defaultChecked === "true" : el.defaultChecked;
|
|
7
|
+
return el.checked !== baseline;
|
|
8
|
+
}
|
|
9
|
+
return el.value !== (el.dataset.defaultValue ?? el.defaultValue);
|
|
10
|
+
}
|
|
11
|
+
function getDirtyFormData(form) {
|
|
12
|
+
const dirty = new FormData();
|
|
13
|
+
for (const e of Array.from(form.elements)) {
|
|
14
|
+
if (e instanceof HTMLInputElement || e instanceof HTMLTextAreaElement || e instanceof HTMLSelectElement) {
|
|
15
|
+
if (!e.name) continue;
|
|
16
|
+
if (isDirty(e)) {
|
|
17
|
+
if (e.type === "checkbox" || e.type === "radio") {
|
|
18
|
+
if (e.checked) {
|
|
19
|
+
dirty.append(e.name, e.value);
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
dirty.append(e.name, e.value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return dirty;
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
getDirtyFormData
|
|
31
|
+
};
|