@omer-x/tailwind-ui-kit 1.0.0-alpha.3 → 1.0.0-alpha.31
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/MIGRATION.md +283 -0
- package/README.md +792 -0
- package/dist/components/button/BaseButton.d.ts +39 -12
- package/dist/components/button/BaseButton.js +57 -51
- package/dist/components/button/index.d.ts +2 -4
- package/dist/components/button/index.js +1 -4
- package/dist/components/dialog/BaseAlertDialog.d.ts +49 -0
- package/dist/components/dialog/BaseAlertDialog.js +27 -0
- package/dist/components/dialog/BaseAlertDialogTrigger.d.ts +16 -0
- package/dist/components/dialog/BaseAlertDialogTrigger.js +11 -0
- package/dist/components/dialog/BaseDialog.d.ts +58 -0
- package/dist/components/dialog/BaseDialog.js +24 -0
- package/dist/components/dialog/BaseDialogClose.d.ts +13 -0
- package/dist/components/dialog/BaseDialogClose.js +7 -0
- package/dist/components/dialog/BaseDialogDescription.d.ts +8 -0
- package/dist/components/dialog/BaseDialogDescription.js +7 -0
- package/dist/components/dialog/BaseDialogPopup.d.ts +41 -0
- package/dist/components/dialog/BaseDialogPopup.js +42 -0
- package/dist/components/dialog/BaseDialogTitle.d.ts +8 -0
- package/dist/components/dialog/BaseDialogTitle.js +11 -0
- package/dist/components/dialog/BaseDialogTrigger.d.ts +20 -0
- package/dist/components/dialog/BaseDialogTrigger.js +11 -0
- package/dist/components/dialog/createAlertDialogHandle.d.ts +14 -0
- package/dist/components/dialog/createAlertDialogHandle.js +11 -0
- package/dist/components/dialog/createDialogHandle.d.ts +36 -0
- package/dist/components/dialog/createDialogHandle.js +31 -0
- package/dist/components/dialog/index.d.ts +8 -5
- package/dist/components/dialog/index.js +5 -8
- package/dist/components/disclosure/BaseAccordion.d.ts +48 -0
- package/dist/components/disclosure/BaseAccordion.js +18 -0
- package/dist/components/disclosure/BaseAccordionItem.d.ts +20 -0
- package/dist/components/disclosure/BaseAccordionItem.js +10 -0
- package/dist/components/disclosure/BaseAccordionPanel.d.ts +20 -0
- package/dist/components/disclosure/BaseAccordionPanel.js +15 -0
- package/dist/components/disclosure/BaseAccordionTrigger.d.ts +22 -0
- package/dist/components/disclosure/BaseAccordionTrigger.js +23 -0
- package/dist/components/disclosure/index.d.ts +4 -0
- package/dist/components/disclosure/index.js +1 -0
- package/dist/components/disclosure/panelTransition.d.ts +7 -0
- package/dist/components/disclosure/panelTransition.js +9 -0
- package/dist/components/display/BaseAvatar.d.ts +24 -0
- package/dist/components/display/BaseAvatar.js +35 -0
- package/dist/components/display/BaseAvatarFallback.d.ts +14 -0
- package/dist/components/display/BaseAvatarFallback.js +14 -0
- package/dist/components/display/BaseAvatarImage.d.ts +23 -0
- package/dist/components/display/BaseAvatarImage.js +14 -0
- package/dist/components/display/index.d.ts +3 -0
- package/dist/components/display/index.js +1 -0
- package/dist/components/elements/JustAnchor.d.ts +7 -0
- package/dist/components/elements/JustAnchor.js +11 -0
- package/dist/components/elements/JustButton.d.ts +7 -0
- package/dist/components/elements/JustButton.js +11 -0
- package/dist/components/elements/JustDiv.d.ts +7 -0
- package/dist/components/elements/JustDiv.js +11 -0
- package/dist/components/elements/JustInput.d.ts +7 -0
- package/dist/components/elements/JustInput.js +11 -0
- package/dist/components/elements/JustSpan.d.ts +7 -0
- package/dist/components/elements/JustSpan.js +11 -0
- package/dist/components/elements/index.d.ts +6 -0
- package/dist/components/elements/index.js +6 -0
- package/dist/components/form/BaseCheckbox.d.ts +44 -0
- package/dist/components/form/BaseCheckbox.js +24 -0
- package/dist/components/form/BaseForm.d.ts +13 -0
- package/dist/components/form/BaseForm.js +78 -0
- package/dist/components/form/BaseFormField.d.ts +24 -0
- package/dist/components/form/BaseFormField.js +33 -0
- package/dist/components/form/BaseInput.d.ts +7 -0
- package/dist/components/form/BaseInput.js +22 -0
- package/dist/components/form/BaseSwitch.d.ts +35 -0
- package/dist/components/form/BaseSwitch.js +21 -0
- package/dist/components/form/ClientSideError.d.ts +9 -0
- package/dist/components/form/ClientSideError.js +39 -0
- package/dist/components/form/FieldRoot.d.ts +8 -0
- package/dist/components/form/FieldRoot.js +21 -0
- package/dist/components/form/FormContext.d.ts +28 -0
- package/dist/components/form/FormContext.js +12 -0
- package/dist/components/form/ServerSideError.d.ts +8 -0
- package/dist/components/form/ServerSideError.js +15 -0
- package/dist/components/form/index.d.ts +8 -0
- package/dist/components/form/index.js +7 -0
- package/dist/components/form/useFormMessages.d.ts +10 -0
- package/dist/components/form/useFormMessages.js +16 -0
- package/dist/components/layout/Container.d.ts +4 -10
- package/dist/components/layout/Container.js +9 -10
- package/dist/components/layout/index.d.ts +2 -3
- package/dist/components/layout/index.js +1 -4
- package/dist/components/page/Pagination.d.ts +27 -0
- package/dist/components/page/Pagination.js +68 -0
- package/dist/components/page/index.d.ts +3 -0
- package/dist/components/page/index.js +3 -0
- package/dist/components/page/usePagination.d.ts +30 -0
- package/dist/components/page/usePagination.js +41 -0
- package/dist/components/table/TableCell.d.ts +13 -0
- package/dist/components/table/TableCell.js +17 -0
- package/dist/components/table/Tdx.d.ts +6 -10
- package/dist/components/table/Tdx.js +11 -9
- package/dist/components/table/index.d.ts +3 -3
- package/dist/components/table/index.js +3 -4
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useRipples.d.ts +2 -5
- package/dist/hooks/useRipples.js +44 -32
- package/dist/utils/cn.d.ts +6 -5
- package/dist/utils/cn.js +8 -6
- package/dist/utils/dirty-form.d.ts +7 -0
- package/dist/utils/dirty-form.js +40 -0
- package/dist/utils/index.d.ts +3 -2
- package/dist/utils/index.js +2 -3
- package/dist/utils/render.d.ts +31 -0
- package/dist/utils/render.js +24 -0
- package/package.json +60 -26
- package/typescript-badge.d.ts +4 -0
- package/dist/components/button/PulseButton.d.ts +0 -10
- package/dist/components/button/PulseButton.js +0 -24
- package/dist/components/dialog/AnimatedPopup.d.ts +0 -6
- package/dist/components/dialog/AnimatedPopup.js +0 -34
- package/dist/components/dialog/DialogBackdrop.d.ts +0 -6
- package/dist/components/dialog/DialogBackdrop.js +0 -26
- package/dist/components/dialog/DialogTitle.d.ts +0 -6
- package/dist/components/dialog/DialogTitle.js +0 -19
- package/dist/styles/ripple.module.css +0 -10
- package/dist/styles/ripple.module.d.ts +0 -2
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Form } from "@base-ui/react/form";
|
|
3
|
+
import { Fragment, startTransition, useActionState, useRef } from "react";
|
|
4
|
+
import { getDirtyFormData } from "../../utils/dirty-form.js";
|
|
5
|
+
import { FormContext } from "./FormContext.js";
|
|
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
|
|
12
|
+
};
|
|
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
|
+
});
|
|
77
|
+
};
|
|
78
|
+
export { BaseForm as default };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RenderProp } from "../../utils/render.js";
|
|
2
|
+
import { ZodType } from "zod";
|
|
3
|
+
export type FieldState = {
|
|
4
|
+
disabled: boolean;
|
|
5
|
+
/** The control has lost focus at least once. */
|
|
6
|
+
touched: boolean;
|
|
7
|
+
/** The value differs from the initial one. */
|
|
8
|
+
dirty: boolean;
|
|
9
|
+
/** `null` until the field is validated. */
|
|
10
|
+
valid: boolean | null;
|
|
11
|
+
filled: boolean;
|
|
12
|
+
focused: boolean;
|
|
13
|
+
};
|
|
14
|
+
type BaseFormFieldProps = {
|
|
15
|
+
schema: ZodType;
|
|
16
|
+
name: string;
|
|
17
|
+
label?: string;
|
|
18
|
+
control?: RenderProp<"input", FieldState>;
|
|
19
|
+
renderLabel?: RenderProp<"label", FieldState>;
|
|
20
|
+
renderErrors?: RenderProp<"div", FieldState>;
|
|
21
|
+
className?: string;
|
|
22
|
+
};
|
|
23
|
+
declare const BaseFormField: ({ schema, name, label, control, renderLabel, renderErrors, className }: BaseFormFieldProps) => import("react").JSX.Element;
|
|
24
|
+
export { BaseFormField as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Field } from "@base-ui/react/field";
|
|
3
|
+
import { getFieldConstraints } from "@omer-x/zod-to-form-constraints";
|
|
4
|
+
import { cn } from "cn";
|
|
5
|
+
import ClientSideError from "./ClientSideError.js";
|
|
6
|
+
import FieldRoot from "./FieldRoot.js";
|
|
7
|
+
import ServerSideError from "./ServerSideError.js";
|
|
8
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/components/form/BaseFormField.tsx
|
|
10
|
+
const BaseFormField = ({ schema, name, label, control, renderLabel, renderErrors, className }) => /* @__PURE__ */ jsxs(FieldRoot, {
|
|
11
|
+
name,
|
|
12
|
+
className: cn("flex w-full flex-col items-start gap-1", className),
|
|
13
|
+
children: [
|
|
14
|
+
typeof label !== "undefined" && /* @__PURE__ */ jsx(Field.Label, {
|
|
15
|
+
render: renderLabel,
|
|
16
|
+
children: label
|
|
17
|
+
}),
|
|
18
|
+
/* @__PURE__ */ jsx(Field.Control, {
|
|
19
|
+
name,
|
|
20
|
+
render: control,
|
|
21
|
+
...getFieldConstraints(schema)
|
|
22
|
+
}),
|
|
23
|
+
/* @__PURE__ */ jsx(ClientSideError, {
|
|
24
|
+
schema,
|
|
25
|
+
render: renderErrors
|
|
26
|
+
}),
|
|
27
|
+
/* @__PURE__ */ jsx(ServerSideError, {
|
|
28
|
+
name,
|
|
29
|
+
render: renderErrors
|
|
30
|
+
})
|
|
31
|
+
]
|
|
32
|
+
});
|
|
33
|
+
export { BaseFormField as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ComponentProps } from "react";
|
|
2
|
+
import JustInput from "../elements/JustInput.js";
|
|
3
|
+
type BaseInputProps = Omit<ComponentProps<typeof JustInput>, "size"> & {
|
|
4
|
+
size?: "lg" | "md" | "sm" | "xs";
|
|
5
|
+
};
|
|
6
|
+
declare const BaseInput: ({ size, className, ...otherProps }: BaseInputProps) => import("react").JSX.Element;
|
|
7
|
+
export { BaseInput as default };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "cn";
|
|
3
|
+
import "react";
|
|
4
|
+
import JustInput from "../elements/JustInput.js";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
// src/components/form/BaseInput.tsx
|
|
7
|
+
const BaseInput = ({ size = "md", className, ...otherProps }) => /* @__PURE__ */ jsx(JustInput, {
|
|
8
|
+
...otherProps,
|
|
9
|
+
className: cn({
|
|
10
|
+
"h-12 px-5 text-base": size === "lg",
|
|
11
|
+
"h-9 px-3 text-sm": size === "md",
|
|
12
|
+
"h-8 px-2.5 text-sm": size === "sm",
|
|
13
|
+
"h-7 px-2 text-xs": size === "xs"
|
|
14
|
+
}, [
|
|
15
|
+
"rounded-md",
|
|
16
|
+
"outline-2",
|
|
17
|
+
"outline-offset-2",
|
|
18
|
+
"outline-transparent",
|
|
19
|
+
"transition-colors"
|
|
20
|
+
], className)
|
|
21
|
+
});
|
|
22
|
+
export { BaseInput as default };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ComponentProps, ReactNode, Ref } from "react";
|
|
2
|
+
import { RenderProp } from "../../utils/render.js";
|
|
3
|
+
export type SwitchState = {
|
|
4
|
+
checked: boolean;
|
|
5
|
+
disabled: boolean;
|
|
6
|
+
readOnly: boolean;
|
|
7
|
+
required: boolean;
|
|
8
|
+
};
|
|
9
|
+
type BaseSwitchProps = Omit<ComponentProps<"span">, "onChange" | "defaultChecked" | "defaultValue"> & {
|
|
10
|
+
label: ReactNode;
|
|
11
|
+
render?: RenderProp<"span", SwitchState>;
|
|
12
|
+
/** Classes for the `<label>` around the switch and its text. */
|
|
13
|
+
labelClassName?: string;
|
|
14
|
+
renderThumb?: RenderProp<"span", SwitchState>;
|
|
15
|
+
name?: string;
|
|
16
|
+
/** Submitted when checked. */
|
|
17
|
+
value?: string;
|
|
18
|
+
/** Submitted when unchecked. */
|
|
19
|
+
uncheckedValue?: string;
|
|
20
|
+
checked?: boolean;
|
|
21
|
+
defaultChecked?: boolean;
|
|
22
|
+
/** Called with the new state and the DOM event that changed it. */
|
|
23
|
+
onCheckedChange?: (checked: boolean, event: Event) => void;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
readOnly?: boolean;
|
|
26
|
+
required?: boolean;
|
|
27
|
+
/** The `id` of a `<form>` to submit with, when the switch isn't inside it. */
|
|
28
|
+
form?: string;
|
|
29
|
+
/** A ref to the hidden `<input>` that holds the value. */
|
|
30
|
+
inputRef?: Ref<HTMLInputElement>;
|
|
31
|
+
/** Set to `true` when `render` renders a `<button>`. */
|
|
32
|
+
nativeButton?: boolean;
|
|
33
|
+
};
|
|
34
|
+
declare const BaseSwitch: ({ label, labelClassName, renderThumb, onCheckedChange, value, uncheckedValue, className, ...otherProps }: BaseSwitchProps) => import("react").JSX.Element;
|
|
35
|
+
export { BaseSwitch as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Switch } from "@base-ui/react/switch";
|
|
3
|
+
import { cn } from "cn";
|
|
4
|
+
import "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/components/form/BaseSwitch.tsx
|
|
7
|
+
const BaseSwitch = ({ label, labelClassName, renderThumb, onCheckedChange, value = "true", uncheckedValue = "false", className, ...otherProps }) => /* @__PURE__ */ jsxs("label", {
|
|
8
|
+
className: cn("flex items-center gap-2 text-sm font-normal", labelClassName),
|
|
9
|
+
children: [/* @__PURE__ */ jsx(Switch.Root, {
|
|
10
|
+
...otherProps,
|
|
11
|
+
value,
|
|
12
|
+
uncheckedValue,
|
|
13
|
+
onCheckedChange: onCheckedChange && ((checked, details) => onCheckedChange(checked, details.event)),
|
|
14
|
+
className: cn("flex", "h-5", "w-9", "shrink-0", "p-0.5", "transition-colors", "duration-150", "ease-[ease]", "focus-visible:outline-2", "focus-visible:outline-offset-2", className),
|
|
15
|
+
children: /* @__PURE__ */ jsx(Switch.Thumb, {
|
|
16
|
+
className: cn("size-3.5", "transition-[translate,background-color]", "duration-150", "ease-[ease]", "data-checked:translate-x-4"),
|
|
17
|
+
render: renderThumb
|
|
18
|
+
})
|
|
19
|
+
}), label]
|
|
20
|
+
});
|
|
21
|
+
export { BaseSwitch as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FieldState } from "./BaseFormField.js";
|
|
2
|
+
import { RenderProp } from "../../utils/render.js";
|
|
3
|
+
import { ZodType } from "zod";
|
|
4
|
+
type ClientSideErrorProps = {
|
|
5
|
+
schema: ZodType;
|
|
6
|
+
render?: RenderProp<"div", FieldState>;
|
|
7
|
+
};
|
|
8
|
+
declare const ClientSideError: ({ schema, render }: ClientSideErrorProps) => import("react").JSX.Element[];
|
|
9
|
+
export { ClientSideError as default };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Field } from "@base-ui/react/field";
|
|
3
|
+
import { getErrorMessagePayload } from "@omer-x/zod-to-form-constraints";
|
|
4
|
+
import { use } from "react";
|
|
5
|
+
import { FormContext } from "./FormContext.js";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
// src/components/form/ClientSideError.tsx
|
|
8
|
+
const VALIDITY_STATES = [
|
|
9
|
+
"valueMissing",
|
|
10
|
+
"typeMismatch",
|
|
11
|
+
"patternMismatch",
|
|
12
|
+
"tooShort",
|
|
13
|
+
"tooLong",
|
|
14
|
+
"rangeUnderflow",
|
|
15
|
+
"rangeOverflow",
|
|
16
|
+
"stepMismatch",
|
|
17
|
+
"badInput"
|
|
18
|
+
];
|
|
19
|
+
const PARAM_NAMES = {
|
|
20
|
+
tooShort: "minimum",
|
|
21
|
+
rangeUnderflow: "minimum",
|
|
22
|
+
tooLong: "maximum",
|
|
23
|
+
rangeOverflow: "maximum"
|
|
24
|
+
};
|
|
25
|
+
const FormattedError = ({ schema, state }) => {
|
|
26
|
+
const { formatMessage } = use(FormContext);
|
|
27
|
+
const paramName = PARAM_NAMES[state];
|
|
28
|
+
if (!paramName) return formatMessage(state);
|
|
29
|
+
return formatMessage(state, { [paramName]: getErrorMessagePayload(schema, state) });
|
|
30
|
+
};
|
|
31
|
+
const ClientSideError = ({ schema, render }) => VALIDITY_STATES.map((state) => /* @__PURE__ */ jsx(Field.Error, {
|
|
32
|
+
match: state,
|
|
33
|
+
render,
|
|
34
|
+
children: /* @__PURE__ */ jsx(FormattedError, {
|
|
35
|
+
schema,
|
|
36
|
+
state
|
|
37
|
+
})
|
|
38
|
+
}, state));
|
|
39
|
+
export { ClientSideError as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
type FieldRootProps = {
|
|
3
|
+
name: string;
|
|
4
|
+
className?: string;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
};
|
|
7
|
+
declare const FieldRoot: ({ name, className, children }: FieldRootProps) => import("react").JSX.Element;
|
|
8
|
+
export { FieldRoot as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Field } from "@base-ui/react/field";
|
|
3
|
+
import { use, useEffect } from "react";
|
|
4
|
+
import { FormContext } from "./FormContext.js";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
// src/components/form/FieldRoot.tsx
|
|
7
|
+
const FieldRoot = ({ name, className, children }) => {
|
|
8
|
+
const { registerField, unregisterField } = use(FormContext);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
registerField(name);
|
|
11
|
+
return () => {
|
|
12
|
+
unregisterField(name);
|
|
13
|
+
};
|
|
14
|
+
}, [name]);
|
|
15
|
+
return /* @__PURE__ */ jsx(Field.Root, {
|
|
16
|
+
name,
|
|
17
|
+
className,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export { FieldRoot as default };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { FieldError, FormMessage } from "@omer-x/next-form-actions";
|
|
2
|
+
/**
|
|
3
|
+
* Turns an error code (a server error code, a browser validity state or a form message text)
|
|
4
|
+
* and its params into display text.
|
|
5
|
+
*/
|
|
6
|
+
export type MessageFormatter = (code: string, params?: Record<string, unknown>) => string;
|
|
7
|
+
/**
|
|
8
|
+
* A form-level message of the latest result, already formatted.
|
|
9
|
+
*/
|
|
10
|
+
export type DisplayedFormMessage = {
|
|
11
|
+
/**
|
|
12
|
+
* Stable within one result, for use as a React key.
|
|
13
|
+
*/
|
|
14
|
+
key: string;
|
|
15
|
+
type: FormMessage["type"];
|
|
16
|
+
text: string;
|
|
17
|
+
};
|
|
18
|
+
interface FormContextInterface {
|
|
19
|
+
fieldErrors: Record<string, FieldError[]>;
|
|
20
|
+
messages: DisplayedFormMessage[];
|
|
21
|
+
submissionId: number;
|
|
22
|
+
formatMessage: MessageFormatter;
|
|
23
|
+
pending: boolean;
|
|
24
|
+
registerField: (fieldName: string) => void;
|
|
25
|
+
unregisterField: (fieldName: string) => void;
|
|
26
|
+
}
|
|
27
|
+
export declare const FormContext: import("react").Context<FormContextInterface>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
// src/components/form/FormContext.ts
|
|
3
|
+
const FormContext = createContext({
|
|
4
|
+
fieldErrors: {},
|
|
5
|
+
messages: [],
|
|
6
|
+
submissionId: 0,
|
|
7
|
+
formatMessage: (code) => code,
|
|
8
|
+
pending: false,
|
|
9
|
+
registerField: () => null,
|
|
10
|
+
unregisterField: () => null
|
|
11
|
+
});
|
|
12
|
+
export { FormContext };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FieldState } from "./BaseFormField.js";
|
|
2
|
+
import { RenderProp } from "../../utils/render.js";
|
|
3
|
+
type ServerSideErrorProps = {
|
|
4
|
+
name: string;
|
|
5
|
+
render?: RenderProp<"div", FieldState>;
|
|
6
|
+
};
|
|
7
|
+
declare const ServerSideError: ({ name, render }: ServerSideErrorProps) => import("react").JSX.Element[];
|
|
8
|
+
export { ServerSideError as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Field } from "@base-ui/react/field";
|
|
3
|
+
import { use } from "react";
|
|
4
|
+
import { FormContext } from "./FormContext.js";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
// src/components/form/ServerSideError.tsx
|
|
7
|
+
const ServerSideError = ({ name, render }) => {
|
|
8
|
+
const { fieldErrors, formatMessage } = use(FormContext);
|
|
9
|
+
return (Object.hasOwn(fieldErrors, name) ? fieldErrors[name] : []).map((error, index) => /* @__PURE__ */ jsx(Field.Error, {
|
|
10
|
+
match: true,
|
|
11
|
+
render,
|
|
12
|
+
children: formatMessage(error.code, error.params)
|
|
13
|
+
}, `${index}:${error.code}`));
|
|
14
|
+
};
|
|
15
|
+
export { ServerSideError as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import BaseInput from "./BaseInput.js";
|
|
2
|
+
import BaseSwitch, { SwitchState } from "./BaseSwitch.js";
|
|
3
|
+
import BaseCheckbox, { CheckboxState } from "./BaseCheckbox.js";
|
|
4
|
+
import BaseForm from "./BaseForm.js";
|
|
5
|
+
import BaseFormField, { FieldState } from "./BaseFormField.js";
|
|
6
|
+
import useFormMessages from "./useFormMessages.js";
|
|
7
|
+
import { DisplayedFormMessage, MessageFormatter } from "./FormContext.js";
|
|
8
|
+
export { BaseCheckbox, BaseForm, BaseFormField, BaseInput, BaseSwitch, type CheckboxState, type DisplayedFormMessage, type FieldState, type MessageFormatter, type SwitchState, useFormMessages };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import BaseInput from "./BaseInput.js";
|
|
2
|
+
import BaseSwitch from "./BaseSwitch.js";
|
|
3
|
+
import BaseCheckbox from "./BaseCheckbox.js";
|
|
4
|
+
import BaseForm from "./BaseForm.js";
|
|
5
|
+
import BaseFormField from "./BaseFormField.js";
|
|
6
|
+
import useFormMessages from "./useFormMessages.js";
|
|
7
|
+
export { BaseCheckbox, BaseForm, BaseFormField, BaseInput, BaseSwitch, useFormMessages };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DisplayedFormMessage } from "./FormContext.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the form-level messages of the latest result of the surrounding `BaseForm`:
|
|
4
|
+
* the action's message, plus field errors that have no field in the form.
|
|
5
|
+
* `submissionId` changes once per completed submission, e.g. to fire toasts only once.
|
|
6
|
+
*/
|
|
7
|
+
export default function useFormMessages(): {
|
|
8
|
+
messages: DisplayedFormMessage[];
|
|
9
|
+
submissionId: number;
|
|
10
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { use } from "react";
|
|
2
|
+
import { FormContext } from "./FormContext.js";
|
|
3
|
+
// src/components/form/useFormMessages.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns the form-level messages of the latest result of the surrounding `BaseForm`:
|
|
6
|
+
* the action's message, plus field errors that have no field in the form.
|
|
7
|
+
* `submissionId` changes once per completed submission, e.g. to fire toasts only once.
|
|
8
|
+
*/
|
|
9
|
+
function useFormMessages() {
|
|
10
|
+
const { messages, submissionId } = use(FormContext);
|
|
11
|
+
return {
|
|
12
|
+
messages,
|
|
13
|
+
submissionId
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { useFormMessages as default };
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
className?: string;
|
|
6
|
-
children: ReactNode;
|
|
7
|
-
};
|
|
8
|
-
declare const Container: ({ className, children, }: ContainerProps) => react_jsx_runtime.JSX.Element;
|
|
9
|
-
|
|
10
|
-
export { Container as default };
|
|
1
|
+
import { ComponentProps } from "react";
|
|
2
|
+
type ContainerProps = ComponentProps<"div">;
|
|
3
|
+
declare const Container: ({ className, ...props }: ContainerProps) => import("react").JSX.Element;
|
|
4
|
+
export { Container as default };
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "cn";
|
|
3
|
+
import "react";
|
|
1
4
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var Container_default = Container;
|
|
9
|
-
export {
|
|
10
|
-
Container_default as default
|
|
11
|
-
};
|
|
5
|
+
// src/components/layout/Container.tsx
|
|
6
|
+
const Container = ({ className, ...props }) => /* @__PURE__ */ jsx("div", {
|
|
7
|
+
className: cn("container mx-auto px-3", className),
|
|
8
|
+
...props
|
|
9
|
+
});
|
|
10
|
+
export { Container as default };
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import 'react';
|
|
1
|
+
import Container from "./Container.js";
|
|
2
|
+
export { Container };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ComponentProps, ReactElement, ReactNode } from "react";
|
|
2
|
+
import JustButton from "../elements/JustButton.js";
|
|
3
|
+
import JustSpan from "../elements/JustSpan.js";
|
|
4
|
+
import { PaginationOptions } from "./usePagination.js";
|
|
5
|
+
export type PaginationLinkProps = {
|
|
6
|
+
"children": ReactNode;
|
|
7
|
+
"aria-label"?: string;
|
|
8
|
+
"aria-current"?: "page";
|
|
9
|
+
};
|
|
10
|
+
type PaginationProps = Omit<ComponentProps<"nav">, "children"> & PaginationOptions & {
|
|
11
|
+
/** Builds the link to a page. Spread `props` onto it. It can also be a `<button onClick>` */
|
|
12
|
+
renderLink: (page: number, props: PaginationLinkProps) => ReactElement;
|
|
13
|
+
/** Renders a disabled arrow (a disabled `<button>`) */
|
|
14
|
+
renderButton?: ComponentProps<typeof JustButton>["render"];
|
|
15
|
+
/** Renders an ellipsis (a `<span>`) */
|
|
16
|
+
renderEllipsis?: ComponentProps<typeof JustSpan>["render"];
|
|
17
|
+
/** The previous arrow's accessible name. */
|
|
18
|
+
previousLabel?: string;
|
|
19
|
+
/** The next arrow's accessible name. */
|
|
20
|
+
nextLabel?: string;
|
|
21
|
+
/** The previous arrow's content, e.g. an icon. Default `«` */
|
|
22
|
+
previousContent?: ReactNode;
|
|
23
|
+
/** The next arrow's content, e.g. an icon. Default `»` */
|
|
24
|
+
nextContent?: ReactNode;
|
|
25
|
+
};
|
|
26
|
+
declare const Pagination: ({ totalItems, itemsPerPage, currentPage, siblings, renderLink, renderButton, renderEllipsis, previousLabel, nextLabel, previousContent, nextContent, className, ...otherProps }: PaginationProps) => import("react").JSX.Element;
|
|
27
|
+
export { Pagination as default };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { cn } from "cn";
|
|
3
|
+
import "react";
|
|
4
|
+
import JustButton from "../elements/JustButton.js";
|
|
5
|
+
import JustSpan from "../elements/JustSpan.js";
|
|
6
|
+
import usePagination from "./usePagination.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
// src/components/page/Pagination.tsx
|
|
9
|
+
const PaginationItemView = ({ item, renderLink, renderButton, renderEllipsis, previousLabel, nextLabel, previousContent, nextContent }) => {
|
|
10
|
+
switch (item.type) {
|
|
11
|
+
case "ellipsis": return /* @__PURE__ */ jsx(JustSpan, {
|
|
12
|
+
render: renderEllipsis,
|
|
13
|
+
"aria-hidden": true,
|
|
14
|
+
children: "…"
|
|
15
|
+
});
|
|
16
|
+
case "page": return renderLink(item.page, {
|
|
17
|
+
"children": item.page,
|
|
18
|
+
"aria-current": item.current ? "page" : void 0
|
|
19
|
+
});
|
|
20
|
+
case "previous":
|
|
21
|
+
case "next": {
|
|
22
|
+
const label = item.type === "previous" ? previousLabel : nextLabel;
|
|
23
|
+
const children = item.type === "previous" ? previousContent : nextContent;
|
|
24
|
+
if (item.disabled) return /* @__PURE__ */ jsx(JustButton, {
|
|
25
|
+
render: renderButton,
|
|
26
|
+
type: "button",
|
|
27
|
+
"aria-label": label,
|
|
28
|
+
disabled: true,
|
|
29
|
+
children
|
|
30
|
+
});
|
|
31
|
+
return renderLink(item.page, {
|
|
32
|
+
children,
|
|
33
|
+
"aria-label": label
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const itemKey = (item, index) => {
|
|
39
|
+
switch (item.type) {
|
|
40
|
+
case "page": return `page-${item.page}`;
|
|
41
|
+
case "ellipsis": return `ellipsis-${index}`;
|
|
42
|
+
default: return item.type;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const Pagination = ({ totalItems, itemsPerPage, currentPage, siblings, renderLink, renderButton, renderEllipsis, previousLabel = "Previous page", nextLabel = "Next page", previousContent = "«", nextContent = "»", className, ...otherProps }) => {
|
|
46
|
+
const items = usePagination({
|
|
47
|
+
totalItems,
|
|
48
|
+
itemsPerPage,
|
|
49
|
+
currentPage,
|
|
50
|
+
siblings
|
|
51
|
+
});
|
|
52
|
+
return /* @__PURE__ */ jsx("nav", {
|
|
53
|
+
"aria-label": "Pagination",
|
|
54
|
+
...otherProps,
|
|
55
|
+
className: cn("flex gap-3", className),
|
|
56
|
+
children: items.map((item, index) => /* @__PURE__ */ jsx(PaginationItemView, {
|
|
57
|
+
item,
|
|
58
|
+
renderLink,
|
|
59
|
+
renderButton,
|
|
60
|
+
renderEllipsis,
|
|
61
|
+
previousLabel,
|
|
62
|
+
nextLabel,
|
|
63
|
+
previousContent,
|
|
64
|
+
nextContent
|
|
65
|
+
}, itemKey(item, index)))
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
export { Pagination as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type PaginationOptions = {
|
|
2
|
+
totalItems: number;
|
|
3
|
+
itemsPerPage: number;
|
|
4
|
+
currentPage: number;
|
|
5
|
+
/** Pages shown on each side of the current one (default 2) */
|
|
6
|
+
siblings?: number;
|
|
7
|
+
};
|
|
8
|
+
type ArrowItem = {
|
|
9
|
+
type: "previous" | "next";
|
|
10
|
+
page: number;
|
|
11
|
+
disabled: boolean;
|
|
12
|
+
};
|
|
13
|
+
type PageItem = {
|
|
14
|
+
type: "page";
|
|
15
|
+
page: number;
|
|
16
|
+
current: boolean;
|
|
17
|
+
};
|
|
18
|
+
type EllipsisItem = {
|
|
19
|
+
type: "ellipsis";
|
|
20
|
+
};
|
|
21
|
+
export type PaginationItem = ArrowItem | PageItem | EllipsisItem;
|
|
22
|
+
/**
|
|
23
|
+
* Decides which pages to show: previous/next arrows, the first and last pages,
|
|
24
|
+
* `siblings` pages on each side of the current one, and ellipses for gaps.
|
|
25
|
+
* A gap of a single page shows that page instead of an ellipsis.
|
|
26
|
+
*
|
|
27
|
+
* It holds no state, so it also works in server components.
|
|
28
|
+
*/
|
|
29
|
+
declare const usePagination: ({ totalItems, itemsPerPage, currentPage, siblings }: PaginationOptions) => PaginationItem[];
|
|
30
|
+
export { usePagination as default };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/components/page/usePagination.ts
|
|
2
|
+
/**
|
|
3
|
+
* Decides which pages to show: previous/next arrows, the first and last pages,
|
|
4
|
+
* `siblings` pages on each side of the current one, and ellipses for gaps.
|
|
5
|
+
* A gap of a single page shows that page instead of an ellipsis.
|
|
6
|
+
*
|
|
7
|
+
* It holds no state, so it also works in server components.
|
|
8
|
+
*/
|
|
9
|
+
const usePagination = ({ totalItems, itemsPerPage, currentPage, siblings = 2 }) => {
|
|
10
|
+
const totalPages = Math.max(1, Math.ceil(totalItems / itemsPerPage));
|
|
11
|
+
const start = Math.max(1, currentPage - siblings);
|
|
12
|
+
const end = Math.min(totalPages, currentPage + siblings);
|
|
13
|
+
const page = (n) => ({
|
|
14
|
+
type: "page",
|
|
15
|
+
page: n,
|
|
16
|
+
current: n === currentPage
|
|
17
|
+
});
|
|
18
|
+
const items = [{
|
|
19
|
+
type: "previous",
|
|
20
|
+
page: currentPage - 1,
|
|
21
|
+
disabled: currentPage <= 1
|
|
22
|
+
}];
|
|
23
|
+
if (start > 1) {
|
|
24
|
+
items.push(page(1));
|
|
25
|
+
if (start === 3) items.push(page(2));
|
|
26
|
+
if (start > 3) items.push({ type: "ellipsis" });
|
|
27
|
+
}
|
|
28
|
+
for (let n = start; n <= end; n++) items.push(page(n));
|
|
29
|
+
if (end < totalPages) {
|
|
30
|
+
if (end < totalPages - 2) items.push({ type: "ellipsis" });
|
|
31
|
+
if (end === totalPages - 2) items.push(page(totalPages - 1));
|
|
32
|
+
items.push(page(totalPages));
|
|
33
|
+
}
|
|
34
|
+
items.push({
|
|
35
|
+
type: "next",
|
|
36
|
+
page: currentPage + 1,
|
|
37
|
+
disabled: currentPage >= totalPages
|
|
38
|
+
});
|
|
39
|
+
return items;
|
|
40
|
+
};
|
|
41
|
+
export { usePagination as default };
|