@olenbetong/appframe-ds 0.8.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 +70 -0
- package/package.json +50 -6
- package/scripts/copyAssets.mjs +13 -0
- package/src/AfLookup/LookupCombobox.tsx +3 -1
- 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 +1 -0
- package/src/input/InputAdornments.tsx +6 -1
- 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,309 @@
|
|
|
1
|
+
import "./FieldFilterPanel.css";
|
|
2
|
+
|
|
3
|
+
import { Button, Checkbox, Search, Spinner } from "@digdir/designsystemet-react";
|
|
4
|
+
import { BarChartIcon, PlusIcon, SortUpIcon } from "@navikt/aksel-icons";
|
|
5
|
+
import {
|
|
6
|
+
changeOperator,
|
|
7
|
+
createExpression,
|
|
8
|
+
type FieldMetadata,
|
|
9
|
+
type FilterExpression,
|
|
10
|
+
type FilterOperator,
|
|
11
|
+
type FilterTree,
|
|
12
|
+
getLocalizedString,
|
|
13
|
+
getOperatorLabel,
|
|
14
|
+
getOperatorsForField,
|
|
15
|
+
normalizeOperator,
|
|
16
|
+
} from "@olenbetong/appframe-core";
|
|
17
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
18
|
+
import clsx from "clsx";
|
|
19
|
+
import { type ReactNode, useMemo, useState } from "react";
|
|
20
|
+
|
|
21
|
+
import { getInListValues, setInListValues } from "./fieldFilter.js";
|
|
22
|
+
import { FilterValueEditor } from "./FilterValueEditor.js";
|
|
23
|
+
import { supportsDistinctValues, useDistinctValues } from "./useDistinctValues.js";
|
|
24
|
+
|
|
25
|
+
export type FieldFilterPanelProps = {
|
|
26
|
+
/** The field being filtered. Decides the operators and the value editor. */
|
|
27
|
+
field: FieldMetadata;
|
|
28
|
+
/** Data object the distinct value list is loaded from. */
|
|
29
|
+
dataObject: DataObject<any>;
|
|
30
|
+
/** The field's criteria. Controlled — the panel never stages edits itself. */
|
|
31
|
+
value: FilterExpression[];
|
|
32
|
+
onChange: (expressions: FilterExpression[]) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Heading above the criteria. Defaults to the field's caption; pass `null`
|
|
35
|
+
* to leave it out, e.g. when the surrounding dialog already has a title.
|
|
36
|
+
*/
|
|
37
|
+
heading?: ReactNode;
|
|
38
|
+
/**
|
|
39
|
+
* The filter the field lives in, used to scope the distinct value list to
|
|
40
|
+
* the rows the other criteria leave. The field's own criteria are ignored,
|
|
41
|
+
* so unchecking a value does not make the remaining values disappear.
|
|
42
|
+
*/
|
|
43
|
+
filter?: FilterTree | null;
|
|
44
|
+
/** Set to false to leave out the distinct value list. */
|
|
45
|
+
showDistinctValues?: boolean;
|
|
46
|
+
/** Rendered at the bottom, for actions such as Clear and Apply. */
|
|
47
|
+
children?: ReactNode;
|
|
48
|
+
className?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type DistinctSort = "value" | "count";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Filter criteria for a single field: a row per criterion, a button to add
|
|
55
|
+
* another, and a checkable list of the field's distinct values.
|
|
56
|
+
*
|
|
57
|
+
* The list of values is loaded straight from the data object's data handler,
|
|
58
|
+
* so the data object's own data and parameters are untouched. It is only shown
|
|
59
|
+
* for the types where it makes sense (string, number, uniqueidentifier) and
|
|
60
|
+
* only ever loads a bounded number of values — it is a picker, not a report.
|
|
61
|
+
*
|
|
62
|
+
* The checkboxes own the field's single `inlist` criterion, which is why that
|
|
63
|
+
* criterion is not also shown as a criterion row.
|
|
64
|
+
*
|
|
65
|
+
* The component is fully controlled. To let the user cancel, keep a draft in
|
|
66
|
+
* the caller and only commit it on apply — that is what `AfGrid`'s header
|
|
67
|
+
* filter panel does.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```jsx
|
|
71
|
+
* let [criteria, setCriteria] = useState([]);
|
|
72
|
+
*
|
|
73
|
+
* <FieldFilterPanel field={field} dataObject={dsProjects} value={criteria} onChange={setCriteria}>
|
|
74
|
+
* <Button onClick={() => setCriteria([])}>Clear</Button>
|
|
75
|
+
* </FieldFilterPanel>
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function FieldFilterPanel({
|
|
79
|
+
field,
|
|
80
|
+
dataObject,
|
|
81
|
+
value,
|
|
82
|
+
onChange,
|
|
83
|
+
heading,
|
|
84
|
+
filter = null,
|
|
85
|
+
showDistinctValues = true,
|
|
86
|
+
children,
|
|
87
|
+
className,
|
|
88
|
+
}: FieldFilterPanelProps) {
|
|
89
|
+
let [search, setSearch] = useState("");
|
|
90
|
+
let [sort, setSort] = useState<DistinctSort>("value");
|
|
91
|
+
|
|
92
|
+
let showDistinct = showDistinctValues && supportsDistinctValues(field);
|
|
93
|
+
let { values, loading, error, truncated } = useDistinctValues(dataObject, field, {
|
|
94
|
+
filter,
|
|
95
|
+
search: search.trim() || undefined,
|
|
96
|
+
enabled: showDistinct,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
let operators = useMemo(() => getOperatorsForField(field), [field]);
|
|
100
|
+
let criteria = value.filter((expression) => normalizeOperator(expression.operator) !== "inlist");
|
|
101
|
+
let selected = useMemo(() => new Set(getInListValues(value)), [value]);
|
|
102
|
+
|
|
103
|
+
let sortedValues = useMemo(() => {
|
|
104
|
+
let list = [...values];
|
|
105
|
+
|
|
106
|
+
if (sort === "count") {
|
|
107
|
+
list.sort((a, b) => (b.count ?? 0) - (a.count ?? 0) || a.value.localeCompare(b.value));
|
|
108
|
+
} else {
|
|
109
|
+
list.sort((a, b) => a.value.localeCompare(b.value));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return list;
|
|
113
|
+
}, [values, sort]);
|
|
114
|
+
|
|
115
|
+
let allSelected = sortedValues.length > 0 && sortedValues.every((item) => selected.has(item.value));
|
|
116
|
+
|
|
117
|
+
function setCriteria(next: FilterExpression[]) {
|
|
118
|
+
onChange([...next, ...value.filter((expression) => normalizeOperator(expression.operator) === "inlist")]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function updateCriterion(index: number, expression: FilterExpression | null) {
|
|
122
|
+
let next = [...criteria];
|
|
123
|
+
|
|
124
|
+
if (expression === null) {
|
|
125
|
+
next.splice(index, 1);
|
|
126
|
+
} else {
|
|
127
|
+
next[index] = expression;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
setCriteria(next);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function toggleValues(changed: string[], checked: boolean) {
|
|
134
|
+
let next = new Set(selected);
|
|
135
|
+
|
|
136
|
+
for (let item of changed) {
|
|
137
|
+
if (checked) {
|
|
138
|
+
next.add(item);
|
|
139
|
+
} else {
|
|
140
|
+
next.delete(item);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
onChange(setInListValues(value, [...next], field));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let headingContent = heading === undefined ? (field.caption ?? field.name) : heading;
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div className={clsx("ObFieldFilterPanel-root", className)}>
|
|
151
|
+
{headingContent != null && <div className="ObFieldFilterPanel-heading">{headingContent}</div>}
|
|
152
|
+
|
|
153
|
+
<div className="ObFieldFilterPanel-criteria">
|
|
154
|
+
{criteria.map((expression, index) => (
|
|
155
|
+
// Criteria have no stable identity and cannot be reordered here,
|
|
156
|
+
// so the index is a safe key.
|
|
157
|
+
// oxlint-disable-next-line no-array-index-key -- see above
|
|
158
|
+
<div className="ObFieldFilterPanel-criterion" key={index}>
|
|
159
|
+
<Field
|
|
160
|
+
expression={expression}
|
|
161
|
+
field={field}
|
|
162
|
+
operators={operators}
|
|
163
|
+
onChange={(next) => updateCriterion(index, next)}
|
|
164
|
+
onRemove={criteria.length > 1 ? () => updateCriterion(index, null) : undefined}
|
|
165
|
+
/>
|
|
166
|
+
</div>
|
|
167
|
+
))}
|
|
168
|
+
<Button
|
|
169
|
+
type="button"
|
|
170
|
+
variant="tertiary"
|
|
171
|
+
data-size="sm"
|
|
172
|
+
className="ObFieldFilterPanel-addCriterion"
|
|
173
|
+
onClick={() => setCriteria([...criteria, createExpression(field)])}
|
|
174
|
+
>
|
|
175
|
+
<PlusIcon aria-hidden />
|
|
176
|
+
{getLocalizedString("Criteria")}
|
|
177
|
+
</Button>
|
|
178
|
+
</div>
|
|
179
|
+
|
|
180
|
+
{showDistinct && (
|
|
181
|
+
<div className="ObFieldFilterPanel-distinct">
|
|
182
|
+
<div className="ObFieldFilterPanel-distinctToolbar">
|
|
183
|
+
<Search data-size="sm">
|
|
184
|
+
<Search.Input
|
|
185
|
+
aria-label={getLocalizedString("Search in distinct list")}
|
|
186
|
+
placeholder={getLocalizedString("Search in distinct list")}
|
|
187
|
+
value={search}
|
|
188
|
+
onChange={(event) => setSearch(event.currentTarget.value)}
|
|
189
|
+
/>
|
|
190
|
+
<Search.Clear onClick={() => setSearch("")} />
|
|
191
|
+
</Search>
|
|
192
|
+
<Button
|
|
193
|
+
type="button"
|
|
194
|
+
icon
|
|
195
|
+
variant="tertiary"
|
|
196
|
+
data-size="sm"
|
|
197
|
+
aria-pressed={sort === "value"}
|
|
198
|
+
aria-label={getLocalizedString("Sort by value")}
|
|
199
|
+
title={getLocalizedString("Sort by value")}
|
|
200
|
+
onClick={() => setSort("value")}
|
|
201
|
+
>
|
|
202
|
+
<SortUpIcon aria-hidden />
|
|
203
|
+
</Button>
|
|
204
|
+
<Button
|
|
205
|
+
type="button"
|
|
206
|
+
icon
|
|
207
|
+
variant="tertiary"
|
|
208
|
+
data-size="sm"
|
|
209
|
+
aria-pressed={sort === "count"}
|
|
210
|
+
aria-label={getLocalizedString("Sort by count")}
|
|
211
|
+
title={getLocalizedString("Sort by count")}
|
|
212
|
+
onClick={() => setSort("count")}
|
|
213
|
+
>
|
|
214
|
+
<BarChartIcon aria-hidden />
|
|
215
|
+
</Button>
|
|
216
|
+
</div>
|
|
217
|
+
|
|
218
|
+
{error && (
|
|
219
|
+
<p className="ObFieldFilterPanel-error" role="alert">
|
|
220
|
+
{error}
|
|
221
|
+
</p>
|
|
222
|
+
)}
|
|
223
|
+
|
|
224
|
+
{loading ? (
|
|
225
|
+
<div className="ObFieldFilterPanel-valuesBox ObFieldFilterPanel-loading">
|
|
226
|
+
<Spinner data-size="xs" aria-label={getLocalizedString("Loading...")} />
|
|
227
|
+
</div>
|
|
228
|
+
) : (
|
|
229
|
+
<ul className="ObFieldFilterPanel-valuesBox ObFieldFilterPanel-values">
|
|
230
|
+
<li className="ObFieldFilterPanel-value">
|
|
231
|
+
<Checkbox
|
|
232
|
+
data-size="sm"
|
|
233
|
+
label={getLocalizedString("Select all")}
|
|
234
|
+
checked={allSelected}
|
|
235
|
+
disabled={sortedValues.length === 0}
|
|
236
|
+
onChange={(event) =>
|
|
237
|
+
toggleValues(
|
|
238
|
+
sortedValues.map((item) => item.value),
|
|
239
|
+
event.currentTarget.checked,
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
/>
|
|
243
|
+
</li>
|
|
244
|
+
{sortedValues.map((item) => (
|
|
245
|
+
<li className="ObFieldFilterPanel-value" key={item.value}>
|
|
246
|
+
<Checkbox
|
|
247
|
+
data-size="sm"
|
|
248
|
+
label={item.count === null ? item.value : `${item.value} (${item.count})`}
|
|
249
|
+
checked={selected.has(item.value)}
|
|
250
|
+
onChange={(event) => toggleValues([item.value], event.currentTarget.checked)}
|
|
251
|
+
/>
|
|
252
|
+
</li>
|
|
253
|
+
))}
|
|
254
|
+
</ul>
|
|
255
|
+
)}
|
|
256
|
+
|
|
257
|
+
{truncated && (
|
|
258
|
+
<p className="ObFieldFilterPanel-hint">
|
|
259
|
+
{getLocalizedString("Only the first values are shown. Search to narrow the list.")}
|
|
260
|
+
</p>
|
|
261
|
+
)}
|
|
262
|
+
</div>
|
|
263
|
+
)}
|
|
264
|
+
|
|
265
|
+
{children}
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** One criterion: an operator picker, a value editor and an optional remove button. */
|
|
271
|
+
function Field({
|
|
272
|
+
expression,
|
|
273
|
+
field,
|
|
274
|
+
operators,
|
|
275
|
+
onChange,
|
|
276
|
+
onRemove,
|
|
277
|
+
}: {
|
|
278
|
+
expression: FilterExpression;
|
|
279
|
+
field: FieldMetadata;
|
|
280
|
+
operators: FilterOperator[];
|
|
281
|
+
onChange: (expression: FilterExpression) => void;
|
|
282
|
+
onRemove?: () => void;
|
|
283
|
+
}) {
|
|
284
|
+
return (
|
|
285
|
+
<>
|
|
286
|
+
{/* A DS `Select` renders its own field wrapper, which would add a
|
|
287
|
+
second row of spacing inside an already dense panel. */}
|
|
288
|
+
<select
|
|
289
|
+
className="ds-input ObFieldFilterPanel-operator"
|
|
290
|
+
data-size="sm"
|
|
291
|
+
aria-label={getLocalizedString("Operator")}
|
|
292
|
+
value={normalizeOperator(expression.operator)}
|
|
293
|
+
onChange={(event) => onChange(changeOperator(expression, event.target.value as FilterOperator, field))}
|
|
294
|
+
>
|
|
295
|
+
{operators.map((operator) => (
|
|
296
|
+
<option key={operator} value={operator}>
|
|
297
|
+
{getOperatorLabel(operator, field.type)}
|
|
298
|
+
</option>
|
|
299
|
+
))}
|
|
300
|
+
</select>
|
|
301
|
+
<FilterValueEditor expression={expression} field={field} data-size="sm" onChange={onChange} />
|
|
302
|
+
{onRemove && (
|
|
303
|
+
<Button type="button" variant="tertiary" data-size="sm" onClick={onRemove}>
|
|
304
|
+
{getLocalizedString("Remove")}
|
|
305
|
+
</Button>
|
|
306
|
+
)}
|
|
307
|
+
</>
|
|
308
|
+
);
|
|
309
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Colours use the contextual `--ds-color-*` aliases so the builder follows the
|
|
3
|
+
* active colour scheme. Never add hex fallbacks.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
.ObFilterBuilder-root {
|
|
7
|
+
display: grid;
|
|
8
|
+
gap: var(--ds-size-4);
|
|
9
|
+
/*
|
|
10
|
+
* The saved-filter list sits beside the editor. The buttons acting on the
|
|
11
|
+
* selected filter share the bottom row with OK/Cancel rather than being
|
|
12
|
+
* confined to the list's column, so they have room to stay on one line.
|
|
13
|
+
* On narrow viewports everything stacks.
|
|
14
|
+
*/
|
|
15
|
+
grid-template:
|
|
16
|
+
"sidebar main" 1fr
|
|
17
|
+
"error error" auto
|
|
18
|
+
"footer footer" auto
|
|
19
|
+
/ minmax(14rem, 22rem) minmax(0, 1fr);
|
|
20
|
+
block-size: 100%;
|
|
21
|
+
min-block-size: 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.ObFilterBuilder-sidebar {
|
|
25
|
+
grid-area: sidebar;
|
|
26
|
+
overflow: auto;
|
|
27
|
+
padding: var(--ds-size-2);
|
|
28
|
+
border: var(--ds-border-width-default) solid var(--ds-color-border-subtle);
|
|
29
|
+
border-radius: var(--ds-border-radius-md);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* The editor manages its own scrolling — the conditions scroll, the filter
|
|
34
|
+
* string stays put — so this only has to hand it the available height. A caller
|
|
35
|
+
* that replaces the editor through `renderMain` gets the same treatment.
|
|
36
|
+
*/
|
|
37
|
+
.ObFilterBuilder-main {
|
|
38
|
+
grid-area: main;
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
gap: var(--ds-size-3);
|
|
42
|
+
min-inline-size: 0;
|
|
43
|
+
min-block-size: 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.ObFilterBuilder-main > * {
|
|
47
|
+
flex: 1 1 auto;
|
|
48
|
+
min-inline-size: 0;
|
|
49
|
+
min-block-size: 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.ObFilterBuilder-error {
|
|
53
|
+
grid-area: error;
|
|
54
|
+
margin: 0;
|
|
55
|
+
color: var(--ds-color-danger-text-default);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.ObFilterBuilder-footer {
|
|
59
|
+
grid-area: footer;
|
|
60
|
+
display: flex;
|
|
61
|
+
flex-wrap: wrap;
|
|
62
|
+
align-items: center;
|
|
63
|
+
justify-content: space-between;
|
|
64
|
+
gap: var(--ds-size-4);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.ObFilterBuilder-toolbar,
|
|
68
|
+
.ObFilterBuilder-actions {
|
|
69
|
+
display: flex;
|
|
70
|
+
flex-wrap: wrap;
|
|
71
|
+
align-items: center;
|
|
72
|
+
gap: var(--ds-size-2);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.ObFilterBuilder-actions {
|
|
76
|
+
margin-inline-start: auto;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/*
|
|
80
|
+
* Without the saved-filter list there are no buttons acting on one either, so
|
|
81
|
+
* the editor takes the full width.
|
|
82
|
+
*/
|
|
83
|
+
.ObFilterBuilder-root--noSavedFilters {
|
|
84
|
+
grid-template:
|
|
85
|
+
"main" 1fr
|
|
86
|
+
"error" auto
|
|
87
|
+
"footer" auto
|
|
88
|
+
/ minmax(0, 1fr);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* `Dialog` caps itself at `--dsc-dialog-max-width` (40rem), so setting a width
|
|
93
|
+
* here would have no effect — the variable has to be raised instead. It needs
|
|
94
|
+
* to be wide enough that a condition — field, operator, value and the group
|
|
95
|
+
* buttons — fits on one line next to the saved-filter column.
|
|
96
|
+
*/
|
|
97
|
+
.ObFilterBuilder-dialog {
|
|
98
|
+
--dsc-dialog-max-width: 84rem;
|
|
99
|
+
--dsc-dialog-max-height: 90dvh;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/*
|
|
103
|
+
* A fixed height keeps the dialog from resizing as conditions are added and
|
|
104
|
+
* removed, and gives the saved-filter column and the editor a height to fill.
|
|
105
|
+
* The dialog itself must not scroll — the body does — or the heading would
|
|
106
|
+
* scroll away and the OK/Cancel row would not stay at the bottom.
|
|
107
|
+
*/
|
|
108
|
+
.ObFilterBuilder-dialog[open] {
|
|
109
|
+
display: flex;
|
|
110
|
+
flex-direction: column;
|
|
111
|
+
block-size: 48rem;
|
|
112
|
+
overflow: hidden;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
* The heading and the close button share the header row.
|
|
117
|
+
*/
|
|
118
|
+
.ObFilterBuilder-dialogHeader {
|
|
119
|
+
display: flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
justify-content: space-between;
|
|
122
|
+
gap: var(--ds-size-2);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.ObFilterBuilder-dialogBody {
|
|
126
|
+
flex: 1 1 auto;
|
|
127
|
+
min-block-size: 0;
|
|
128
|
+
overflow: hidden;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@media (width < 48rem) {
|
|
132
|
+
.ObFilterBuilder-root:not(.ObFilterBuilder-root--noSavedFilters) {
|
|
133
|
+
grid-template:
|
|
134
|
+
"sidebar" auto
|
|
135
|
+
"main" 1fr
|
|
136
|
+
"error" auto
|
|
137
|
+
"footer" auto
|
|
138
|
+
/ minmax(0, 1fr);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/*
|
|
142
|
+
* Stacked, the dialog body is the scroll container: the editor cannot give
|
|
143
|
+
* both the list and the conditions a useful height in the space left over.
|
|
144
|
+
*/
|
|
145
|
+
.ObFilterBuilder-dialogBody {
|
|
146
|
+
overflow: auto;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
.ObFilterBuilder-sidebar {
|
|
150
|
+
max-block-size: 12rem;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { FieldMetadata, SavedFilter } from "@olenbetong/appframe-core";
|
|
2
|
+
import { render, screen, waitFor } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
const save = vi.fn(async () => undefined);
|
|
7
|
+
const remove = vi.fn(async () => undefined);
|
|
8
|
+
const rename = vi.fn(async () => undefined);
|
|
9
|
+
const setCrossDomain = vi.fn(async () => undefined);
|
|
10
|
+
const setNamedFilter = vi.fn();
|
|
11
|
+
const parseFilterString = vi.fn(async () => null);
|
|
12
|
+
let savedFilters: SavedFilter[] = [];
|
|
13
|
+
|
|
14
|
+
vi.mock("@olenbetong/appframe-react", async (importOriginal) => ({
|
|
15
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
16
|
+
setNamedFilter: (...args: unknown[]) => setNamedFilter(...args),
|
|
17
|
+
useSavedFilters: () => ({
|
|
18
|
+
filters: savedFilters,
|
|
19
|
+
mine: savedFilters.filter((filter) => filter.createdByUser),
|
|
20
|
+
shared: savedFilters.filter((filter) => !filter.createdByUser),
|
|
21
|
+
loading: false,
|
|
22
|
+
error: null,
|
|
23
|
+
refresh: vi.fn(),
|
|
24
|
+
save,
|
|
25
|
+
rename,
|
|
26
|
+
remove,
|
|
27
|
+
setCrossDomain,
|
|
28
|
+
}),
|
|
29
|
+
useFilterConversion: () => ({
|
|
30
|
+
filterString: "[PersonID] Like '%a%'",
|
|
31
|
+
isConverting: false,
|
|
32
|
+
error: null,
|
|
33
|
+
parseFilterString,
|
|
34
|
+
}),
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
const { FilterBuilder } = await import("./FilterBuilder.js");
|
|
38
|
+
|
|
39
|
+
const FIELDS: FieldMetadata[] = [
|
|
40
|
+
{ name: "PersonID", caption: "Person ID", type: "string", nullable: true },
|
|
41
|
+
{ name: "Age", caption: "Age", type: "number" },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
function savedFilter(overrides: Partial<SavedFilter> & Pick<SavedFilter, "filterId" | "name">): SavedFilter {
|
|
45
|
+
return {
|
|
46
|
+
primKey: `pk-${overrides.filterId}`,
|
|
47
|
+
criteria: "[PersonID] Like '%a%'",
|
|
48
|
+
dbObjectId: "aviw_Test_View",
|
|
49
|
+
accessLevel: "Manager",
|
|
50
|
+
createdByUser: true,
|
|
51
|
+
createdBy: "bvh",
|
|
52
|
+
domain: "OB",
|
|
53
|
+
crossDomain: false,
|
|
54
|
+
isPersonallyShared: false,
|
|
55
|
+
hideCriteria: false,
|
|
56
|
+
headerText: null,
|
|
57
|
+
comment: null,
|
|
58
|
+
parameters: null,
|
|
59
|
+
reportTitle: null,
|
|
60
|
+
created: null,
|
|
61
|
+
updated: null,
|
|
62
|
+
...overrides,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function renderBuilder(props: Record<string, unknown> = {}) {
|
|
67
|
+
return render(<FilterBuilder fields={FIELDS} viewName="aviw_Test_View" variant="inline" {...props} />);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe("FilterBuilder", () => {
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
vi.clearAllMocks();
|
|
73
|
+
savedFilters = [savedFilter({ filterId: 1, name: "Agder" })];
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("renders the saved filters beside the editor", () => {
|
|
77
|
+
renderBuilder();
|
|
78
|
+
|
|
79
|
+
expect(screen.getByRole("button", { name: "New Filter" })).toBeTruthy();
|
|
80
|
+
expect(screen.getByRole("button", { name: "Agder" })).toBeTruthy();
|
|
81
|
+
expect(screen.getByLabelText("Add condition")).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("disables save and share until a filter is selected", () => {
|
|
85
|
+
renderBuilder();
|
|
86
|
+
|
|
87
|
+
expect(screen.getByRole("button", { name: "Sharing..." }).hasAttribute("disabled")).toBe(true);
|
|
88
|
+
expect(screen.getByRole("button", { name: "Save" }).hasAttribute("disabled")).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("disables save as while the filter is empty", () => {
|
|
92
|
+
renderBuilder();
|
|
93
|
+
|
|
94
|
+
expect(screen.getByRole("button", { name: "Save as" }).hasAttribute("disabled")).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("parses the criteria of a selected filter back into the tree", async () => {
|
|
98
|
+
renderBuilder();
|
|
99
|
+
await userEvent.click(screen.getByRole("button", { name: "Agder" }));
|
|
100
|
+
|
|
101
|
+
expect(parseFilterString).toHaveBeenCalledWith("[PersonID] Like '%a%'");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("applies the pruned filter to the data object", async () => {
|
|
105
|
+
let dataObject = {} as never;
|
|
106
|
+
renderBuilder({
|
|
107
|
+
dataObject,
|
|
108
|
+
defaultValue: {
|
|
109
|
+
type: "group",
|
|
110
|
+
mode: "and",
|
|
111
|
+
items: [{ type: "expression", column: "PersonID", operator: "like", value: "a", valueType: "string" }],
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await userEvent.click(screen.getByRole("button", { name: "OK" }));
|
|
116
|
+
|
|
117
|
+
expect(setNamedFilter).toHaveBeenCalledWith(
|
|
118
|
+
dataObject,
|
|
119
|
+
"filterBuilder",
|
|
120
|
+
expect.objectContaining({ type: "group" }),
|
|
121
|
+
{ type: "filterObject" },
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("reports an empty filter as null", async () => {
|
|
126
|
+
let onApply = vi.fn();
|
|
127
|
+
renderBuilder({ onApply });
|
|
128
|
+
|
|
129
|
+
await userEvent.click(screen.getByRole("button", { name: "OK" }));
|
|
130
|
+
|
|
131
|
+
expect(onApply).toHaveBeenCalledWith(null, "[PersonID] Like '%a%'");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("saves under a new name and replaces a filter that already uses it", async () => {
|
|
135
|
+
renderBuilder({
|
|
136
|
+
defaultValue: {
|
|
137
|
+
type: "group",
|
|
138
|
+
mode: "and",
|
|
139
|
+
items: [{ type: "expression", column: "PersonID", operator: "like", value: "a", valueType: "string" }],
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await userEvent.click(screen.getByRole("button", { name: "Save as" }));
|
|
144
|
+
await userEvent.type(screen.getByRole("textbox", { name: "Filter name" }), "Agder");
|
|
145
|
+
await userEvent.click(screen.getByRole("button", { name: "Replace" }));
|
|
146
|
+
|
|
147
|
+
await waitFor(() =>
|
|
148
|
+
expect(save).toHaveBeenCalledWith(
|
|
149
|
+
expect.objectContaining({ filterId: 1, name: "Agder", criteria: "[PersonID] Like '%a%'" }),
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|