@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,51 @@
|
|
|
1
|
+
import type { GridApi } from "@mui/x-data-grid-pro";
|
|
2
|
+
import type { FieldMetadata, FilterExpression, FilterTree } from "@olenbetong/appframe-core";
|
|
3
|
+
import { createContext, useContext } from "react";
|
|
4
|
+
|
|
5
|
+
export type AfGridContextValue = {
|
|
6
|
+
/** The grid's `id` prop. Used to key the named filter slot. */
|
|
7
|
+
id: string;
|
|
8
|
+
apiRef: React.RefObject<GridApi | null>;
|
|
9
|
+
/** Fields the filter builder can filter on, derived from the data object and the columns. */
|
|
10
|
+
filterFields: FieldMetadata[];
|
|
11
|
+
/** Opens the filter builder dialog. */
|
|
12
|
+
openFilterBuilder: () => void;
|
|
13
|
+
/**
|
|
14
|
+
* The grid's filter tree. The header filters and the filter builder edit the
|
|
15
|
+
* same tree, so a criterion added in one shows up in the other.
|
|
16
|
+
*/
|
|
17
|
+
filter: FilterTree;
|
|
18
|
+
/** Replaces the whole filter tree. */
|
|
19
|
+
setFilter: (filter: FilterTree) => void;
|
|
20
|
+
/** The criteria a header filter owns for a field. See `getFieldExpressions`. */
|
|
21
|
+
getFieldCriteria: (field: string) => FilterExpression[];
|
|
22
|
+
/** Replaces the criteria a header filter owns for a field. */
|
|
23
|
+
setFieldCriteria: (field: string, expressions: FilterExpression[]) => void;
|
|
24
|
+
/** Number of criteria in the filter, shown on the toolbar button. */
|
|
25
|
+
filterCount: number;
|
|
26
|
+
/** Opens the column panel. */
|
|
27
|
+
openColumnsPanel: () => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const AfGridContext = createContext<AfGridContextValue | null>(null);
|
|
31
|
+
|
|
32
|
+
export const AfGridContextProvider = AfGridContext.Provider;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The grid state shared with the toolbar, the header filters and the panels.
|
|
36
|
+
* Only available inside an `AfGrid`.
|
|
37
|
+
*/
|
|
38
|
+
export function useAfGridContext() {
|
|
39
|
+
let context = useContext(AfGridContext);
|
|
40
|
+
|
|
41
|
+
if (!context) {
|
|
42
|
+
throw new Error("useAfGridContext must be used inside an AfGrid");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return context;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Like `useAfGridContext`, but returns null outside an `AfGrid`. */
|
|
49
|
+
export function useOptionalAfGridContext() {
|
|
50
|
+
return useContext(AfGridContext);
|
|
51
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Alert, Button } from "@digdir/designsystemet-react";
|
|
2
|
+
import type { GridApi } from "@mui/x-data-grid-pro";
|
|
3
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
4
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
5
|
+
import { useError } from "@olenbetong/appframe-react";
|
|
6
|
+
import { useEffect, useRef, useState } from "react";
|
|
7
|
+
import { createPortal } from "react-dom";
|
|
8
|
+
|
|
9
|
+
type AfGridErrorProps = {
|
|
10
|
+
apiRef: React.RefObject<GridApi | null>;
|
|
11
|
+
dataObject: DataObject<any>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function AfGridError({ apiRef, dataObject }: AfGridErrorProps) {
|
|
15
|
+
let rawLoadError = useError(dataObject);
|
|
16
|
+
let [errorDismissed, setErrorDismissed] = useState(false);
|
|
17
|
+
let [gridRootEl, setGridRootEl] = useState<Element | null>(null);
|
|
18
|
+
let prevRawLoadErrorRef = useRef(rawLoadError);
|
|
19
|
+
|
|
20
|
+
if (prevRawLoadErrorRef.current !== rawLoadError) {
|
|
21
|
+
prevRawLoadErrorRef.current = rawLoadError;
|
|
22
|
+
if (rawLoadError) setErrorDismissed(false);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let loadError = errorDismissed ? null : rawLoadError;
|
|
26
|
+
|
|
27
|
+
// Capture the DataGrid root element after mount so we can portal into it.
|
|
28
|
+
// This lets us use --DataGrid-topContainerHeight to position below the headers.
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
setGridRootEl(apiRef.current?.rootElementRef?.current ?? null);
|
|
31
|
+
}, [apiRef]);
|
|
32
|
+
|
|
33
|
+
if (!loadError || !gridRootEl) return null;
|
|
34
|
+
|
|
35
|
+
return createPortal(
|
|
36
|
+
<div className="AfGrid-errorOverlay">
|
|
37
|
+
<Alert data-color="danger" data-size="sm" className="AfGrid-errorAlert">
|
|
38
|
+
<span>{loadError}</span>
|
|
39
|
+
<Button variant="tertiary" data-size="sm" data-color="danger" onClick={() => setErrorDismissed(true)}>
|
|
40
|
+
{getLocalizedString("Dismiss")}
|
|
41
|
+
</Button>
|
|
42
|
+
</Alert>
|
|
43
|
+
</div>,
|
|
44
|
+
gridRootEl,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Button, Popover } from "@digdir/designsystemet-react";
|
|
2
|
+
import type { GridRenderHeaderFilterProps } from "@mui/x-data-grid-pro";
|
|
3
|
+
import { FunnelFillIcon, FunnelIcon, XMarkIcon } from "@navikt/aksel-icons";
|
|
4
|
+
import {
|
|
5
|
+
createExpression,
|
|
6
|
+
type FieldMetadata,
|
|
7
|
+
type FilterExpression,
|
|
8
|
+
getLocalizedString,
|
|
9
|
+
} from "@olenbetong/appframe-core";
|
|
10
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
11
|
+
import { useState } from "react";
|
|
12
|
+
|
|
13
|
+
import { InputAdornments, InputSlot } from "../input/index.js";
|
|
14
|
+
import { useAfGridContext } from "./AfGridContext.js";
|
|
15
|
+
import { AfHeaderFilterPanel } from "./AfHeaderFilterPanel.js";
|
|
16
|
+
import { getSimpleValue, isSimpleFieldFilter, summarizeFieldFilter } from "../filter/fieldFilter.js";
|
|
17
|
+
import { findFilterField } from "./useAfFilter.js";
|
|
18
|
+
import type { AfGridColDef } from "./useAfColumns.js";
|
|
19
|
+
|
|
20
|
+
export type AfHeaderFilterCellProps = GridRenderHeaderFilterProps & {
|
|
21
|
+
dataObject: DataObject<any>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The contents of a header filter cell: an unlabelled input with a filter
|
|
26
|
+
* button that opens the per-field filter panel.
|
|
27
|
+
*
|
|
28
|
+
* MUI keeps ownership of the cell itself (width, pinning, borders, roles and
|
|
29
|
+
* keyboard navigation) — this only replaces what goes inside it.
|
|
30
|
+
*
|
|
31
|
+
* Note that the input deliberately does *not* drive MUI's header filter edit
|
|
32
|
+
* mode from `onFocus`/`onBlur` the way MUI's own inputs do. The cell refocuses
|
|
33
|
+
* itself in a layout effect whenever `hasFocus` or `isEditing` change, so
|
|
34
|
+
* toggling edit mode on every blur means any mousedown that leaves the input
|
|
35
|
+
* synchronously pulls focus back into it. That made it impossible to focus the
|
|
36
|
+
* filter panel, and it cancelled column drags started from a header while the
|
|
37
|
+
* input had focus (a `focus()` call during drag start aborts the drag).
|
|
38
|
+
*
|
|
39
|
+
* Nothing is lost by staying out of edit mode: MUI focuses the cell's
|
|
40
|
+
* `tabindex="0"` element — this input — whenever the cell gains focus, so
|
|
41
|
+
* typing and keyboard navigation still work.
|
|
42
|
+
*
|
|
43
|
+
* A single criterion taking one literal value is edited straight from the
|
|
44
|
+
* input. Anything else (no criteria, a list, several criteria, a parameterless
|
|
45
|
+
* operator) is shown as a read-only summary and edited in the panel.
|
|
46
|
+
*/
|
|
47
|
+
export function AfHeaderFilterCell({ colDef, hasFocus, inputRef, dataObject }: AfHeaderFilterCellProps) {
|
|
48
|
+
let { apiRef, filter, filterFields, getFieldCriteria, setFieldCriteria } = useAfGridContext();
|
|
49
|
+
let [isPanelOpen, setPanelOpen] = useState(false);
|
|
50
|
+
|
|
51
|
+
let fieldName = (colDef as AfGridColDef).filterColumn ?? colDef.field;
|
|
52
|
+
let field = findFilterField(filterFields, fieldName);
|
|
53
|
+
|
|
54
|
+
if (!field || colDef.filterable === false || colDef.type === "actions") {
|
|
55
|
+
return <span className="AfGrid-headerFilterEmpty" />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let expressions = getFieldCriteria(fieldName);
|
|
59
|
+
let isSimple = isSimpleFieldFilter(expressions);
|
|
60
|
+
let hasFilter = expressions.length > 0;
|
|
61
|
+
let caption = colDef.headerName ?? colDef.field;
|
|
62
|
+
|
|
63
|
+
function apply(next: FilterExpression[]) {
|
|
64
|
+
setFieldCriteria(fieldName, next);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* MUI suppresses its refocus effect while its own header filter menu is
|
|
69
|
+
* open, so the panel claims that flag for as long as it is open.
|
|
70
|
+
*/
|
|
71
|
+
function handleOpen() {
|
|
72
|
+
setPanelOpen(true);
|
|
73
|
+
apiRef.current?.showHeaderFilterMenu(colDef.field);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function handleClose() {
|
|
77
|
+
setPanelOpen(false);
|
|
78
|
+
apiRef.current?.hideHeaderFilterMenu();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function handleChange(value: string) {
|
|
82
|
+
if (!field) return;
|
|
83
|
+
|
|
84
|
+
if (value === "") {
|
|
85
|
+
apply([]);
|
|
86
|
+
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Typing into an empty cell starts a criterion with the field's default
|
|
91
|
+
// operator, the same one the panel would offer.
|
|
92
|
+
let expression = isSimple ? (expressions[0] as FilterExpression) : createExpression(field);
|
|
93
|
+
|
|
94
|
+
apply([{ ...expression, value }]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<Popover.TriggerContext>
|
|
99
|
+
<InputAdornments
|
|
100
|
+
className="AfGrid-headerFilterInput"
|
|
101
|
+
data-size="sm"
|
|
102
|
+
aria-label={getLocalizedString("Filter by {0}").replace("{0}", caption)}
|
|
103
|
+
ref={inputRef as React.Ref<HTMLInputElement>}
|
|
104
|
+
tabIndex={hasFocus ? 0 : -1}
|
|
105
|
+
readOnly={hasFilter && !isSimple}
|
|
106
|
+
value={isSimple || !hasFilter ? getSimpleValue(expressions) : summarizeFieldFilter(expressions, field)}
|
|
107
|
+
onChange={(event) => handleChange(event.currentTarget.value)}
|
|
108
|
+
>
|
|
109
|
+
<InputSlot />
|
|
110
|
+
{hasFilter && (
|
|
111
|
+
<Button
|
|
112
|
+
type="button"
|
|
113
|
+
icon
|
|
114
|
+
variant="tertiary"
|
|
115
|
+
data-size="sm"
|
|
116
|
+
tabIndex={-1}
|
|
117
|
+
aria-label={getLocalizedString("Clear filter")}
|
|
118
|
+
title={getLocalizedString("Clear filter")}
|
|
119
|
+
onClick={() => apply([])}
|
|
120
|
+
>
|
|
121
|
+
<XMarkIcon aria-hidden />
|
|
122
|
+
</Button>
|
|
123
|
+
)}
|
|
124
|
+
<Popover.Trigger
|
|
125
|
+
type="button"
|
|
126
|
+
icon
|
|
127
|
+
variant="tertiary"
|
|
128
|
+
data-size="sm"
|
|
129
|
+
tabIndex={-1}
|
|
130
|
+
className="AfGrid-headerFilterButton"
|
|
131
|
+
data-active={hasFilter || undefined}
|
|
132
|
+
aria-label={getLocalizedString("Filter options")}
|
|
133
|
+
title={getLocalizedString("Filter options")}
|
|
134
|
+
>
|
|
135
|
+
{hasFilter ? <FunnelFillIcon aria-hidden /> : <FunnelIcon aria-hidden />}
|
|
136
|
+
</Popover.Trigger>
|
|
137
|
+
</InputAdornments>
|
|
138
|
+
<AfHeaderFilterPanel
|
|
139
|
+
open={isPanelOpen}
|
|
140
|
+
onOpen={handleOpen}
|
|
141
|
+
onClose={handleClose}
|
|
142
|
+
caption={caption}
|
|
143
|
+
field={field as FieldMetadata}
|
|
144
|
+
dataObject={dataObject}
|
|
145
|
+
filter={filter}
|
|
146
|
+
expressions={expressions}
|
|
147
|
+
onApply={apply}
|
|
148
|
+
/>
|
|
149
|
+
</Popover.TriggerContext>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Button, Popover } from "@digdir/designsystemet-react";
|
|
2
|
+
import {
|
|
3
|
+
createExpression,
|
|
4
|
+
type FieldMetadata,
|
|
5
|
+
type FilterExpression,
|
|
6
|
+
type FilterTree,
|
|
7
|
+
getLocalizedString,
|
|
8
|
+
isCompleteExpression,
|
|
9
|
+
} from "@olenbetong/appframe-core";
|
|
10
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
11
|
+
import { useEffect, useState } from "react";
|
|
12
|
+
|
|
13
|
+
import { FieldFilterPanel } from "../filter/FieldFilterPanel.js";
|
|
14
|
+
|
|
15
|
+
export type AfHeaderFilterPanelProps = {
|
|
16
|
+
open: boolean;
|
|
17
|
+
/** Called when the popover trigger is activated while the panel is closed. */
|
|
18
|
+
onOpen: () => void;
|
|
19
|
+
onClose: () => void;
|
|
20
|
+
/** The column caption, used as the panel heading. */
|
|
21
|
+
caption: string;
|
|
22
|
+
field: FieldMetadata;
|
|
23
|
+
dataObject: DataObject<any>;
|
|
24
|
+
/** The whole grid filter, used to scope the distinct value list. */
|
|
25
|
+
filter: FilterTree;
|
|
26
|
+
/** The field's current top level criteria. */
|
|
27
|
+
expressions: FilterExpression[];
|
|
28
|
+
/** Called with the field's new criteria when the user applies the panel. */
|
|
29
|
+
onApply: (expressions: FilterExpression[]) => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A `FieldFilterPanel` in a popover, opened from a header filter cell. Must be
|
|
34
|
+
* rendered inside a `Popover.TriggerContext` together with the button that
|
|
35
|
+
* opens it.
|
|
36
|
+
*
|
|
37
|
+
* This adds the open/close lifecycle the standalone panel deliberately leaves
|
|
38
|
+
* out: edits are staged while the popover is open and only committed on apply,
|
|
39
|
+
* so dismissing it leaves the grid's filter untouched.
|
|
40
|
+
*/
|
|
41
|
+
export function AfHeaderFilterPanel({
|
|
42
|
+
open,
|
|
43
|
+
onOpen,
|
|
44
|
+
onClose,
|
|
45
|
+
caption,
|
|
46
|
+
field,
|
|
47
|
+
dataObject,
|
|
48
|
+
filter,
|
|
49
|
+
expressions,
|
|
50
|
+
onApply,
|
|
51
|
+
}: AfHeaderFilterPanelProps) {
|
|
52
|
+
let [draft, setDraft] = useState<FilterExpression[]>(expressions);
|
|
53
|
+
|
|
54
|
+
// Opening the panel discards anything left over from the last time it was
|
|
55
|
+
// dismissed without applying, and picks up criteria edited in the header
|
|
56
|
+
// input in the meantime. An empty field gets one blank criterion so there
|
|
57
|
+
// is always something to type into.
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!open) return;
|
|
60
|
+
|
|
61
|
+
setDraft(expressions.length > 0 ? expressions : [createExpression(field)]);
|
|
62
|
+
// Only reset when the panel opens — reacting to `expressions` as well
|
|
63
|
+
// would throw away the user's edits on every keystroke.
|
|
64
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- see above
|
|
65
|
+
}, [open]);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<Popover
|
|
69
|
+
open={open}
|
|
70
|
+
placement="bottom-start"
|
|
71
|
+
className="AfGrid-headerFilterPanel"
|
|
72
|
+
data-size="sm"
|
|
73
|
+
onOpen={onOpen}
|
|
74
|
+
onClose={onClose}
|
|
75
|
+
>
|
|
76
|
+
{/*
|
|
77
|
+
* Mounting only while open keeps the distinct value query from running
|
|
78
|
+
* for every column in the grid at once.
|
|
79
|
+
*/}
|
|
80
|
+
{open && (
|
|
81
|
+
<FieldFilterPanel
|
|
82
|
+
field={field}
|
|
83
|
+
dataObject={dataObject}
|
|
84
|
+
filter={filter}
|
|
85
|
+
heading={caption}
|
|
86
|
+
value={draft}
|
|
87
|
+
onChange={setDraft}
|
|
88
|
+
>
|
|
89
|
+
<div className="ObFieldFilterPanel-actions">
|
|
90
|
+
<Button type="button" variant="tertiary" data-size="sm" onClick={() => setDraft([createExpression(field)])}>
|
|
91
|
+
{getLocalizedString("Clear")}
|
|
92
|
+
</Button>
|
|
93
|
+
<Button
|
|
94
|
+
type="button"
|
|
95
|
+
data-size="sm"
|
|
96
|
+
onClick={() => {
|
|
97
|
+
// A criterion the user never finished typing would be
|
|
98
|
+
// dropped by `pruneFilter` anyway, and keeping it would
|
|
99
|
+
// make the column look filtered when it is not.
|
|
100
|
+
onApply(draft.filter(isCompleteExpression));
|
|
101
|
+
onClose();
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{getLocalizedString("Apply")}
|
|
105
|
+
</Button>
|
|
106
|
+
</div>
|
|
107
|
+
</FieldFilterPanel>
|
|
108
|
+
)}
|
|
109
|
+
</Popover>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { Button, Dropdown } from "@digdir/designsystemet-react";
|
|
2
|
+
import {
|
|
3
|
+
type GridApi,
|
|
4
|
+
type GridExportMenuItemProps,
|
|
5
|
+
gridVisibleColumnDefinitionsSelector,
|
|
6
|
+
useGridApiContext,
|
|
7
|
+
} from "@mui/x-data-grid-pro";
|
|
8
|
+
import {
|
|
9
|
+
ArrowsCirclepathIcon,
|
|
10
|
+
Density1Icon,
|
|
11
|
+
Density2Icon,
|
|
12
|
+
Density3Icon,
|
|
13
|
+
FileCsvIcon,
|
|
14
|
+
FileExcelIcon,
|
|
15
|
+
FilterIcon,
|
|
16
|
+
MenuElipsisVerticalIcon,
|
|
17
|
+
PlusIcon,
|
|
18
|
+
TableIcon,
|
|
19
|
+
} from "@navikt/aksel-icons";
|
|
20
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
21
|
+
import { type DataObject, exportOptionsFromDataObject } from "@olenbetong/appframe-data";
|
|
22
|
+
import { useDataObject, useLoading, usePermissions } from "@olenbetong/appframe-react";
|
|
23
|
+
|
|
24
|
+
import { openDataObjectReportWithProgress } from "../report-downloader/index.js";
|
|
25
|
+
import { useAfGridContext } from "./AfGridContext.js";
|
|
26
|
+
import { addRow } from "./editing.js";
|
|
27
|
+
import type { AfGridApi } from "./useAfGridApi.js";
|
|
28
|
+
|
|
29
|
+
export function GridToolbarAddButton({ onClick }: { onClick: () => void }) {
|
|
30
|
+
return (
|
|
31
|
+
<Button variant="tertiary" data-size="sm" onClick={onClick}>
|
|
32
|
+
<PlusIcon aria-hidden />
|
|
33
|
+
{getLocalizedString("Add")}
|
|
34
|
+
</Button>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function GridToolbarAddIfAllowedButton({ disableInsert }: { disableInsert?: boolean }) {
|
|
39
|
+
let dataObject = useDataObject();
|
|
40
|
+
let apiRef = useGridApiContext();
|
|
41
|
+
let permissions = usePermissions(dataObject);
|
|
42
|
+
|
|
43
|
+
return permissions.allowInsert && !disableInsert ? (
|
|
44
|
+
<GridToolbarAddButton onClick={() => addRow({ apiRef, dataObject })} />
|
|
45
|
+
) : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function GridToolbarRefreshButton() {
|
|
49
|
+
let dataObject = useDataObject();
|
|
50
|
+
let isLoading = useLoading(dataObject);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Button variant="tertiary" data-size="sm" loading={isLoading} onClick={() => dataObject.refreshDataSource()}>
|
|
54
|
+
{/* Designsystemet prepends the spinner rather than replacing the children, so
|
|
55
|
+
the icon is hidden while loading to keep the button from changing width */}
|
|
56
|
+
{!isLoading && <ArrowsCirclepathIcon aria-hidden />}
|
|
57
|
+
{getLocalizedString("Refresh")}
|
|
58
|
+
</Button>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function GridToolbarColumnsButton() {
|
|
63
|
+
let { openColumnsPanel } = useAfGridContext();
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Button variant="tertiary" data-size="sm" onClick={openColumnsPanel}>
|
|
67
|
+
<TableIcon aria-hidden />
|
|
68
|
+
{getLocalizedString("Columns")}
|
|
69
|
+
</Button>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Opens the filter builder instead of the grid's own filter panel. The count
|
|
75
|
+
* reflects the conditions in the builder's filter, not the header filters.
|
|
76
|
+
*/
|
|
77
|
+
export function GridToolbarFilterButton() {
|
|
78
|
+
let { openFilterBuilder, filterCount } = useAfGridContext();
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<Button variant="tertiary" data-size="sm" onClick={openFilterBuilder}>
|
|
82
|
+
<FilterIcon aria-hidden />
|
|
83
|
+
{getLocalizedString("Filter")}
|
|
84
|
+
{filterCount > 0 ? ` (${filterCount})` : ""}
|
|
85
|
+
</Button>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function GridToolbarResetLayoutButton() {
|
|
90
|
+
let apiRef = useGridApiContext();
|
|
91
|
+
|
|
92
|
+
function handleReset() {
|
|
93
|
+
(apiRef.current as AfGridApi).resetLayout?.();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<Button variant="tertiary" data-size="sm" onClick={handleReset}>
|
|
98
|
+
<ArrowsCirclepathIcon aria-hidden />
|
|
99
|
+
{getLocalizedString("Reset layout")}
|
|
100
|
+
</Button>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function handleExportToExcel(
|
|
105
|
+
apiRef: React.RefObject<GridApi>,
|
|
106
|
+
dataObject: DataObject<any>,
|
|
107
|
+
fileName?: string,
|
|
108
|
+
title?: string,
|
|
109
|
+
) {
|
|
110
|
+
let visibleColumns = gridVisibleColumnDefinitionsSelector(apiRef);
|
|
111
|
+
openDataObjectReportWithProgress({
|
|
112
|
+
dataObject: dataObject.getDataSourceId(),
|
|
113
|
+
exportUrl: dataObject.getExportURL("xlsx"),
|
|
114
|
+
fileName: fileName ?? dataObject.getDataSourceId(),
|
|
115
|
+
format: "xlsx",
|
|
116
|
+
options: exportOptionsFromDataObject(dataObject, {
|
|
117
|
+
columns: visibleColumns
|
|
118
|
+
.filter((c) => !!dataObject.getFieldDefinition(c.field))
|
|
119
|
+
.map((column) => ({
|
|
120
|
+
name: column.field,
|
|
121
|
+
caption: column.headerName ?? column.field,
|
|
122
|
+
})),
|
|
123
|
+
maxRecords: -1,
|
|
124
|
+
}),
|
|
125
|
+
title,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export type AfExcelExportOptions = { fileName?: string; title?: string };
|
|
130
|
+
export type AfGridExcelExportOptions = GridExportMenuItemProps<AfExcelExportOptions>;
|
|
131
|
+
|
|
132
|
+
export function AfGridExcelExportMenuItem({ options = {}, hideMenu }: AfGridExcelExportOptions) {
|
|
133
|
+
let apiRef = useGridApiContext();
|
|
134
|
+
let dataObject = useDataObject();
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<Dropdown.Item>
|
|
138
|
+
<Dropdown.Button
|
|
139
|
+
onClick={() => {
|
|
140
|
+
handleExportToExcel(apiRef, dataObject, options.fileName, options.title);
|
|
141
|
+
hideMenu?.();
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
<FileExcelIcon aria-hidden />
|
|
145
|
+
{getLocalizedString("Download as Excel")}
|
|
146
|
+
</Dropdown.Button>
|
|
147
|
+
</Dropdown.Item>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function AfGridToolbarExcelExportButton({ fileName, title }: { fileName?: string; title?: string }) {
|
|
152
|
+
let apiRef = useGridApiContext();
|
|
153
|
+
let dataObject = useDataObject();
|
|
154
|
+
|
|
155
|
+
return (
|
|
156
|
+
<Button variant="tertiary" data-size="sm" onClick={() => handleExportToExcel(apiRef, dataObject, fileName, title)}>
|
|
157
|
+
<FileExcelIcon aria-hidden />
|
|
158
|
+
{getLocalizedString("Export")}
|
|
159
|
+
</Button>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function GridToolbarMoreOptionsButton() {
|
|
164
|
+
let apiRef = useGridApiContext();
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<Dropdown.TriggerContext>
|
|
168
|
+
<Dropdown.Trigger variant="tertiary" icon data-size="sm" aria-label={getLocalizedString("More options")}>
|
|
169
|
+
<MenuElipsisVerticalIcon aria-hidden />
|
|
170
|
+
</Dropdown.Trigger>
|
|
171
|
+
<Dropdown placement="bottom-end" data-size="sm">
|
|
172
|
+
<Dropdown.List>
|
|
173
|
+
<Dropdown.Item>
|
|
174
|
+
<Dropdown.Button onClick={() => apiRef.current.exportDataAsCsv()}>
|
|
175
|
+
<FileCsvIcon aria-hidden />
|
|
176
|
+
{getLocalizedString("Export as CSV")}
|
|
177
|
+
</Dropdown.Button>
|
|
178
|
+
</Dropdown.Item>
|
|
179
|
+
<AfGridExcelExportMenuItem />
|
|
180
|
+
<Dropdown.Item>
|
|
181
|
+
<Dropdown.Button onClick={() => apiRef.current.setDensity("compact")}>
|
|
182
|
+
<Density1Icon aria-hidden />
|
|
183
|
+
{getLocalizedString("Compact")}
|
|
184
|
+
</Dropdown.Button>
|
|
185
|
+
</Dropdown.Item>
|
|
186
|
+
<Dropdown.Item>
|
|
187
|
+
<Dropdown.Button onClick={() => apiRef.current.setDensity("standard")}>
|
|
188
|
+
<Density2Icon aria-hidden />
|
|
189
|
+
{getLocalizedString("Standard")}
|
|
190
|
+
</Dropdown.Button>
|
|
191
|
+
</Dropdown.Item>
|
|
192
|
+
<Dropdown.Item>
|
|
193
|
+
<Dropdown.Button onClick={() => apiRef.current.setDensity("comfortable")}>
|
|
194
|
+
<Density3Icon aria-hidden />
|
|
195
|
+
{getLocalizedString("Comfortable")}
|
|
196
|
+
</Dropdown.Button>
|
|
197
|
+
</Dropdown.Item>
|
|
198
|
+
<Dropdown.Item>
|
|
199
|
+
<Dropdown.Button onClick={() => (apiRef.current as AfGridApi).resetLayout?.()}>
|
|
200
|
+
<ArrowsCirclepathIcon aria-hidden />
|
|
201
|
+
{getLocalizedString("Reset layout")}
|
|
202
|
+
</Dropdown.Button>
|
|
203
|
+
</Dropdown.Item>
|
|
204
|
+
</Dropdown.List>
|
|
205
|
+
</Dropdown>
|
|
206
|
+
</Dropdown.TriggerContext>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function AfGridToolbar({ children, disableInsert }: React.PropsWithChildren<{ disableInsert?: boolean }>) {
|
|
211
|
+
return (
|
|
212
|
+
<GridToolbarContainer>
|
|
213
|
+
<GridToolbarAddIfAllowedButton disableInsert={disableInsert} />
|
|
214
|
+
<GridToolbarColumnsButton />
|
|
215
|
+
<GridToolbarFilterButton />
|
|
216
|
+
<GridToolbarRefreshButton />
|
|
217
|
+
<GridToolbarMoreOptionsButton />
|
|
218
|
+
{children}
|
|
219
|
+
</GridToolbarContainer>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The toolbar surface itself, for grids that compose their own set of buttons.
|
|
225
|
+
* Replaces MUI's `DataGrid.Toolbar`.
|
|
226
|
+
*/
|
|
227
|
+
export function GridToolbarContainer({ children, className, ...props }: React.ComponentProps<"div">) {
|
|
228
|
+
return (
|
|
229
|
+
<div {...props} className={className ? `AfGrid-toolbar ${className}` : "AfGrid-toolbar"}>
|
|
230
|
+
{children}
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type GridApi, gridFocusCellSelector, gridVisibleColumnDefinitionsSelector } from "@mui/x-data-grid-pro";
|
|
2
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
3
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
4
|
+
|
|
5
|
+
import type React from "react";
|
|
6
|
+
|
|
7
|
+
export function addRow<T extends { PrimKey: string }>({
|
|
8
|
+
apiRef,
|
|
9
|
+
dataObject,
|
|
10
|
+
}: {
|
|
11
|
+
apiRef: React.RefObject<GridApi | null>;
|
|
12
|
+
dataObject: DataObject<T>;
|
|
13
|
+
}) {
|
|
14
|
+
let newRow = dataObject.newRow();
|
|
15
|
+
let id = (newRow as any)[(af as any).data.uid];
|
|
16
|
+
apiRef.current?.updateRows([{ [(af as any).data.uid]: id, id, isNew: true } as any]);
|
|
17
|
+
apiRef.current?.startRowEditMode({ id });
|
|
18
|
+
|
|
19
|
+
// Wait for the grid to render with the new row
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
apiRef.current?.scrollToIndexes({
|
|
22
|
+
rowIndex: apiRef.current?.getRowsCount() - 1,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
let visibleColumns = gridVisibleColumnDefinitionsSelector(apiRef);
|
|
26
|
+
let firstEditable = visibleColumns.find((c) => c.editable === true);
|
|
27
|
+
|
|
28
|
+
if (firstEditable) {
|
|
29
|
+
apiRef.current?.setCellFocus(id, firstEditable.field);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function deleteRow<T extends { PrimKey: string }>({
|
|
35
|
+
apiRef,
|
|
36
|
+
dataObject,
|
|
37
|
+
id,
|
|
38
|
+
idField,
|
|
39
|
+
}: {
|
|
40
|
+
apiRef: React.RefObject<GridApi | null>;
|
|
41
|
+
dataObject: DataObject<T>;
|
|
42
|
+
id: string | number;
|
|
43
|
+
idField: string;
|
|
44
|
+
}) {
|
|
45
|
+
try {
|
|
46
|
+
if (!window.confirm(getLocalizedString("Are you sure you want to delete the selected row?"))) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let focusBefore = gridFocusCellSelector(apiRef);
|
|
51
|
+
let index = dataObject.findIndex((r) => (r as any)[idField] === id || (r as any)[(af as any).data.uid] === id);
|
|
52
|
+
|
|
53
|
+
if (index < 0) {
|
|
54
|
+
alert(getLocalizedString("Row not found"));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
await dataObject.deleteRow(index);
|
|
59
|
+
|
|
60
|
+
let focusAfter = gridFocusCellSelector(apiRef);
|
|
61
|
+
|
|
62
|
+
// Need to run this operation to prevent MUI errors
|
|
63
|
+
apiRef.current?.updateRows([{ [af.data.uid]: id, id, _action: "delete" }]);
|
|
64
|
+
|
|
65
|
+
if (!focusAfter || focusAfter.id === id) {
|
|
66
|
+
if (dataObject.getDataLength() > 0) {
|
|
67
|
+
let newIndex = Math.max(0, Math.min(dataObject.getDataLength() - 1, index - 1));
|
|
68
|
+
let newId = (dataObject.getData(newIndex) as any)[(af as any).data.uid];
|
|
69
|
+
let newField = focusBefore?.field ?? gridVisibleColumnDefinitionsSelector(apiRef)[0]?.field;
|
|
70
|
+
|
|
71
|
+
if (newId && newField) {
|
|
72
|
+
apiRef.current?.setCellFocus(newId, newField);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} catch (error) {
|
|
77
|
+
alert((error as Error).message ?? error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function cancelEdit<T extends { PrimKey: string }>({
|
|
82
|
+
apiRef,
|
|
83
|
+
id,
|
|
84
|
+
}: {
|
|
85
|
+
apiRef: React.RefObject<GridApi>;
|
|
86
|
+
dataObject: DataObject<T>;
|
|
87
|
+
id: string | number;
|
|
88
|
+
}) {
|
|
89
|
+
apiRef.current.stopRowEditMode({ id, ignoreModifications: true });
|
|
90
|
+
}
|