@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 +46 -0
- package/README.md +33 -9
- package/dist/components/elements/JustSpan.d.ts +3 -0
- package/dist/components/elements/JustSpan.js +10 -0
- package/dist/components/page/Pagination.d.ts +17 -9
- package/dist/components/page/Pagination.js +56 -54
- package/dist/components/page/index.d.ts +3 -2
- package/dist/components/page/index.js +3 -1
- package/dist/components/page/usePagination.d.ts +30 -0
- package/dist/components/page/usePagination.js +41 -0
- package/dist/utils/dirty-form.d.ts +6 -0
- package/dist/utils/dirty-form.js +31 -21
- package/package.json +1 -1
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
|
|
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
|
-
| `
|
|
339
|
-
| `renderLink` |
|
|
340
|
-
| `renderButton` | render prop | Renders
|
|
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 "
|
|
349
|
+
import { Link } from "@/i18n/navigation";
|
|
344
350
|
|
|
345
351
|
<Pagination
|
|
346
352
|
totalItems={total}
|
|
347
353
|
itemsPerPage={20}
|
|
348
354
|
currentPage={page}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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`
|
|
@@ -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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
|
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
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/* @__PURE__ */ jsx(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
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 };
|
|
@@ -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;
|
package/dist/utils/dirty-form.js
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
// src/utils/dirty-form.ts
|
|
2
|
-
function
|
|
3
|
-
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
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 (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
}
|