@coding_doc/sveltedt 0.0.0 → 0.1.0-rehearsal.0

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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +52 -0
  3. package/README.md +618 -2
  4. package/dist/DataTable.svelte +313 -0
  5. package/dist/DataTable.svelte.d.ts +26 -0
  6. package/dist/Pagination.svelte +125 -0
  7. package/dist/Pagination.svelte.d.ts +5 -0
  8. package/dist/checks.d.ts +5 -0
  9. package/dist/checks.js +7 -0
  10. package/dist/connection/answer.d.ts +6 -0
  11. package/dist/connection/answer.js +47 -0
  12. package/dist/connection/connection.svelte.d.ts +16 -0
  13. package/dist/connection/connection.svelte.js +214 -0
  14. package/dist/connection/index.d.ts +8 -0
  15. package/dist/connection/index.js +7 -0
  16. package/dist/connection/request.d.ts +4 -0
  17. package/dist/connection/request.js +71 -0
  18. package/dist/connection/types.d.ts +182 -0
  19. package/dist/connection/types.js +1 -0
  20. package/dist/faults.d.ts +6 -0
  21. package/dist/faults.js +64 -0
  22. package/dist/filters.d.ts +23 -0
  23. package/dist/filters.js +65 -0
  24. package/dist/index.d.ts +9 -0
  25. package/dist/index.js +8 -0
  26. package/dist/messages.d.ts +4 -0
  27. package/dist/messages.js +62 -0
  28. package/dist/pager.d.ts +19 -0
  29. package/dist/pager.js +80 -0
  30. package/dist/parts/ColumnFilter.svelte +213 -0
  31. package/dist/parts/ColumnFilter.svelte.d.ts +11 -0
  32. package/dist/parts/HeaderCell.svelte +90 -0
  33. package/dist/parts/HeaderCell.svelte.d.ts +34 -0
  34. package/dist/parts/Search.svelte +79 -0
  35. package/dist/parts/Search.svelte.d.ts +9 -0
  36. package/dist/parts/Toolbar.svelte +70 -0
  37. package/dist/parts/Toolbar.svelte.d.ts +29 -0
  38. package/dist/picks.svelte.d.ts +6 -0
  39. package/dist/picks.svelte.js +42 -0
  40. package/dist/search.svelte.d.ts +15 -0
  41. package/dist/search.svelte.js +76 -0
  42. package/dist/sort.d.ts +6 -0
  43. package/dist/sort.js +32 -0
  44. package/dist/styles.css +230 -0
  45. package/dist/types.d.ts +323 -0
  46. package/dist/types.js +1 -0
  47. package/dist/validate.d.ts +5 -0
  48. package/dist/validate.js +201 -0
  49. package/dist/view.svelte.d.ts +7 -0
  50. package/dist/view.svelte.js +75 -0
  51. package/package.json +83 -3
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * `@coding_doc/sveltedt`: the table and the pager. Their look comes from the stylesheet `@coding_doc/sveltedt/styles.css`,
3
+ * whose tokens and classes are in [the styling guide](https://github.com/hniedner/sveltedt/blob/main/docs/styling.md).
4
+ *
5
+ * @module sveltedt
6
+ */
7
+ export { default as DataTable } from "./DataTable.svelte";
8
+ export { default as Pagination } from "./Pagination.svelte";
@@ -0,0 +1,4 @@
1
+ import type { DataTableMessages, PaginationMessages } from "./types.ts";
2
+ export declare const defaultMessages: DataTableMessages;
3
+ export declare const defaultPagerMessages: PaginationMessages;
4
+ export declare function mergeMessages<Words extends object>(defaults: Words, given: Partial<Words> | undefined): Words;
@@ -0,0 +1,62 @@
1
+ // Numbers group the English way whatever the runtime's locale (I18N-01)
2
+ const number = new Intl.NumberFormat("en-US");
3
+ // The English default of every fixed word the table shows or announces (RND-06, SORT-17). The alert texts hold no
4
+ // value taken from the input (RND-09).
5
+ export const defaultMessages = {
6
+ loading: "Loading data…",
7
+ failed: "The data could not be loaded.",
8
+ empty: "No data.",
9
+ stopped: "Loading stopped",
10
+ stillLoading: "Still loading",
11
+ cancel: "Cancel",
12
+ retry: "Try again",
13
+ rowCount: (count) => `${number.format(count)} ${count === 1 ? "row" : "rows"} displayed`,
14
+ invalidInput: "This table cannot be shown.",
15
+ sortableHint: "Column names that are buttons sort the table.",
16
+ emptyOption: "(empty)",
17
+ filterButton: (label) => `Filter ${label}`,
18
+ filterDialog: (label) => `${label} filter`,
19
+ filterText: (label) => `Filter ${label} text`,
20
+ findOptions: (label) => `Find ${label} options`,
21
+ noOptions: "No options match.",
22
+ badgeText: ({ label, text }) => `${label}: contains “${text}”`,
23
+ badgeOptions: ({ label, options }) => `${label}: ${options.join(", ")}`,
24
+ clearFilter: (words) => `Clear filter ${words}`,
25
+ clearFilters: "Clear column filters",
26
+ reset: "Reset table",
27
+ searchLabel: "Search",
28
+ pressEnter: "Press Enter to apply.",
29
+ textHiddenCharacter: "The text contains a hidden character, often from pasted text. Clear the field and type the text again.",
30
+ textTooLong: (maxLength) => `Use at most ${number.format(maxLength)} characters.`,
31
+ sortRank: (rank) => String(rank),
32
+ sortDescription: ({ direction, rank }) => {
33
+ const hint = "Shift+click, Shift+Enter or Shift+Space adds this column to the sort.";
34
+ if (direction === null)
35
+ return hint;
36
+ const words = direction === "asc" ? "ascending" : "descending";
37
+ return `sorted ${words}${rank === null ? "" : `, priority ${rank}`}. ${hint}`;
38
+ },
39
+ };
40
+ // The pager's English words, the information line's included (PAGE-37, PAGE-38), and the alert text of PAGE-16
41
+ export const defaultPagerMessages = {
42
+ firstPage: "First page",
43
+ previousPage: "Previous page",
44
+ nextPage: "Next page",
45
+ lastPage: "Last page",
46
+ pageLabel: (page) => `Page ${number.format(page)}`,
47
+ pageNumber: (page) => number.format(page),
48
+ sizeLabel: "rows per page",
49
+ sizeOption: (size) => number.format(size),
50
+ info: ({ first, last, total }) => `Showing ${number.format(first)} to ${number.format(last)} of ${number.format(total)} ${total === 1 ? "row" : "rows"}`,
51
+ infoFiltered: (total) => `(filtered from ${number.format(total)} total ${total === 1 ? "row" : "rows"})`,
52
+ invalidInput: "Page navigation cannot be shown.",
53
+ };
54
+ // The defaults with each given entry that is of its default's type and not a blank text; an undefined, null,
55
+ // wrongly typed or blank entry keeps the default (RND-06, PAGE-19)
56
+ export function mergeMessages(defaults, given) {
57
+ return {
58
+ ...defaults,
59
+ ...Object.fromEntries(Object.entries(given ?? {}).filter(([key, word]) => typeof word === typeof defaults[key] &&
60
+ !(typeof word === "string" && word.trim() === ""))),
61
+ };
62
+ }
@@ -0,0 +1,19 @@
1
+ import type { PaginationProps } from "./types.ts";
2
+ export type PageItem = number | "gap-start" | "gap-end";
3
+ export declare function pageNumbers(current: number, pages: number): PageItem[];
4
+ export interface PagerState {
5
+ pages: number;
6
+ page: number;
7
+ current: number | null;
8
+ canPrevious: boolean;
9
+ canNext: boolean;
10
+ previous: number;
11
+ next: number;
12
+ last: number;
13
+ firstRow: number;
14
+ lastRow: number;
15
+ }
16
+ export declare function pagerState(offset: number, limit: number, total: number): PagerState;
17
+ export type PagerFault = "totalInvalid" | "offsetInvalid" | "limitInvalid" | "labelEmpty" | "sizesInvalid" | "limitNotInSizes" | "pageHandlerMissing" | "sizeHandlerMissing" | "unfilteredTotalInvalid";
18
+ export declare const isCount: (value: unknown) => boolean;
19
+ export declare function validatePager(input: PaginationProps): PagerFault | null;
package/dist/pager.js ADDED
@@ -0,0 +1,80 @@
1
+ import { firstFault, hasDuplicate, isEmpty } from "./checks.js";
2
+ // The number row for page index `current` of `pages` pages: a page index, or a named gap (two need distinct keys);
3
+ // at most seven places (PAGE-02)
4
+ export function pageNumbers(current, pages) {
5
+ if (pages <= 7)
6
+ return Array.from({ length: pages }, (_, index) => index);
7
+ const last = pages - 1;
8
+ if (current <= 3)
9
+ return [0, 1, 2, 3, 4, "gap-end", last];
10
+ if (current >= pages - 4)
11
+ return [0, "gap-start", last - 4, last - 3, last - 2, last - 1, last];
12
+ return [0, "gap-start", current - 1, current, current + 1, "gap-end", last];
13
+ }
14
+ // Where the pager stands, from the section 8 preamble: every target is on the page grid (PAGE-27)
15
+ export function pagerState(offset, limit, total) {
16
+ const pages = Math.max(1, Math.ceil(total / limit));
17
+ const last = (pages - 1) * limit;
18
+ // Past the end: the last page, with no current page and no row (PAGE-25, PAGE-37); Previous goes to the last page
19
+ // (PAGE-26)
20
+ if (offset > 0 && offset >= total)
21
+ return {
22
+ pages,
23
+ page: pages,
24
+ current: null,
25
+ canPrevious: true,
26
+ canNext: false,
27
+ previous: last,
28
+ // Stryker disable next-line ArithmeticOperator: equivalent, Next is disabled past the end (canNext), so its target is never used
29
+ next: pages * limit,
30
+ last,
31
+ firstRow: 0,
32
+ lastRow: 0,
33
+ };
34
+ const page = Math.floor(offset / limit) + 1;
35
+ // No rows are passed in, so the last row is the smaller of offset + limit and total (PAGE-37); with no row on the
36
+ // page (a total of 0), first and last are 0
37
+ const lastRow = Math.min(offset + limit, total);
38
+ return {
39
+ pages,
40
+ page,
41
+ current: page,
42
+ canPrevious: offset > 0,
43
+ // The page that holds `offset` ends before `total` (PAGE-05)
44
+ canNext: page * limit < total,
45
+ previous: Math.max(0, (page - 2) * limit),
46
+ next: page * limit,
47
+ last,
48
+ firstRow: lastRow === 0 ? 0 : offset + 1,
49
+ lastRow,
50
+ };
51
+ }
52
+ export const isCount = (value) => Number.isSafeInteger(value) && value >= 0;
53
+ const isSize = (value) => isCount(value) && value !== 0;
54
+ // The code of the first fault, in the order of PAGE-16's table, or null when the input is valid
55
+ export function validatePager(input) {
56
+ const { offset, limit, total, label, onpage, onsize } = input;
57
+ // sizes given as null is no sizes, as left out (PAGE-15)
58
+ const sizes = input.sizes ?? [];
59
+ return firstFault([
60
+ ["totalInvalid", () => !isCount(total)],
61
+ ["offsetInvalid", () => !isCount(offset)],
62
+ ["limitInvalid", () => !isSize(limit)],
63
+ ["labelEmpty", () => isEmpty(label)],
64
+ [
65
+ "sizesInvalid",
66
+ () => !Array.isArray(sizes) || !sizes.every(isSize) || hasDuplicate(sizes),
67
+ ],
68
+ ["limitNotInSizes", () => sizes.length > 0 && !sizes.includes(limit)],
69
+ ["pageHandlerMissing", () => typeof onpage !== "function"],
70
+ [
71
+ "sizeHandlerMissing",
72
+ () => sizes.length > 0 && typeof onsize !== "function",
73
+ ],
74
+ // unfilteredTotal given as null is not given (PAGE-16)
75
+ [
76
+ "unfilteredTotalInvalid",
77
+ () => input.unfilteredTotal != null && !isCount(input.unfilteredTotal),
78
+ ],
79
+ ]);
80
+ }
@@ -0,0 +1,213 @@
1
+ <script lang="ts">
2
+ import type {
3
+ DataTableFilter,
4
+ DataTableFilterState,
5
+ DataTableMessages,
6
+ DataTableProps,
7
+ } from "../types.js";
8
+ import { narrowed, offered, toggled } from "../filters.js";
9
+ import { Picks } from "../picks.svelte.js";
10
+ import Search from "./Search.svelte";
11
+
12
+ type Props = Pick<
13
+ DataTableProps<unknown>,
14
+ "searchDelay" | "searchOnEnter" | "maxLength" | "onfilter"
15
+ > & {
16
+ id: string;
17
+ label: string;
18
+ filter: DataTableFilter;
19
+ // The filter the application applied to this column
20
+ applied: DataTableFilterState | undefined;
21
+ words: Pick<
22
+ DataTableMessages,
23
+ | "filterButton"
24
+ | "filterDialog"
25
+ | "filterText"
26
+ | "findOptions"
27
+ | "noOptions"
28
+ | "emptyOption"
29
+ | "pressEnter"
30
+ | "textHiddenCharacter"
31
+ | "textTooLong"
32
+ >;
33
+ };
34
+
35
+ const {
36
+ id,
37
+ label,
38
+ filter,
39
+ applied,
40
+ words,
41
+ searchDelay,
42
+ searchOnEnter,
43
+ maxLength,
44
+ onfilter,
45
+ }: Props = $props();
46
+ // Ties the button to its dialog, unique per part on the page
47
+ const dialogId = $props.id();
48
+ // The browser holds whether the dialog is open; this mirrors it from the event that precedes every change, so the
49
+ // fields exist only while it is open and their unsent text and pause go with it, and the button's aria-expanded
50
+ // follows it (FILT-11)
51
+ let open = $state(false);
52
+ // The find field's text: it narrows the options shown, never the selection, and goes with the dialog (FILT-18)
53
+ let find = $state("");
54
+ // What the dialog shows and works from: the applied selection, or the last press `filters` has not answered (FILT-61)
55
+ const picks = new Picks(
56
+ // Stryker disable next-line ArrayDeclaration: a value that is no option is never shown or emitted (toggled keeps option values)
57
+ () => (applied?.kind === "categorical" ? applied.selected : []),
58
+ () => open,
59
+ );
60
+ const isSelected = (value: string) => picks.selected.includes(value);
61
+ let button: HTMLButtonElement;
62
+ let dialog: HTMLElement;
63
+ // Escape in the open dialog, or on its button, closes it and gives focus back to the button (FILT-07); the key goes
64
+ // no further, so an application's Escape listener on `document` or `window` never acts on a press that only closed
65
+ // the dialog (FILT-36). While the dialog is closed the key is left to the page, and so is an Escape that cancels
66
+ // text an input method is composing (WebKit sends that keydown after the composition ended, with keyCode 229 its
67
+ // only mark)
68
+ const closeOnEscape = (event: KeyboardEvent) => {
69
+ if (!open || event.key !== "Escape") return;
70
+ if (event.isComposing || event.keyCode === 229) return;
71
+ event.preventDefault();
72
+ event.stopPropagation();
73
+ dialog.hidePopover();
74
+ button.focus();
75
+ };
76
+ // Enter on a field of the dialog (the find field, a checkbox) does not submit a form the table sits in, as Enter in a
77
+ // text filter's field does not (FILT-65); an Enter that ends a composition is the input method's (FILT-66)
78
+ const keepFormOnEnter = (event: KeyboardEvent) => {
79
+ if (event.key !== "Enter" || event.isComposing || event.keyCode === 229)
80
+ return;
81
+ if (event.target instanceof HTMLInputElement) event.preventDefault();
82
+ };
83
+ // Focus that moves from the dialog or its button to another element closes the dialog (FILT-20); between the two it
84
+ // stays open (FILT-21). Focus that goes to no element, as on a switch to another window, leaves it open: it returns
85
+ // where it was
86
+ const closeOnLeave = (event: FocusEvent) => {
87
+ const to = event.relatedTarget;
88
+ if (!open || !(to instanceof Node)) return;
89
+ if (dialog.contains(to) || button.contains(to)) return;
90
+ dialog.hidePopover();
91
+ };
92
+ // A press on the button of the open dialog closes it and leaves focus on the button (FILT-63). WebKit does not focus
93
+ // a pressed button, so focus would stay in the closing dialog and fall to the body; the press runs before the
94
+ // browser closes the dialog, while focus moving to the button keeps it open (FILT-21)
95
+ const keepFocusOnClose = () => {
96
+ if (open) button.focus();
97
+ };
98
+ </script>
99
+
100
+ <!-- Adapted from ontoprism, frontend/src/lib/components/data-table/DataTableFilterPopover.svelte (Apache License 2.0,
101
+ the owner's own project): the dialog named "<label> filter" (FILT-02). The button and its dialog, which follows it
102
+ in the document (the intro of the filter dialog section of spec/table.md). The dialog is a popover: the browser
103
+ opens it on a press of the button and holds whether it is open. The text filter's field is the search field's part
104
+ and class, given this column's applied text (FILT-01). A categorical filter's options are one checkbox each,
105
+ checked while its value is selected, or while a press `filters` has not answered selects it (FILT-17, FILT-62),
106
+ with `emptyOption` one more for no value, last (FILT-53); a press emits the next selection, in the options' order
107
+ (FILT-04); a press toggles within the selection a press `filters` has not answered (FILT-61). Over 10 options a
108
+ find field narrows the ones shown (FILT-52, FILT-18); a press still builds on all of them -->
109
+ <button
110
+ class="sdt-button"
111
+ bind:this={button}
112
+ type="button"
113
+ aria-label={words.filterButton(label)}
114
+ aria-expanded={open}
115
+ popovertarget={dialogId}
116
+ onkeydown={closeOnEscape}
117
+ onfocusout={closeOnLeave}
118
+ onclick={keepFocusOnClose}
119
+ >
120
+ <svg
121
+ aria-hidden="true"
122
+ focusable="false"
123
+ viewBox="0 0 16 16"
124
+ width="12"
125
+ height="12"
126
+ >
127
+ <path d="M1 2h14l-5.5 6.5V14l-3-1.5V8.5z" fill="currentColor" />
128
+ </svg>
129
+ </button>
130
+ <!-- tabindex -1: a press inside the dialog that lands on no field keeps focus in it, where Escape is heard (FILT-36) -->
131
+ <div
132
+ class="sdt-dialog"
133
+ bind:this={dialog}
134
+ id={dialogId}
135
+ popover="auto"
136
+ tabindex="-1"
137
+ role="dialog"
138
+ aria-label={words.filterDialog(label)}
139
+ onkeydown={(event) => {
140
+ closeOnEscape(event);
141
+ keepFormOnEnter(event);
142
+ }}
143
+ onfocusout={closeOnLeave}
144
+ onbeforetoggle={(event) => {
145
+ open = event.newState === "open";
146
+ find = "";
147
+ }}
148
+ ontoggle={(event) => {
149
+ // The fields render after beforetoggle, so the browser's own focusing found none: focus goes to the first (FILT-12)
150
+ if (event.newState !== "open") return;
151
+ // Stryker disable next-line OptionalChaining: unreachable fallback, every dialog holds a field, as a categorical filter without options is a fault (FILT-32)
152
+ event.currentTarget.querySelector("input")?.focus();
153
+ }}
154
+ >
155
+ {#if open}
156
+ {#if filter.kind === "text"}
157
+ <Search
158
+ label={words.filterText(label)}
159
+ {words}
160
+ search={applied?.kind === "text" ? applied.text : ""}
161
+ {searchDelay}
162
+ {searchOnEnter}
163
+ {maxLength}
164
+ onsearch={(next: string) =>
165
+ // Stryker disable next-line OptionalChaining: unreachable fallback, a filter exists only with an onfilter (FILT-32)
166
+ onfilter?.({ id, state: { kind: "text", text: next } })}
167
+ />
168
+ {:else}
169
+ {@const options = offered(filter, words.emptyOption)}
170
+ <!-- A find field above the options while there are more than 10, the option for no value included (FILT-52) -->
171
+ {@const findable = options.length > 10}
172
+ {@const shown = findable ? narrowed(options, find) : options}
173
+ {#if findable}
174
+ <input
175
+ class="sdt-field"
176
+ type="search"
177
+ aria-label={words.findOptions(label)}
178
+ value={find}
179
+ oninput={(event) => (find = event.currentTarget.value)}
180
+ />
181
+ {/if}
182
+ {#each shown as option (option.value)}
183
+ <label class="sdt-option">
184
+ <input
185
+ type="checkbox"
186
+ checked={isSelected(option.value)}
187
+ onchange={(event) => {
188
+ // The browser has already flipped the box; put it back to what the dialog showed, the value Svelte wrote
189
+ // last: Svelte skips a write equal to that one, so the render after the press, or after an answer
190
+ // `onfilter` gives at once, sets the box (FILT-62)
191
+ event.currentTarget.checked = isSelected(option.value);
192
+ const next = toggled(options, isSelected, option.value);
193
+ picks.press(next);
194
+ // Stryker disable next-line OptionalChaining: unreachable fallback, a filter exists only with an onfilter (FILT-32)
195
+ onfilter?.({
196
+ id,
197
+ state: {
198
+ kind: "categorical",
199
+ selected: next,
200
+ },
201
+ });
202
+ }}
203
+ />
204
+ {option.label}
205
+ </label>
206
+ {/each}
207
+ {#if findable}
208
+ <!-- A status message while no option matches, in an element there before its text (FILT-64) -->
209
+ <p role="status">{shown.length === 0 ? words.noOptions : ""}</p>
210
+ {/if}
211
+ {/if}
212
+ {/if}
213
+ </div>
@@ -0,0 +1,11 @@
1
+ import type { DataTableFilter, DataTableFilterState, DataTableMessages, DataTableProps } from "../types.js";
2
+ type Props = Pick<DataTableProps<unknown>, "searchDelay" | "searchOnEnter" | "maxLength" | "onfilter"> & {
3
+ id: string;
4
+ label: string;
5
+ filter: DataTableFilter;
6
+ applied: DataTableFilterState | undefined;
7
+ words: Pick<DataTableMessages, "filterButton" | "filterDialog" | "filterText" | "findOptions" | "noOptions" | "emptyOption" | "pressEnter" | "textHiddenCharacter" | "textTooLong">;
8
+ };
9
+ declare const ColumnFilter: import("svelte").Component<Props, {}, "">;
10
+ type ColumnFilter = ReturnType<typeof ColumnFilter>;
11
+ export default ColumnFilter;
@@ -0,0 +1,90 @@
1
+ <script lang="ts" generics="Row">
2
+ import { sortState } from "../sort.js";
3
+ import type {
4
+ DataTableColumn,
5
+ DataTableFilterState,
6
+ DataTableMessages,
7
+ DataTableProps,
8
+ DataTableSortEntry,
9
+ SortDirection,
10
+ } from "../types.js";
11
+ import ColumnFilter from "./ColumnFilter.svelte";
12
+
13
+ type Props = Pick<
14
+ DataTableProps<Row>,
15
+ "searchDelay" | "searchOnEnter" | "maxLength" | "onfilter"
16
+ > & {
17
+ column: DataTableColumn<Row>;
18
+ order: readonly DataTableSortEntry[];
19
+ // The id of the hidden description of this column's sort button
20
+ describedBy: string;
21
+ words: DataTableMessages;
22
+ // The table's handlers: the Shift state they share is the table's, one for all its sort buttons
23
+ onkeydown: (event: KeyboardEvent) => void;
24
+ onblur: () => void;
25
+ onpress: (event: MouseEvent) => void;
26
+ // The filter the application applied to this column
27
+ applied: DataTableFilterState | undefined;
28
+ };
29
+
30
+ const {
31
+ column,
32
+ order,
33
+ describedBy,
34
+ words,
35
+ onkeydown,
36
+ onblur,
37
+ onpress,
38
+ applied,
39
+ searchDelay,
40
+ searchOnEnter,
41
+ maxLength,
42
+ onfilter,
43
+ }: Props = $props();
44
+ // The direction mark of a sorted column (SORT-13)
45
+ const arrow = (direction: SortDirection): string =>
46
+ direction === "asc" ? "▲" : "▼";
47
+ const state = $derived(sortState(order, column.id));
48
+ </script>
49
+
50
+ <!-- aria-sort on the lead column's header only (A11Y-03) -->
51
+ <th
52
+ class="sdt-th"
53
+ scope="col"
54
+ aria-label={column.label}
55
+ aria-sort={order[0]?.id !== column.id
56
+ ? undefined
57
+ : state.direction === "asc"
58
+ ? "ascending"
59
+ : "descending"}
60
+ >
61
+ {#if column.sortable}
62
+ <!-- The table emits the next order and shows only the one it is given (SORT-12) -->
63
+ <button
64
+ class="sdt-sort"
65
+ type="button"
66
+ aria-describedby={describedBy}
67
+ {onkeydown}
68
+ {onblur}
69
+ onclick={onpress}
70
+ >
71
+ {column.label}
72
+ </button>{#if state.direction}<span aria-hidden="true">
73
+ {arrow(state.direction)}
74
+ </span>{/if}{#if state.rank !== null}<span aria-hidden="true">
75
+ {words.sortRank(state.rank)}
76
+ </span>{/if}
77
+ {:else}
78
+ {column.label}
79
+ {/if}{#if column.filter}<ColumnFilter
80
+ id={column.id}
81
+ label={column.label}
82
+ filter={column.filter}
83
+ {applied}
84
+ {words}
85
+ {searchDelay}
86
+ {searchOnEnter}
87
+ {maxLength}
88
+ {onfilter}
89
+ />{/if}
90
+ </th>
@@ -0,0 +1,34 @@
1
+ import type { DataTableColumn, DataTableFilterState, DataTableMessages, DataTableProps, DataTableSortEntry } from "../types.js";
2
+ declare function $$render<Row>(): {
3
+ props: Pick<DataTableProps<Row>, "searchDelay" | "searchOnEnter" | "maxLength" | "onfilter"> & {
4
+ column: DataTableColumn<Row>;
5
+ order: readonly DataTableSortEntry[];
6
+ describedBy: string;
7
+ words: DataTableMessages;
8
+ onkeydown: (event: KeyboardEvent) => void;
9
+ onblur: () => void;
10
+ onpress: (event: MouseEvent) => void;
11
+ applied: DataTableFilterState | undefined;
12
+ };
13
+ exports: {};
14
+ bindings: "";
15
+ slots: {};
16
+ events: {};
17
+ };
18
+ declare class __sveltets_Render<Row> {
19
+ props(): ReturnType<typeof $$render<Row>>['props'];
20
+ events(): ReturnType<typeof $$render<Row>>['events'];
21
+ slots(): ReturnType<typeof $$render<Row>>['slots'];
22
+ bindings(): "";
23
+ exports(): {};
24
+ }
25
+ interface $$IsomorphicComponent {
26
+ new <Row>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<Row>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<Row>['props']>, ReturnType<__sveltets_Render<Row>['events']>, ReturnType<__sveltets_Render<Row>['slots']>> & {
27
+ $$bindings?: ReturnType<__sveltets_Render<Row>['bindings']>;
28
+ } & ReturnType<__sveltets_Render<Row>['exports']>;
29
+ <Row>(internal: unknown, props: ReturnType<__sveltets_Render<Row>['props']> & {}): ReturnType<__sveltets_Render<Row>['exports']>;
30
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
31
+ }
32
+ declare const HeaderCell: $$IsomorphicComponent;
33
+ type HeaderCell<Row> = InstanceType<typeof HeaderCell<Row>>;
34
+ export default HeaderCell;
@@ -0,0 +1,79 @@
1
+ <script lang="ts">
2
+ import { SearchText } from "../search.svelte.js";
3
+ import type { DataTableMessages, DataTableProps } from "../types.js";
4
+
5
+ type Props = Pick<
6
+ DataTableProps<unknown>,
7
+ "search" | "searchDelay" | "searchOnEnter" | "maxLength"
8
+ > & {
9
+ label: string;
10
+ words: Pick<
11
+ DataTableMessages,
12
+ "pressEnter" | "textHiddenCharacter" | "textTooLong"
13
+ >;
14
+ onsearch: (text: string) => void;
15
+ };
16
+
17
+ const {
18
+ label,
19
+ words,
20
+ search,
21
+ searchDelay,
22
+ searchOnEnter,
23
+ maxLength,
24
+ onsearch,
25
+ }: Props = $props();
26
+ // Ties the label and the messages to the field, unique per part on the page
27
+ const id = $props.id();
28
+ const limit = $derived(maxLength ?? 255);
29
+ // The defaults are applied here, after the table validated the settings, so a null stays a fault
30
+ const box = new SearchText(
31
+ () => search ?? "",
32
+ () => ({
33
+ delay: searchDelay ?? 400,
34
+ onEnter: searchOnEnter ?? false,
35
+ maxLength: limit,
36
+ }),
37
+ (text) => onsearch(text),
38
+ );
39
+ </script>
40
+
41
+ <!-- Shown for the global search and, in a column's filter dialog, for a text filter's field (FILT-01). A plain
42
+ element, no role and no class: it holds the label and the field. For the global search it comes after the `actions`
43
+ snippet and before the toolbar, the `panels` snippet and the region (FILT-45); in a filter dialog it sits in the
44
+ dialog -->
45
+ <div>
46
+ <label for={id}>{label}</label>
47
+ <input
48
+ class="sdt-field"
49
+ {id}
50
+ type="search"
51
+ autocomplete="off"
52
+ value={box.text}
53
+ aria-invalid={box.hidden || box.tooLong ? "true" : undefined}
54
+ aria-describedby={[
55
+ searchOnEnter ? `${id}-hint` : null,
56
+ box.hidden ? `${id}-hidden` : null,
57
+ box.tooLong ? `${id}-long` : null,
58
+ ]
59
+ .filter(Boolean)
60
+ .join(" ") || undefined}
61
+ oninput={(event) => box.input(event.currentTarget.value)}
62
+ onkeydown={(event) => {
63
+ // Enter that ends an input method's composition confirms the characters and is left to the page (FILT-66);
64
+ // WebKit sends it after the composition ended, with keyCode 229 its only mark
65
+ if (event.key !== "Enter" || event.isComposing || event.keyCode === 229)
66
+ return;
67
+ // Enter applies the text and does not submit a form the table sits in (FILT-65)
68
+ event.preventDefault();
69
+ box.apply();
70
+ }}
71
+ />
72
+ {#if searchOnEnter}<div id="{id}-hint">{words.pressEnter}</div>{/if}
73
+ {#if box.hidden}<div role="alert" id="{id}-hidden">
74
+ {words.textHiddenCharacter}
75
+ </div>{/if}
76
+ {#if box.tooLong}<div role="alert" id="{id}-long">
77
+ {words.textTooLong(limit)}
78
+ </div>{/if}
79
+ </div>
@@ -0,0 +1,9 @@
1
+ import type { DataTableMessages, DataTableProps } from "../types.js";
2
+ type Props = Pick<DataTableProps<unknown>, "search" | "searchDelay" | "searchOnEnter" | "maxLength"> & {
3
+ label: string;
4
+ words: Pick<DataTableMessages, "pressEnter" | "textHiddenCharacter" | "textTooLong">;
5
+ onsearch: (text: string) => void;
6
+ };
7
+ declare const Search: import("svelte").Component<Props, {}, "">;
8
+ type Search = ReturnType<typeof Search>;
9
+ export default Search;