@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,117 @@
|
|
|
1
|
+
import { Select } from "@digdir/designsystemet-react";
|
|
2
|
+
import { XMarkIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import {
|
|
4
|
+
changeOperator,
|
|
5
|
+
createExpression,
|
|
6
|
+
type FieldMetadata,
|
|
7
|
+
type FilterExpression,
|
|
8
|
+
type FilterOperator,
|
|
9
|
+
getLocalizedString,
|
|
10
|
+
getOperatorLabel,
|
|
11
|
+
getOperatorsForField,
|
|
12
|
+
normalizeOperator,
|
|
13
|
+
} from "@olenbetong/appframe-core";
|
|
14
|
+
import { Combobox } from "../autocomplete/index.js";
|
|
15
|
+
|
|
16
|
+
import { FilterValueEditor } from "./FilterValueEditor.js";
|
|
17
|
+
import type { FilterEditorSize, GetFieldOptions } from "./types.js";
|
|
18
|
+
|
|
19
|
+
export function getFieldLabel(field: FieldMetadata) {
|
|
20
|
+
return field.caption || field.name;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type FilterRowProps = {
|
|
24
|
+
expression: FilterExpression;
|
|
25
|
+
fields: FieldMetadata[];
|
|
26
|
+
onChange: (expression: FilterExpression) => void;
|
|
27
|
+
onRemove: () => void;
|
|
28
|
+
getFieldOptions?: GetFieldOptions;
|
|
29
|
+
readOnly?: boolean;
|
|
30
|
+
"data-size"?: FilterEditorSize;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** A single `field operator value` row. */
|
|
34
|
+
export function FilterRow({
|
|
35
|
+
expression,
|
|
36
|
+
fields,
|
|
37
|
+
onChange,
|
|
38
|
+
onRemove,
|
|
39
|
+
getFieldOptions,
|
|
40
|
+
readOnly,
|
|
41
|
+
"data-size": size = "sm",
|
|
42
|
+
}: FilterRowProps) {
|
|
43
|
+
let field = fields.find((item) => item.name === expression.column);
|
|
44
|
+
let operator = normalizeOperator(expression.operator);
|
|
45
|
+
// A filter can reference a field that is no longer in the view. Keep the row
|
|
46
|
+
// editable by falling back to a string field rather than dropping it.
|
|
47
|
+
let effectiveField: FieldMetadata = field ?? { name: expression.column, type: "string" };
|
|
48
|
+
let operators = getOperatorsForField(effectiveField);
|
|
49
|
+
|
|
50
|
+
if (!operators.includes(operator)) {
|
|
51
|
+
operators = [...operators, operator];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function handleFieldChange(next: FieldMetadata | null) {
|
|
55
|
+
if (!next) return;
|
|
56
|
+
|
|
57
|
+
// Keep the operator when the new field supports it, so switching between
|
|
58
|
+
// two fields of the same type does not reset the row.
|
|
59
|
+
let operators = getOperatorsForField(next);
|
|
60
|
+
let keep = operators.includes(operator) ? operator : undefined;
|
|
61
|
+
let created = createExpression(next, keep);
|
|
62
|
+
|
|
63
|
+
onChange(keep ? { ...created, value: expression.value, valueType: expression.valueType } : created);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div className="ObFilterEditor-row">
|
|
68
|
+
<Combobox
|
|
69
|
+
className="ObFilterEditor-field"
|
|
70
|
+
data-size={size}
|
|
71
|
+
readOnly={readOnly}
|
|
72
|
+
disableClearable
|
|
73
|
+
aria-label={getLocalizedString("Field")}
|
|
74
|
+
placeholder={getLocalizedString("Select field")}
|
|
75
|
+
options={fields}
|
|
76
|
+
getOptionLabel={getFieldLabel}
|
|
77
|
+
getOptionKey={(option) => option.name}
|
|
78
|
+
isOptionEqualToValue={(a, b) => a.name === b.name}
|
|
79
|
+
value={effectiveField}
|
|
80
|
+
onChange={(_event, next) => handleFieldChange(next)}
|
|
81
|
+
/>
|
|
82
|
+
<Select
|
|
83
|
+
className="ObFilterEditor-operator"
|
|
84
|
+
data-size={size}
|
|
85
|
+
disabled={readOnly}
|
|
86
|
+
aria-label={getLocalizedString("Operator")}
|
|
87
|
+
value={operator}
|
|
88
|
+
onChange={(event) => onChange(changeOperator(expression, event.target.value as FilterOperator, effectiveField))}
|
|
89
|
+
>
|
|
90
|
+
{operators.map((item) => (
|
|
91
|
+
<Select.Option key={item} value={item}>
|
|
92
|
+
{getOperatorLabel(item, effectiveField.type)}
|
|
93
|
+
</Select.Option>
|
|
94
|
+
))}
|
|
95
|
+
</Select>
|
|
96
|
+
<FilterValueEditor
|
|
97
|
+
expression={expression}
|
|
98
|
+
field={effectiveField}
|
|
99
|
+
options={field ? getFieldOptions?.(field) : undefined}
|
|
100
|
+
onChange={onChange}
|
|
101
|
+
readOnly={readOnly}
|
|
102
|
+
data-size={size}
|
|
103
|
+
/>
|
|
104
|
+
{!readOnly && (
|
|
105
|
+
<button
|
|
106
|
+
type="button"
|
|
107
|
+
className="ObFilterEditor-iconButton"
|
|
108
|
+
title={getLocalizedString("Remove condition")}
|
|
109
|
+
aria-label={getLocalizedString("Remove condition")}
|
|
110
|
+
onClick={onRemove}
|
|
111
|
+
>
|
|
112
|
+
<XMarkIcon aria-hidden />
|
|
113
|
+
</button>
|
|
114
|
+
)}
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
.ObFilterShareDialog-root {
|
|
7
|
+
inline-size: min(40rem, 100%);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.ObFilterShareDialog-candidate {
|
|
11
|
+
display: flex;
|
|
12
|
+
align-items: center;
|
|
13
|
+
gap: var(--ds-size-2);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.ObFilterShareDialog-candidateLogin {
|
|
17
|
+
margin-inline-start: auto;
|
|
18
|
+
color: var(--ds-color-text-subtle);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.ObFilterShareDialog-table {
|
|
22
|
+
inline-size: 100%;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.ObFilterShareDialog-error {
|
|
26
|
+
margin: 0 0 var(--ds-size-2);
|
|
27
|
+
color: var(--ds-color-danger-text-default);
|
|
28
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Button, Dialog, Heading, Select, Table } from "@digdir/designsystemet-react";
|
|
2
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
3
|
+
import { type FilterPermission, useDebounce, useFilterPermissions } from "@olenbetong/appframe-react";
|
|
4
|
+
import { Combobox } from "../autocomplete/index.js";
|
|
5
|
+
import { PersonGroupIcon, PersonIcon, TrashIcon } from "@navikt/aksel-icons";
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
|
|
8
|
+
import "./FilterShareDialog.css";
|
|
9
|
+
|
|
10
|
+
type PermissionCandidate = {
|
|
11
|
+
primKey: string;
|
|
12
|
+
name: string;
|
|
13
|
+
login: string;
|
|
14
|
+
type: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type FilterShareDialogProps = {
|
|
18
|
+
open: boolean;
|
|
19
|
+
onClose: () => void;
|
|
20
|
+
/** `FilterID` of the filter to manage sharing for. */
|
|
21
|
+
filterId: number | null;
|
|
22
|
+
/** Shown in the dialog heading. */
|
|
23
|
+
filterName?: string;
|
|
24
|
+
"data-size"?: "sm" | "md" | "lg";
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Manages who a saved filter is shared with.
|
|
29
|
+
*
|
|
30
|
+
* Only a manager of the filter can load or change the grants — for anyone else
|
|
31
|
+
* `sstp_WebSiteCMS_FilterBuilder_GetPermissions` returns nothing, so the list
|
|
32
|
+
* simply stays empty.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```jsx
|
|
36
|
+
* <FilterShareDialog open={sharing} filterId={selected} filterName={name} onClose={() => setSharing(false)} />
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function FilterShareDialog({
|
|
40
|
+
open,
|
|
41
|
+
onClose,
|
|
42
|
+
filterId,
|
|
43
|
+
filterName,
|
|
44
|
+
"data-size": size = "sm",
|
|
45
|
+
}: FilterShareDialogProps) {
|
|
46
|
+
let { permissions, loading, error, setPermission, removePermission, search } = useFilterPermissions(
|
|
47
|
+
open ? filterId : null,
|
|
48
|
+
);
|
|
49
|
+
let [candidates, setCandidates] = useState<PermissionCandidate[]>([]);
|
|
50
|
+
let [searchText, setSearchText] = useState("");
|
|
51
|
+
let query = useDebounce(searchText, 300);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!open || query.length < 2) {
|
|
55
|
+
setCandidates([]);
|
|
56
|
+
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let isCancelled = false;
|
|
61
|
+
search(query)
|
|
62
|
+
.then((results) => {
|
|
63
|
+
if (isCancelled) return;
|
|
64
|
+
// Anyone who already has a grant is not a candidate for a new one.
|
|
65
|
+
let granted = new Set(permissions.map((permission) => permission.groupRef));
|
|
66
|
+
setCandidates(results.filter((result) => !granted.has(result.primKey)));
|
|
67
|
+
})
|
|
68
|
+
.catch(() => {
|
|
69
|
+
if (!isCancelled) setCandidates([]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return () => {
|
|
73
|
+
isCancelled = true;
|
|
74
|
+
};
|
|
75
|
+
}, [open, query, search, permissions]);
|
|
76
|
+
|
|
77
|
+
function handleAdd(candidate: PermissionCandidate | null) {
|
|
78
|
+
if (!candidate) return;
|
|
79
|
+
|
|
80
|
+
setSearchText("");
|
|
81
|
+
void setPermission(candidate.primKey, "Reader");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Dialog open={open} closedby="any" data-size={size} onClose={onClose} className="ObFilterShareDialog-root">
|
|
86
|
+
<Dialog.Block>
|
|
87
|
+
<Heading level={2} data-size="xs">
|
|
88
|
+
{filterName ? getLocalizedString("Sharing - {0}").replace("{0}", filterName) : getLocalizedString("Sharing")}
|
|
89
|
+
</Heading>
|
|
90
|
+
</Dialog.Block>
|
|
91
|
+
<Dialog.Block>
|
|
92
|
+
<Combobox<PermissionCandidate>
|
|
93
|
+
options={candidates}
|
|
94
|
+
value={null}
|
|
95
|
+
inputValue={searchText}
|
|
96
|
+
loading={loading}
|
|
97
|
+
data-size={size}
|
|
98
|
+
label={getLocalizedString("Add user or group")}
|
|
99
|
+
placeholder={getLocalizedString("Search...")}
|
|
100
|
+
getOptionLabel={(option) => option.name}
|
|
101
|
+
getOptionKey={(option) => option.primKey}
|
|
102
|
+
// The procedure searches both name and login, so filtering again on
|
|
103
|
+
// the label would drop everything matched by login.
|
|
104
|
+
filterOptions={(options) => [...options]}
|
|
105
|
+
noOptionsText={query.length < 2 ? getLocalizedString("Type to search") : getLocalizedString("No matches")}
|
|
106
|
+
renderOption={(option) => (
|
|
107
|
+
<span className="ObFilterShareDialog-candidate">
|
|
108
|
+
{option.type === "User" ? <PersonIcon aria-hidden /> : <PersonGroupIcon aria-hidden />}
|
|
109
|
+
<span>{option.name}</span>
|
|
110
|
+
<span className="ObFilterShareDialog-candidateLogin">{option.login}</span>
|
|
111
|
+
</span>
|
|
112
|
+
)}
|
|
113
|
+
// The picker never keeps a selection; picking an option adds a grant.
|
|
114
|
+
onInputChange={(_event, value) => setSearchText(value)}
|
|
115
|
+
onChange={(_event, value) => handleAdd(value)}
|
|
116
|
+
/>
|
|
117
|
+
</Dialog.Block>
|
|
118
|
+
<Dialog.Block>
|
|
119
|
+
{error && <p className="ObFilterShareDialog-error">{String(error.message ?? error)}</p>}
|
|
120
|
+
<Table data-size={size} className="ObFilterShareDialog-table">
|
|
121
|
+
<Table.Head>
|
|
122
|
+
<Table.Row>
|
|
123
|
+
<Table.HeaderCell>{getLocalizedString("Name")}</Table.HeaderCell>
|
|
124
|
+
<Table.HeaderCell>{getLocalizedString("Login")}</Table.HeaderCell>
|
|
125
|
+
<Table.HeaderCell>{getLocalizedString("Access")}</Table.HeaderCell>
|
|
126
|
+
<Table.HeaderCell />
|
|
127
|
+
</Table.Row>
|
|
128
|
+
</Table.Head>
|
|
129
|
+
<Table.Body>
|
|
130
|
+
{permissions.length === 0 && (
|
|
131
|
+
<Table.Row>
|
|
132
|
+
<Table.Cell colSpan={4}>
|
|
133
|
+
{loading ? getLocalizedString("Loading...") : getLocalizedString("Not shared with anyone")}
|
|
134
|
+
</Table.Cell>
|
|
135
|
+
</Table.Row>
|
|
136
|
+
)}
|
|
137
|
+
{permissions.map((permission) => (
|
|
138
|
+
<PermissionRow
|
|
139
|
+
key={permission.primKey}
|
|
140
|
+
permission={permission}
|
|
141
|
+
size={size}
|
|
142
|
+
onChangeAccess={(accessLevel) => void setPermission(permission.groupRef, accessLevel)}
|
|
143
|
+
onRemove={() => void removePermission(permission.groupRef)}
|
|
144
|
+
/>
|
|
145
|
+
))}
|
|
146
|
+
</Table.Body>
|
|
147
|
+
</Table>
|
|
148
|
+
</Dialog.Block>
|
|
149
|
+
<Dialog.Block>
|
|
150
|
+
<Button type="button" variant="secondary" data-size={size} onClick={onClose}>
|
|
151
|
+
{getLocalizedString("Close")}
|
|
152
|
+
</Button>
|
|
153
|
+
</Dialog.Block>
|
|
154
|
+
</Dialog>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
type PermissionRowProps = {
|
|
159
|
+
permission: FilterPermission;
|
|
160
|
+
size: "sm" | "md" | "lg";
|
|
161
|
+
onChangeAccess: (accessLevel: "Reader" | "Manager") => void;
|
|
162
|
+
onRemove: () => void;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
function PermissionRow({ permission, size, onChangeAccess, onRemove }: PermissionRowProps) {
|
|
166
|
+
return (
|
|
167
|
+
<Table.Row>
|
|
168
|
+
<Table.Cell>
|
|
169
|
+
<span className="ObFilterShareDialog-candidate">
|
|
170
|
+
{permission.type === "User" ? <PersonIcon aria-hidden /> : <PersonGroupIcon aria-hidden />}
|
|
171
|
+
{permission.name}
|
|
172
|
+
</span>
|
|
173
|
+
</Table.Cell>
|
|
174
|
+
<Table.Cell>{permission.login}</Table.Cell>
|
|
175
|
+
<Table.Cell>
|
|
176
|
+
<Select
|
|
177
|
+
data-size={size}
|
|
178
|
+
value={permission.accessLevel}
|
|
179
|
+
aria-label={getLocalizedString("Access for {0}").replace("{0}", permission.name)}
|
|
180
|
+
onChange={(event) => onChangeAccess(event.currentTarget.value as "Reader" | "Manager")}
|
|
181
|
+
>
|
|
182
|
+
<Select.Option value="Reader">{getLocalizedString("Reader")}</Select.Option>
|
|
183
|
+
<Select.Option value="Manager">{getLocalizedString("Manager")}</Select.Option>
|
|
184
|
+
</Select>
|
|
185
|
+
</Table.Cell>
|
|
186
|
+
<Table.Cell>
|
|
187
|
+
<Button
|
|
188
|
+
type="button"
|
|
189
|
+
variant="tertiary"
|
|
190
|
+
data-color="danger"
|
|
191
|
+
data-size={size}
|
|
192
|
+
icon
|
|
193
|
+
aria-label={getLocalizedString("Remove access for {0}").replace("{0}", permission.name)}
|
|
194
|
+
onClick={onRemove}
|
|
195
|
+
>
|
|
196
|
+
<TrashIcon aria-hidden />
|
|
197
|
+
</Button>
|
|
198
|
+
</Table.Cell>
|
|
199
|
+
</Table.Row>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Textfield, ValidationMessage } from "@digdir/designsystemet-react";
|
|
2
|
+
import { type FilterTree, getLocalizedString, toFilterTree } from "@olenbetong/appframe-core";
|
|
3
|
+
import { useFilterConversion } from "@olenbetong/appframe-react";
|
|
4
|
+
import { useEffect, useState } from "react";
|
|
5
|
+
|
|
6
|
+
import type { FilterEditorSize } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export type FilterStringFieldProps = {
|
|
9
|
+
filter: FilterTree;
|
|
10
|
+
onChange: (filter: FilterTree) => void;
|
|
11
|
+
viewName?: string;
|
|
12
|
+
readOnly?: boolean;
|
|
13
|
+
"data-size"?: FilterEditorSize;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Two-way filter string editor. The tree is the source of truth: it is
|
|
18
|
+
* converted to a string whenever it changes. Editing the string and leaving the
|
|
19
|
+
* field parses it back into a tree.
|
|
20
|
+
*/
|
|
21
|
+
export function FilterStringField({
|
|
22
|
+
filter,
|
|
23
|
+
onChange,
|
|
24
|
+
viewName,
|
|
25
|
+
readOnly,
|
|
26
|
+
"data-size": size = "sm",
|
|
27
|
+
}: FilterStringFieldProps) {
|
|
28
|
+
let { filterString, isConverting, error, warning, parseFilterString } = useFilterConversion(filter, { viewName });
|
|
29
|
+
let [draft, setDraft] = useState<string | null>(null);
|
|
30
|
+
let [parseError, setParseError] = useState<string | null>(null);
|
|
31
|
+
|
|
32
|
+
// The tree changed underneath us, so any half-typed string is stale.
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setDraft(null);
|
|
35
|
+
setParseError(null);
|
|
36
|
+
}, [filter]);
|
|
37
|
+
|
|
38
|
+
async function commit() {
|
|
39
|
+
if (draft === null || draft === filterString) {
|
|
40
|
+
setDraft(null);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let parsed = await parseFilterString(draft);
|
|
45
|
+
|
|
46
|
+
if (parsed) {
|
|
47
|
+
setDraft(null);
|
|
48
|
+
setParseError(null);
|
|
49
|
+
onChange(toFilterTree(parsed));
|
|
50
|
+
} else {
|
|
51
|
+
setParseError(getLocalizedString("The filter could not be parsed"));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let message = parseError ?? error?.message ?? warning;
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div className="ObFilterEditor-filterString">
|
|
59
|
+
<Textfield
|
|
60
|
+
multiline
|
|
61
|
+
rows={3}
|
|
62
|
+
data-size={size}
|
|
63
|
+
readOnly={readOnly}
|
|
64
|
+
label={
|
|
65
|
+
<span className="ObFilterEditor-filterStringLabel">
|
|
66
|
+
{getLocalizedString("Filter string")}
|
|
67
|
+
{isConverting && <span className="ObFilterEditor-hint">{getLocalizedString("Converting…")}</span>}
|
|
68
|
+
</span>
|
|
69
|
+
}
|
|
70
|
+
value={draft ?? filterString}
|
|
71
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
72
|
+
onBlur={commit}
|
|
73
|
+
/>
|
|
74
|
+
{message && <ValidationMessage data-color={parseError ? "danger" : "warning"}>{message}</ValidationMessage>}
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { Select, Textfield } from "@digdir/designsystemet-react";
|
|
2
|
+
import { PencilIcon } from "@navikt/aksel-icons";
|
|
3
|
+
import {
|
|
4
|
+
type DynamicValue,
|
|
5
|
+
type FieldMetadata,
|
|
6
|
+
type FilterExpression,
|
|
7
|
+
type FilterOperator,
|
|
8
|
+
getDynamicValueLabel,
|
|
9
|
+
getDynamicValuesForField,
|
|
10
|
+
getLocalizedString,
|
|
11
|
+
getOperatorArity,
|
|
12
|
+
getValueType,
|
|
13
|
+
normalizeOperator,
|
|
14
|
+
} from "@olenbetong/appframe-core";
|
|
15
|
+
import { Combobox } from "../autocomplete/index.js";
|
|
16
|
+
|
|
17
|
+
import { ChipListInput } from "./ChipListInput.js";
|
|
18
|
+
import type { FilterEditorSize, FilterValueOption } from "./types.js";
|
|
19
|
+
|
|
20
|
+
export type FilterValueEditorProps = {
|
|
21
|
+
expression: FilterExpression;
|
|
22
|
+
field?: FieldMetadata;
|
|
23
|
+
options?: FilterValueOption[];
|
|
24
|
+
onChange: (expression: FilterExpression) => void;
|
|
25
|
+
readOnly?: boolean;
|
|
26
|
+
"data-size"?: FilterEditorSize;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The `input` type used for a field's scalar editor. */
|
|
30
|
+
function getInputType(field: FieldMetadata | undefined): "text" | "number" | "date" | "datetime-local" {
|
|
31
|
+
switch (field?.type) {
|
|
32
|
+
case "number":
|
|
33
|
+
return "number";
|
|
34
|
+
case "date":
|
|
35
|
+
return "date";
|
|
36
|
+
case "datetime":
|
|
37
|
+
return "datetime-local";
|
|
38
|
+
default:
|
|
39
|
+
return "text";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function toStringValue(value: FilterExpression["value"]): string {
|
|
44
|
+
if (value === null || value === undefined) return "";
|
|
45
|
+
if (value instanceof Date) return value.toISOString();
|
|
46
|
+
|
|
47
|
+
return String(value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function toArrayValue(value: FilterExpression["value"], length?: number): string[] {
|
|
51
|
+
let array = Array.isArray(value) ? value.map(toStringValue) : [];
|
|
52
|
+
if (length === undefined) return array;
|
|
53
|
+
|
|
54
|
+
// Pair editors always render two inputs, even before both are filled in.
|
|
55
|
+
return Array.from({ length }, (_, index) => array[index] ?? "");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The value part of a filter row. Which editor is rendered depends on the
|
|
60
|
+
* operator's arity, the field's type, and whether the value is a dynamic
|
|
61
|
+
* ("special") value such as `Today` or `CurrentUser`.
|
|
62
|
+
*/
|
|
63
|
+
export function FilterValueEditor({
|
|
64
|
+
expression,
|
|
65
|
+
field,
|
|
66
|
+
options,
|
|
67
|
+
onChange,
|
|
68
|
+
readOnly,
|
|
69
|
+
"data-size": size = "sm",
|
|
70
|
+
}: FilterValueEditorProps) {
|
|
71
|
+
let operator = normalizeOperator(expression.operator);
|
|
72
|
+
let arity = getOperatorArity(operator);
|
|
73
|
+
let isDynamic = expression.valueType === "special";
|
|
74
|
+
let dynamicValues = field ? getDynamicValuesForField(field) : [];
|
|
75
|
+
let inputType = getInputType(field);
|
|
76
|
+
|
|
77
|
+
function setValue(value: FilterExpression["value"]) {
|
|
78
|
+
onChange({ ...expression, value });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function setDynamic(enabled: boolean) {
|
|
82
|
+
if (enabled) {
|
|
83
|
+
onChange({ ...expression, value: dynamicValues[0] ?? null, valueType: "special" });
|
|
84
|
+
} else {
|
|
85
|
+
onChange({
|
|
86
|
+
...expression,
|
|
87
|
+
value: arity === "pair" ? ["", ""] : arity === "list" ? [] : "",
|
|
88
|
+
valueType: field ? getValueType(field, operator as FilterOperator) : "string",
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (arity === "none") return null;
|
|
94
|
+
|
|
95
|
+
let editor: React.ReactNode;
|
|
96
|
+
|
|
97
|
+
if (isDynamic) {
|
|
98
|
+
editor = (
|
|
99
|
+
<Select
|
|
100
|
+
className="ObFilterEditor-value"
|
|
101
|
+
aria-label={getLocalizedString("Value")}
|
|
102
|
+
data-size={size}
|
|
103
|
+
disabled={readOnly}
|
|
104
|
+
value={toStringValue(expression.value)}
|
|
105
|
+
onChange={(event) => setValue(event.target.value)}
|
|
106
|
+
>
|
|
107
|
+
{dynamicValues.map((dynamicValue) => (
|
|
108
|
+
<Select.Option key={dynamicValue} value={dynamicValue}>
|
|
109
|
+
{getDynamicValueLabel(dynamicValue as DynamicValue)}
|
|
110
|
+
</Select.Option>
|
|
111
|
+
))}
|
|
112
|
+
</Select>
|
|
113
|
+
);
|
|
114
|
+
} else if (arity === "list") {
|
|
115
|
+
editor = options ? (
|
|
116
|
+
<Combobox
|
|
117
|
+
className="ObFilterEditor-value"
|
|
118
|
+
multiple
|
|
119
|
+
data-size={size}
|
|
120
|
+
readOnly={readOnly}
|
|
121
|
+
placeholder={getLocalizedString("Select values")}
|
|
122
|
+
aria-label={getLocalizedString("Value")}
|
|
123
|
+
options={options}
|
|
124
|
+
getOptionLabel={(option) => option.label ?? option.value}
|
|
125
|
+
isOptionEqualToValue={(a, b) => a.value === b.value}
|
|
126
|
+
value={toArrayValue(expression.value).map(
|
|
127
|
+
(item) => options.find((option) => option.value === item) ?? { value: item },
|
|
128
|
+
)}
|
|
129
|
+
onChange={(_event, selected) => setValue(selected.map((option) => option.value))}
|
|
130
|
+
/>
|
|
131
|
+
) : (
|
|
132
|
+
<ChipListInput
|
|
133
|
+
aria-label={getLocalizedString("Value")}
|
|
134
|
+
data-size={size}
|
|
135
|
+
readOnly={readOnly}
|
|
136
|
+
type={inputType}
|
|
137
|
+
value={toArrayValue(expression.value)}
|
|
138
|
+
onChange={setValue}
|
|
139
|
+
/>
|
|
140
|
+
);
|
|
141
|
+
} else if (arity === "pair") {
|
|
142
|
+
let [from, to] = toArrayValue(expression.value, 2);
|
|
143
|
+
|
|
144
|
+
editor = (
|
|
145
|
+
<div className="ObFilterEditor-pair">
|
|
146
|
+
<Textfield
|
|
147
|
+
className="ObFilterEditor-value"
|
|
148
|
+
aria-label={getLocalizedString("From")}
|
|
149
|
+
data-size={size}
|
|
150
|
+
readOnly={readOnly}
|
|
151
|
+
type={inputType}
|
|
152
|
+
value={from}
|
|
153
|
+
onChange={(event) => setValue([event.target.value, to])}
|
|
154
|
+
/>
|
|
155
|
+
<span className="ObFilterEditor-pairSeparator">{getLocalizedString("and")}</span>
|
|
156
|
+
<Textfield
|
|
157
|
+
className="ObFilterEditor-value"
|
|
158
|
+
aria-label={getLocalizedString("To")}
|
|
159
|
+
data-size={size}
|
|
160
|
+
readOnly={readOnly}
|
|
161
|
+
type={inputType}
|
|
162
|
+
value={to}
|
|
163
|
+
onChange={(event) => setValue([from, event.target.value])}
|
|
164
|
+
/>
|
|
165
|
+
</div>
|
|
166
|
+
);
|
|
167
|
+
} else if (options) {
|
|
168
|
+
let current = toStringValue(expression.value);
|
|
169
|
+
|
|
170
|
+
editor = (
|
|
171
|
+
<Combobox
|
|
172
|
+
className="ObFilterEditor-value"
|
|
173
|
+
data-size={size}
|
|
174
|
+
readOnly={readOnly}
|
|
175
|
+
placeholder={getLocalizedString("Select value")}
|
|
176
|
+
aria-label={getLocalizedString("Value")}
|
|
177
|
+
options={options}
|
|
178
|
+
getOptionLabel={(option) => option.label ?? option.value}
|
|
179
|
+
isOptionEqualToValue={(a, b) => a.value === b.value}
|
|
180
|
+
value={current ? (options.find((option) => option.value === current) ?? { value: current }) : null}
|
|
181
|
+
onChange={(_event, selected) => setValue(selected?.value ?? "")}
|
|
182
|
+
/>
|
|
183
|
+
);
|
|
184
|
+
} else {
|
|
185
|
+
editor = (
|
|
186
|
+
<Textfield
|
|
187
|
+
className="ObFilterEditor-value"
|
|
188
|
+
aria-label={getLocalizedString("Value")}
|
|
189
|
+
data-size={size}
|
|
190
|
+
readOnly={readOnly}
|
|
191
|
+
type={inputType}
|
|
192
|
+
value={toStringValue(expression.value)}
|
|
193
|
+
onChange={(event) => setValue(event.target.value)}
|
|
194
|
+
/>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Dynamic values are resolved server side, so they can only replace the whole
|
|
199
|
+
// value. Pairs would need one toggle per side, which the Windows editor does
|
|
200
|
+
// not offer either.
|
|
201
|
+
let canToggleDynamic = !readOnly && dynamicValues.length > 0 && arity === "single";
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<div className="ObFilterEditor-valueCell">
|
|
205
|
+
{editor}
|
|
206
|
+
{canToggleDynamic && (
|
|
207
|
+
<button
|
|
208
|
+
type="button"
|
|
209
|
+
className="ObFilterEditor-iconButton"
|
|
210
|
+
aria-pressed={isDynamic}
|
|
211
|
+
title={getLocalizedString("Use a relative value")}
|
|
212
|
+
aria-label={getLocalizedString("Use a relative value")}
|
|
213
|
+
onClick={() => setDynamic(!isDynamic)}
|
|
214
|
+
>
|
|
215
|
+
<PencilIcon aria-hidden />
|
|
216
|
+
</button>
|
|
217
|
+
)}
|
|
218
|
+
</div>
|
|
219
|
+
);
|
|
220
|
+
}
|