@omer-x/tailwind-ui-kit 1.0.0-alpha.17 → 1.0.0-alpha.19

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.
Files changed (67) hide show
  1. package/MIGRATION.md +164 -0
  2. package/README.md +390 -0
  3. package/dist/components/button/BaseButton.d.ts +27 -11
  4. package/dist/components/button/BaseButton.js +57 -51
  5. package/dist/components/button/index.d.ts +2 -4
  6. package/dist/components/button/index.js +1 -4
  7. package/dist/components/dialog/AnimatedPopup.d.ts +3 -6
  8. package/dist/components/dialog/AnimatedPopup.js +6 -31
  9. package/dist/components/dialog/DialogBackdrop.d.ts +3 -6
  10. package/dist/components/dialog/DialogBackdrop.js +6 -22
  11. package/dist/components/dialog/DialogTitle.d.ts +3 -6
  12. package/dist/components/dialog/DialogTitle.js +6 -15
  13. package/dist/components/dialog/index.d.ts +4 -5
  14. package/dist/components/dialog/index.js +4 -8
  15. package/dist/components/elements/JustAnchor.d.ts +3 -6
  16. package/dist/components/elements/JustAnchor.js +8 -13
  17. package/dist/components/elements/JustButton.d.ts +3 -6
  18. package/dist/components/elements/JustButton.js +8 -13
  19. package/dist/components/elements/JustDiv.d.ts +3 -6
  20. package/dist/components/elements/JustDiv.js +8 -13
  21. package/dist/components/elements/JustInput.d.ts +3 -6
  22. package/dist/components/elements/JustInput.js +8 -13
  23. package/dist/components/form/BaseForm.d.ts +12 -15
  24. package/dist/components/form/BaseForm.js +72 -70
  25. package/dist/components/form/BaseFormField.d.ts +12 -19
  26. package/dist/components/form/BaseFormField.js +29 -24
  27. package/dist/components/form/BaseInput.d.ts +5 -9
  28. package/dist/components/form/BaseInput.js +20 -31
  29. package/dist/components/form/BaseSwitch.d.ts +6 -9
  30. package/dist/components/form/BaseSwitch.js +18 -51
  31. package/dist/components/form/ClientSideError.d.ts +9 -0
  32. package/dist/components/form/ClientSideError.js +39 -0
  33. package/dist/components/form/FieldRoot.d.ts +6 -9
  34. package/dist/components/form/FieldRoot.js +16 -18
  35. package/dist/components/form/FormContext.d.ts +26 -9
  36. package/dist/components/form/FormContext.js +9 -7
  37. package/dist/components/form/ServerSideError.d.ts +6 -10
  38. package/dist/components/form/ServerSideError.js +10 -14
  39. package/dist/components/form/index.d.ts +7 -11
  40. package/dist/components/form/index.js +6 -10
  41. package/dist/components/form/useFormMessages.d.ts +10 -0
  42. package/dist/components/form/useFormMessages.js +16 -0
  43. package/dist/components/layout/Container.d.ts +5 -8
  44. package/dist/components/layout/Container.js +8 -10
  45. package/dist/components/layout/index.d.ts +2 -2
  46. package/dist/components/layout/index.js +1 -4
  47. package/dist/components/page/Pagination.d.ts +11 -15
  48. package/dist/components/page/Pagination.js +60 -48
  49. package/dist/components/page/index.d.ts +2 -5
  50. package/dist/components/page/index.js +1 -4
  51. package/dist/components/table/Tdx.d.ts +5 -8
  52. package/dist/components/table/Tdx.js +13 -9
  53. package/dist/components/table/index.d.ts +2 -2
  54. package/dist/components/table/index.js +1 -4
  55. package/dist/hooks/useRipples.d.ts +2 -5
  56. package/dist/hooks/useRipples.js +46 -33
  57. package/dist/styles/ripple.module.css +1 -0
  58. package/dist/utils/cn.d.ts +6 -5
  59. package/dist/utils/cn.js +8 -6
  60. package/dist/utils/dirty-form.d.ts +1 -3
  61. package/dist/utils/dirty-form.js +24 -39
  62. package/dist/utils/index.d.ts +2 -2
  63. package/dist/utils/index.js +1 -4
  64. package/package.json +27 -24
  65. package/dist/components/button/PulseButton.d.ts +0 -8
  66. package/dist/components/button/PulseButton.js +0 -41
  67. package/dist/styles/ripple.module.d.ts +0 -2
