@olenbetong/appframe-ds 0.7.0 → 0.9.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/CHANGELOG.md +175 -0
- package/package.json +52 -8
- package/scripts/copyAssets.mjs +13 -0
- package/src/AfLookup/LookupCombobox.css +1 -1
- package/src/AfLookup/LookupCombobox.tsx +3 -1
- package/src/autocomplete/Autocomplete.css +63 -42
- package/src/autocomplete/Autocomplete.tsx +2 -0
- package/src/autocomplete/AutocompleteFrame.tsx +21 -2
- package/src/autocomplete/Combobox.tsx +15 -2
- package/src/autocomplete/types.ts +4 -0
- package/src/container/Container.tsx +49 -0
- package/src/container/index.ts +1 -0
- package/src/filter/ChipListInput.tsx +79 -0
- package/src/filter/FieldFilterPanel.css +127 -0
- package/src/filter/FieldFilterPanel.tsx +309 -0
- package/src/filter/FilterBuilder.css +152 -0
- package/src/filter/FilterBuilder.test.tsx +153 -0
- package/src/filter/FilterBuilder.tsx +435 -0
- package/src/filter/FilterEditor.css +231 -0
- package/src/filter/FilterEditor.test.tsx +259 -0
- package/src/filter/FilterEditor.tsx +74 -0
- package/src/filter/FilterGroupEditor.tsx +160 -0
- package/src/filter/FilterNameDialog.css +22 -0
- package/src/filter/FilterNameDialog.tsx +81 -0
- package/src/filter/FilterRow.tsx +117 -0
- package/src/filter/FilterShareDialog.css +28 -0
- package/src/filter/FilterShareDialog.tsx +201 -0
- package/src/filter/FilterStringField.tsx +77 -0
- package/src/filter/FilterValueEditor.tsx +220 -0
- package/src/filter/SavedFilterTree.css +107 -0
- package/src/filter/SavedFilterTree.test.tsx +94 -0
- package/src/filter/SavedFilterTree.tsx +222 -0
- package/src/filter/fieldFilter.test.ts +158 -0
- package/src/filter/fieldFilter.ts +139 -0
- package/src/filter/index.ts +27 -0
- package/src/filter/types.ts +38 -0
- package/src/filter/useDistinctValues.test.ts +102 -0
- package/src/filter/useDistinctValues.ts +230 -0
- package/src/global.d.ts +11 -0
- package/src/grid/AfGridColumnsPanel.tsx +118 -0
- package/src/grid/AfGridContext.tsx +51 -0
- package/src/grid/AfGridError.tsx +46 -0
- package/src/grid/AfHeaderFilterCell.tsx +151 -0
- package/src/grid/AfHeaderFilterPanel.tsx +111 -0
- package/src/grid/Toolbar.tsx +233 -0
- package/src/grid/editing.ts +90 -0
- package/src/grid/filter.ts +78 -0
- package/src/grid/formatters.ts +48 -0
- package/src/grid/index.css +187 -0
- package/src/grid/index.tsx +376 -0
- package/src/grid/license.ts +48 -0
- package/src/grid/localization.ts +369 -0
- package/src/grid/slots/index.tsx +275 -0
- package/src/grid/slots/slots.css +53 -0
- package/src/grid/theme.tsx +185 -0
- package/src/grid/useAfColumns.tsx +313 -0
- package/src/grid/useAfCurrentIndex.ts +55 -0
- package/src/grid/useAfData.ts +20 -0
- package/src/grid/useAfFilter.ts +160 -0
- package/src/grid/useAfFilterFields.ts +55 -0
- package/src/grid/useAfGridApi.ts +66 -0
- package/src/grid/useAfKeyBindings.ts +36 -0
- package/src/grid/useAfPagination.ts +53 -0
- package/src/grid/useAfPersistedState.ts +222 -0
- package/src/grid/useAfRowEditModel.ts +137 -0
- package/src/grid/useAfSortModel.ts +72 -0
- package/src/index.ts +2 -0
- package/src/input/InputAdornments.tsx +6 -1
- package/src/paper/Paper.tsx +2 -0
- package/src/report-downloader/ReportDownloader.css +67 -0
- package/src/report-downloader/components/StatusItem.tsx +86 -0
- package/src/report-downloader/components/StatusList.tsx +28 -0
- package/src/report-downloader/index.ts +41 -0
- package/src/report-downloader/status.tsx +92 -0
- package/src/test/setup.ts +8 -0
- package/vitest.config.ts +10 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import type { FieldMetadata, FilterExpression, FilterTree } from "@olenbetong/appframe-core";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { FilterEditor } from "./FilterEditor.js";
|
|
7
|
+
|
|
8
|
+
const FIELDS: FieldMetadata[] = [
|
|
9
|
+
{ name: "PersonID", caption: "Person ID", type: "string", nullable: true },
|
|
10
|
+
{ name: "Age", caption: "Age", type: "number" },
|
|
11
|
+
{ name: "Created", caption: "Created", type: "datetime" },
|
|
12
|
+
{ name: "IsActive", caption: "Is active", type: "boolean" },
|
|
13
|
+
{ name: "Secret", caption: "Secret", type: "string", excludeFromFilter: true },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/** A tree with a single root `and` group holding one expression. */
|
|
17
|
+
function filterWith(column: string, operator: string, value: unknown, valueType = "string"): FilterTree {
|
|
18
|
+
return {
|
|
19
|
+
type: "group",
|
|
20
|
+
mode: "and",
|
|
21
|
+
items: [{ type: "expression", column, operator, value, valueType } as never],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function firstExpression(onChange: ReturnType<typeof vi.fn>, call = 0) {
|
|
26
|
+
let [filter] = onChange.mock.calls[call] as [FilterTree];
|
|
27
|
+
|
|
28
|
+
return filter.items[0] as FilterExpression;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("FilterEditor", () => {
|
|
32
|
+
it("renders one row per expression", () => {
|
|
33
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} />);
|
|
34
|
+
|
|
35
|
+
expect(screen.getAllByLabelText("Field")).toHaveLength(1);
|
|
36
|
+
expect(screen.getByLabelText("Operator")).toHaveValue("beginswith");
|
|
37
|
+
expect(screen.getByLabelText("Value")).toHaveValue("silv");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("wraps a bare expression in a group", () => {
|
|
41
|
+
let filter = {
|
|
42
|
+
type: "expression",
|
|
43
|
+
column: "Age",
|
|
44
|
+
operator: "equals",
|
|
45
|
+
value: "42",
|
|
46
|
+
valueType: "number",
|
|
47
|
+
} as never as FilterTree;
|
|
48
|
+
|
|
49
|
+
render(<FilterEditor fields={FIELDS} value={filter} />);
|
|
50
|
+
|
|
51
|
+
expect(screen.getByLabelText("Value")).toHaveValue(42);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("hides fields marked as excluded from the field picker", async () => {
|
|
55
|
+
let user = userEvent.setup();
|
|
56
|
+
render(<FilterEditor fields={FIELDS} value={{ type: "group", mode: "and", items: [] }} />);
|
|
57
|
+
|
|
58
|
+
await user.click(screen.getByLabelText("Add condition"));
|
|
59
|
+
|
|
60
|
+
expect(screen.queryByRole("option", { name: "Secret" })).not.toBeInTheDocument();
|
|
61
|
+
expect(screen.getByRole("option", { name: "Person ID" })).toBeInTheDocument();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("adds an expression when a field is picked in the blank row", async () => {
|
|
65
|
+
let user = userEvent.setup();
|
|
66
|
+
let onChange = vi.fn();
|
|
67
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} onChange={onChange} />);
|
|
68
|
+
|
|
69
|
+
await user.click(screen.getByLabelText("Add condition"));
|
|
70
|
+
await user.click(screen.getByRole("option", { name: "Age" }));
|
|
71
|
+
|
|
72
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
73
|
+
let [filter] = onChange.mock.calls[0] as [FilterTree];
|
|
74
|
+
|
|
75
|
+
expect(filter.items).toHaveLength(2);
|
|
76
|
+
// Numbers default to "equals", strings to "begins with".
|
|
77
|
+
expect(filter.items[1]).toMatchObject({ column: "Age", operator: "equals", valueType: "number" });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("changes the operator and resets the value when the arity changes", async () => {
|
|
81
|
+
let user = userEvent.setup();
|
|
82
|
+
let onChange = vi.fn();
|
|
83
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("Age", "equals", "42", "number")} onChange={onChange} />);
|
|
84
|
+
|
|
85
|
+
await user.selectOptions(screen.getByLabelText("Operator"), "between");
|
|
86
|
+
|
|
87
|
+
expect(firstExpression(onChange)).toMatchObject({ operator: "between", value: ["", ""] });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("renders no value editor for parameterless operators", () => {
|
|
91
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("IsActive", "istrue", null, "null")} />);
|
|
92
|
+
|
|
93
|
+
expect(screen.queryByLabelText("Value")).not.toBeInTheDocument();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("renders two inputs for range operators", () => {
|
|
97
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("Age", "between", ["1", "10"], "number")} />);
|
|
98
|
+
|
|
99
|
+
expect(screen.getByLabelText("From")).toHaveValue(1);
|
|
100
|
+
expect(screen.getByLabelText("To")).toHaveValue(10);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("renders one chip per value for list operators", () => {
|
|
104
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "inlist", ["a", "b"])} />);
|
|
105
|
+
|
|
106
|
+
expect(screen.getByRole("button", { name: "Remove a" })).toBeInTheDocument();
|
|
107
|
+
expect(screen.getByRole("button", { name: "Remove b" })).toBeInTheDocument();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("adds a list value on Enter", async () => {
|
|
111
|
+
let user = userEvent.setup();
|
|
112
|
+
let onChange = vi.fn();
|
|
113
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "inlist", ["a"])} onChange={onChange} />);
|
|
114
|
+
|
|
115
|
+
await user.type(screen.getByLabelText("Value"), "b{Enter}");
|
|
116
|
+
|
|
117
|
+
let [filter] = onChange.mock.calls.at(-1) as [FilterTree];
|
|
118
|
+
|
|
119
|
+
expect((filter.items[0] as FilterExpression).value).toEqual(["a", "b"]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("switches a date value to a relative value", async () => {
|
|
123
|
+
let user = userEvent.setup();
|
|
124
|
+
let onChange = vi.fn();
|
|
125
|
+
render(
|
|
126
|
+
<FilterEditor fields={FIELDS} value={filterWith("Created", "equals", "", "datetime")} onChange={onChange} />,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
await user.click(screen.getByRole("button", { name: "Use a relative value" }));
|
|
130
|
+
|
|
131
|
+
expect(firstExpression(onChange)).toMatchObject({ value: "Yesterday", valueType: "special" });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("does not offer relative values for numbers", () => {
|
|
135
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("Age", "equals", "1", "number")} />);
|
|
136
|
+
|
|
137
|
+
expect(screen.queryByRole("button", { name: "Use a relative value" })).not.toBeInTheDocument();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("removes a condition", async () => {
|
|
141
|
+
let user = userEvent.setup();
|
|
142
|
+
let onChange = vi.fn();
|
|
143
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} onChange={onChange} />);
|
|
144
|
+
|
|
145
|
+
await user.click(screen.getByRole("button", { name: "Remove condition" }));
|
|
146
|
+
|
|
147
|
+
let [filter] = onChange.mock.calls[0] as [FilterTree];
|
|
148
|
+
|
|
149
|
+
expect(filter.items).toHaveLength(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("hides every editing affordance when read only", () => {
|
|
153
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} readOnly />);
|
|
154
|
+
|
|
155
|
+
expect(screen.queryByLabelText("Add condition")).not.toBeInTheDocument();
|
|
156
|
+
expect(screen.queryByRole("button", { name: "Remove condition" })).not.toBeInTheDocument();
|
|
157
|
+
expect(screen.queryByRole("button", { name: "Add group" })).not.toBeInTheDocument();
|
|
158
|
+
expect(screen.getByLabelText("Group operator")).toBeDisabled();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("keeps a row editable when the field is not in the view", () => {
|
|
162
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("Removed", "equals", "x")} />);
|
|
163
|
+
|
|
164
|
+
expect(screen.getByLabelText("Field")).toHaveValue("Removed");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("uses supplied options as a value list", () => {
|
|
168
|
+
render(
|
|
169
|
+
<FilterEditor
|
|
170
|
+
fields={FIELDS}
|
|
171
|
+
value={filterWith("PersonID", "equals", "a")}
|
|
172
|
+
getFieldOptions={(field) =>
|
|
173
|
+
field.name === "PersonID"
|
|
174
|
+
? [
|
|
175
|
+
{ value: "a", label: "Alpha" },
|
|
176
|
+
{ value: "b", label: "Bravo" },
|
|
177
|
+
]
|
|
178
|
+
: undefined
|
|
179
|
+
}
|
|
180
|
+
/>,
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
expect(screen.getByLabelText("Value")).toHaveValue("Alpha");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("changes the mode of the root group", async () => {
|
|
187
|
+
let user = userEvent.setup();
|
|
188
|
+
let onChange = vi.fn();
|
|
189
|
+
render(<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} onChange={onChange} />);
|
|
190
|
+
|
|
191
|
+
expect(screen.getByLabelText("Group operator")).toHaveValue("and");
|
|
192
|
+
|
|
193
|
+
await user.selectOptions(screen.getByLabelText("Group operator"), "or");
|
|
194
|
+
|
|
195
|
+
let [filter] = onChange.mock.calls[0] as [FilterTree];
|
|
196
|
+
|
|
197
|
+
expect(filter.mode).toBe("or");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("adds and removes nested groups", async () => {
|
|
201
|
+
let user = userEvent.setup();
|
|
202
|
+
let onChange = vi.fn();
|
|
203
|
+
let { rerender } = render(
|
|
204
|
+
<FilterEditor fields={FIELDS} value={filterWith("PersonID", "beginswith", "silv")} onChange={onChange} />,
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
expect(screen.queryByRole("button", { name: "Remove group" })).not.toBeInTheDocument();
|
|
208
|
+
|
|
209
|
+
await user.click(screen.getByRole("button", { name: "Add group" }));
|
|
210
|
+
|
|
211
|
+
let [withGroup] = onChange.mock.calls[0] as [FilterTree];
|
|
212
|
+
expect(withGroup.items).toHaveLength(2);
|
|
213
|
+
// A nested group defaults to the opposite mode of its parent.
|
|
214
|
+
expect(withGroup.items[1]).toMatchObject({ type: "group", mode: "or", items: [] });
|
|
215
|
+
|
|
216
|
+
rerender(<FilterEditor fields={FIELDS} value={withGroup} onChange={onChange} />);
|
|
217
|
+
expect(screen.getAllByLabelText("Group operator")).toHaveLength(2);
|
|
218
|
+
|
|
219
|
+
await user.click(screen.getByRole("button", { name: "Remove group" }));
|
|
220
|
+
|
|
221
|
+
let [removed] = onChange.mock.calls[1] as [FilterTree];
|
|
222
|
+
expect(removed.items).toHaveLength(1);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("edits a condition inside a nested group", async () => {
|
|
226
|
+
let user = userEvent.setup();
|
|
227
|
+
let onChange = vi.fn();
|
|
228
|
+
let filter: FilterTree = {
|
|
229
|
+
type: "group",
|
|
230
|
+
mode: "and",
|
|
231
|
+
items: [
|
|
232
|
+
{ type: "expression", column: "Age", operator: "equals", value: "1", valueType: "number" },
|
|
233
|
+
{
|
|
234
|
+
type: "group",
|
|
235
|
+
mode: "or",
|
|
236
|
+
items: [{ type: "expression", column: "PersonID", operator: "beginswith", value: "a", valueType: "string" }],
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
};
|
|
240
|
+
render(<FilterEditor fields={FIELDS} value={filter} onChange={onChange} />);
|
|
241
|
+
|
|
242
|
+
await user.click(screen.getAllByRole("button", { name: "Remove condition" })[1]);
|
|
243
|
+
|
|
244
|
+
let [next] = onChange.mock.calls[0] as [FilterTree];
|
|
245
|
+
|
|
246
|
+
expect(next.items[0]).toMatchObject({ column: "Age" });
|
|
247
|
+
expect(next.items[1]).toMatchObject({ type: "group", items: [] });
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it("keeps its own state when uncontrolled", async () => {
|
|
251
|
+
let user = userEvent.setup();
|
|
252
|
+
render(<FilterEditor fields={FIELDS} defaultValue={filterWith("PersonID", "beginswith", "silv")} />);
|
|
253
|
+
|
|
254
|
+
await user.clear(screen.getByLabelText("Value"));
|
|
255
|
+
await user.type(screen.getByLabelText("Value"), "gold");
|
|
256
|
+
|
|
257
|
+
expect(screen.getByLabelText("Value")).toHaveValue("gold");
|
|
258
|
+
});
|
|
259
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "./FilterEditor.css";
|
|
2
|
+
|
|
3
|
+
import { type FilterTree, getLocalizedString, toFilterTree } from "@olenbetong/appframe-core";
|
|
4
|
+
import clsx from "clsx";
|
|
5
|
+
import { useMemo, useState } from "react";
|
|
6
|
+
|
|
7
|
+
import { FilterGroupEditor } from "./FilterGroupEditor.js";
|
|
8
|
+
import { FilterStringField } from "./FilterStringField.js";
|
|
9
|
+
import type { FilterEditorProps } from "./types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The criteria tree of the filter builder. Groups can be nested and each group
|
|
13
|
+
* combines its items with either `and` or `or`, matching the advanced filter
|
|
14
|
+
* editor in the Windows client.
|
|
15
|
+
*
|
|
16
|
+
* Works both controlled (`value` + `onChange`) and uncontrolled
|
|
17
|
+
* (`defaultValue`). Saving and sharing filters is handled by `FilterBuilder`,
|
|
18
|
+
* which composes this component.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```jsx
|
|
22
|
+
* <FilterEditor fields={fields} value={filter} onChange={setFilter} showFilterString />
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function FilterEditor({
|
|
26
|
+
fields,
|
|
27
|
+
value,
|
|
28
|
+
defaultValue,
|
|
29
|
+
onChange,
|
|
30
|
+
readOnly,
|
|
31
|
+
showFilterString,
|
|
32
|
+
viewName,
|
|
33
|
+
getFieldOptions,
|
|
34
|
+
className,
|
|
35
|
+
"data-size": size = "sm",
|
|
36
|
+
}: FilterEditorProps) {
|
|
37
|
+
let [uncontrolled, setUncontrolled] = useState<FilterTree>(() => toFilterTree(defaultValue));
|
|
38
|
+
let isControlled = value !== undefined;
|
|
39
|
+
// Normalizing on every render would create a new tree each time, which
|
|
40
|
+
// retriggers the filter string conversion.
|
|
41
|
+
let controlled = useMemo(() => toFilterTree(value), [value]);
|
|
42
|
+
let filter = isControlled ? controlled : uncontrolled;
|
|
43
|
+
|
|
44
|
+
let visibleFields = useMemo(() => fields.filter((field) => !field.excludeFromFilter), [fields]);
|
|
45
|
+
|
|
46
|
+
function update(next: FilterTree) {
|
|
47
|
+
if (!isControlled) setUncontrolled(next);
|
|
48
|
+
onChange?.(next);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<div className={clsx("ObFilterEditor-root", className)} data-size={size}>
|
|
53
|
+
<div className="ObFilterEditor-body">
|
|
54
|
+
<p className="ObFilterEditor-intro">
|
|
55
|
+
{filter.mode === "and"
|
|
56
|
+
? getLocalizedString("Find rows that match all of these criteria:")
|
|
57
|
+
: getLocalizedString("Find rows that match one or more of these criteria:")}
|
|
58
|
+
</p>
|
|
59
|
+
<FilterGroupEditor
|
|
60
|
+
filter={filter}
|
|
61
|
+
path={[]}
|
|
62
|
+
fields={visibleFields}
|
|
63
|
+
getFieldOptions={getFieldOptions}
|
|
64
|
+
readOnly={readOnly}
|
|
65
|
+
data-size={size}
|
|
66
|
+
onChange={update}
|
|
67
|
+
/>
|
|
68
|
+
</div>
|
|
69
|
+
{showFilterString && (
|
|
70
|
+
<FilterStringField filter={filter} viewName={viewName} readOnly={readOnly} data-size={size} onChange={update} />
|
|
71
|
+
)}
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Button, Select } from "@digdir/designsystemet-react";
|
|
2
|
+
import { FolderPlusIcon, TrashIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import {
|
|
4
|
+
addGroupAtPath,
|
|
5
|
+
createExpression,
|
|
6
|
+
type FieldMetadata,
|
|
7
|
+
type FilterExpression,
|
|
8
|
+
type FilterGroup,
|
|
9
|
+
type FilterNode,
|
|
10
|
+
type FilterPath,
|
|
11
|
+
type FilterTree,
|
|
12
|
+
getLocalizedString,
|
|
13
|
+
getNodeAtPath,
|
|
14
|
+
insertNodeAtPath,
|
|
15
|
+
isExpression,
|
|
16
|
+
isGroup,
|
|
17
|
+
removeNodeAtPath,
|
|
18
|
+
setGroupMode,
|
|
19
|
+
updateNodeAtPath,
|
|
20
|
+
} from "@olenbetong/appframe-core";
|
|
21
|
+
import { Combobox } from "../autocomplete/index.js";
|
|
22
|
+
|
|
23
|
+
import { FilterRow, getFieldLabel } from "./FilterRow.js";
|
|
24
|
+
import type { FilterEditorSize, GetFieldOptions } from "./types.js";
|
|
25
|
+
|
|
26
|
+
export type FilterGroupEditorProps = {
|
|
27
|
+
/** The whole tree. Groups edit it through their own `path`. */
|
|
28
|
+
filter: FilterTree;
|
|
29
|
+
/** Path to the group this editor renders. The root group is the empty path. */
|
|
30
|
+
path: FilterPath;
|
|
31
|
+
fields: FieldMetadata[];
|
|
32
|
+
onChange: (filter: FilterTree) => void;
|
|
33
|
+
getFieldOptions?: GetFieldOptions;
|
|
34
|
+
readOnly?: boolean;
|
|
35
|
+
"data-size"?: FilterEditorSize;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One group of the filter tree. Groups can be nested and each one combines its
|
|
40
|
+
* items with either `and` or `or`, matching the advanced (DevExpress) filter
|
|
41
|
+
* editor in the Windows client.
|
|
42
|
+
*
|
|
43
|
+
* A blank row is always rendered at the end; picking a field in it appends a
|
|
44
|
+
* real condition to the group.
|
|
45
|
+
*/
|
|
46
|
+
export function FilterGroupEditor({
|
|
47
|
+
filter,
|
|
48
|
+
path,
|
|
49
|
+
fields,
|
|
50
|
+
onChange,
|
|
51
|
+
getFieldOptions,
|
|
52
|
+
readOnly,
|
|
53
|
+
"data-size": size = "sm",
|
|
54
|
+
}: FilterGroupEditorProps) {
|
|
55
|
+
let node = getNodeAtPath(filter, path);
|
|
56
|
+
if (!node || !isGroup(node)) return null;
|
|
57
|
+
|
|
58
|
+
let group: FilterGroup = node;
|
|
59
|
+
let isRoot = path.length === 0;
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<div className="ObFilterEditor-group" data-root={isRoot ? "" : undefined}>
|
|
63
|
+
<div className="ObFilterEditor-groupHeader">
|
|
64
|
+
<Select
|
|
65
|
+
className="ObFilterEditor-groupMode"
|
|
66
|
+
data-size={size}
|
|
67
|
+
disabled={readOnly}
|
|
68
|
+
aria-label={getLocalizedString("Group operator")}
|
|
69
|
+
value={group.mode}
|
|
70
|
+
onChange={(event) => onChange(setGroupMode(filter, path, event.target.value as FilterGroup["mode"]))}
|
|
71
|
+
>
|
|
72
|
+
<Select.Option value="and">{getLocalizedString("And")}</Select.Option>
|
|
73
|
+
<Select.Option value="or">{getLocalizedString("Or")}</Select.Option>
|
|
74
|
+
</Select>
|
|
75
|
+
{!readOnly && (
|
|
76
|
+
<>
|
|
77
|
+
<Button
|
|
78
|
+
type="button"
|
|
79
|
+
variant="tertiary"
|
|
80
|
+
data-size={size}
|
|
81
|
+
className="ObFilterEditor-groupButton"
|
|
82
|
+
aria-label={getLocalizedString("Add group")}
|
|
83
|
+
onClick={() => onChange(addGroupAtPath(filter, path))}
|
|
84
|
+
>
|
|
85
|
+
<FolderPlusIcon aria-hidden />
|
|
86
|
+
<span className="ObFilterEditor-groupButtonLabel">{getLocalizedString("Add group")}</span>
|
|
87
|
+
</Button>
|
|
88
|
+
{!isRoot && (
|
|
89
|
+
<Button
|
|
90
|
+
type="button"
|
|
91
|
+
variant="tertiary"
|
|
92
|
+
data-color="danger"
|
|
93
|
+
data-size={size}
|
|
94
|
+
className="ObFilterEditor-groupButton"
|
|
95
|
+
aria-label={getLocalizedString("Remove group")}
|
|
96
|
+
onClick={() => onChange(removeNodeAtPath(filter, path))}
|
|
97
|
+
>
|
|
98
|
+
<TrashIcon aria-hidden />
|
|
99
|
+
<span className="ObFilterEditor-groupButtonLabel">{getLocalizedString("Remove group")}</span>
|
|
100
|
+
</Button>
|
|
101
|
+
)}
|
|
102
|
+
</>
|
|
103
|
+
)}
|
|
104
|
+
</div>
|
|
105
|
+
<div className="ObFilterEditor-groupItems">
|
|
106
|
+
{group.items.map((item, index) =>
|
|
107
|
+
isExpression(item) ? (
|
|
108
|
+
<FilterRow
|
|
109
|
+
// Rows have no stable identity, and reordering is not supported,
|
|
110
|
+
// so the index is the only key available.
|
|
111
|
+
// oxlint-disable-next-line no-array-index-key -- see above
|
|
112
|
+
key={index}
|
|
113
|
+
expression={item}
|
|
114
|
+
fields={fields}
|
|
115
|
+
getFieldOptions={getFieldOptions}
|
|
116
|
+
readOnly={readOnly}
|
|
117
|
+
data-size={size}
|
|
118
|
+
onChange={(next) => onChange(replaceNode(filter, [...path, index], next))}
|
|
119
|
+
onRemove={() => onChange(removeNodeAtPath(filter, [...path, index]))}
|
|
120
|
+
/>
|
|
121
|
+
) : (
|
|
122
|
+
<FilterGroupEditor
|
|
123
|
+
// oxlint-disable-next-line no-array-index-key -- see above
|
|
124
|
+
key={index}
|
|
125
|
+
filter={filter}
|
|
126
|
+
path={[...path, index]}
|
|
127
|
+
fields={fields}
|
|
128
|
+
getFieldOptions={getFieldOptions}
|
|
129
|
+
readOnly={readOnly}
|
|
130
|
+
data-size={size}
|
|
131
|
+
onChange={onChange}
|
|
132
|
+
/>
|
|
133
|
+
),
|
|
134
|
+
)}
|
|
135
|
+
{!readOnly && (
|
|
136
|
+
<div className="ObFilterEditor-row ObFilterEditor-draftRow">
|
|
137
|
+
<Combobox
|
|
138
|
+
className="ObFilterEditor-field"
|
|
139
|
+
data-size={size}
|
|
140
|
+
aria-label={getLocalizedString("Add condition")}
|
|
141
|
+
placeholder={getLocalizedString("Add condition")}
|
|
142
|
+
options={fields}
|
|
143
|
+
getOptionLabel={getFieldLabel}
|
|
144
|
+
getOptionKey={(option) => option.name}
|
|
145
|
+
isOptionEqualToValue={(a, b) => a.name === b.name}
|
|
146
|
+
value={null}
|
|
147
|
+
onChange={(_event, field) => {
|
|
148
|
+
if (field) onChange(insertNodeAtPath(filter, path, createExpression(field)));
|
|
149
|
+
}}
|
|
150
|
+
/>
|
|
151
|
+
</div>
|
|
152
|
+
)}
|
|
153
|
+
</div>
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function replaceNode(filter: FilterTree, path: FilterPath, node: FilterExpression): FilterTree {
|
|
159
|
+
return updateNodeAtPath<FilterNode>(filter, path, () => node) as FilterTree;
|
|
160
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Colours use the contextual `--ds-color-*` aliases so the dialog follows the
|
|
3
|
+
* active colour scheme. Never add hex fallbacks.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Designsystemet drops the dialog's own padding when its children are blocks,
|
|
8
|
+
* but the rule only looks at direct children, and the blocks here are wrapped
|
|
9
|
+
* in the form. Without this the blocks are inset and their dividers stop short
|
|
10
|
+
* of the dialog's edges.
|
|
11
|
+
*/
|
|
12
|
+
.ObFilterNameDialog-root {
|
|
13
|
+
inline-size: min(28rem, 100%);
|
|
14
|
+
padding: 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.ObFilterNameDialog-actions {
|
|
18
|
+
display: flex;
|
|
19
|
+
flex-direction: row;
|
|
20
|
+
justify-content: flex-end;
|
|
21
|
+
gap: var(--ds-size-2);
|
|
22
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import "./FilterNameDialog.css";
|
|
2
|
+
|
|
3
|
+
import { Button, Dialog, Heading, Textfield } from "@digdir/designsystemet-react";
|
|
4
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
5
|
+
import { useEffect, useState } from "react";
|
|
6
|
+
|
|
7
|
+
export type FilterNameDialogProps = {
|
|
8
|
+
open: boolean;
|
|
9
|
+
/** Pre-fills the field, e.g. with the current filter's name. */
|
|
10
|
+
defaultName?: string;
|
|
11
|
+
/** Names already in use; entering one asks the user to confirm the overwrite. */
|
|
12
|
+
existingNames?: string[];
|
|
13
|
+
onCancel: () => void;
|
|
14
|
+
onSubmit: (name: string) => void;
|
|
15
|
+
"data-size"?: "sm" | "md" | "lg";
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Asks for the name to save a filter under.
|
|
20
|
+
*
|
|
21
|
+
* Reusing an existing name overwrites that filter, so the dialog warns before
|
|
22
|
+
* submitting rather than silently creating a second filter with the same name.
|
|
23
|
+
*/
|
|
24
|
+
export function FilterNameDialog({
|
|
25
|
+
open,
|
|
26
|
+
defaultName = "",
|
|
27
|
+
existingNames = [],
|
|
28
|
+
onCancel,
|
|
29
|
+
onSubmit,
|
|
30
|
+
"data-size": size = "sm",
|
|
31
|
+
}: FilterNameDialogProps) {
|
|
32
|
+
let [name, setName] = useState(defaultName);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (open) setName(defaultName);
|
|
36
|
+
}, [open, defaultName]);
|
|
37
|
+
|
|
38
|
+
let trimmed = name.trim();
|
|
39
|
+
// A filter keeping its own name is an update, not a collision.
|
|
40
|
+
let collides = trimmed !== defaultName && existingNames.some((existing) => existing === trimmed);
|
|
41
|
+
|
|
42
|
+
function handleSubmit(event: React.FormEvent) {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
if (!trimmed) return;
|
|
45
|
+
|
|
46
|
+
onSubmit(trimmed);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<Dialog open={open} closedby="any" data-size={size} className="ObFilterNameDialog-root" onClose={onCancel}>
|
|
51
|
+
<form onSubmit={handleSubmit}>
|
|
52
|
+
<Dialog.Block>
|
|
53
|
+
<Heading level={2} data-size="xs">
|
|
54
|
+
{getLocalizedString("Save filter")}
|
|
55
|
+
</Heading>
|
|
56
|
+
</Dialog.Block>
|
|
57
|
+
<Dialog.Block>
|
|
58
|
+
<Textfield
|
|
59
|
+
// oxlint-disable-next-line jsx-a11y/no-autofocus -- the field is the only control in the dialog
|
|
60
|
+
autoFocus
|
|
61
|
+
data-size={size}
|
|
62
|
+
label={getLocalizedString("Filter name")}
|
|
63
|
+
value={name}
|
|
64
|
+
error={
|
|
65
|
+
collides ? getLocalizedString("A filter with this name already exists and will be replaced.") : undefined
|
|
66
|
+
}
|
|
67
|
+
onChange={(event) => setName(event.currentTarget.value)}
|
|
68
|
+
/>
|
|
69
|
+
</Dialog.Block>
|
|
70
|
+
<Dialog.Block className="ObFilterNameDialog-actions">
|
|
71
|
+
<Button type="submit" data-size={size} disabled={!trimmed}>
|
|
72
|
+
{collides ? getLocalizedString("Replace") : getLocalizedString("Save")}
|
|
73
|
+
</Button>
|
|
74
|
+
<Button type="button" variant="secondary" data-size={size} onClick={onCancel}>
|
|
75
|
+
{getLocalizedString("Cancel")}
|
|
76
|
+
</Button>
|
|
77
|
+
</Dialog.Block>
|
|
78
|
+
</form>
|
|
79
|
+
</Dialog>
|
|
80
|
+
);
|
|
81
|
+
}
|