@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
|
@@ -4,7 +4,7 @@ import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
|
|
|
4
4
|
import { ChipRemovable } from "@digdir/designsystemet-react";
|
|
5
5
|
import { ChevronDownIcon } from "@navikt/aksel-icons";
|
|
6
6
|
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
7
|
-
import { useId, useMemo } from "react";
|
|
7
|
+
import { useEffect, useId, useMemo, useRef } from "react";
|
|
8
8
|
|
|
9
9
|
import { AutocompleteFrame } from "./AutocompleteFrame.js";
|
|
10
10
|
import type { AutocompleteBaseProps, AutocompleteChangeReason, AutocompleteValue } from "./types.js";
|
|
@@ -107,6 +107,7 @@ export function Combobox<Option, Multiple extends boolean | undefined = false>(p
|
|
|
107
107
|
disableClearable,
|
|
108
108
|
placeholder,
|
|
109
109
|
inputRef,
|
|
110
|
+
"aria-label": ariaLabel,
|
|
110
111
|
"data-size": size,
|
|
111
112
|
"data-color": color,
|
|
112
113
|
} = props;
|
|
@@ -116,7 +117,18 @@ export function Combobox<Option, Multiple extends boolean | undefined = false>(p
|
|
|
116
117
|
|
|
117
118
|
let emptyValue = (multiple ? [] : null) as AutocompleteValue<Option, Multiple>;
|
|
118
119
|
let [value, setValue] = useControlled(valueProp, defaultValue ?? emptyValue);
|
|
119
|
-
|
|
120
|
+
// The input shows the selected option's label. Base UI keeps it in sync while
|
|
121
|
+
// the user interacts, but not when the selection is set from the outside.
|
|
122
|
+
let selectedLabel = multiple || value == null ? "" : getOptionLabel(value as Option);
|
|
123
|
+
let [inputValue, setInputValue] = useControlled(inputValueProp, selectedLabel);
|
|
124
|
+
let previousLabel = useRef(selectedLabel);
|
|
125
|
+
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (previousLabel.current === selectedLabel) return;
|
|
128
|
+
|
|
129
|
+
previousLabel.current = selectedLabel;
|
|
130
|
+
setInputValue(selectedLabel);
|
|
131
|
+
}, [selectedLabel, setInputValue]);
|
|
120
132
|
|
|
121
133
|
let filteredOptions = useMemo(() => {
|
|
122
134
|
let filter = (filterOptions ?? defaultFilterOptions) as (
|
|
@@ -240,6 +252,7 @@ export function Combobox<Option, Multiple extends boolean | undefined = false>(p
|
|
|
240
252
|
loading={loading}
|
|
241
253
|
disableClearable={disableClearable}
|
|
242
254
|
inputRef={inputRef}
|
|
255
|
+
aria-label={ariaLabel}
|
|
243
256
|
chips={chips}
|
|
244
257
|
trigger={
|
|
245
258
|
<BaseCombobox.Trigger
|
|
@@ -195,6 +195,10 @@ export interface AutocompleteBaseProps<Option, Multiple extends boolean | undefi
|
|
|
195
195
|
*/
|
|
196
196
|
popupClassName?: string;
|
|
197
197
|
inputRef?: Ref<HTMLInputElement>;
|
|
198
|
+
/**
|
|
199
|
+
* Accessible name for the input. Use when the field has no visible label.
|
|
200
|
+
*/
|
|
201
|
+
"aria-label"?: string;
|
|
198
202
|
"data-size"?: AutocompleteSize;
|
|
199
203
|
"data-color"?: string;
|
|
200
204
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PageBlock, type PageBlockProps, type PageBlockWidth } from "../page/PageBlock.js";
|
|
2
|
+
|
|
3
|
+
export const containerMaxWidths = ["xs", "sm", "md", "lg", "xl"] as const;
|
|
4
|
+
|
|
5
|
+
export type ContainerMaxWidth = (typeof containerMaxWidths)[number];
|
|
6
|
+
|
|
7
|
+
const maxWidthToBlockWidth: Record<ContainerMaxWidth, PageBlockWidth> = {
|
|
8
|
+
xs: "text",
|
|
9
|
+
sm: "text",
|
|
10
|
+
md: "md",
|
|
11
|
+
lg: "lg",
|
|
12
|
+
xl: "xl",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ContainerProps = Omit<PageBlockProps, "width"> & {
|
|
16
|
+
/**
|
|
17
|
+
* Predefined max-width, using the same scale as MUI `Container`. Content is
|
|
18
|
+
* horizontally centered.
|
|
19
|
+
*
|
|
20
|
+
* - `xs`/`sm`: 36rem (576px) — recommended line-length for running text
|
|
21
|
+
* - `md`: 48rem (768px)
|
|
22
|
+
* - `lg`: 64rem (1024px)
|
|
23
|
+
* - `xl`: 80rem (1280px)
|
|
24
|
+
* - `false`: no max-width
|
|
25
|
+
*
|
|
26
|
+
* @default "lg"
|
|
27
|
+
*/
|
|
28
|
+
maxWidth?: ContainerMaxWidth | false;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Centered, max-width content wrapper — the MUI `Container` equivalent.
|
|
33
|
+
*
|
|
34
|
+
* A thin alias over `Page.Block` with gutters enabled by default, for the very
|
|
35
|
+
* common case of "center this content and give it sensible side padding"
|
|
36
|
+
* without having to reach for the full `Page` composition.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```jsx
|
|
40
|
+
* <Container maxWidth="sm">
|
|
41
|
+
* <Heading>Expenses</Heading>
|
|
42
|
+
* </Container>
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function Container({ gutters = true, maxWidth = "lg", ...props }: ContainerProps) {
|
|
46
|
+
let width = maxWidth === false ? undefined : maxWidthToBlockWidth[maxWidth];
|
|
47
|
+
|
|
48
|
+
return <PageBlock gutters={gutters} width={width} {...props} />;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Container.js";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ChipRemovable, Textfield } from "@digdir/designsystemet-react";
|
|
2
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
export type ChipListInputProps = {
|
|
6
|
+
value: string[];
|
|
7
|
+
onChange: (value: string[]) => void;
|
|
8
|
+
readOnly?: boolean;
|
|
9
|
+
"aria-label": string;
|
|
10
|
+
"data-size"?: "sm" | "md" | "lg";
|
|
11
|
+
type?: "text" | "number" | "date" | "datetime-local";
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Free text editor for the list operators. Values are added with Enter and
|
|
16
|
+
* rendered as removable chips.
|
|
17
|
+
*/
|
|
18
|
+
export function ChipListInput({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
readOnly,
|
|
22
|
+
type = "text",
|
|
23
|
+
"aria-label": ariaLabel,
|
|
24
|
+
"data-size": size,
|
|
25
|
+
}: ChipListInputProps) {
|
|
26
|
+
let [draft, setDraft] = useState("");
|
|
27
|
+
|
|
28
|
+
function commit() {
|
|
29
|
+
let trimmed = draft.trim();
|
|
30
|
+
if (!trimmed || value.includes(trimmed)) {
|
|
31
|
+
setDraft("");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
onChange([...value, trimmed]);
|
|
36
|
+
setDraft("");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
|
|
40
|
+
if (event.key === "Enter") {
|
|
41
|
+
// The editor is often rendered inside a dialog; committing a value
|
|
42
|
+
// must not submit or close it.
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
commit();
|
|
45
|
+
} else if (event.key === "Backspace" && draft === "" && value.length > 0) {
|
|
46
|
+
onChange(value.slice(0, -1));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div className="ObFilterEditor-chipList">
|
|
52
|
+
{value.map((item, index) => (
|
|
53
|
+
<ChipRemovable
|
|
54
|
+
// Values are deduplicated on add, so the value is a stable key.
|
|
55
|
+
key={item}
|
|
56
|
+
data-size={size}
|
|
57
|
+
disabled={readOnly}
|
|
58
|
+
aria-label={`${getLocalizedString("Remove")} ${item}`}
|
|
59
|
+
onClick={() => onChange(value.filter((_, i) => i !== index))}
|
|
60
|
+
>
|
|
61
|
+
{item}
|
|
62
|
+
</ChipRemovable>
|
|
63
|
+
))}
|
|
64
|
+
{!readOnly && (
|
|
65
|
+
<Textfield
|
|
66
|
+
className="ObFilterEditor-chipListInput"
|
|
67
|
+
aria-label={ariaLabel}
|
|
68
|
+
data-size={size}
|
|
69
|
+
type={type}
|
|
70
|
+
value={draft}
|
|
71
|
+
placeholder={getLocalizedString("Add value")}
|
|
72
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
73
|
+
onKeyDown={handleKeyDown}
|
|
74
|
+
onBlur={commit}
|
|
75
|
+
/>
|
|
76
|
+
)}
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
.ObFieldFilterPanel-root {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
gap: var(--ds-size-3, 0.75rem);
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The panel is sized by whatever it is put in — a popover, a dialog, a
|
|
8
|
+
* sidebar. It fills that container and lets the distinct list take up the
|
|
9
|
+
* slack, so the list grows and shrinks with the container instead of
|
|
10
|
+
* carrying a height of its own.
|
|
11
|
+
*/
|
|
12
|
+
block-size: 100%;
|
|
13
|
+
min-block-size: 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.ObFieldFilterPanel-heading {
|
|
17
|
+
font-weight: var(--ds-font-weight-medium, 500);
|
|
18
|
+
color: var(--ds-color-neutral-text-default);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.ObFieldFilterPanel-criteria {
|
|
22
|
+
display: flex;
|
|
23
|
+
flex-direction: column;
|
|
24
|
+
flex: 0 0 auto;
|
|
25
|
+
gap: var(--ds-size-2, 0.5rem);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.ObFieldFilterPanel-criterion {
|
|
29
|
+
display: flex;
|
|
30
|
+
flex-direction: column;
|
|
31
|
+
gap: var(--ds-size-1, 0.25rem);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* The filter editor's value cell is written for the filter builder's horizontal
|
|
36
|
+
* rows, where growing is what makes it fill the row. Stacked vertically that
|
|
37
|
+
* same `flex-grow` makes it stretch to fill the whole panel instead.
|
|
38
|
+
*/
|
|
39
|
+
.ObFieldFilterPanel-criterion > .ObFilterEditor-valueCell {
|
|
40
|
+
flex: 0 0 auto;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.ObFieldFilterPanel-distinct {
|
|
44
|
+
display: flex;
|
|
45
|
+
flex-direction: column;
|
|
46
|
+
flex: 1 1 auto;
|
|
47
|
+
gap: var(--ds-size-2, 0.5rem);
|
|
48
|
+
min-block-size: 0;
|
|
49
|
+
padding-block-start: var(--ds-size-2, 0.5rem);
|
|
50
|
+
border-block-start: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-subtle, #e7e7e7);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.ObFieldFilterPanel-distinctToolbar {
|
|
54
|
+
display: flex;
|
|
55
|
+
flex: 0 0 auto;
|
|
56
|
+
gap: var(--ds-size-1, 0.25rem);
|
|
57
|
+
align-items: center;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.ObFieldFilterPanel-distinctToolbar .ds-search {
|
|
61
|
+
flex: 1;
|
|
62
|
+
min-inline-size: 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* The active sort is held down, so it is clear which of the two is in effect.
|
|
67
|
+
* Neutral tokens rather than accent ones: the accent scale is only defined
|
|
68
|
+
* inside a `data-color` scope, and the panel can be dropped anywhere.
|
|
69
|
+
*/
|
|
70
|
+
.ObFieldFilterPanel-distinctToolbar .ds-button[aria-pressed="true"] {
|
|
71
|
+
color: var(--ds-color-neutral-text-default, #292c30);
|
|
72
|
+
background: var(--ds-color-neutral-surface-active, #c9cacb);
|
|
73
|
+
box-shadow: inset 0 0 0 var(--ds-border-width-default, 1px) var(--ds-color-neutral-border-subtle, #e7e7e7);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Both the list and the spinner that replaces it while loading use this box, so
|
|
78
|
+
* the panel does not jump when the values arrive. It is the part that takes up
|
|
79
|
+
* whatever room the container has left, and it never collapses to nothing.
|
|
80
|
+
*/
|
|
81
|
+
.ObFieldFilterPanel-valuesBox {
|
|
82
|
+
flex: 1 1 auto;
|
|
83
|
+
min-block-size: 8rem;
|
|
84
|
+
border: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-subtle, #e7e7e7);
|
|
85
|
+
border-radius: var(--ds-border-radius-md, 0.25rem);
|
|
86
|
+
background: var(--ds-color-neutral-surface-tinted, rgb(0 0 0 / 2%));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.ObFieldFilterPanel-values {
|
|
90
|
+
display: flex;
|
|
91
|
+
flex-direction: column;
|
|
92
|
+
gap: var(--ds-size-1, 0.25rem);
|
|
93
|
+
margin: 0;
|
|
94
|
+
padding: var(--ds-size-1, 0.25rem);
|
|
95
|
+
overflow-y: auto;
|
|
96
|
+
list-style: none;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.ObFieldFilterPanel-value {
|
|
100
|
+
padding-inline: var(--ds-size-1, 0.25rem);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.ObFieldFilterPanel-loading {
|
|
104
|
+
display: flex;
|
|
105
|
+
align-items: center;
|
|
106
|
+
justify-content: center;
|
|
107
|
+
padding: var(--ds-size-4, 1rem);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.ObFieldFilterPanel-error {
|
|
111
|
+
margin: 0;
|
|
112
|
+
color: var(--ds-color-danger-text-default);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
.ObFieldFilterPanel-hint {
|
|
116
|
+
margin: 0;
|
|
117
|
+
flex: 0 0 auto;
|
|
118
|
+
font-size: var(--ds-font-size-1, 0.75rem);
|
|
119
|
+
color: var(--ds-color-neutral-text-subtle, #68707c);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.ObFieldFilterPanel-actions {
|
|
123
|
+
display: flex;
|
|
124
|
+
flex: 0 0 auto;
|
|
125
|
+
gap: var(--ds-size-2, 0.5rem);
|
|
126
|
+
justify-content: flex-end;
|
|
127
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import "./FieldFilterPanel.css";
|
|
2
|
+
|
|
3
|
+
import { Button, Checkbox, Search, Spinner } from "@digdir/designsystemet-react";
|
|
4
|
+
import { BarChartIcon, PlusIcon, SortUpIcon } from "@navikt/aksel-icons";
|
|
5
|
+
import {
|
|
6
|
+
changeOperator,
|
|
7
|
+
createExpression,
|
|
8
|
+
type FieldMetadata,
|
|
9
|
+
type FilterExpression,
|
|
10
|
+
type FilterOperator,
|
|
11
|
+
type FilterTree,
|
|
12
|
+
getLocalizedString,
|
|
13
|
+
getOperatorLabel,
|
|
14
|
+
getOperatorsForField,
|
|
15
|
+
normalizeOperator,
|
|
16
|
+
} from "@olenbetong/appframe-core";
|
|
17
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
18
|
+
import clsx from "clsx";
|
|
19
|
+
import { type ReactNode, useMemo, useState } from "react";
|
|
20
|
+
|
|
21
|
+
import { getInListValues, setInListValues } from "./fieldFilter.js";
|
|
22
|
+
import { FilterValueEditor } from "./FilterValueEditor.js";
|
|
23
|
+
import { supportsDistinctValues, useDistinctValues } from "./useDistinctValues.js";
|
|
24
|
+
|
|
25
|
+
export type FieldFilterPanelProps = {
|
|
26
|
+
/** The field being filtered. Decides the operators and the value editor. */
|
|
27
|
+
field: FieldMetadata;
|
|
28
|
+
/** Data object the distinct value list is loaded from. */
|
|
29
|
+
dataObject: DataObject<any>;
|
|
30
|
+
/** The field's criteria. Controlled — the panel never stages edits itself. */
|
|
31
|
+
value: FilterExpression[];
|
|
32
|
+
onChange: (expressions: FilterExpression[]) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Heading above the criteria. Defaults to the field's caption; pass `null`
|
|
35
|
+
* to leave it out, e.g. when the surrounding dialog already has a title.
|
|
36
|
+
*/
|
|
37
|
+
heading?: ReactNode;
|
|
38
|
+
/**
|
|
39
|
+
* The filter the field lives in, used to scope the distinct value list to
|
|
40
|
+
* the rows the other criteria leave. The field's own criteria are ignored,
|
|
41
|
+
* so unchecking a value does not make the remaining values disappear.
|
|
42
|
+
*/
|
|
43
|
+
filter?: FilterTree | null;
|
|
44
|
+
/** Set to false to leave out the distinct value list. */
|
|
45
|
+
showDistinctValues?: boolean;
|
|
46
|
+
/** Rendered at the bottom, for actions such as Clear and Apply. */
|
|
47
|
+
children?: ReactNode;
|
|
48
|
+
className?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type DistinctSort = "value" | "count";
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Filter criteria for a single field: a row per criterion, a button to add
|
|
55
|
+
* another, and a checkable list of the field's distinct values.
|
|
56
|
+
*
|
|
57
|
+
* The list of values is loaded straight from the data object's data handler,
|
|
58
|
+
* so the data object's own data and parameters are untouched. It is only shown
|
|
59
|
+
* for the types where it makes sense (string, number, uniqueidentifier) and
|
|
60
|
+
* only ever loads a bounded number of values — it is a picker, not a report.
|
|
61
|
+
*
|
|
62
|
+
* The checkboxes own the field's single `inlist` criterion, which is why that
|
|
63
|
+
* criterion is not also shown as a criterion row.
|
|
64
|
+
*
|
|
65
|
+
* The component is fully controlled. To let the user cancel, keep a draft in
|
|
66
|
+
* the caller and only commit it on apply — that is what `AfGrid`'s header
|
|
67
|
+
* filter panel does.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```jsx
|
|
71
|
+
* let [criteria, setCriteria] = useState([]);
|
|
72
|
+
*
|
|
73
|
+
* <FieldFilterPanel field={field} dataObject={dsProjects} value={criteria} onChange={setCriteria}>
|
|
74
|
+
* <Button onClick={() => setCriteria([])}>Clear</Button>
|
|
75
|
+
* </FieldFilterPanel>
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function FieldFilterPanel({
|
|
79
|
+
field,
|
|
80
|
+
dataObject,
|
|
81
|
+
value,
|
|
82
|
+
onChange,
|
|
83
|
+
heading,
|
|
84
|
+
filter = null,
|
|
85
|
+
showDistinctValues = true,
|
|
86
|
+
children,
|
|
87
|
+
className,
|
|
88
|
+
}: FieldFilterPanelProps) {
|
|
89
|
+
let [search, setSearch] = useState("");
|
|
90
|
+
let [sort, setSort] = useState<DistinctSort>("value");
|
|
91
|
+
|
|
92
|
+
let showDistinct = showDistinctValues && supportsDistinctValues(field);
|
|
93
|
+
let { values, loading, error, truncated } = useDistinctValues(dataObject, field, {
|
|
94
|
+
filter,
|
|
95
|
+
search: search.trim() || undefined,
|
|
96
|
+
enabled: showDistinct,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
let operators = useMemo(() => getOperatorsForField(field), [field]);
|
|
100
|
+
let criteria = value.filter((expression) => normalizeOperator(expression.operator) !== "inlist");
|
|
101
|
+
let selected = useMemo(() => new Set(getInListValues(value)), [value]);
|
|
102
|
+
|
|
103
|
+
let sortedValues = useMemo(() => {
|
|
104
|
+
let list = [...values];
|
|
105
|
+
|
|
106
|
+
if (sort === "count") {
|
|
107
|
+
list.sort((a, b) => (b.count ?? 0) - (a.count ?? 0) || a.value.localeCompare(b.value));
|
|
108
|
+
} else {
|
|
109
|
+
list.sort((a, b) => a.value.localeCompare(b.value));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return list;
|
|
113
|
+
}, [values, sort]);
|
|
114
|
+
|
|
115
|
+
let allSelected = sortedValues.length > 0 && sortedValues.every((item) => selected.has(item.value));
|
|
116
|
+
|
|
117
|
+
function setCriteria(next: FilterExpression[]) {
|
|
118
|
+
onChange([...next, ...value.filter((expression) => normalizeOperator(expression.operator) === "inlist")]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function updateCriterion(index: number, expression: FilterExpression | null) {
|
|
122
|
+
let next = [...criteria];
|
|
123
|
+
|
|
124
|
+
if (expression === null) {
|
|
125
|
+
next.splice(index, 1);
|
|
126
|
+
} else {
|
|
127
|
+
next[index] = expression;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
setCriteria(next);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function toggleValues(changed: string[], checked: boolean) {
|
|
134
|
+
let next = new Set(selected);
|
|
135
|
+
|
|
136
|
+
for (let item of changed) {
|
|
137
|
+
if (checked) {
|
|
138
|
+
next.add(item);
|
|
139
|
+
} else {
|
|
140
|
+
next.delete(item);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
onChange(setInListValues(value, [...next], field));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let headingContent = heading === undefined ? (field.caption ?? field.name) : heading;
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div className={clsx("ObFieldFilterPanel-root", className)}>
|
|
151
|
+
{headingContent != null && <div className="ObFieldFilterPanel-heading">{headingContent}</div>}
|
|
152
|
+
|
|
153
|
+
<div className="ObFieldFilterPanel-criteria">
|
|
154
|
+
{criteria.map((expression, index) => (
|
|
155
|
+
// Criteria have no stable identity and cannot be reordered here,
|
|
156
|
+
// so the index is a safe key.
|
|
157
|
+
// oxlint-disable-next-line no-array-index-key -- see above
|
|
158
|
+
<div className="ObFieldFilterPanel-criterion" key={index}>
|
|
159
|
+
<Field
|
|
160
|
+
expression={expression}
|
|
161
|
+
field={field}
|
|
162
|
+
operators={operators}
|
|
163
|
+
onChange={(next) => updateCriterion(index, next)}
|
|
164
|
+
onRemove={criteria.length > 1 ? () => updateCriterion(index, null) : undefined}
|
|
165
|
+
/>
|
|
166
|
+
</div>
|
|
167
|
+
))}
|
|
168
|
+
<Button
|
|
169
|
+
type="button"
|
|
170
|
+
variant="tertiary"
|
|
171
|
+
data-size="sm"
|
|
172
|
+
className="ObFieldFilterPanel-addCriterion"
|
|
173
|
+
onClick={() => setCriteria([...criteria, createExpression(field)])}
|
|
174
|
+
>
|
|
175
|
+
<PlusIcon aria-hidden />
|
|
176
|
+
{getLocalizedString("Criteria")}
|
|
177
|
+
</Button>
|
|
178
|
+
</div>
|
|
179
|
+
|
|
180
|
+
{showDistinct && (
|
|
181
|
+
<div className="ObFieldFilterPanel-distinct">
|
|
182
|
+
<div className="ObFieldFilterPanel-distinctToolbar">
|
|
183
|
+
<Search data-size="sm">
|
|
184
|
+
<Search.Input
|
|
185
|
+
aria-label={getLocalizedString("Search in distinct list")}
|
|
186
|
+
placeholder={getLocalizedString("Search in distinct list")}
|
|
187
|
+
value={search}
|
|
188
|
+
onChange={(event) => setSearch(event.currentTarget.value)}
|
|
189
|
+
/>
|
|
190
|
+
<Search.Clear onClick={() => setSearch("")} />
|
|
191
|
+
</Search>
|
|
192
|
+
<Button
|
|
193
|
+
type="button"
|
|
194
|
+
icon
|
|
195
|
+
variant="tertiary"
|
|
196
|
+
data-size="sm"
|
|
197
|
+
aria-pressed={sort === "value"}
|
|
198
|
+
aria-label={getLocalizedString("Sort by value")}
|
|
199
|
+
title={getLocalizedString("Sort by value")}
|
|
200
|
+
onClick={() => setSort("value")}
|
|
201
|
+
>
|
|
202
|
+
<SortUpIcon aria-hidden />
|
|
203
|
+
</Button>
|
|
204
|
+
<Button
|
|
205
|
+
type="button"
|
|
206
|
+
icon
|
|
207
|
+
variant="tertiary"
|
|
208
|
+
data-size="sm"
|
|
209
|
+
aria-pressed={sort === "count"}
|
|
210
|
+
aria-label={getLocalizedString("Sort by count")}
|
|
211
|
+
title={getLocalizedString("Sort by count")}
|
|
212
|
+
onClick={() => setSort("count")}
|
|
213
|
+
>
|
|
214
|
+
<BarChartIcon aria-hidden />
|
|
215
|
+
</Button>
|
|
216
|
+
</div>
|
|
217
|
+
|
|
218
|
+
{error && (
|
|
219
|
+
<p className="ObFieldFilterPanel-error" role="alert">
|
|
220
|
+
{error}
|
|
221
|
+
</p>
|
|
222
|
+
)}
|
|
223
|
+
|
|
224
|
+
{loading ? (
|
|
225
|
+
<div className="ObFieldFilterPanel-valuesBox ObFieldFilterPanel-loading">
|
|
226
|
+
<Spinner data-size="xs" aria-label={getLocalizedString("Loading...")} />
|
|
227
|
+
</div>
|
|
228
|
+
) : (
|
|
229
|
+
<ul className="ObFieldFilterPanel-valuesBox ObFieldFilterPanel-values">
|
|
230
|
+
<li className="ObFieldFilterPanel-value">
|
|
231
|
+
<Checkbox
|
|
232
|
+
data-size="sm"
|
|
233
|
+
label={getLocalizedString("Select all")}
|
|
234
|
+
checked={allSelected}
|
|
235
|
+
disabled={sortedValues.length === 0}
|
|
236
|
+
onChange={(event) =>
|
|
237
|
+
toggleValues(
|
|
238
|
+
sortedValues.map((item) => item.value),
|
|
239
|
+
event.currentTarget.checked,
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
/>
|
|
243
|
+
</li>
|
|
244
|
+
{sortedValues.map((item) => (
|
|
245
|
+
<li className="ObFieldFilterPanel-value" key={item.value}>
|
|
246
|
+
<Checkbox
|
|
247
|
+
data-size="sm"
|
|
248
|
+
label={item.count === null ? item.value : `${item.value} (${item.count})`}
|
|
249
|
+
checked={selected.has(item.value)}
|
|
250
|
+
onChange={(event) => toggleValues([item.value], event.currentTarget.checked)}
|
|
251
|
+
/>
|
|
252
|
+
</li>
|
|
253
|
+
))}
|
|
254
|
+
</ul>
|
|
255
|
+
)}
|
|
256
|
+
|
|
257
|
+
{truncated && (
|
|
258
|
+
<p className="ObFieldFilterPanel-hint">
|
|
259
|
+
{getLocalizedString("Only the first values are shown. Search to narrow the list.")}
|
|
260
|
+
</p>
|
|
261
|
+
)}
|
|
262
|
+
</div>
|
|
263
|
+
)}
|
|
264
|
+
|
|
265
|
+
{children}
|
|
266
|
+
</div>
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** One criterion: an operator picker, a value editor and an optional remove button. */
|
|
271
|
+
function Field({
|
|
272
|
+
expression,
|
|
273
|
+
field,
|
|
274
|
+
operators,
|
|
275
|
+
onChange,
|
|
276
|
+
onRemove,
|
|
277
|
+
}: {
|
|
278
|
+
expression: FilterExpression;
|
|
279
|
+
field: FieldMetadata;
|
|
280
|
+
operators: FilterOperator[];
|
|
281
|
+
onChange: (expression: FilterExpression) => void;
|
|
282
|
+
onRemove?: () => void;
|
|
283
|
+
}) {
|
|
284
|
+
return (
|
|
285
|
+
<>
|
|
286
|
+
{/* A DS `Select` renders its own field wrapper, which would add a
|
|
287
|
+
second row of spacing inside an already dense panel. */}
|
|
288
|
+
<select
|
|
289
|
+
className="ds-input ObFieldFilterPanel-operator"
|
|
290
|
+
data-size="sm"
|
|
291
|
+
aria-label={getLocalizedString("Operator")}
|
|
292
|
+
value={normalizeOperator(expression.operator)}
|
|
293
|
+
onChange={(event) => onChange(changeOperator(expression, event.target.value as FilterOperator, field))}
|
|
294
|
+
>
|
|
295
|
+
{operators.map((operator) => (
|
|
296
|
+
<option key={operator} value={operator}>
|
|
297
|
+
{getOperatorLabel(operator, field.type)}
|
|
298
|
+
</option>
|
|
299
|
+
))}
|
|
300
|
+
</select>
|
|
301
|
+
<FilterValueEditor expression={expression} field={field} data-size="sm" onChange={onChange} />
|
|
302
|
+
{onRemove && (
|
|
303
|
+
<Button type="button" variant="tertiary" data-size="sm" onClick={onRemove}>
|
|
304
|
+
{getLocalizedString("Remove")}
|
|
305
|
+
</Button>
|
|
306
|
+
)}
|
|
307
|
+
</>
|
|
308
|
+
);
|
|
309
|
+
}
|