@gemologic/sheen-table 0.1.0-rc.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.
- package/LICENSE +19 -0
- package/README.md +19 -0
- package/dist/DataTable.d.ts +159 -0
- package/dist/DataTable.js +2724 -0
- package/dist/DataTableToolbar.d.ts +28 -0
- package/dist/DataTableToolbar.js +132 -0
- package/dist/FilterBar.d.ts +24 -0
- package/dist/FilterBar.js +681 -0
- package/dist/QueryBuilder.d.ts +11 -0
- package/dist/QueryBuilder.js +693 -0
- package/dist/cell-edit.d.ts +50 -0
- package/dist/cell-edit.js +178 -0
- package/dist/client-state.d.ts +28 -0
- package/dist/client-state.js +57 -0
- package/dist/client-view.d.ts +31 -0
- package/dist/client-view.js +30 -0
- package/dist/column-layout.d.ts +10 -0
- package/dist/column-layout.js +34 -0
- package/dist/columns.d.ts +108 -0
- package/dist/columns.js +217 -0
- package/dist/core.d.ts +54 -0
- package/dist/core.js +28 -0
- package/dist/export.d.ts +14 -0
- package/dist/export.js +27 -0
- package/dist/facets.d.ts +15 -0
- package/dist/facets.js +41 -0
- package/dist/filter-bar-state.d.ts +22 -0
- package/dist/filter-bar-state.js +128 -0
- package/dist/filter-client.d.ts +9 -0
- package/dist/filter-client.js +69 -0
- package/dist/filter.d.ts +64 -0
- package/dist/filter.js +132 -0
- package/dist/grouping.d.ts +37 -0
- package/dist/grouping.js +126 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +31 -0
- package/dist/messages.d.ts +115 -0
- package/dist/messages.js +121 -0
- package/dist/pagination.d.ts +36 -0
- package/dist/pagination.js +102 -0
- package/dist/query-builder-state.d.ts +28 -0
- package/dist/query-builder-state.js +128 -0
- package/dist/requests.d.ts +26 -0
- package/dist/requests.js +106 -0
- package/dist/row-hierarchy.d.ts +32 -0
- package/dist/row-hierarchy.js +170 -0
- package/dist/row-identity.d.ts +11 -0
- package/dist/row-identity.js +36 -0
- package/dist/saved-views.d.ts +52 -0
- package/dist/saved-views.js +153 -0
- package/dist/search-controller.d.ts +22 -0
- package/dist/search-controller.js +75 -0
- package/dist/search.d.ts +28 -0
- package/dist/search.js +111 -0
- package/dist/selection-client.d.ts +9 -0
- package/dist/selection-client.js +14 -0
- package/dist/selection.d.ts +25 -0
- package/dist/selection.js +60 -0
- package/dist/server-export.d.ts +27 -0
- package/dist/server-export.js +105 -0
- package/dist/sorting.d.ts +17 -0
- package/dist/sorting.js +58 -0
- package/dist/table-state-changes.d.ts +27 -0
- package/dist/table-state-changes.js +67 -0
- package/dist/table-state.d.ts +20 -0
- package/dist/table-state.js +77 -0
- package/dist/text-normalization.d.ts +2 -0
- package/dist/text-normalization.js +12 -0
- package/dist/url-state.d.ts +39 -0
- package/dist/url-state.js +75 -0
- package/package.json +66 -0
- package/src/DataTable.tsx +2322 -0
- package/src/DataTableToolbar.tsx +69 -0
- package/src/FilterBar.tsx +396 -0
- package/src/QueryBuilder.tsx +274 -0
- package/src/cell-edit.ts +187 -0
- package/src/client-state.ts +52 -0
- package/src/client-view.ts +61 -0
- package/src/column-layout.ts +47 -0
- package/src/columns.ts +271 -0
- package/src/core.ts +54 -0
- package/src/export.ts +39 -0
- package/src/facets.ts +46 -0
- package/src/filter-bar-state.ts +118 -0
- package/src/filter-client.ts +79 -0
- package/src/filter.ts +113 -0
- package/src/grouping.ts +185 -0
- package/src/index.ts +7 -0
- package/src/messages.ts +29 -0
- package/src/pagination.ts +81 -0
- package/src/query-builder-state.ts +105 -0
- package/src/requests.ts +85 -0
- package/src/row-hierarchy.ts +280 -0
- package/src/row-identity.ts +39 -0
- package/src/saved-views.ts +143 -0
- package/src/search-controller.ts +61 -0
- package/src/search.ts +137 -0
- package/src/selection-client.ts +23 -0
- package/src/selection.ts +62 -0
- package/src/server-export.ts +89 -0
- package/src/sorting.ts +63 -0
- package/src/styles.css +197 -0
- package/src/table-state-changes.ts +51 -0
- package/src/table-state.ts +72 -0
- package/src/text-normalization.ts +14 -0
- package/src/url-state.ts +62 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Button, DropdownMenu, useTheme } from "@gemologic/sheen";
|
|
2
|
+
import type { MenuItem } from "@gemologic/sheen";
|
|
3
|
+
import { For, Show, children, createMemo } from "solid-js";
|
|
4
|
+
import type { JSX } from "solid-js";
|
|
5
|
+
|
|
6
|
+
interface DataTableToolbarActionBase {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly disabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface DataTableToolbarButton extends DataTableToolbarActionBase { readonly kind: "action"; readonly onSelect: () => void; readonly items?: never; }
|
|
12
|
+
export interface DataTableToolbarMenu extends DataTableToolbarActionBase { readonly kind: "menu"; readonly items: readonly MenuItem[]; readonly onSelect?: never; }
|
|
13
|
+
export type DataTableToolbarAction = DataTableToolbarButton | DataTableToolbarMenu;
|
|
14
|
+
|
|
15
|
+
export interface DataTableToolbarProps {
|
|
16
|
+
readonly label: string;
|
|
17
|
+
readonly query?: JSX.Element | undefined;
|
|
18
|
+
readonly result?: string | undefined;
|
|
19
|
+
readonly resultPrevious?: string | undefined;
|
|
20
|
+
readonly actions: readonly DataTableToolbarAction[];
|
|
21
|
+
readonly status?: JSX.Element | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function validateActions(actions: readonly DataTableToolbarAction[]): ReadonlyMap<string, DataTableToolbarAction> {
|
|
25
|
+
if (!Array.isArray(actions)) throw new Error("DataTable toolbar actions must be an array");
|
|
26
|
+
const result = new Map<string, DataTableToolbarAction>();
|
|
27
|
+
for (const action of actions) {
|
|
28
|
+
if (!action.id.trim() || !action.label.trim() || result.has(action.id)) throw new Error("DataTable toolbar actions require unique nonempty IDs and labels");
|
|
29
|
+
if (action.kind === "action" && (typeof action.onSelect !== "function" || action.items !== undefined)) throw new Error(`DataTable toolbar action ${action.id} requires onSelect only`);
|
|
30
|
+
if (action.kind === "menu" && (!Array.isArray(action.items) || action.items.length === 0 || action.onSelect !== undefined)) throw new Error(`DataTable toolbar menu ${action.id} requires nonempty items only`);
|
|
31
|
+
result.set(action.id, action);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function DirectAction(props: { readonly action: DataTableToolbarAction }): JSX.Element {
|
|
37
|
+
return <Show when={props.action.kind === "menu"} fallback={<Button size="sm" {...(props.action.disabled === undefined ? {} : { disabled: props.action.disabled })}
|
|
38
|
+
onClick={() => { if (props.action.kind === "action") props.action.onSelect(); }}>{props.action.label}</Button>}>
|
|
39
|
+
<DropdownMenu trigger={props.action.label} items={props.action.kind === "menu" ? props.action.items : []}
|
|
40
|
+
{...(props.action.disabled === undefined ? {} : { disabled: props.action.disabled })} />
|
|
41
|
+
</Show>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function overflowItem(action: DataTableToolbarAction): MenuItem {
|
|
45
|
+
if (action.kind === "menu") return Object.freeze({ kind: "submenu", id: action.id, label: action.label, items: action.items, ...(action.disabled === undefined ? {} : { disabled: action.disabled }) });
|
|
46
|
+
return Object.freeze({ kind: "action", id: action.id, label: action.label, onSelect: action.onSelect, ...(action.disabled === undefined ? {} : { disabled: action.disabled }) });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function DataTableToolbar(props: DataTableToolbarProps): JSX.Element {
|
|
50
|
+
const theme = useTheme();
|
|
51
|
+
const query = children(() => props.query);
|
|
52
|
+
const status = children(() => props.status);
|
|
53
|
+
const actions = createMemo(() => {
|
|
54
|
+
if (!props.label.trim()) throw new Error("DataTable toolbar requires a nonempty label");
|
|
55
|
+
return validateActions(props.actions);
|
|
56
|
+
});
|
|
57
|
+
const overflow = createMemo<readonly MenuItem[]>(() => [...actions().values()].map(overflowItem));
|
|
58
|
+
const actionIds = createMemo(() => [...actions().keys()]);
|
|
59
|
+
|
|
60
|
+
return <div class="sheen-data-table-toolbar" role="toolbar" aria-label={props.label}>
|
|
61
|
+
<Show when={query()}><div class="sheen-data-table-toolbar-query">{query()}</div></Show>
|
|
62
|
+
<Show when={props.result}>{result => <output class="sheen-data-table-result-count" data-previous={props.resultPrevious ? "" : undefined} aria-live="polite" aria-atomic="true">{result()}<Show when={props.resultPrevious}>{previous => <span class="sheen-data-table-previous-label">{previous()}</span>}</Show></output>}</Show>
|
|
63
|
+
<Show when={status()}><div class="sheen-data-table-toolbar-status">{status()}</div></Show>
|
|
64
|
+
<Show when={actionIds().length > 0}><div class="sheen-data-table-toolbar-actions">
|
|
65
|
+
<For each={actionIds()}>{id => <Show when={actions().get(id)}>{action => <div class="sheen-data-table-toolbar-action" data-action={id}><DirectAction action={action()} /></div>}</Show>}</For>
|
|
66
|
+
<div class="sheen-data-table-toolbar-overflow"><DropdownMenu trigger={theme.messages().moreActions} items={overflow()} /></div>
|
|
67
|
+
</div></Show>
|
|
68
|
+
</div>;
|
|
69
|
+
}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { Button, Checkbox, CheckboxGroup, Input, Popover, Select, cn, useTheme } from "@gemologic/sheen";
|
|
2
|
+
import { For, Show, createEffect, createMemo, createSignal, untrack } from "solid-js";
|
|
3
|
+
import type { Accessor, JSX } from "solid-js";
|
|
4
|
+
import { Dynamic } from "solid-js/web";
|
|
5
|
+
import { readColumnDefinitions } from "./columns.ts";
|
|
6
|
+
import type { ColumnFilter, SheenColumns } from "./columns.ts";
|
|
7
|
+
import { parseFilter } from "./filter.ts";
|
|
8
|
+
import type { FilterColumn, FilterNode } from "./filter.ts";
|
|
9
|
+
import { appendFilterCondition, listFilterConditions, removeFilterCondition, replaceFilterCondition } from "./filter-bar-state.ts";
|
|
10
|
+
import type { FilterCondition, FilterConditionEntry } from "./filter-bar-state.ts";
|
|
11
|
+
import { resolveTableMessages } from "./messages.ts";
|
|
12
|
+
import type { TableMessages } from "./messages.ts";
|
|
13
|
+
|
|
14
|
+
export interface FilterBarProps<Row extends object> {
|
|
15
|
+
readonly columns: SheenColumns<Row>;
|
|
16
|
+
readonly value: FilterNode;
|
|
17
|
+
readonly onChange: (filter: FilterNode) => void;
|
|
18
|
+
readonly facets?: Readonly<Record<string, Readonly<Record<string, number>>>> | undefined;
|
|
19
|
+
readonly disabled?: boolean;
|
|
20
|
+
readonly class?: string;
|
|
21
|
+
/** Optional package-isolated date selector. The native date fields remain the no-dependency fallback. */
|
|
22
|
+
readonly dateEditor?: FilterDateEditor;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface FilterDateEditorProps {
|
|
26
|
+
readonly mode: "single" | "range";
|
|
27
|
+
readonly label: string;
|
|
28
|
+
readonly endLabel: string;
|
|
29
|
+
readonly value: string;
|
|
30
|
+
readonly endValue: string;
|
|
31
|
+
readonly onValueChange: (value: string, endValue: string) => void;
|
|
32
|
+
readonly disabled: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type FilterDateEditor = (props: FilterDateEditorProps) => JSX.Element;
|
|
36
|
+
|
|
37
|
+
interface FilterDefinition {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
readonly header: string;
|
|
40
|
+
readonly filter: ColumnFilter;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface EntryRecord {
|
|
44
|
+
readonly key: string;
|
|
45
|
+
readonly entry: Accessor<FilterConditionEntry>;
|
|
46
|
+
readonly update: (entry: FilterConditionEntry) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function entrySignature(entry: FilterConditionEntry): string {
|
|
50
|
+
return JSON.stringify({ condition: entry.condition, directNegated: entry.directNegated, inheritedNegated: entry.inheritedNegated, groups: entry.groups });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function entryPath(entry: FilterConditionEntry): string {
|
|
54
|
+
return `${entry.condition.column}\u0000${JSON.stringify(entry.path)}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function addRecord(bucket: Map<string, EntryRecord[]>, key: string, record: EntryRecord): void {
|
|
58
|
+
const records = bucket.get(key);
|
|
59
|
+
if (records) records.push(record);
|
|
60
|
+
else bucket.set(key, [record]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function takeRecord(bucket: Map<string, EntryRecord[]>, key: string, available: Set<EntryRecord>): EntryRecord | undefined {
|
|
64
|
+
const records = bucket.get(key);
|
|
65
|
+
while (records?.length) {
|
|
66
|
+
const record = records.shift();
|
|
67
|
+
if (record && available.has(record)) return record;
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type NumericOperator = "eq" | "lt" | "lte" | "gt" | "gte" | "between";
|
|
73
|
+
type TextOperator = "eq" | "contains" | "startsWith" | "endsWith";
|
|
74
|
+
|
|
75
|
+
function numericOperator(value: string): value is NumericOperator {
|
|
76
|
+
return value === "eq" || value === "lt" || value === "lte" || value === "gt" || value === "gte" || value === "between";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function textOperator(value: string): value is TextOperator {
|
|
80
|
+
return value === "eq" || value === "contains" || value === "startsWith" || value === "endsWith";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function dateInputValue(timestamp: number): string {
|
|
84
|
+
return new Date(timestamp).toISOString().slice(0, 10);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parseDateInput(value: string): number | null {
|
|
88
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return null;
|
|
89
|
+
const timestamp = Date.parse(`${value}T00:00:00.000Z`);
|
|
90
|
+
return Number.isFinite(timestamp) && dateInputValue(timestamp) === value ? timestamp : null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function startValue(condition: FilterCondition): string {
|
|
94
|
+
if (condition.kind === "number") return String(condition.operator === "between" ? condition.min : condition.value);
|
|
95
|
+
if (condition.kind === "date") return dateInputValue(condition.operator === "between" ? condition.min : condition.value);
|
|
96
|
+
return "";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function endValue(condition: FilterCondition): string {
|
|
100
|
+
if (condition.kind === "number") return condition.operator === "between" ? String(condition.max) : "";
|
|
101
|
+
if (condition.kind === "date") return condition.operator === "between" ? dateInputValue(condition.max) : "";
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function initialOperator(condition: FilterCondition): string {
|
|
106
|
+
return condition.kind === "empty" ? "empty" : condition.operator;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function schemaColumn(definition: FilterDefinition): FilterColumn {
|
|
110
|
+
return definition.filter.type === "enum"
|
|
111
|
+
? Object.freeze({ id: definition.id, type: "enum", options: definition.filter.options })
|
|
112
|
+
: Object.freeze({ id: definition.id, type: definition.filter.type });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function FilterEditor(props: {
|
|
116
|
+
readonly definition: FilterDefinition;
|
|
117
|
+
readonly condition: FilterCondition;
|
|
118
|
+
readonly initialOperator?: string | undefined;
|
|
119
|
+
readonly directNegated: boolean;
|
|
120
|
+
readonly inheritedNegated: boolean;
|
|
121
|
+
readonly facets?: Readonly<Record<string, number>> | undefined;
|
|
122
|
+
readonly disabled?: boolean | undefined;
|
|
123
|
+
readonly onApply: (condition: FilterCondition, negated: boolean) => void;
|
|
124
|
+
readonly dateEditor?: FilterDateEditor | undefined;
|
|
125
|
+
}): JSX.Element {
|
|
126
|
+
const theme = useTheme();
|
|
127
|
+
const messages = createMemo(() => resolveTableMessages(theme.messages()));
|
|
128
|
+
const [operator, setOperator] = createSignal(props.initialOperator ?? initialOperator(props.condition));
|
|
129
|
+
const [text, setText] = createSignal(props.condition.kind === "text" ? props.condition.value : "");
|
|
130
|
+
const [start, setStart] = createSignal(startValue(props.condition));
|
|
131
|
+
const [end, setEnd] = createSignal(endValue(props.condition));
|
|
132
|
+
const [values, setValues] = createSignal<readonly string[]>(props.condition.kind === "enum" ? props.condition.values : []);
|
|
133
|
+
const [caseSensitive, setCaseSensitive] = createSignal(props.condition.kind === "text" && props.condition.caseSensitive);
|
|
134
|
+
const [negated, setNegated] = createSignal(props.directNegated);
|
|
135
|
+
const [error, setError] = createSignal<string | null>(null);
|
|
136
|
+
const numberFormatter = createMemo(() => new Intl.NumberFormat(theme.state().locale));
|
|
137
|
+
|
|
138
|
+
function buildCondition(): FilterCondition | null {
|
|
139
|
+
const currentOperator = operator();
|
|
140
|
+
if (currentOperator === "empty") return Object.freeze({ kind: "empty", column: props.definition.id });
|
|
141
|
+
if (props.definition.filter.type === "text") {
|
|
142
|
+
if (!textOperator(currentOperator)) return null;
|
|
143
|
+
return Object.freeze({ kind: "text", column: props.definition.id, operator: currentOperator, value: text(), caseSensitive: caseSensitive() });
|
|
144
|
+
}
|
|
145
|
+
if (props.definition.filter.type === "enum") {
|
|
146
|
+
if (currentOperator !== "in" || values().length === 0) return null;
|
|
147
|
+
return Object.freeze({ kind: "enum", column: props.definition.id, operator: "in", values: Object.freeze([...values()]) });
|
|
148
|
+
}
|
|
149
|
+
if (!numericOperator(currentOperator) || !start().trim()) return null;
|
|
150
|
+
if (props.definition.filter.type === "number") {
|
|
151
|
+
const first = Number(start());
|
|
152
|
+
if (!Number.isFinite(first)) return null;
|
|
153
|
+
if (currentOperator !== "between") return Object.freeze({ kind: "number", column: props.definition.id, operator: currentOperator, value: first });
|
|
154
|
+
if (!end().trim()) return null;
|
|
155
|
+
const last = Number(end());
|
|
156
|
+
if (!Number.isFinite(last) || first > last) return null;
|
|
157
|
+
return Object.freeze({ kind: "number", column: props.definition.id, operator: "between", min: first, max: last });
|
|
158
|
+
}
|
|
159
|
+
const first = parseDateInput(start());
|
|
160
|
+
if (first === null) return null;
|
|
161
|
+
if (currentOperator !== "between") return Object.freeze({ kind: "date", column: props.definition.id, operator: currentOperator, value: first });
|
|
162
|
+
const finalDay = parseDateInput(end());
|
|
163
|
+
if (finalDay === null) return null;
|
|
164
|
+
const last = finalDay + 86_400_000 - 1;
|
|
165
|
+
if (!Number.isFinite(last) || first > last) return null;
|
|
166
|
+
return Object.freeze({ kind: "date", column: props.definition.id, operator: "between", min: first, max: last });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const textOperators = () => [
|
|
170
|
+
{ value: "contains", label: messages().filterContains },
|
|
171
|
+
{ value: "eq", label: messages().filterEquals },
|
|
172
|
+
{ value: "startsWith", label: messages().filterStartsWith },
|
|
173
|
+
{ value: "endsWith", label: messages().filterEndsWith },
|
|
174
|
+
{ value: "empty", label: messages().filterIsEmpty },
|
|
175
|
+
];
|
|
176
|
+
const scalarOperators = () => [
|
|
177
|
+
{ value: "eq", label: messages().filterEquals },
|
|
178
|
+
{ value: "lt", label: messages().filterLessThan },
|
|
179
|
+
{ value: "lte", label: messages().filterAtMost },
|
|
180
|
+
{ value: "gt", label: messages().filterGreaterThan },
|
|
181
|
+
{ value: "gte", label: messages().filterAtLeast },
|
|
182
|
+
{ value: "between", label: messages().filterBetween },
|
|
183
|
+
{ value: "empty", label: messages().filterIsEmpty },
|
|
184
|
+
];
|
|
185
|
+
const enumOperators = () => [
|
|
186
|
+
{ value: "in", label: messages().filterIsAnyOf },
|
|
187
|
+
{ value: "empty", label: messages().filterIsEmpty },
|
|
188
|
+
];
|
|
189
|
+
const options = () => props.definition.filter.type === "text" ? textOperators() : props.definition.filter.type === "enum" ? enumOperators() : scalarOperators();
|
|
190
|
+
const enumOptions = () => props.definition.filter.type === "enum" ? props.definition.filter.options.map(value => {
|
|
191
|
+
const count = props.facets?.[value];
|
|
192
|
+
return { value, label: count === undefined ? value : `${value} (${numberFormatter().format(count)})` };
|
|
193
|
+
}) : [];
|
|
194
|
+
|
|
195
|
+
function apply(event: SubmitEvent): void {
|
|
196
|
+
event.preventDefault();
|
|
197
|
+
if (props.disabled) return;
|
|
198
|
+
const condition = buildCondition();
|
|
199
|
+
if (!condition) {
|
|
200
|
+
setError(messages().filterInvalid);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
setError(null);
|
|
204
|
+
props.onApply(condition, negated());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return <form class="sheen-filter-editor-form" onSubmit={apply}>
|
|
208
|
+
<Select label={messages().filterOperator} options={options()} value={operator()} onValueChange={value => { if (value) { setOperator(value); setError(null); } }} disabled={props.disabled ?? false} />
|
|
209
|
+
<Show when={operator() !== "empty" && props.definition.filter.type === "text"}>
|
|
210
|
+
<Input label={messages().filterValue} value={text()} onInput={event => setText(event.currentTarget.value)} disabled={props.disabled ?? false} />
|
|
211
|
+
<Checkbox label={messages().filterCaseSensitive} checked={caseSensitive()} onCheckedChange={setCaseSensitive} disabled={props.disabled ?? false} />
|
|
212
|
+
</Show>
|
|
213
|
+
<Show when={operator() !== "empty" && props.definition.filter.type === "number"}>
|
|
214
|
+
<Input type="number" inputMode="decimal" label={operator() === "between" ? messages().filterMinimum : messages().filterValue} value={start()} onInput={event => setStart(event.currentTarget.value)} disabled={props.disabled ?? false} />
|
|
215
|
+
<Show when={operator() === "between"}><Input type="number" inputMode="decimal" label={messages().filterMaximum} value={end()} onInput={event => setEnd(event.currentTarget.value)} disabled={props.disabled ?? false} /></Show>
|
|
216
|
+
</Show>
|
|
217
|
+
<Show when={operator() !== "empty" && props.definition.filter.type === "date"}>
|
|
218
|
+
<Show when={props.dateEditor} fallback={<>
|
|
219
|
+
<Input type="date" label={operator() === "between" ? messages().filterStartDate : messages().filterDate} value={start()} onInput={event => setStart(event.currentTarget.value)} disabled={props.disabled ?? false} />
|
|
220
|
+
<Show when={operator() === "between"}><Input type="date" label={messages().filterEndDate} value={end()} onInput={event => setEnd(event.currentTarget.value)} disabled={props.disabled ?? false} /></Show>
|
|
221
|
+
</>}>{editor => <Dynamic component={editor()} mode={operator() === "between" ? "range" : "single"}
|
|
222
|
+
label={operator() === "between" ? messages().filterStartDate : messages().filterDate} endLabel={messages().filterEndDate}
|
|
223
|
+
value={start()} endValue={end()} disabled={props.disabled ?? false}
|
|
224
|
+
onValueChange={(value, endValue) => { setStart(value); setEnd(endValue); setError(null); }} />}</Show>
|
|
225
|
+
</Show>
|
|
226
|
+
<Show when={operator() === "in" && props.definition.filter.type === "enum"}>
|
|
227
|
+
<CheckboxGroup label={messages().filterValues} options={enumOptions()} value={values()} onValueChange={setValues} disabled={props.disabled ?? false} />
|
|
228
|
+
</Show>
|
|
229
|
+
<Checkbox label={messages().filterNegated} {...(props.inheritedNegated ? { description: messages().filterInheritedNegation } : {})} checked={negated()} onCheckedChange={setNegated} disabled={props.disabled ?? false} />
|
|
230
|
+
<Show when={error()}>{message => <p class="sheen-filter-error" role="alert">{message()}</p>}</Show>
|
|
231
|
+
<Button type="submit" disabled={props.disabled}>{messages().filterApply}</Button>
|
|
232
|
+
</form>;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function operatorLabel(operator: string, messages: TableMessages): string {
|
|
236
|
+
if (operator === "empty") return messages.filterIsEmpty;
|
|
237
|
+
if (operator === "eq") return messages.filterEquals;
|
|
238
|
+
if (operator === "contains") return messages.filterContains;
|
|
239
|
+
if (operator === "startsWith") return messages.filterStartsWith;
|
|
240
|
+
if (operator === "endsWith") return messages.filterEndsWith;
|
|
241
|
+
if (operator === "lt") return messages.filterLessThan;
|
|
242
|
+
if (operator === "lte") return messages.filterAtMost;
|
|
243
|
+
if (operator === "gt") return messages.filterGreaterThan;
|
|
244
|
+
if (operator === "gte") return messages.filterAtLeast;
|
|
245
|
+
if (operator === "between") return messages.filterBetween;
|
|
246
|
+
return messages.filterIsAnyOf;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function newConditionOperator(definition: FilterDefinition): string {
|
|
250
|
+
if (definition.filter.type === "text") return "contains";
|
|
251
|
+
if (definition.filter.type === "enum") return "in";
|
|
252
|
+
if (definition.filter.type === "date") return "between";
|
|
253
|
+
return "eq";
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function conditionSummary(entry: FilterConditionEntry, definition: FilterDefinition, locale: string, messages: TableMessages): string {
|
|
257
|
+
const condition = entry.condition;
|
|
258
|
+
let value = "";
|
|
259
|
+
if (condition.kind === "text") value = condition.value;
|
|
260
|
+
else if (condition.kind === "number") {
|
|
261
|
+
const format = new Intl.NumberFormat(locale);
|
|
262
|
+
value = condition.operator === "between" ? `${format.format(condition.min)}–${format.format(condition.max)}` : format.format(condition.value);
|
|
263
|
+
} else if (condition.kind === "date") {
|
|
264
|
+
const format = new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeZone: "UTC" });
|
|
265
|
+
value = condition.operator === "between" ? `${format.format(condition.min)}–${format.format(condition.max)}` : format.format(condition.value);
|
|
266
|
+
} else if (condition.kind === "enum") value = new Intl.ListFormat(locale, { style: "short", type: "disjunction" }).format(condition.values);
|
|
267
|
+
const operator = operatorLabel(condition.kind === "empty" ? "empty" : condition.operator, messages);
|
|
268
|
+
const summary = messages.filterSummary.replace("{column}", definition.header).replace("{operator}", operator).replace("{value}", value).trim();
|
|
269
|
+
const directly = entry.directNegated ? messages.filterNot.replace("{filter}", summary) : summary;
|
|
270
|
+
return entry.inheritedNegated ? messages.filterInheritedNot.replace("{filter}", directly) : directly;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function ConditionChip(props: {
|
|
274
|
+
readonly record: EntryRecord;
|
|
275
|
+
readonly definition: () => FilterDefinition;
|
|
276
|
+
readonly facets?: Readonly<Record<string, Readonly<Record<string, number>>>> | undefined;
|
|
277
|
+
readonly disabled?: boolean | undefined;
|
|
278
|
+
readonly onApply: (key: string, entry: FilterConditionEntry, condition: FilterCondition, negated: boolean) => void;
|
|
279
|
+
readonly onRemove: (key: string, entry: FilterConditionEntry) => void;
|
|
280
|
+
readonly removeRef: (key: string, element: HTMLButtonElement) => void;
|
|
281
|
+
readonly dateEditor?: FilterDateEditor | undefined;
|
|
282
|
+
}): JSX.Element {
|
|
283
|
+
const theme = useTheme();
|
|
284
|
+
const messages = createMemo(() => resolveTableMessages(theme.messages()));
|
|
285
|
+
const [open, setOpen] = createSignal(false);
|
|
286
|
+
const summary = createMemo(() => conditionSummary(props.record.entry(), props.definition(), theme.state().locale, messages()));
|
|
287
|
+
return <li class="sheen-filter-chip" data-disabled={props.disabled || undefined}>
|
|
288
|
+
<Popover trigger={summary()} title={`${messages().filterEdit}: ${props.definition().header}`} open={open()} onOpenChange={setOpen} class="sheen-filter-editor">
|
|
289
|
+
<Show when={open()}><FilterEditor definition={props.definition()} condition={props.record.entry().condition} directNegated={props.record.entry().directNegated}
|
|
290
|
+
inheritedNegated={props.record.entry().inheritedNegated} facets={props.facets?.[props.definition().id]} disabled={props.disabled}
|
|
291
|
+
dateEditor={props.dateEditor}
|
|
292
|
+
onApply={(condition, negated) => { setOpen(false); props.onApply(props.record.key, props.record.entry(), condition, negated); }} /></Show>
|
|
293
|
+
</Popover>
|
|
294
|
+
<button ref={element => props.removeRef(props.record.key, element)} type="button" class="sheen-filter-remove" disabled={props.disabled}
|
|
295
|
+
aria-label={`${messages().filterRemove}: ${summary()}`} onClick={() => props.onRemove(props.record.key, props.record.entry())}><span aria-hidden="true">×</span></button>
|
|
296
|
+
</li>;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function FilterBar<Row extends object>(props: FilterBarProps<Row>): JSX.Element {
|
|
300
|
+
const theme = useTheme();
|
|
301
|
+
const messages = createMemo(() => resolveTableMessages(theme.messages()));
|
|
302
|
+
const definitions = readColumnDefinitions(props.columns).flatMap((column): readonly FilterDefinition[] => column.filter ? [Object.freeze({ id: column.id, header: column.header, filter: column.filter })] : []);
|
|
303
|
+
if (definitions.length === 0) throw new Error("FilterBar requires at least one filterable column");
|
|
304
|
+
const filterColumns = Object.freeze(definitions.map(schemaColumn));
|
|
305
|
+
const definitionById = new Map(definitions.map(definition => [definition.id, definition]));
|
|
306
|
+
const accepted = createMemo(() => parseFilter(props.value, filterColumns));
|
|
307
|
+
let sequence = 0;
|
|
308
|
+
function createEntryRecord(entry: FilterConditionEntry): EntryRecord {
|
|
309
|
+
const [current, update] = createSignal(entry);
|
|
310
|
+
return Object.freeze({ key: `filter-${++sequence}`, entry: current, update });
|
|
311
|
+
}
|
|
312
|
+
const [entries, setEntries] = createSignal<readonly EntryRecord[]>(Object.freeze(listFilterConditions(accepted()).map(createEntryRecord)), { equals: false });
|
|
313
|
+
createEffect(() => {
|
|
314
|
+
const nextEntries = listFilterConditions(accepted());
|
|
315
|
+
const previous = untrack(entries);
|
|
316
|
+
const available = new Set(previous);
|
|
317
|
+
const exactRecords = new Map<string, EntryRecord[]>();
|
|
318
|
+
const pathRecords = new Map<string, EntryRecord[]>();
|
|
319
|
+
for (const record of previous) untrack(() => {
|
|
320
|
+
addRecord(exactRecords, entrySignature(record.entry()), record);
|
|
321
|
+
addRecord(pathRecords, entryPath(record.entry()), record);
|
|
322
|
+
});
|
|
323
|
+
const next = nextEntries.map(entry => {
|
|
324
|
+
const existing = takeRecord(exactRecords, entrySignature(entry), available) ?? takeRecord(pathRecords, entryPath(entry), available);
|
|
325
|
+
if (!existing) return createEntryRecord(entry);
|
|
326
|
+
available.delete(existing);
|
|
327
|
+
existing.update(entry);
|
|
328
|
+
return existing;
|
|
329
|
+
});
|
|
330
|
+
setEntries(Object.freeze(next));
|
|
331
|
+
});
|
|
332
|
+
const [addOpen, setAddOpen] = createSignal(false);
|
|
333
|
+
const [selectedColumn, setSelectedColumn] = createSignal<string | null>(null);
|
|
334
|
+
const [search, setSearch] = createSignal("");
|
|
335
|
+
let root: HTMLDivElement | undefined;
|
|
336
|
+
let addRoot: HTMLDivElement | undefined;
|
|
337
|
+
|
|
338
|
+
const visibleColumns = createMemo(() => {
|
|
339
|
+
const query = search().trim().toLocaleLowerCase(theme.state().locale);
|
|
340
|
+
return query ? definitions.filter(definition => `${definition.header} ${definition.id}`.toLocaleLowerCase(theme.state().locale).includes(query)) : definitions;
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
function publish(filter: FilterNode): void {
|
|
344
|
+
if (props.disabled) return;
|
|
345
|
+
props.onChange(parseFilter(filter, filterColumns));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function updateCondition(key: string, entry: FilterConditionEntry, condition: FilterCondition, negated: boolean): void {
|
|
349
|
+
if (!entries().some(candidate => candidate.key === key)) return;
|
|
350
|
+
publish(replaceFilterCondition(accepted(), entry.path, condition, negated));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function removeCondition(key: string, entry: FilterConditionEntry): void {
|
|
354
|
+
const keys = entries().map(record => record.key);
|
|
355
|
+
const index = keys.indexOf(key);
|
|
356
|
+
const targetKey = keys[index + 1] ?? keys[index - 1];
|
|
357
|
+
publish(removeFilterCondition(accepted(), entry.path));
|
|
358
|
+
const view = root?.ownerDocument.defaultView;
|
|
359
|
+
view?.requestAnimationFrame(() => view.requestAnimationFrame(() => {
|
|
360
|
+
const target = targetKey
|
|
361
|
+
? [...root?.querySelectorAll<HTMLButtonElement>("button[data-filter-key]") ?? []].find(element => element.dataset.filterKey === targetKey)
|
|
362
|
+
: addRoot?.querySelector<HTMLButtonElement>("button");
|
|
363
|
+
if (target?.isConnected) target.focus({ preventScroll: true });
|
|
364
|
+
}));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const chosen = () => definitionById.get(selectedColumn() ?? "");
|
|
368
|
+
function resetPicker(): void { setSelectedColumn(null); setSearch(""); }
|
|
369
|
+
function changeAddOpen(open: boolean): void { setAddOpen(open); if (!open) resetPicker(); }
|
|
370
|
+
|
|
371
|
+
return <div ref={root} class={cn("sheen-filter-bar", props.class)} aria-label={messages().filters} data-disabled={props.disabled || undefined}>
|
|
372
|
+
<ul class="sheen-filter-list"><For each={entries()}>{record => {
|
|
373
|
+
const definition = () => {
|
|
374
|
+
const value = definitionById.get(record.entry().condition.column);
|
|
375
|
+
if (!value) throw new Error(`Missing filter definition for ${record.entry().condition.column}`);
|
|
376
|
+
return value;
|
|
377
|
+
};
|
|
378
|
+
return <ConditionChip record={record} definition={definition} facets={props.facets} disabled={props.disabled} dateEditor={props.dateEditor} onApply={updateCondition} onRemove={removeCondition}
|
|
379
|
+
removeRef={(key, element) => { element.dataset.filterKey = key; }} />;
|
|
380
|
+
}}</For></ul>
|
|
381
|
+
<div ref={addRoot} class="sheen-filter-add">
|
|
382
|
+
<Popover trigger={messages().filterAdd} title={chosen() ? `${messages().filterAdd}: ${chosen()?.header}` : messages().filterColumns}
|
|
383
|
+
open={addOpen()} onOpenChange={changeAddOpen} class="sheen-filter-picker">
|
|
384
|
+
<Show when={chosen()} fallback={<>
|
|
385
|
+
<Input type="search" label={messages().filterSearchColumns} value={search()} onInput={event => setSearch(event.currentTarget.value)} disabled={props.disabled} />
|
|
386
|
+
<ul class="sheen-filter-column-list"><For each={visibleColumns()}>{definition => <li><Button variant="ghost" disabled={props.disabled} onClick={() => setSelectedColumn(definition.id)}>{definition.header}</Button></li>}</For></ul>
|
|
387
|
+
<Show when={visibleColumns().length === 0}><p>{messages().noResults}</p></Show>
|
|
388
|
+
</>}>{definition => <FilterEditor definition={definition()} condition={Object.freeze({ kind: "empty", column: definition().id })} initialOperator={newConditionOperator(definition())} directNegated={false}
|
|
389
|
+
inheritedNegated={false} facets={props.facets?.[definition().id]} disabled={props.disabled} dateEditor={props.dateEditor} onApply={(condition, negated) => {
|
|
390
|
+
changeAddOpen(false);
|
|
391
|
+
publish(appendFilterCondition(accepted(), condition, negated));
|
|
392
|
+
}} />}</Show>
|
|
393
|
+
</Popover>
|
|
394
|
+
</div>
|
|
395
|
+
</div>;
|
|
396
|
+
}
|