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

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 CHANGED
@@ -47,6 +47,35 @@ Remove any `pending={isSubmitting}` you wired up by hand for submit buttons of `
47
47
 
48
48
  `iconOnly` makes the button square at every size, and the types require an `aria-label`. While pending, the spinner replaces the icon.
49
49
 
50
+ ## Page
51
+
52
+ ### `Pagination`: the app builds the links (mechanical, #16)
53
+
54
+ `queryParams` is removed, and `renderLink` is now a function of the page number. The kit no longer builds URLs, so keep your query parameters and choose the page parameter yourself:
55
+
56
+ ```diff
57
+ <Pagination
58
+ totalItems={total}
59
+ itemsPerPage={20}
60
+ currentPage={page}
61
+ - queryParams={{ q: search }}
62
+ - renderLink={props => <Link {...props} href={props.href!} className="..." />}
63
+ + renderLink={(page, props) => <Link {...props} href={{ pathname: "/products", query: { q: search, page } }} className="..." />}
64
+ renderButton={<button className="..." />}
65
+ />
66
+ ```
67
+
68
+ **Why:** typed hrefs (e.g. next-intl's typed pathnames) can't be built from a string, and the kit put `undefined` and joined arrays into links.
69
+
70
+ ### `Pagination`: new markup (check manually, #16)
71
+
72
+ - The wrapper is a `<nav aria-label="Pagination">` instead of a `<div>`.
73
+ - The current page goes through `renderLink` with `aria-current="page"`, not through `renderButton` as a disabled button. Style it with `aria-[current=page]:` instead of `disabled:`.
74
+ - Ellipses are a `<span aria-hidden>` from the new `renderEllipsis`, not disabled buttons. `renderButton` only renders disabled arrows now.
75
+ - The arrows have accessible names (`previousLabel` / `nextLabel`).
76
+
77
+ **Why:** screen readers announced a plain group of buttons and links, with no current page and unnamed arrows.
78
+
50
79
  ## Form
51
80
 
52
81
  ### Fields are validated in the browser (check manually, #65)
@@ -162,3 +191,20 @@ If a form should be cleared after a **successful** submit (e.g. "add another"),
162
191
  - <Form key={formKey} action={create} onStateChange={() => setFormKey(k => k + 1)}>
163
192
  + <Form action={create} resetOnSuccess>
164
193
  ```