@@ -1,52 +1,58 @@
1
- import { jsx, jsxs } from "react/jsx-runtime";
2
- import { faSpinner } from "@fortawesome/free-solid-svg-icons/faSpinner";
3
- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
4
- import {} from "react";
5
- import { cn } from "../../utils/cn.js";
6
- import PulseButton from "./PulseButton.js";
7
- const BaseButton = ({
8
- size = "md",
9
- pending = false,
10
- disabled,
11
- focusableWhenDisabled,
12
- className,
13
- children,
14
- ...otherProps
15
- }) => /* @__PURE__ */ jsxs(
16
- PulseButton,
17
- {
18
- ...otherProps,
19
- className: cn({
20
- // Size
21
- "h-12 px-5 text-base leading-7 gap-3": size === "lg",
22
- "h-10 px-4 text-sm leading-6 gap-3": size === "md",
23
- "h-9 px-3 text-sm leading-5 gap-2": size === "sm",
24
- "h-8 px-2.5 text-xs leading-4 gap-2": size === "xs"
25
- }, [
26
- // Base
27
- "relative",
28
- "flex items-center justify-center",
29
- "rounded-md",
30
- "font-medium",
31
- "select-none",
32
- "outline-0",
33
- "transition-colors",
34
- // Focus
35
- "focus-visible:outline-2",
36
- "focus-visible:outline-offset-2",
37
- // Cursor
38
- "cursor-pointer",
39
- "data-disabled:cursor-default"
40
- ], className),
41
- disabled,
42
- focusableWhenDisabled: pending || focusableWhenDisabled,
43
- children: [
44
- pending && /* @__PURE__ */ jsx(FontAwesomeIcon, { icon: faSpinner, size: "lg", className: "fa-spin-pulse" }),
45
- children
46
- ]
47
- }
48
- );
49
- var BaseButton_default = BaseButton;
50
- export {
51
- BaseButton_default as default
1
+ "use client";
2
+ import { Button } from "@base-ui/react/button";
3
+ import { cn } from "cn";
4
+ import { use } from "react";
5
+ import { useFormStatus } from "react-dom";
6
+ import { useRipples } from "../../hooks/useRipples.js";
7
+ import { FormContext } from "../form/FormContext.js";
8
+ import { jsxs } from "react/jsx-runtime";
9
+ // src/components/button/BaseButton.tsx
10
+ const BaseButton = ({ size = "md", pending: pendingProp = false, spinner, iconOnly = false, type, disabled = false, focusableWhenDisabled = false, onClick, className, children, ...otherProps }) => {
11
+ const { pending: nativeFormPending } = useFormStatus();
12
+ const { pending: baseFormPending } = use(FormContext);
13
+ const pending = pendingProp || type === "submit" && (nativeFormPending || baseFormPending);
14
+ const [triggerRippleEffect, ripples] = useRipples();
15
+ const handleClick = (e) => {
16
+ onClick?.(e);
17
+ if (!e.defaultPrevented) triggerRippleEffect(e);
18
+ };
19
+ return /* @__PURE__ */ jsxs(Button, {
20
+ ...otherProps,
21
+ type,
22
+ disabled: pending || disabled,
23
+ focusableWhenDisabled: pending || focusableWhenDisabled,
24
+ onClick: handleClick,
25
+ "aria-busy": pending || void 0,
26
+ "data-pending": pending ? "" : void 0,
27
+ "data-icon-only": iconOnly ? "" : void 0,
28
+ className: cn({
29
+ "h-12 text-base leading-7 gap-3": size === "lg",
30
+ "h-10 text-sm leading-6 gap-3": size === "md",
31
+ "h-9 text-sm leading-5 gap-2": size === "sm",
32
+ "h-8 text-xs leading-4 gap-2": size === "xs",
33
+ "px-5": size === "lg" && !iconOnly,
34
+ "px-4": size === "md" && !iconOnly,
35
+ "px-3": size === "sm" && !iconOnly,
36
+ "px-2.5": size === "xs" && !iconOnly,
37
+ "aspect-square px-0": iconOnly
38
+ }, [
39
+ "relative",
40
+ "flex items-center justify-center",
41
+ "rounded-md",
42
+ "font-medium",
43
+ "select-none",
44
+ "outline-0",
45
+ "transition-colors",
46
+ "focus-visible:outline-2",
47
+ "focus-visible:outline-offset-2",
48
+ "cursor-pointer",
49
+ "data-disabled:cursor-default"
50
+ ], className),
51
+ children: [
52
+ ripples,
53
+ pending && spinner,
54
+ !(pending && iconOnly && spinner) && children
55
+ ]
56
+ });
52
57
  };
58
+ export { BaseButton as default };
@@ -1,4 +1,2 @@
1
- export { default as BaseButton } from './BaseButton.js';
2
- import 'react';
3
- import './PulseButton.js';
4
- import '@base-ui/react/button';
1
+ import BaseButton from "./BaseButton.js";
2
+ export { BaseButton };
@@ -1,4 +1 @@
1
- import { default as default2 } from "./BaseButton.js";
2
- export {
3
- default2 as BaseButton
4
- };
1
+ export { default as BaseButton } from "./BaseButton.js";
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const AnimatedPopup: ({ render, className, ...otherProps }: useRender.ComponentProps<"div">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { AnimatedPopup as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const AnimatedPopup: ({ render, className, ...otherProps }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { AnimatedPopup as default };
@@ -1,35 +1,10 @@
1
1
  import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
- import { cn } from "../../utils/cn.js";
3
+ import { cn } from "cn";
4
+ // src/components/dialog/AnimatedPopup.tsx
4
5
  const AnimatedPopup = ({ render, className, ...otherProps }) => useRender({
5
- defaultTagName: "div",
6
- render,
7
- props: mergeProps({
8
- className: cn(
9
- "fixed",
10
- "top-[calc(50%+1.25rem*var(--nested-dialogs))]",
11
- "left-1/2",
12
- "-mt-8",
13
- "w-96",
14
- "max-w-[calc(100vw-3rem)]",
15
- "-translate-x-1/2",
16
- "-translate-y-1/2",
17
- "scale-[calc(1-0.1*var(--nested-dialogs))]",
18
- "transition-all",
19
- "duration-150",
20
- "data-[ending-style]:scale-90",
21
- "data-[ending-style]:opacity-0",
22
- "data-[nested-dialog-open]:after:absolute",
23
- "data-[nested-dialog-open]:after:inset-0",
24
- "data-[nested-dialog-open]:after:rounded-[inherit]",
25
- "data-[nested-dialog-open]:after:bg-black/5",
26
- "data-[starting-style]:scale-90",
27
- "data-[starting-style]:opacity-0",
28
- className
29
- )
30
- }, otherProps)
6
+ defaultTagName: "div",
7
+ render,
8
+ props: mergeProps({ className: cn("fixed", "top-[calc(50%+1.25rem*var(--nested-dialogs))]", "left-1/2", "-mt-8", "w-96", "max-w-[calc(100vw-3rem)]", "-translate-x-1/2", "-translate-y-1/2", "scale-[calc(1-0.1*var(--nested-dialogs))]", "transition-all", "duration-150", "data-[ending-style]:scale-90", "data-[ending-style]:opacity-0", "data-[nested-dialog-open]:after:absolute", "data-[nested-dialog-open]:after:inset-0", "data-[nested-dialog-open]:after:rounded-[inherit]", "data-[nested-dialog-open]:after:bg-black/5", "data-[starting-style]:scale-90", "data-[starting-style]:opacity-0", className) }, otherProps)
31
9
  });
32
- var AnimatedPopup_default = AnimatedPopup;
33
- export {
34
- AnimatedPopup_default as default
35
- };
10
+ export { AnimatedPopup as default };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const DialogBackdrop: ({ render, ...otherProps }: useRender.ComponentProps<"div">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { DialogBackdrop as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const DialogBackdrop: ({ render, ...otherProps }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { DialogBackdrop as default };
@@ -1,26 +1,10 @@
1
1
  import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
- import { cn } from "../../utils/cn.js";
3
+ import { cn } from "cn";
4
+ // src/components/dialog/DialogBackdrop.tsx
4
5
  const DialogBackdrop = ({ render, ...otherProps }) => useRender({
5
- defaultTagName: "div",
6
- render,
7
- props: mergeProps({
8
- className: cn(
9
- "fixed",
10
- "inset-0",
11
- "min-h-dvh",
12
- "bg-black",
13
- "opacity-20",
14
- "transition-all",
15
- "duration-150",
16
- "data-[ending-style]:opacity-0",
17
- "data-[starting-style]:opacity-0",
18
- "dark:opacity-70",
19
- "supports-[-webkit-touch-callout:none]:absolute"
20
- )
21
- }, otherProps)
6
+ defaultTagName: "div",
7
+ render,
8
+ props: mergeProps({ className: cn("fixed", "inset-0", "min-h-dvh", "bg-black", "opacity-20", "transition-all", "duration-150", "data-[ending-style]:opacity-0", "data-[starting-style]:opacity-0", "dark:opacity-70", "supports-[-webkit-touch-callout:none]:absolute") }, otherProps)
22
9
  });
23
- var DialogBackdrop_default = DialogBackdrop;
24
- export {
25
- DialogBackdrop_default as default
26
- };
10
+ export { DialogBackdrop as default };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const DialogTitle: ({ render, ...otherProps }: useRender.ComponentProps<"h2">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { DialogTitle as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const DialogTitle: ({ render, ...otherProps }: useRender.ComponentProps<"h2">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { DialogTitle as default };
@@ -1,19 +1,10 @@
1
1
  import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
- import { cn } from "../../utils/cn.js";
3
+ import { cn } from "cn";
4
+ // src/components/dialog/DialogTitle.tsx
4
5
  const DialogTitle = ({ render, ...otherProps }) => useRender({
5
- defaultTagName: "h2",
6
- render,
7
- props: mergeProps({
8
- className: cn(
9
- "-mt-1.5",
10
- "mb-1",
11
- "text-xl",
12
- "font-semibold"
13
- )
14
- }, otherProps)
6
+ defaultTagName: "h2",
7
+ render,
8
+ props: mergeProps({ className: cn("-mt-1.5", "mb-1", "text-xl", "font-semibold") }, otherProps)
15
9
  });
16
- var DialogTitle_default = DialogTitle;
17
- export {
18
- DialogTitle_default as default
19
- };
10
+ export { DialogTitle as default };
@@ -1,5 +1,4 @@
1
- export { default as AnimatedPopup } from './AnimatedPopup.js';
2
- export { default as DialogBackdrop } from './DialogBackdrop.js';
3
- export { default as DialogTitle } from './DialogTitle.js';
4
- import 'react';
5
- import '@base-ui/react/use-render';
1
+ import AnimatedPopup from "./AnimatedPopup.js";
2
+ import DialogBackdrop from "./DialogBackdrop.js";
3
+ import DialogTitle from "./DialogTitle.js";
4
+ export { AnimatedPopup, DialogBackdrop, DialogTitle };
@@ -1,8 +1,4 @@
1
- import { default as default2 } from "./AnimatedPopup.js";
2
- import { default as default3 } from "./DialogBackdrop.js";
3
- import { default as default4 } from "./DialogTitle.js";
4
- export {
5
- default2 as AnimatedPopup,
6
- default3 as DialogBackdrop,
7
- default4 as DialogTitle
8
- };
1
+ import AnimatedPopup from "./AnimatedPopup.js";
2
+ import DialogBackdrop from "./DialogBackdrop.js";
3
+ import DialogTitle from "./DialogTitle.js";
4
+ export { AnimatedPopup, DialogBackdrop, DialogTitle };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const JustAnchor: ({ render, ...props }: useRender.ComponentProps<"a">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { JustAnchor as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const JustAnchor: ({ render, ...props }: useRender.ComponentProps<"a">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { JustAnchor as default };
@@ -1,15 +1,10 @@
1
1
  import { useRender } from "@base-ui/react/use-render";
2
- const JustAnchor = ({
3
- render,
4
- ...props
5
- }) => {
6
- return useRender({
7
- defaultTagName: "a",
8
- render,
9
- props
10
- });
11
- };
12
- var JustAnchor_default = JustAnchor;
13
- export {
14
- JustAnchor_default as default
2
+ // src/components/elements/JustAnchor.tsx
3
+ const JustAnchor = ({ render, ...props }) => {
4
+ return useRender({
5
+ defaultTagName: "a",
6
+ render,
7
+ props
8
+ });
15
9
  };
10
+ export { JustAnchor as default };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const JustButton: ({ render, ...props }: useRender.ComponentProps<"button">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { JustButton as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const JustButton: ({ render, ...props }: useRender.ComponentProps<"button">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { JustButton as default };
@@ -1,15 +1,10 @@
1
1
  import { useRender } from "@base-ui/react/use-render";
2
- const JustButton = ({
3
- render,
4
- ...props
5
- }) => {
6
- return useRender({
7
- defaultTagName: "button",
8
- render,
9
- props
10
- });
11
- };
12
- var JustButton_default = JustButton;
13
- export {
14
- JustButton_default as default
2
+ // src/components/elements/JustButton.tsx
3
+ const JustButton = ({ render, ...props }) => {
4
+ return useRender({
5
+ defaultTagName: "button",
6
+ render,
7
+ props
8
+ });
15
9
  };
10
+ export { JustButton as default };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const JustDiv: ({ render, ...props }: useRender.ComponentProps<"div">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { JustDiv as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const JustDiv: ({ render, ...props }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { JustDiv as default };
@@ -1,15 +1,10 @@
1
1
  import { useRender } from "@base-ui/react/use-render";
2
- const JustDiv = ({
3
- render,
4
- ...props
5
- }) => {
6
- return useRender({
7
- defaultTagName: "div",
8
- render,
9
- props
10
- });
11
- };
12
- var JustDiv_default = JustDiv;
13
- export {
14
- JustDiv_default as default
2
+ // src/components/elements/JustDiv.tsx
3
+ const JustDiv = ({ render, ...props }) => {
4
+ return useRender({
5
+ defaultTagName: "div",
6
+ render,
7
+ props
8
+ });
15
9
  };
10
+ export { JustDiv as default };
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { useRender } from '@base-ui/react/use-render';
3
-
4
- declare const JustInput: ({ render, ...props }: useRender.ComponentProps<"input">) => react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
5
-
6
- export { JustInput as default };
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const JustInput: ({ render, ...props }: useRender.ComponentProps<"input">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { JustInput as default };
@@ -1,15 +1,10 @@
1
1
  import { useRender } from "@base-ui/react/use-render";
2
- const JustInput = ({
3
- render,
4
- ...props
5
- }) => {
6
- return useRender({
7
- defaultTagName: "input",
8
- render,
9
- props
10
- });
11
- };
12
- var JustInput_default = JustInput;
13
- export {
14
- JustInput_default as default
2
+ // src/components/elements/JustInput.tsx
3
+ const JustInput = ({ render, ...props }) => {
4
+ return useRender({
5
+ defaultTagName: "input",
6
+ render,
7
+ props
8
+ });
15
9
  };
10
+ export { JustInput as default };
@@ -1,16 +1,13 @@
1
- import * as react from 'react';
2
- import { ComponentPropsWithoutRef, ComponentProps } from 'react';
3
- import JustDiv from '../elements/JustDiv.js';
4
- import '@base-ui/react/use-render';
5
-
6
- type BaseFormProps = Omit<ComponentPropsWithoutRef<"form">, "action"> & {
7
- action: (initialState: unknown, formData: FormData) => Promise<Record<string, string>>;
8
- defaultValues?: Record<string, unknown>;
9
- onlyDirty?: boolean;
10
- messages?: Partial<Record<string, string>>;
11
- renderMessages?: ComponentProps<typeof JustDiv>["render"];
12
- onStateChange?: (state: Record<string, string>) => void;
1
+ import { ComponentPropsWithoutRef } from "react";
2
+ import { MessageFormatter } from "./FormContext.js";
3
+ import { FormAction, FormActionState } from "@omer-x/next-form-actions";
4
+ type BaseFormProps<Data> = Omit<ComponentPropsWithoutRef<"form">, "action"> & {
5
+ action: FormAction<Data>;
6
+ formatMessage?: MessageFormatter;
7
+ defaultValues?: Record<string, unknown>;
8
+ onlyDirty?: boolean;
9
+ resetOnSuccess?: boolean;
10
+ onResult?: (result: FormActionState<Data>) => void;
13
11
  };
14
- declare const BaseForm: ({ action, defaultValues, onlyDirty, messages: messageTranslations, renderMessages, onStateChange, children, ...otherProps }: BaseFormProps) => react.JSX.Element;
15
-
16
- export { BaseForm as default };
12
+ declare const BaseForm: <Data>({ action, formatMessage, defaultValues, onlyDirty, resetOnSuccess, onResult, onSubmit, children, ...otherProps }: BaseFormProps<Data>) => import("react").JSX.Element;
13
+ export { BaseForm as default };
@@ -1,76 +1,78 @@
1
1
  "use client";
2
- import { jsx, jsxs } from "react/jsx-runtime";
3
2
  import { Form } from "@base-ui/react/form";
4
- import { useActionState, useEffect, useRef } from "react";
3
+ import { Fragment, startTransition, useActionState, useRef } from "react";
5
4
  import { getDirtyFormData } from "../../utils/dirty-form.js";
6
- import JustDiv from "../elements/JustDiv.js";
7
5
  import { FormContext } from "./FormContext.js";
8
- const BaseForm = ({
9
- action,
10
- defaultValues = {},
11
- onlyDirty = false,
12
- messages: messageTranslations = {},
13
- renderMessages,
14
- onStateChange,
15
- children,
16
- ...otherProps
17
- }) => {
18
- const [state, formAction, _loading] = useActionState(action, {});
19
- const fieldsRef = useRef(/* @__PURE__ */ new Set());
20
- const formRef = useRef(null);
21
- useEffect(() => {
22
- onStateChange?.(state);
23
- }, [state]);
24
- const getFieldMessage = (fieldName) => {
25
- if (fieldName in state) {
26
- return state[fieldName];
27
- }
28
- return null;
29
- };
30
- const registerField = (fieldName) => {
31
- fieldsRef.current.add(fieldName);
32
- };
33
- const unregisterField = (fieldName) => {
34
- fieldsRef.current.delete(fieldName);
35
- };
36
- const { formMessages, fieldMessages } = Object.keys(state).reduce((bundle, key) => {
37
- if (fieldsRef.current.has(key)) {
38
- return { ...bundle, fieldMessages: { ...bundle.fieldMessages, [key]: state[key] } };
39
- }
40
- return { ...bundle, formMessages: { ...bundle.formMessages, [key]: state[key] } };
41
- }, {
42
- formMessages: {},
43
- fieldMessages: {}
44
- });
45
- const alteredAction = async (fd) => {
46
- if (!onlyDirty) return formAction(fd);
47
- const form = formRef.current;
48
- if (!form) return formAction(fd);
49
- return formAction(getDirtyFormData(form, defaultValues));
50
- };
51
- return /* @__PURE__ */ jsxs(
52
- Form,
53
- {
54
- ...otherProps,
55
- ref: formRef,
56
- action: alteredAction,
57
- errors: fieldMessages,
58
- children: [
59
- Object.entries(formMessages).map(([messageKey, message]) => /* @__PURE__ */ jsx(
60
- JustDiv,
61
- {
62
- "data-message-type": messageKey.startsWith("[") ? messageKey.replace(/^\[|\]$/g, "") : "default",
63
- render: renderMessages,
64
- children: messageTranslations[message] || message
65
- },
66
- messageKey
67
- )),
68
- /* @__PURE__ */ jsx(FormContext, { value: { getFieldMessage, messageTranslations, registerField, unregisterField }, children })
69
- ]
70
- }
71
- );
6
+ import { jsx } from "react/jsx-runtime";
7
+ // src/components/form/BaseForm.tsx
8
+ const INITIAL_STATE = {
9
+ result: { status: "idle" },
10
+ submissionId: 0,
11
+ resetCount: 0
72
12
  };
73
- var BaseForm_default = BaseForm;
74
- export {
75
- BaseForm_default as default
13
+ const NO_FIELD_ERRORS = {};
14
+ const formatAsIs = (code) => code;
15
+ const BaseForm = ({ action, formatMessage = formatAsIs, defaultValues = {}, onlyDirty = false, resetOnSuccess = false, onResult, onSubmit, children, ...otherProps }) => {
16
+ const submit = async (previous, formData) => {
17
+ const result = await action(previous.result, formData);
18
+ onResult?.(result);
19
+ return {
20
+ result,
21
+ submissionId: previous.submissionId + 1,
22
+ resetCount: resetOnSuccess && result.status === "success" ? previous.resetCount + 1 : previous.resetCount
23
+ };
24
+ };
25
+ const [{ result: state, submissionId, resetCount }, formAction, pending] = useActionState(submit, INITIAL_STATE);
26
+ const fieldsRef = useRef(/* @__PURE__ */ new Set());
27
+ const registerField = (fieldName) => {
28
+ fieldsRef.current.add(fieldName);
29
+ };
30
+ const unregisterField = (fieldName) => {
31
+ fieldsRef.current.delete(fieldName);
32
+ };
33
+ const fieldErrors = state.status === "failure" ? state.fieldErrors : NO_FIELD_ERRORS;
34
+ const mountedFieldErrors = {};
35
+ const messages = [];
36
+ if (state.status === "failure" && state.message) messages.push({
37
+ key: "message",
38
+ type: state.message.type,
39
+ text: formatMessage(state.message.text)
40
+ });
41
+ for (const [fieldName, errors] of Object.entries(fieldErrors)) {
42
+ const texts = errors.map((error) => formatMessage(error.code, error.params));
43
+ if (fieldsRef.current.has(fieldName)) mountedFieldErrors[fieldName] = texts;
44
+ else texts.forEach((text, index) => {
45
+ messages.push({
46
+ key: `field:${fieldName}:${index}`,
47
+ type: "error",
48
+ text
49
+ });
50
+ });
51
+ }
52
+ return /* @__PURE__ */ jsx(Form, {
53
+ ...otherProps,
54
+ errors: mountedFieldErrors,
55
+ onSubmit: (e) => {
56
+ e.stopPropagation();
57
+ onSubmit?.(e);
58
+ if (e.defaultPrevented) return;
59
+ e.preventDefault();
60
+ const form = e.currentTarget;
61
+ const formData = onlyDirty ? getDirtyFormData(form, defaultValues) : new FormData(form, e.nativeEvent.submitter);
62
+ startTransition(() => formAction(formData));
63
+ },
64
+ children: /* @__PURE__ */ jsx(FormContext, {
65
+ value: {
66
+ fieldErrors,
67
+ messages,
68
+ submissionId,
69
+ formatMessage,
70
+ pending,
71
+ registerField,
72
+ unregisterField
73
+ },
74
+ children: /* @__PURE__ */ jsx(Fragment, { children }, resetCount)
75
+ })
76
+ });
76
77
  };
78
+ export { BaseForm as default };