@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,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type FieldMetadata,
|
|
3
|
+
type FilterExpression,
|
|
4
|
+
type FilterOperator,
|
|
5
|
+
type FilterTree,
|
|
6
|
+
getLocalizedString,
|
|
7
|
+
getOperatorArity,
|
|
8
|
+
getOperatorLabel,
|
|
9
|
+
getValueType,
|
|
10
|
+
isExpression,
|
|
11
|
+
normalizeOperator,
|
|
12
|
+
} from "@olenbetong/appframe-core";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The criteria a header filter owns for a field: the expressions directly in
|
|
16
|
+
* the filter's root group that filter on that field.
|
|
17
|
+
*
|
|
18
|
+
* Expressions nested inside sub-groups are deliberately left out. A header
|
|
19
|
+
* filter is a flat list of criteria combined with the root group's operator, so
|
|
20
|
+
* it cannot represent a nested group without silently changing its meaning.
|
|
21
|
+
* Those stay editable in the filter builder only.
|
|
22
|
+
*/
|
|
23
|
+
export function getFieldExpressions(filter: FilterTree | null | undefined, field: string): FilterExpression[] {
|
|
24
|
+
if (!filter) return [];
|
|
25
|
+
|
|
26
|
+
let lowerField = field.toLowerCase();
|
|
27
|
+
|
|
28
|
+
return filter.items.filter(
|
|
29
|
+
(item): item is FilterExpression => isExpression(item) && item.column?.toLowerCase() === lowerField,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns a copy of the tree where the field's top-level expressions have been
|
|
35
|
+
* replaced by `expressions`, keeping the position of the first one so criteria
|
|
36
|
+
* do not jump around while they are edited.
|
|
37
|
+
*/
|
|
38
|
+
export function setFieldExpressions(filter: FilterTree, field: string, expressions: FilterExpression[]): FilterTree {
|
|
39
|
+
let lowerField = field.toLowerCase();
|
|
40
|
+
let isOwn = (item: FilterTree["items"][number]) => isExpression(item) && item.column?.toLowerCase() === lowerField;
|
|
41
|
+
let firstIndex = filter.items.findIndex(isOwn);
|
|
42
|
+
let others = filter.items.filter((item) => !isOwn(item));
|
|
43
|
+
|
|
44
|
+
if (expressions.length === 0) {
|
|
45
|
+
return { ...filter, items: others };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let insertAt = firstIndex === -1 ? others.length : Math.min(firstIndex, others.length);
|
|
49
|
+
let items = [...others.slice(0, insertAt), ...expressions, ...others.slice(insertAt)];
|
|
50
|
+
|
|
51
|
+
return { ...filter, items };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* True when the criteria can be edited straight from the header input: exactly
|
|
56
|
+
* one criterion, taking exactly one literal value.
|
|
57
|
+
*/
|
|
58
|
+
export function isSimpleFieldFilter(expressions: FilterExpression[]): boolean {
|
|
59
|
+
if (expressions.length !== 1) return false;
|
|
60
|
+
|
|
61
|
+
let expression = expressions[0] as FilterExpression;
|
|
62
|
+
|
|
63
|
+
return getOperatorArity(normalizeOperator(expression.operator)) === "single" && expression.valueType !== "special";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function toDisplayValue(value: FilterExpression["value"]): string {
|
|
67
|
+
if (value === null || value === undefined) return "";
|
|
68
|
+
if (value instanceof Date) return value.toLocaleDateString();
|
|
69
|
+
if (Array.isArray(value)) return value.join(", ");
|
|
70
|
+
|
|
71
|
+
return String(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The value shown in the header input when the criteria are directly editable. */
|
|
75
|
+
export function getSimpleValue(expressions: FilterExpression[]): string {
|
|
76
|
+
return isSimpleFieldFilter(expressions) ? toDisplayValue((expressions[0] as FilterExpression).value) : "";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Short read-only description of a field's criteria, shown in the header input
|
|
81
|
+
* when they are too complex to edit inline — e.g. `In list (3)`.
|
|
82
|
+
*/
|
|
83
|
+
export function summarizeFieldFilter(expressions: FilterExpression[], field?: FieldMetadata): string {
|
|
84
|
+
if (expressions.length === 0) return "";
|
|
85
|
+
|
|
86
|
+
if (expressions.length > 1) {
|
|
87
|
+
return getLocalizedString("{0} criteria").replace("{0}", String(expressions.length));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let expression = expressions[0] as FilterExpression;
|
|
91
|
+
let operator = normalizeOperator(expression.operator);
|
|
92
|
+
let label = getOperatorLabel(operator, field?.type);
|
|
93
|
+
let arity = getOperatorArity(operator);
|
|
94
|
+
|
|
95
|
+
if (arity === "none") return label;
|
|
96
|
+
|
|
97
|
+
if (arity === "list") {
|
|
98
|
+
let count = Array.isArray(expression.value) ? expression.value.length : 0;
|
|
99
|
+
|
|
100
|
+
return `${label} (${count})`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return `${label} ${toDisplayValue(expression.value)}`.trim();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The values of the field's `inlist` criterion, which is what the distinct
|
|
108
|
+
* value checkbox list edits. Everything else in the panel is a normal
|
|
109
|
+
* criterion row.
|
|
110
|
+
*/
|
|
111
|
+
export function getInListValues(expressions: FilterExpression[]): string[] {
|
|
112
|
+
let inList = expressions.find((expression) => normalizeOperator(expression.operator) === "inlist");
|
|
113
|
+
|
|
114
|
+
return Array.isArray(inList?.value) ? inList.value.map(String) : [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Replaces the field's `inlist` criterion with one holding `values`, or removes
|
|
119
|
+
* it when the selection is empty.
|
|
120
|
+
*/
|
|
121
|
+
export function setInListValues(
|
|
122
|
+
expressions: FilterExpression[],
|
|
123
|
+
values: string[],
|
|
124
|
+
field: FieldMetadata,
|
|
125
|
+
): FilterExpression[] {
|
|
126
|
+
let others = expressions.filter((expression) => normalizeOperator(expression.operator) !== "inlist");
|
|
127
|
+
|
|
128
|
+
if (values.length === 0) return others;
|
|
129
|
+
|
|
130
|
+
let inList: FilterExpression = {
|
|
131
|
+
type: "expression",
|
|
132
|
+
column: field.name,
|
|
133
|
+
operator: "inlist" satisfies FilterOperator,
|
|
134
|
+
value: values,
|
|
135
|
+
valueType: getValueType(field, "inlist"),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return [...others, inList];
|
|
139
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { ChipListInput, type ChipListInputProps } from "./ChipListInput.js";
|
|
2
|
+
export {
|
|
3
|
+
getFieldExpressions,
|
|
4
|
+
getInListValues,
|
|
5
|
+
getSimpleValue,
|
|
6
|
+
isSimpleFieldFilter,
|
|
7
|
+
setFieldExpressions,
|
|
8
|
+
setInListValues,
|
|
9
|
+
summarizeFieldFilter,
|
|
10
|
+
} from "./fieldFilter.js";
|
|
11
|
+
export { FieldFilterPanel, type FieldFilterPanelProps } from "./FieldFilterPanel.js";
|
|
12
|
+
export { FilterBuilder, type FilterBuilderProps, type SavedFilterValues } from "./FilterBuilder.js";
|
|
13
|
+
export { FilterEditor } from "./FilterEditor.js";
|
|
14
|
+
export { FilterGroupEditor, type FilterGroupEditorProps } from "./FilterGroupEditor.js";
|
|
15
|
+
export { FilterNameDialog, type FilterNameDialogProps } from "./FilterNameDialog.js";
|
|
16
|
+
export { FilterRow, type FilterRowProps } from "./FilterRow.js";
|
|
17
|
+
export { FilterShareDialog, type FilterShareDialogProps } from "./FilterShareDialog.js";
|
|
18
|
+
export { FilterStringField, type FilterStringFieldProps } from "./FilterStringField.js";
|
|
19
|
+
export { FilterValueEditor, type FilterValueEditorProps } from "./FilterValueEditor.js";
|
|
20
|
+
export { SavedFilterTree, type SavedFilterSelection, type SavedFilterTreeProps } from "./SavedFilterTree.js";
|
|
21
|
+
export type { FilterEditorProps, FilterEditorSize, FilterValueOption, GetFieldOptions } from "./types.js";
|
|
22
|
+
export {
|
|
23
|
+
type DistinctValue,
|
|
24
|
+
supportsDistinctValues,
|
|
25
|
+
useDistinctValues,
|
|
26
|
+
type UseDistinctValuesResult,
|
|
27
|
+
} from "./useDistinctValues.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FieldMetadata, FilterTree } from "@olenbetong/appframe-core";
|
|
2
|
+
|
|
3
|
+
/** A selectable value for a field that has a fixed list of values. */
|
|
4
|
+
export type FilterValueOption = {
|
|
5
|
+
value: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the list of values offered in the value editor for a field. Return
|
|
11
|
+
* `undefined` to fall back to a free text editor.
|
|
12
|
+
*
|
|
13
|
+
* Lookup fields carry a `valueListRecordSource` in their metadata, but loading
|
|
14
|
+
* that record source needs a data object, which is out of scope for this
|
|
15
|
+
* component. Supply the options from the outside instead.
|
|
16
|
+
*/
|
|
17
|
+
export type GetFieldOptions = (field: FieldMetadata) => FilterValueOption[] | undefined;
|
|
18
|
+
|
|
19
|
+
export type FilterEditorSize = "sm" | "md" | "lg";
|
|
20
|
+
|
|
21
|
+
export type FilterEditorProps = {
|
|
22
|
+
/** The fields the user can filter on. Fields with `excludeFromFilter` are hidden. */
|
|
23
|
+
fields: FieldMetadata[];
|
|
24
|
+
/** The filter tree. Use together with `onChange` for a controlled component. */
|
|
25
|
+
value?: FilterTree | null;
|
|
26
|
+
/** The initial filter tree when uncontrolled. */
|
|
27
|
+
defaultValue?: FilterTree | null;
|
|
28
|
+
onChange?: (filter: FilterTree) => void;
|
|
29
|
+
/** Renders the tree without any editing affordances. */
|
|
30
|
+
readOnly?: boolean;
|
|
31
|
+
/** Shows the filter string below the tree, editable unless `readOnly`. */
|
|
32
|
+
showFilterString?: boolean;
|
|
33
|
+
/** View the filter applies to. Passed to the conversion endpoint. */
|
|
34
|
+
viewName?: string;
|
|
35
|
+
getFieldOptions?: GetFieldOptions;
|
|
36
|
+
className?: string;
|
|
37
|
+
"data-size"?: FilterEditorSize;
|
|
38
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { FieldMetadata, FilterExpression, FilterTree } from "@olenbetong/appframe-core";
|
|
2
|
+
import { describe, expect, test } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { buildDistinctParameters } from "./useDistinctValues.js";
|
|
5
|
+
|
|
6
|
+
const field: FieldMetadata = { name: "Name", caption: "Name", type: "string", nullable: true };
|
|
7
|
+
|
|
8
|
+
function expression(column: string, value = "x"): FilterExpression {
|
|
9
|
+
return { type: "expression", column, operator: "contains", value } as FilterExpression;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function tree(...items: FilterTree["items"]): FilterTree {
|
|
13
|
+
return { type: "group", mode: "and", items };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe("buildDistinctParameters", () => {
|
|
17
|
+
test("groups by the field and counts another field", () => {
|
|
18
|
+
let parameters = buildDistinctParameters({
|
|
19
|
+
field,
|
|
20
|
+
countField: "PrimKey",
|
|
21
|
+
filter: null,
|
|
22
|
+
withCounts: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(parameters.fields).toEqual([{ name: "Name" }, { name: "PrimKey", aggregate: "COUNT" }]);
|
|
26
|
+
expect(parameters).toMatchObject({ groupBy: ["Name"], sortOrder: [{ Name: "asc" }] });
|
|
27
|
+
expect(parameters).not.toHaveProperty("distinctRows");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("falls back to a plain distinct list without counts", () => {
|
|
31
|
+
let parameters = buildDistinctParameters({
|
|
32
|
+
field,
|
|
33
|
+
countField: "PrimKey",
|
|
34
|
+
filter: null,
|
|
35
|
+
withCounts: false,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
expect(parameters.fields).toEqual([{ name: "Name" }]);
|
|
39
|
+
expect(parameters).toMatchObject({ distinctRows: true });
|
|
40
|
+
expect(parameters).not.toHaveProperty("groupBy");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("has no counts when no other field is available to count", () => {
|
|
44
|
+
let parameters = buildDistinctParameters({ field, countField: null, filter: null, withCounts: true });
|
|
45
|
+
|
|
46
|
+
expect(parameters.fields).toEqual([{ name: "Name" }]);
|
|
47
|
+
expect(parameters).toMatchObject({ distinctRows: true });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("keeps other criteria but drops the field's own", () => {
|
|
51
|
+
let filter = tree(expression("Name"), expression("Other"));
|
|
52
|
+
let parameters = buildDistinctParameters({ field, countField: "PrimKey", filter, withCounts: true });
|
|
53
|
+
|
|
54
|
+
expect(parameters.filterObject).toEqual(tree(expression("Other")));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("has no filter when the field owns every criterion", () => {
|
|
58
|
+
let parameters = buildDistinctParameters({
|
|
59
|
+
field,
|
|
60
|
+
countField: "PrimKey",
|
|
61
|
+
filter: tree(expression("Name")),
|
|
62
|
+
withCounts: true,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(parameters.filterObject).toBe(null);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("adds the search text as a criterion on the field", () => {
|
|
69
|
+
let parameters = buildDistinctParameters({
|
|
70
|
+
field,
|
|
71
|
+
countField: "PrimKey",
|
|
72
|
+
filter: null,
|
|
73
|
+
search: "ab",
|
|
74
|
+
withCounts: true,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
expect(parameters.filterObject).toMatchObject({
|
|
78
|
+
type: "group",
|
|
79
|
+
items: [{ column: "Name", operator: "contains", value: "ab" }],
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("combines the search text with the other criteria", () => {
|
|
84
|
+
let parameters = buildDistinctParameters({
|
|
85
|
+
field,
|
|
86
|
+
countField: "PrimKey",
|
|
87
|
+
filter: tree(expression("Other")),
|
|
88
|
+
search: "ab",
|
|
89
|
+
withCounts: true,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(parameters.filterObject).toMatchObject({
|
|
93
|
+
items: [tree(expression("Other")), { column: "Name", value: "ab" }],
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("caps the number of values loaded", () => {
|
|
98
|
+
let parameters = buildDistinctParameters({ field, countField: "PrimKey", filter: null, withCounts: true });
|
|
99
|
+
|
|
100
|
+
expect(parameters.maxRecords).toBe(200);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import type { FieldMetadata, FilterExpression, FilterTree } from "@olenbetong/appframe-core";
|
|
2
|
+
import { getValueType } from "@olenbetong/appframe-core";
|
|
3
|
+
import type { DataObject, Filter } from "@olenbetong/appframe-data";
|
|
4
|
+
import { useEffect, useMemo, useState } from "react";
|
|
5
|
+
|
|
6
|
+
import { getFieldExpressions } from "./fieldFilter.js";
|
|
7
|
+
|
|
8
|
+
export type DistinctValue = {
|
|
9
|
+
/** The raw value, as sent back in an `inlist` filter. */
|
|
10
|
+
value: string;
|
|
11
|
+
/** How many rows have this value, or null when counts are unavailable. */
|
|
12
|
+
count: number | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type UseDistinctValuesResult = {
|
|
16
|
+
values: DistinctValue[];
|
|
17
|
+
loading: boolean;
|
|
18
|
+
error: string | null;
|
|
19
|
+
/** True when the list was truncated at `maxRecords`. */
|
|
20
|
+
truncated: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** How many distinct values to load. The list is a picker, not a report. */
|
|
24
|
+
const MAX_DISTINCT_VALUES = 200;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Builds the retrieve payload for the distinct value list of a field.
|
|
28
|
+
*
|
|
29
|
+
* `groupBy` plus a `COUNT` aggregate gives values and counts in one round trip.
|
|
30
|
+
* The counted field must be a different field than the grouped one — asking for
|
|
31
|
+
* `COUNT` of the grouped field collapses into a single column server side.
|
|
32
|
+
*
|
|
33
|
+
* The list respects every other active filter but ignores the field's own
|
|
34
|
+
* criteria, so unchecking a value does not make the remaining values disappear.
|
|
35
|
+
*/
|
|
36
|
+
export function buildDistinctParameters({
|
|
37
|
+
field,
|
|
38
|
+
countField,
|
|
39
|
+
filter,
|
|
40
|
+
search,
|
|
41
|
+
withCounts,
|
|
42
|
+
}: {
|
|
43
|
+
field: FieldMetadata;
|
|
44
|
+
countField: string | null;
|
|
45
|
+
filter: FilterTree | null;
|
|
46
|
+
search?: string;
|
|
47
|
+
withCounts: boolean;
|
|
48
|
+
}) {
|
|
49
|
+
let filterObject = withoutField(filter, field.name);
|
|
50
|
+
|
|
51
|
+
if (search) {
|
|
52
|
+
let searchExpression: FilterExpression = {
|
|
53
|
+
type: "expression",
|
|
54
|
+
column: field.name,
|
|
55
|
+
operator: "contains",
|
|
56
|
+
value: search,
|
|
57
|
+
valueType: getValueType(field, "contains"),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
filterObject = filterObject
|
|
61
|
+
? { type: "group", mode: "and", items: [filterObject, searchExpression] }
|
|
62
|
+
: { type: "group", mode: "and", items: [searchExpression] };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let fields =
|
|
66
|
+
withCounts && countField
|
|
67
|
+
? [{ name: field.name }, { name: countField, aggregate: "COUNT" }]
|
|
68
|
+
: [{ name: field.name }];
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
fields,
|
|
72
|
+
...(withCounts && countField ? { groupBy: [field.name] } : { distinctRows: true }),
|
|
73
|
+
filterObject,
|
|
74
|
+
maxRecords: MAX_DISTINCT_VALUES,
|
|
75
|
+
sortOrder: [{ [field.name]: "asc" }],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns a copy of the filter with the field's own top level criteria removed,
|
|
81
|
+
* and null when nothing is left.
|
|
82
|
+
*/
|
|
83
|
+
function withoutField(filter: FilterTree | null, field: string): Filter {
|
|
84
|
+
if (!filter) return null;
|
|
85
|
+
|
|
86
|
+
let own = new Set<unknown>(getFieldExpressions(filter, field));
|
|
87
|
+
let items = filter.items.filter((item) => !own.has(item));
|
|
88
|
+
|
|
89
|
+
if (items.length === 0) return null;
|
|
90
|
+
|
|
91
|
+
return { type: "group", mode: filter.mode, items };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether a distinct value list makes sense for this field's type. */
|
|
95
|
+
export function supportsDistinctValues(field: FieldMetadata | undefined): boolean {
|
|
96
|
+
return field?.type === "string" || field?.type === "number" || field?.type === "uniqueidentifier";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Loads the distinct values of a field, with a row count per value.
|
|
101
|
+
*
|
|
102
|
+
* The query goes straight to the data object's data handler rather than through
|
|
103
|
+
* the data object, so the grid's own data and parameters are untouched.
|
|
104
|
+
*
|
|
105
|
+
* @param dataObject The grid's data object
|
|
106
|
+
* @param field The field to list values for
|
|
107
|
+
* @param options Current filter, search text, and whether to load at all
|
|
108
|
+
*/
|
|
109
|
+
export function useDistinctValues(
|
|
110
|
+
dataObject: DataObject<any>,
|
|
111
|
+
field: FieldMetadata | undefined,
|
|
112
|
+
{ filter, search, enabled = true }: { filter: FilterTree | null; search?: string; enabled?: boolean },
|
|
113
|
+
): UseDistinctValuesResult {
|
|
114
|
+
let [result, setResult] = useState<UseDistinctValuesResult>({
|
|
115
|
+
values: [],
|
|
116
|
+
loading: false,
|
|
117
|
+
error: null,
|
|
118
|
+
truncated: false,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// The count column has to be a field other than the grouped one.
|
|
122
|
+
let countField = useMemo(() => {
|
|
123
|
+
if (!field) return null;
|
|
124
|
+
|
|
125
|
+
let lowerField = field.name.toLowerCase();
|
|
126
|
+
let names = dataObject
|
|
127
|
+
.getFields()
|
|
128
|
+
.map((definition) => definition.name)
|
|
129
|
+
.filter((name) => !!name && name.toLowerCase() !== lowerField);
|
|
130
|
+
|
|
131
|
+
return names.find((name) => name.toLowerCase() === "primkey") ?? names[0] ?? null;
|
|
132
|
+
}, [dataObject, field]);
|
|
133
|
+
|
|
134
|
+
// The filter is only used to scope the list, so re-running on every keystroke
|
|
135
|
+
// in an unrelated criterion is avoided by keying on its serialized form.
|
|
136
|
+
let filterKey = useMemo(() => JSON.stringify(filter ?? null), [filter]);
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!enabled || !field || !supportsDistinctValues(field)) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let controller = new AbortController();
|
|
144
|
+
let cancelled = false;
|
|
145
|
+
|
|
146
|
+
setResult((current) => ({ ...current, loading: true, error: null }));
|
|
147
|
+
|
|
148
|
+
async function load() {
|
|
149
|
+
if (!field) return;
|
|
150
|
+
|
|
151
|
+
let parsedFilter: FilterTree | null = JSON.parse(filterKey);
|
|
152
|
+
|
|
153
|
+
async function run(withCounts: boolean) {
|
|
154
|
+
let parameters = buildDistinctParameters({
|
|
155
|
+
field: field as FieldMetadata,
|
|
156
|
+
countField,
|
|
157
|
+
filter: parsedFilter,
|
|
158
|
+
search,
|
|
159
|
+
withCounts,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return (await dataObject.dataHandler.retrieve(parameters as any, {
|
|
163
|
+
signal: controller.signal,
|
|
164
|
+
})) as any[];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
let withCounts = !!countField;
|
|
169
|
+
let rows: any[];
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
rows = await run(withCounts);
|
|
173
|
+
} catch (cause) {
|
|
174
|
+
if (controller.signal.aborted || !withCounts) throw cause;
|
|
175
|
+
|
|
176
|
+
// Counts are best effort: some views cannot be grouped, so fall
|
|
177
|
+
// back to a plain distinct list rather than showing an error.
|
|
178
|
+
withCounts = false;
|
|
179
|
+
rows = await run(false);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (cancelled) return;
|
|
183
|
+
|
|
184
|
+
let values = rows
|
|
185
|
+
.map((row) => toDistinctValue(row, field as FieldMetadata, countField, withCounts))
|
|
186
|
+
.filter((value): value is DistinctValue => value !== null);
|
|
187
|
+
|
|
188
|
+
setResult({
|
|
189
|
+
values,
|
|
190
|
+
loading: false,
|
|
191
|
+
error: null,
|
|
192
|
+
truncated: values.length >= MAX_DISTINCT_VALUES,
|
|
193
|
+
});
|
|
194
|
+
} catch (cause) {
|
|
195
|
+
if (cancelled || controller.signal.aborted) return;
|
|
196
|
+
|
|
197
|
+
setResult({
|
|
198
|
+
values: [],
|
|
199
|
+
loading: false,
|
|
200
|
+
error: String((cause as Error)?.message ?? cause),
|
|
201
|
+
truncated: false,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
void load();
|
|
207
|
+
|
|
208
|
+
return () => {
|
|
209
|
+
cancelled = true;
|
|
210
|
+
controller.abort();
|
|
211
|
+
};
|
|
212
|
+
}, [dataObject, field, countField, filterKey, search, enabled]);
|
|
213
|
+
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function toDistinctValue(
|
|
218
|
+
row: Record<string, unknown>,
|
|
219
|
+
field: FieldMetadata,
|
|
220
|
+
countField: string | null,
|
|
221
|
+
withCounts: boolean,
|
|
222
|
+
): DistinctValue | null {
|
|
223
|
+
let raw = row[field.name];
|
|
224
|
+
|
|
225
|
+
if (raw === null || raw === undefined || raw === "") return null;
|
|
226
|
+
|
|
227
|
+
let count = withCounts && countField ? Number(row[countField]) : null;
|
|
228
|
+
|
|
229
|
+
return { value: String(raw), count: Number.isFinite(count as number) ? (count as number) : null };
|
|
230
|
+
}
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference path="@olenbetong/appframe-core" />
|
|
2
|
+
/// <reference path="@olenbetong/appframe-data" />
|
|
3
|
+
|
|
4
|
+
declare module "*.css" {}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The MUI X license key, replaced at build time by the `appframe()` Vite plugin.
|
|
8
|
+
* Declared as possibly undefined, because consumers are free to bundle this
|
|
9
|
+
* package without the define.
|
|
10
|
+
*/
|
|
11
|
+
declare const __MUI_X_LICENSE_KEY__: string | undefined;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Button, Checkbox, Dialog, Heading, Search } from "@digdir/designsystemet-react";
|
|
2
|
+
import {
|
|
3
|
+
type GridApi,
|
|
4
|
+
gridColumnDefinitionsSelector,
|
|
5
|
+
gridColumnVisibilityModelSelector,
|
|
6
|
+
useGridSelector,
|
|
7
|
+
} from "@mui/x-data-grid-pro";
|
|
8
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
9
|
+
|
|
10
|
+
import { useState } from "react";
|
|
11
|
+
import { useAfGridContext } from "./AfGridContext.js";
|
|
12
|
+
|
|
13
|
+
export type AfGridColumnsPanelProps = {
|
|
14
|
+
open: boolean;
|
|
15
|
+
onClose: () => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Replacement for the grid's own column panel, built with Designsystemet
|
|
20
|
+
* components. Columns can be searched, toggled and reordered by dragging.
|
|
21
|
+
*
|
|
22
|
+
* Rendered as a sibling of the grid rather than inside it, so it reads the grid
|
|
23
|
+
* api from `AfGridContext` — `useGridApiContext` is only available inside the
|
|
24
|
+
* `DataGrid` subtree. `AfGrid` mounts it only while it is open, so the grid
|
|
25
|
+
* state is always initialized by the time the selectors run.
|
|
26
|
+
*/
|
|
27
|
+
export function AfGridColumnsPanel({ open, onClose }: AfGridColumnsPanelProps) {
|
|
28
|
+
let { apiRef } = useAfGridContext();
|
|
29
|
+
let columns = useGridSelector(apiRef as React.RefObject<GridApi>, gridColumnDefinitionsSelector);
|
|
30
|
+
let visibilityModel = useGridSelector(apiRef as React.RefObject<GridApi>, gridColumnVisibilityModelSelector);
|
|
31
|
+
let [search, setSearch] = useState("");
|
|
32
|
+
let [dragField, setDragField] = useState<string | null>(null);
|
|
33
|
+
|
|
34
|
+
let togglable = columns.filter((column) => column.hideable !== false && column.type !== "actions");
|
|
35
|
+
let query = search.trim().toLowerCase();
|
|
36
|
+
let matching = query
|
|
37
|
+
? togglable.filter((column) => (column.headerName ?? column.field).toLowerCase().includes(query))
|
|
38
|
+
: togglable;
|
|
39
|
+
|
|
40
|
+
function setAll(isVisible: boolean) {
|
|
41
|
+
let model = { ...visibilityModel };
|
|
42
|
+
|
|
43
|
+
for (let column of matching) {
|
|
44
|
+
model[column.field] = isVisible;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
apiRef.current?.setColumnVisibilityModel(model);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function handleDrop(targetField: string) {
|
|
51
|
+
if (!dragField || dragField === targetField) return;
|
|
52
|
+
|
|
53
|
+
let order = apiRef.current?.getAllColumns().map((column) => column.field) ?? [];
|
|
54
|
+
let targetIndex = order.indexOf(targetField);
|
|
55
|
+
|
|
56
|
+
if (targetIndex >= 0) {
|
|
57
|
+
apiRef.current?.setColumnIndex(dragField, targetIndex);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setDragField(null);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<Dialog
|
|
65
|
+
open={open}
|
|
66
|
+
className="AfGrid-columnsPanel"
|
|
67
|
+
closedby="any"
|
|
68
|
+
onClose={onClose}
|
|
69
|
+
closeButton={getLocalizedString("Close")}
|
|
70
|
+
>
|
|
71
|
+
<Dialog.Block>
|
|
72
|
+
<Heading level={2} data-size="xs">
|
|
73
|
+
{getLocalizedString("Columns")}
|
|
74
|
+
</Heading>
|
|
75
|
+
</Dialog.Block>
|
|
76
|
+
<Dialog.Block className="AfGrid-columnsPanelSearch">
|
|
77
|
+
<Search data-size="sm">
|
|
78
|
+
<Search.Input
|
|
79
|
+
aria-label={getLocalizedString("Search")}
|
|
80
|
+
value={search}
|
|
81
|
+
onChange={(event) => setSearch(event.currentTarget.value)}
|
|
82
|
+
/>
|
|
83
|
+
<Search.Clear onClick={() => setSearch("")} />
|
|
84
|
+
</Search>
|
|
85
|
+
<ul className="AfGrid-columnsPanelList">
|
|
86
|
+
{matching.map((column) => (
|
|
87
|
+
<li
|
|
88
|
+
key={column.field}
|
|
89
|
+
className="AfGrid-columnsPanelItem"
|
|
90
|
+
draggable
|
|
91
|
+
onDragStart={() => setDragField(column.field)}
|
|
92
|
+
onDragOver={(event) => event.preventDefault()}
|
|
93
|
+
onDrop={() => handleDrop(column.field)}
|
|
94
|
+
>
|
|
95
|
+
<Checkbox
|
|
96
|
+
data-size="sm"
|
|
97
|
+
label={column.headerName ?? column.field}
|
|
98
|
+
checked={visibilityModel[column.field] !== false}
|
|
99
|
+
onChange={(event) => apiRef.current?.setColumnVisibility(column.field, event.currentTarget.checked)}
|
|
100
|
+
/>
|
|
101
|
+
</li>
|
|
102
|
+
))}
|
|
103
|
+
</ul>
|
|
104
|
+
</Dialog.Block>
|
|
105
|
+
<Dialog.Block className="AfGrid-columnsPanelActions">
|
|
106
|
+
<Button variant="tertiary" data-size="sm" onClick={() => setAll(false)}>
|
|
107
|
+
{getLocalizedString("Hide all")}
|
|
108
|
+
</Button>
|
|
109
|
+
<Button variant="tertiary" data-size="sm" onClick={() => setAll(true)}>
|
|
110
|
+
{getLocalizedString("Show all")}
|
|
111
|
+
</Button>
|
|
112
|
+
<Button data-size="sm" onClick={onClose}>
|
|
113
|
+
{getLocalizedString("Close")}
|
|
114
|
+
</Button>
|
|
115
|
+
</Dialog.Block>
|
|
116
|
+
</Dialog>
|
|
117
|
+
);
|
|
118
|
+
}
|