194
+
195
+ ### `onlyDirty` sends cleared fields as empty values (check manually, #17)
196
+
197
+ With `onlyDirty`, a field that changed to "no value" is now sent with an empty value (`name=""`) instead of being left out: a checkbox that was checked by default and is now unchecked, a multi-select with everything deselected, a cleared text field. `BaseSwitch` sends its `uncheckedValue` as before. Fields are also compared by name now, so:
198
+
199
+ - file inputs send the selected `File` objects (before: the browser's fake path string),
200
+ - multi-selects and checkbox groups send every selected value, and an array default (`tags: ["a", "b"]`) matches them in any order,
201
+ - radio groups are compared with their string default (before: always sent).
202
+
203
+ On the server, treat an empty value as "cleared", and a missing field as "unchanged":
204
+
205
+ ```diff
206
+ - const active = formData.has("active");
207
+ + const active = formData.has("active") ? formData.get("active") === "on" : undefined;
208
+ ```
209
+
210
+ **Why:** the server couldn't tell an unchecked checkbox from an unchanged one, and files, multi-selects and checkbox groups were sent wrong.
package/README.md CHANGED
@@ -164,7 +164,7 @@ npm install @omer-x/next-form-actions@^2.0.0-alpha.1
164
164
  | `onResult` | `(result: FormActionState<Data>) => void` | Called once per completed submission, when the action returns. Never on mount |
165
165
  | `resetOnSuccess` | `boolean` | After a successful result, reset every field inside the form to its default (see below) |
166
166
  | `defaultValues` | `Record<string, unknown>` | Initial values, used by `onlyDirty` |
167
- | `onlyDirty` | `boolean` | Submit only the fields whose value differs from `defaultValues` |
167
+ | `onlyDirty` | `boolean` | Submit only the fields whose value differs from `defaultValues`. Fields are compared by name, so multi-selects and checkbox groups take an array default. A changed field with no value (an unchecked checkbox, a cleared multi-select) is sent as an empty value |
168
168
  | …rest | `<form>` props | Except `action` |
169
169
 
170
170
  **How the result is used.** The action resolves to `{ status: "idle" }`, `{ status: "success", data }` or `{ status: "failure", fieldErrors, message? }`:
@@ -328,30 +328,54 @@ Tailwind's `container`, centered with horizontal padding (`container mx-auto px-
328
328
 
329
329
  #### `Pagination`
330
330
 
331
- Pagination built from links. It shows previous/next arrows, the first and last pages, up to two pages on each side of the current one, and ellipses for gaps. Each link points to `?page=N`, and page 1 has no `page` parameter.
331
+ Pagination inside a `<nav>`. It shows previous/next arrows, the first and last pages, `siblings` pages on each side of the current one, and ellipses for gaps. It only decides which pages to show: **your app builds each link**, so typed routes stay type-safe.
332
332
 
333
333
  | Prop | Type | Description |
334
334
  |------|------|-------------|
335
335
  | `totalItems` | `number` | Total number of items |
336
336
  | `itemsPerPage` | `number` | Items per page |
337
337
  | `currentPage` | `number` | Current page, starting at 1 |
338
- | `queryParams` | `Record<string, unknown>` | Other query parameters to keep in every link (e.g. filters) |
339
- | `renderLink` | render prop | Renders a page link (an `<a>` with `href`). Use it for your router's link component |
340
- | `renderButton` | render prop | Renders the current page, ellipses and disabled arrows (a disabled `<button>`) |
338
+ | `siblings` | `number` | Pages shown on each side of the current one. Default `2`. `0` gives `« 1 … [50] … 100 »` |
339
+ | `renderLink` | `(page: number, props: PaginationLinkProps) => ReactElement` | Builds the link to `page`. Spread `props` (`children`, `aria-current="page"` on the current page, `aria-label` on the arrows) onto it. It can also be a `<button onClick>` for pagination without URLs |
340
+ | `renderButton` | render prop | Renders a disabled arrow (a disabled `<button>`) |
341
+ | `renderEllipsis` | render prop | Renders an ellipsis (a `<span aria-hidden>`) |
342
+ | `aria-label` | `string` | The navigation's label. Default `"Pagination"` |
343
+ | `previousLabel` / `nextLabel` | `string` | The arrows' accessible names. Default `"Previous page"` / `"Next page"` |
344
+ | …rest | `<nav>` props | |
345
+
346
+ Style the current page with `aria-[current=page]:`.
341
347
 
342
348
  ```tsx
343
- import Link from "next/link";
349
+ import { Link } from "@/i18n/navigation";
344
350
 
345
351
  <Pagination
346
352
  totalItems={total}
347
353
  itemsPerPage={20}
348
354
  currentPage={page}
349
- queryParams={{ q: search }}
350
- renderLink={props => <Link {...props} href={props.href!} className="px-3 py-1 hover:underline" />}
351
- renderButton={<button className="px-3 py-1 font-bold disabled:opacity-50" />}
355
+ renderLink={(page, props) => (
356
+ <Link {...props} href={{ pathname: "/products", query: { ...searchParams, page } }} className="px-3 py-1 aria-[current=page]:font-bold" />
357
+ )}
358
+ renderButton={<button className="px-3 py-1 disabled:opacity-50" />}
359
+ renderEllipsis={<span className="px-3 py-1" />}
352
360
  />
353
361
  ```
354
362
 
363
+ #### `usePagination`
364
+
365
+ The logic behind `Pagination`, for a completely custom layout. It holds no state, so it also works in server components.
366
+
367
+ ```tsx
368
+ const items = usePagination({ totalItems, itemsPerPage, currentPage, siblings: 1 });
369
+ // [
370
+ // { type: "previous", page: 9, disabled: false },
371
+ // { type: "page", page: 1, current: false },
372
+ // { type: "ellipsis" },
373
+ // { type: "page", page: 10, current: true },
374
+ // …
375
+ // { type: "next", page: 11, disabled: false },
376
+ // ]
377
+ ```
378
+
355
379
  ### `@omer-x/tailwind-ui-kit/table`
356
380
 
357
381
  #### `Tdx`
@@ -0,0 +1,3 @@
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ declare const JustSpan: ({ render, ...props }: useRender.ComponentProps<"span">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
+ export { JustSpan as default };
@@ -0,0 +1,10 @@
1
+ import { useRender } from "@base-ui/react/use-render";
2
+ // src/components/elements/JustSpan.tsx
3
+ const JustSpan = ({ render, ...props }) => {
4
+ return useRender({
5
+ defaultTagName: "span",
6
+ render,
7
+ props
8
+ });
9
+ };
10
+ export { JustSpan as default };
@@ -1,13 +1,21 @@
1
- import { ComponentProps } from "react";
2
- import JustAnchor from "../elements/JustAnchor.js";
1
+ import { ComponentProps, ReactElement, ReactNode } from "react";
3
2
  import JustButton from "../elements/JustButton.js";
4
- type PaginationProps = {
5
- totalItems: number;
6
- itemsPerPage: number;
7
- currentPage: number;
8
- queryParams: Record<string, unknown>;
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>`) */
9
14
  renderButton?: ComponentProps<typeof JustButton>["render"];
10
- renderLink?: ComponentProps<typeof JustAnchor>["render"];
15
+ /** Renders an ellipsis (a `<span>`) */
16
+ renderEllipsis?: ComponentProps<typeof JustSpan>["render"];
17
+ previousLabel?: string;
18
+ nextLabel?: string;
11
19
  };
12
- declare const Pagination: ({ totalItems, itemsPerPage, currentPage, queryParams, renderButton, renderLink }: PaginationProps) => import("react").JSX.Element;
20
+ declare const Pagination: ({ totalItems, itemsPerPage, currentPage, siblings, renderLink, renderButton, renderEllipsis, previousLabel, nextLabel, className, ...otherProps }: PaginationProps) => import("react").JSX.Element;
13
21
  export { Pagination as default };
@@ -1,63 +1,65 @@
1
+ import { cn } from "cn";
1
2
  import "react";
2
- import JustAnchor from "../elements/JustAnchor.js";
3
3
  import JustButton from "../elements/JustButton.js";
4
- import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
4
+ import JustSpan from "../elements/JustSpan.js";
5
+ import usePagination from "./usePagination.js";
6
+ import { jsx } from "react/jsx-runtime";
5
7
  // src/components/page/Pagination.tsx
6
- const Pagination = ({ totalItems, itemsPerPage, currentPage, queryParams, renderButton, renderLink }) => {
7
- const totalPages = Math.ceil(totalItems / itemsPerPage);
8
- const buildHref = (page) => {
9
- const searchParams = new URLSearchParams();
10
- for (const [key, value] of Object.entries(queryParams)) searchParams.append(key, `${value}`);
11
- if (page === 1) searchParams.delete("page");
12
- else searchParams.set("page", page.toString());
13
- return `?${searchParams}`;
14
- };
15
- const Item = (props) => {
16
- if (props.active || props.disabled || typeof props.page === "undefined") return /* @__PURE__ */ jsx(JustButton, {
17
- render: renderButton,
18
- disabled: true,
19
- children: /* @__PURE__ */ jsx(Fragment$1, { children: props.children || props.page })
8
+ const PaginationItemView = ({ item, renderLink, renderButton, renderEllipsis, previousLabel, nextLabel }) => {
9
+ switch (item.type) {
10
+ case "ellipsis": return /* @__PURE__ */ jsx(JustSpan, {
11
+ render: renderEllipsis,
12
+ "aria-hidden": true,
13
+ children: "…"
20
14
  });
21
- return /* @__PURE__ */ jsx(JustAnchor, {
22
- render: renderLink,
23
- href: buildHref(props.page),
24
- children: /* @__PURE__ */ jsx(Fragment$1, { children: props.children || props.page })
15
+ case "page": return renderLink(item.page, {
16
+ "children": item.page,
17
+ "aria-current": item.current ? "page" : void 0
25
18
  });
26
- };
27
- return /* @__PURE__ */ jsxs("div", {
28
- className: "flex gap-3",
29
- children: [
30
- /* @__PURE__ */ jsx(Item, {
31
- page: currentPage - 1,
32
- disabled: currentPage <= 1,
33
- children: "«"
34
- }),
35
- currentPage > 3 && /* @__PURE__ */ jsx(Item, { page: 1 }),
36
- currentPage === 5 && /* @__PURE__ */ jsx(Item, { page: 2 }),
37
- currentPage > 5 && /* @__PURE__ */ jsx(Item, {
19
+ case "previous":
20
+ case "next": {
21
+ const label = item.type === "previous" ? previousLabel : nextLabel;
22
+ const children = item.type === "previous" ? "«" : "»";
23
+ if (item.disabled) return /* @__PURE__ */ jsx(JustButton, {
24
+ render: renderButton,
25
+ type: "button",
26
+ "aria-label": label,
38
27
  disabled: true,
39
- children: "…"
40
- }),
41
- currentPage > 2 && /* @__PURE__ */ jsx(Item, { page: currentPage - 2 }),
42
- currentPage > 1 && /* @__PURE__ */ jsx(Item, { page: currentPage - 1 }),
43
- /* @__PURE__ */ jsx(Item, {
44
- active: true,
45
- children: currentPage
46
- }),
47
- currentPage <= totalPages - 1 && /* @__PURE__ */ jsx(Item, { page: currentPage + 1 }),
48
- currentPage <= totalPages - 2 && /* @__PURE__ */ jsx(Item, { page: currentPage + 2 }),
49
- currentPage <= totalPages - 5 && /* @__PURE__ */ jsx(Item, {
50
- disabled: true,
51
- children: "…"
52
- }),
53
- currentPage === totalPages - 4 && /* @__PURE__ */ jsx(Item, { page: totalPages - 1 }),
54
- currentPage <= totalPages - 3 && /* @__PURE__ */ jsx(Item, { page: totalPages }),
55
- /* @__PURE__ */ jsx(Item, {
56
- page: currentPage + 1,
57
- disabled: currentPage >= totalPages,
58
- children: "»"
59
- })
60
- ]
28
+ children
29
+ });
30
+ return renderLink(item.page, {
31
+ children,
32
+ "aria-label": label
33
+ });
34
+ }
35
+ }
36
+ };
37
+ const itemKey = (item, index) => {
38
+ switch (item.type) {
39
+ case "page": return `page-${item.page}`;
40
+ case "ellipsis": return `ellipsis-${index}`;
41
+ default: return item.type;
42
+ }
43
+ };
44
+ const Pagination = ({ totalItems, itemsPerPage, currentPage, siblings, renderLink, renderButton, renderEllipsis, previousLabel = "Previous page", nextLabel = "Next page", className, ...otherProps }) => {
45
+ const items = usePagination({
46
+ totalItems,
47
+ itemsPerPage,
48
+ currentPage,
49
+ siblings
50
+ });
51
+ return /* @__PURE__ */ jsx("nav", {
52
+ "aria-label": "Pagination",
53
+ ...otherProps,
54
+ className: cn("flex gap-3", className),
55
+ children: items.map((item, index) => /* @__PURE__ */ jsx(PaginationItemView, {
56
+ item,
57
+ renderLink,
58
+ renderButton,
59
+ renderEllipsis,
60
+ previousLabel,
61
+ nextLabel
62
+ }, itemKey(item, index)))
61
63
  });
62
64
  };
63
65
  export { Pagination as default };
@@ -1,2 +1,3 @@
1
- import Pagination from "./Pagination.js";
2
- export { Pagination };
1
+ import Pagination, { PaginationLinkProps } from "./Pagination.js";
2
+ import usePagination, { PaginationItem, PaginationOptions } from "./usePagination.js";
3
+ export { Pagination, type PaginationItem, type PaginationLinkProps, type PaginationOptions, usePagination };
@@ -1 +1,3 @@
1
- export { default as Pagination } from "./Pagination.js";
1
+ import Pagination from "./Pagination.js";
2
+ import usePagination from "./usePagination.js";
3
+ export { Pagination, usePagination };
@@ -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 };
@@ -1 +1,7 @@
1
+ /**
2
+ * Builds the form data of the fields whose value differs from `defaultValues`.
3
+ * Fields are compared by name, so multi-selects, checkbox groups and file inputs are handled as a whole.
4
+ * A changed field with no value (an unchecked checkbox, a cleared multi-select) is sent as an empty value,
5
+ * so the server can tell it apart from an unchanged field.
6
+ */
1
7
  export declare function getDirtyFormData(form: HTMLFormElement, defaultValues: Record<string, unknown>): FormData;
@@ -1,29 +1,39 @@
1
1
  // src/utils/dirty-form.ts
2
- function getCurrentValue(el) {
3
- if (el instanceof HTMLSelectElement) return Array.from(el.options).filter((o) => o.selected).map((o) => o.value).join(",");
4
- if (el.type === "checkbox" || el.type === "radio") return el.checked;
5
- return el.value;
2
+ function hasValue(value) {
3
+ return typeof value === "string" ? value !== "" : value.name !== "";
6
4
  }
7
- function isDirty(defaultValue, currentValue) {
8
- switch (typeof defaultValue) {
9
- case "undefined": return currentValue !== "";
10
- case "string":
11
- case "boolean": return defaultValue !== currentValue;
12
- case "bigint":
13
- case "number":
14
- case "object": return JSON.stringify(defaultValue) !== currentValue;
15
- default: return true;
16
- }
5
+ function normalizeDefault(defaultValue) {
6
+ if (defaultValue === void 0 || defaultValue === null) return [];
7
+ return (Array.isArray(defaultValue) ? defaultValue : [defaultValue]).map((value) => typeof value === "object" && value !== null ? JSON.stringify(value) : String(value)).filter((value) => value !== "");
8
+ }
9
+ function isChecked(form, name) {
10
+ return Array.from(form.elements).some((e) => e instanceof HTMLInputElement && e.name === name && (e.type === "checkbox" || e.type === "radio") && e.checked);
11
+ }
12
+ function isDirty(form, name, defaultValue, values) {
13
+ if (typeof defaultValue === "boolean") return isChecked(form, name) !== defaultValue;
14
+ if (values.some((value) => typeof value !== "string")) return true;
15
+ const current = values.toSorted();
16
+ const defaults = normalizeDefault(defaultValue).toSorted();
17
+ return current.length !== defaults.length || current.some((value, index) => value !== defaults[index]);
17
18
  }
19
+ /**
20
+ * Builds the form data of the fields whose value differs from `defaultValues`.
21
+ * Fields are compared by name, so multi-selects, checkbox groups and file inputs are handled as a whole.
22
+ * A changed field with no value (an unchecked checkbox, a cleared multi-select) is sent as an empty value,
23
+ * so the server can tell it apart from an unchanged field.
24
+ */
18
25
  function getDirtyFormData(form, defaultValues) {
19
- const dirty = new FormData();
26
+ const formData = new FormData(form);
27
+ const names = /* @__PURE__ */ new Set();
20
28
  for (const e of Array.from(form.elements)) if (e instanceof HTMLInputElement || e instanceof HTMLTextAreaElement || e instanceof HTMLSelectElement) {
21
- if (!e.name) continue;
22
- if (isDirty(defaultValues[e.name], getCurrentValue(e))) {
23
- if (e.type === "checkbox" || e.type === "radio") {
24
- if (e.checked) dirty.append(e.name, e.value);
25
- } else dirty.append(e.name, e.value);
26
- }
29
+ if (e.name) names.add(e.name);
30
+ }
31
+ const dirty = new FormData();
32
+ for (const name of names) {
33
+ const values = formData.getAll(name).filter(hasValue);
34
+ if (!isDirty(form, name, defaultValues[name], values)) continue;
35
+ if (values.length === 0) dirty.append(name, "");
36
+ for (const value of values) dirty.append(name, value);
27
37
  }
28
38
  return dirty;
29
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omer-x/tailwind-ui-kit",
3
- "version": "1.0.0-alpha.19",
3
+ "version": "1.0.0-alpha.20",
4
4
  "description": "Tailwind CSS-based React component library for building user interfaces",
5
5
  "keywords": [
6
6
  "tailwind",