@olenbetong/appframe-ds 1.6.1 → 1.7.1

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.
@@ -1,186 +1,186 @@
1
- import "./Lookup.css";
2
-
3
- import { Button, Tag, Textfield, type TextfieldProps } from "@digdir/designsystemet-react";
4
- import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons";
5
- import { type DistributiveOmit, useLookupState } from "@olenbetong/appframe-react";
6
-
7
- import clsx from "clsx";
8
- import { useState } from "react";
9
- import { createPortal } from "react-dom";
10
-
11
- import { LookupCombobox, type LookupComboboxProps } from "./LookupCombobox.js";
12
- import { getLocalizedString } from "../i18n.js";
13
-
14
- export type LabelTag = "optional" | "required";
15
-
16
- const LABEL_TAG_COLOR: Record<LabelTag, string> = { optional: "info", required: "warning" };
17
- const LABEL_TAG_TEXT: Record<LabelTag, string> = { optional: "Optional", required: "Required" };
18
-
19
- function labelWithTag(label: string | undefined, labelTag: LabelTag | undefined) {
20
- if (!labelTag) {
21
- return label;
22
- }
23
-
24
- return (
25
- <>
26
- {label}
27
- <Tag className="ObLookup-labelTag" data-color={LABEL_TAG_COLOR[labelTag]} data-size="sm">
28
- {getLocalizedString(LABEL_TAG_TEXT[labelTag])}
29
- </Tag>
30
- </>
31
- );
32
- }
33
-
34
- export type AfLookupProps<T extends Record<string, unknown>> = DistributiveOmit<LookupComboboxProps<T>, "value"> & {
35
- /**
36
- * Allows the user to enter a value manually that might not be in the list.
37
- */
38
- editable?: boolean;
39
- fullWidth?: boolean;
40
- label?: string;
41
- labelTag?: LabelTag;
42
- value?: string | null;
43
- onChange?: (item: T | null) => void;
44
- slotProps?: {
45
- dialog?: Partial<React.HTMLProps<HTMLDialogElement>>;
46
- textField?: Partial<TextfieldProps>;
47
- };
48
- };
49
-
50
- export type LookupProps<T extends Record<string, unknown>> = DistributiveOmit<LookupComboboxProps<T>, "value"> & {
51
- slotProps?: {
52
- dialog?: Partial<React.HTMLProps<HTMLDialogElement>>;
53
- };
54
- getAnchorElement?: () => Element | null;
55
- };
56
-
57
- export function useLookup<T extends Record<string, unknown>>({
58
- getAnchorElement,
59
- slotProps,
60
- ...props
61
- }: LookupProps<T>) {
62
- let { open, openDialog, onKeyDown, dialogRef, anchorName, inputId } = useLookupState({
63
- getAnchorElement,
64
- scrollParentSelector: "[data-virtualized-scroller]",
65
- });
66
- let [initialSearchValue, setInitialSearchValue] = useState<string | undefined>(undefined);
67
-
68
- function handleChange(item: T | null) {
69
- props.onChange?.(item);
70
- dialogRef.current?.close();
71
- }
72
-
73
- function handleOpenDialog(searchValue?: string) {
74
- // openDialog renders synchronously (flushSync), which includes this update, so the combobox
75
- // mounts with the search value already set.
76
- setInitialSearchValue(searchValue);
77
- openDialog();
78
- }
79
-
80
- return {
81
- open,
82
- openDialog: handleOpenDialog,
83
- onKeyDown,
84
- onChange: handleChange,
85
- dialogRef,
86
- anchorName,
87
- inputId,
88
- dialog: createPortal(
89
- <dialog
90
- ref={dialogRef}
91
- className={clsx("ObLookup-dialog", slotProps?.dialog?.className)}
92
- onKeyDown={(evt) => evt.stopPropagation()}
93
- style={{ positionAnchor: anchorName, ...slotProps?.dialog?.style } as React.CSSProperties}
94
- >
95
- {open && (
96
- <LookupCombobox
97
- {...props}
98
- initialSearchValue={initialSearchValue}
99
- onClose={() => dialogRef.current?.close()}
100
- onChange={handleChange}
101
- />
102
- )}
103
- </dialog>,
104
- document.body,
105
- inputId,
106
- ),
107
- };
108
- }
109
-
110
- export function AfLookup<T extends Record<string, unknown>>({
111
- fullWidth = false,
112
- label,
113
- labelTag,
114
- slotProps,
115
- value,
116
- ...props
117
- }: AfLookupProps<T>) {
118
- let { inputId, open: _open, dialog, anchorName, openDialog, onKeyDown } = useLookup<T>(props);
119
-
120
- let hasClear = !!(props.nullable && value && props.onChange);
121
-
122
- function handleInputKeyDown(event: React.KeyboardEvent<HTMLElement>) {
123
- let hasModifier = event.ctrlKey || event.altKey || event.metaKey;
124
- let isTypeaheadKey = event.key.length === 1 && !hasModifier && !event.nativeEvent.isComposing;
125
- if (isTypeaheadKey) {
126
- event.preventDefault();
127
- event.stopPropagation();
128
- openDialog(event.key);
129
- return;
130
- }
131
- onKeyDown(event);
132
- }
133
-
134
- return (
135
- <div style={{ anchorName } as React.CSSProperties}>
136
- <div
137
- className="ObLookup-inputWrap"
138
- style={
139
- {
140
- display: fullWidth ? "block" : "inline-block",
141
- "--ObLookup-buttons-width": hasClear ? "5.5rem" : "3rem",
142
- } as React.CSSProperties
143
- }
144
- >
145
- <Textfield
146
- id={inputId}
147
- value={value ?? ""}
148
- label={labelWithTag(label, labelTag)}
149
- onClick={() => openDialog()}
150
- onKeyDown={handleInputKeyDown}
151
- className={clsx(slotProps?.textField?.className, "ObLookup-input")}
152
- {...(slotProps?.textField as any)}
153
- style={{ width: fullWidth ? "100%" : undefined, ...slotProps?.textField?.style }}
154
- />
155
- <div className="ObLookup-buttons">
156
- {hasClear && (
157
- <Button
158
- icon
159
- variant="tertiary"
160
- data-size="sm"
161
- aria-label={getLocalizedString("Clear")}
162
- onKeyDown={(evt) => evt.stopPropagation()}
163
- onClick={(evt) => {
164
- evt.stopPropagation();
165
- props.onChange?.(null);
166
- document.getElementById(inputId)?.focus();
167
- }}
168
- >
169
- <XMarkIcon />
170
- </Button>
171
- )}
172
- <Button
173
- icon
174
- variant="tertiary"
175
- data-size="sm"
176
- aria-label={getLocalizedString("Search")}
177
- onClick={() => openDialog()}
178
- >
179
- <MagnifyingGlassIcon />
180
- </Button>
181
- </div>
182
- </div>
183
- {dialog}
184
- </div>
185
- );
186
- }
1
+ import "./Lookup.css";
2
+
3
+ import { Button, Tag, Textfield, type TextfieldProps } from "@digdir/designsystemet-react";
4
+ import { MagnifyingGlassIcon, XMarkIcon } from "@navikt/aksel-icons";
5
+ import { type DistributiveOmit, useLookupState } from "@olenbetong/appframe-react";
6
+
7
+ import clsx from "clsx";
8
+ import { useState } from "react";
9
+ import { createPortal } from "react-dom";
10
+
11
+ import { LookupCombobox, type LookupComboboxProps } from "./LookupCombobox.js";
12
+ import { getLocalizedString } from "../i18n.js";
13
+
14
+ export type LabelTag = "optional" | "required";
15
+
16
+ const LABEL_TAG_COLOR: Record<LabelTag, string> = { optional: "info", required: "warning" };
17
+ const LABEL_TAG_TEXT: Record<LabelTag, string> = { optional: "Optional", required: "Required" };
18
+
19
+ function labelWithTag(label: string | undefined, labelTag: LabelTag | undefined) {
20
+ if (!labelTag) {
21
+ return label;
22
+ }
23
+
24
+ return (
25
+ <>
26
+ {label}
27
+ <Tag className="ObLookup-labelTag" data-color={LABEL_TAG_COLOR[labelTag]} data-size="sm">
28
+ {getLocalizedString(LABEL_TAG_TEXT[labelTag])}
29
+ </Tag>
30
+ </>
31
+ );
32
+ }
33
+
34
+ export type AfLookupProps<T extends Record<string, unknown>> = DistributiveOmit<LookupComboboxProps<T>, "value"> & {
35
+ /**
36
+ * Allows the user to enter a value manually that might not be in the list.
37
+ */
38
+ editable?: boolean;
39
+ fullWidth?: boolean;
40
+ label?: string;
41
+ labelTag?: LabelTag;
42
+ value?: string | null;
43
+ onChange?: (item: T | null) => void;
44
+ slotProps?: {
45
+ dialog?: Partial<React.HTMLProps<HTMLDialogElement>>;
46
+ textField?: Partial<TextfieldProps>;
47
+ };
48
+ };
49
+
50
+ export type LookupProps<T extends Record<string, unknown>> = DistributiveOmit<LookupComboboxProps<T>, "value"> & {
51
+ slotProps?: {
52
+ dialog?: Partial<React.HTMLProps<HTMLDialogElement>>;
53
+ };
54
+ getAnchorElement?: () => Element | null;
55
+ };
56
+
57
+ export function useLookup<T extends Record<string, unknown>>({
58
+ getAnchorElement,
59
+ slotProps,
60
+ ...props
61
+ }: LookupProps<T>) {
62
+ let { open, openDialog, onKeyDown, dialogRef, anchorName, inputId } = useLookupState({
63
+ getAnchorElement,
64
+ scrollParentSelector: "[data-virtualized-scroller]",
65
+ });
66
+ let [initialSearchValue, setInitialSearchValue] = useState<string | undefined>(undefined);
67
+
68
+ function handleChange(item: T | null) {
69
+ props.onChange?.(item);
70
+ dialogRef.current?.close();
71
+ }
72
+
73
+ function handleOpenDialog(searchValue?: string) {
74
+ // openDialog renders synchronously (flushSync), which includes this update, so the combobox
75
+ // mounts with the search value already set.
76
+ setInitialSearchValue(searchValue);
77
+ openDialog();
78
+ }
79
+
80
+ return {
81
+ open,
82
+ openDialog: handleOpenDialog,
83
+ onKeyDown,
84
+ onChange: handleChange,
85
+ dialogRef,
86
+ anchorName,
87
+ inputId,
88
+ dialog: createPortal(
89
+ <dialog
90
+ ref={dialogRef}
91
+ className={clsx("ObLookup-dialog", slotProps?.dialog?.className)}
92
+ onKeyDown={(evt) => evt.stopPropagation()}
93
+ style={{ positionAnchor: anchorName, ...slotProps?.dialog?.style } as React.CSSProperties}
94
+ >
95
+ {open && (
96
+ <LookupCombobox
97
+ {...props}
98
+ initialSearchValue={initialSearchValue}
99
+ onClose={() => dialogRef.current?.close()}
100
+ onChange={handleChange}
101
+ />
102
+ )}
103
+ </dialog>,
104
+ document.body,
105
+ inputId,
106
+ ),
107
+ };
108
+ }
109
+
110
+ export function AfLookup<T extends Record<string, unknown>>({
111
+ fullWidth = false,
112
+ label,
113
+ labelTag,
114
+ slotProps,
115
+ value,
116
+ ...props
117
+ }: AfLookupProps<T>) {
118
+ let { inputId, open: _open, dialog, anchorName, openDialog, onKeyDown } = useLookup<T>(props);
119
+
120
+ let hasClear = !!(props.nullable && value && props.onChange);
121
+
122
+ function handleInputKeyDown(event: React.KeyboardEvent<HTMLElement>) {
123
+ let hasModifier = event.ctrlKey || event.altKey || event.metaKey;
124
+ let isTypeaheadKey = event.key.length === 1 && !hasModifier && !event.nativeEvent.isComposing;
125
+ if (isTypeaheadKey) {
126
+ event.preventDefault();
127
+ event.stopPropagation();
128
+ openDialog(event.key);
129
+ return;
130
+ }
131
+ onKeyDown(event);
132
+ }
133
+
134
+ return (
135
+ <div style={{ anchorName } as React.CSSProperties}>
136
+ <div
137
+ className="ObLookup-inputWrap"
138
+ style={
139
+ {
140
+ display: fullWidth ? "block" : "inline-block",
141
+ "--ObLookup-buttons-width": hasClear ? "5.5rem" : "3rem",
142
+ } as React.CSSProperties
143
+ }
144
+ >
145
+ <Textfield
146
+ id={inputId}
147
+ value={value ?? ""}
148
+ label={labelWithTag(label, labelTag)}
149
+ onClick={() => openDialog()}
150
+ onKeyDown={handleInputKeyDown}
151
+ className={clsx(slotProps?.textField?.className, "ObLookup-input")}
152
+ {...(slotProps?.textField as any)}
153
+ style={{ width: fullWidth ? "100%" : undefined, ...slotProps?.textField?.style }}
154
+ />
155
+ <div className="ObLookup-buttons">
156
+ {hasClear && (
157
+ <Button
158
+ icon
159
+ variant="tertiary"
160
+ data-size="sm"
161
+ aria-label={getLocalizedString("Clear")}
162
+ onKeyDown={(evt) => evt.stopPropagation()}
163
+ onClick={(evt) => {
164
+ evt.stopPropagation();
165
+ props.onChange?.(null);
166
+ document.getElementById(inputId)?.focus();
167
+ }}
168
+ >
169
+ <XMarkIcon />
170
+ </Button>
171
+ )}
172
+ <Button
173
+ icon
174
+ variant="tertiary"
175
+ data-size="sm"
176
+ aria-label={getLocalizedString("Search")}
177
+ onClick={() => openDialog()}
178
+ >
179
+ <MagnifyingGlassIcon />
180
+ </Button>
181
+ </div>
182
+ </div>
183
+ {dialog}
184
+ </div>
185
+ );
186
+ }
@@ -1,69 +1,69 @@
1
- /* Search on top (it has the focus), the list filling the middle, Clear and Close as a footer */
2
- .ObCombobox-root {
3
- height: 100%;
4
- display: grid;
5
- grid-template-rows: auto 1fr min-content;
6
- gap: var(--ds-size-2, 0.5rem);
7
-
8
- overscroll-behavior: contain;
9
- }
10
-
11
- .ObCombobox-actions {
12
- display: flex;
13
- justify-content: space-between;
14
- }
15
-
16
- /* Close stays at the end when there is no Clear */
17
- .ObCombobox-close {
18
- margin-inline-start: auto;
19
- }
20
-
21
- .ObCombobox-listWrap {
22
- position: relative;
23
- overflow: hidden;
24
- min-height: 0;
25
- border: 1px solid var(--ds-color-neutral-border-subtle);
26
- border-radius: 0.25rem;
27
- background-color: var(--ds-color-neutral-background-default);
28
- }
29
-
30
- .ObCombobox-list {
31
- height: 100%;
32
- overflow-y: hidden;
33
- overscroll-behavior: contain;
34
- list-style: none;
35
- margin: 0;
36
- padding: 0;
37
- }
38
-
39
- .ObCombobox-input {
40
- background-color: var(--ds-color-neutral-background-default);
41
- }
42
-
43
- /*
44
- * The rows are rendered by the consumer; the divider and the hover and active
45
- * states come from here, so a row only has to lay out its content.
46
- */
47
- .ObCombobox-listItem {
48
- cursor: pointer;
49
- box-sizing: border-box;
50
- list-style: none;
51
- border-bottom: 1px solid var(--ds-color-neutral-border-subtle);
52
-
53
- &.active {
54
- background-color: var(--ds-color-neutral-surface-active);
55
- }
56
-
57
- &:hover {
58
- background-color: var(--ds-color-neutral-surface-hover);
59
- }
60
- }
61
-
62
- /*
63
- * The loading bar is a `LinearProgress overlay`: a zero-height wrapper at the
64
- * top of the list wrap, with the bar laid over the first rows, so nothing moves
65
- * when it appears. The track is square here so it hugs the list border.
66
- */
67
- .ObCombobox-progress .ObLinearProgress-root {
68
- border-radius: 0;
69
- }
1
+ /* Search on top (it has the focus), the list filling the middle, Clear and Close as a footer */
2
+ .ObCombobox-root {
3
+ height: 100%;
4
+ display: grid;
5
+ grid-template-rows: auto 1fr min-content;
6
+ gap: var(--ds-size-2, 0.5rem);
7
+
8
+ overscroll-behavior: contain;
9
+ }
10
+
11
+ .ObCombobox-actions {
12
+ display: flex;
13
+ justify-content: space-between;
14
+ }
15
+
16
+ /* Close stays at the end when there is no Clear */
17
+ .ObCombobox-close {
18
+ margin-inline-start: auto;
19
+ }
20
+
21
+ .ObCombobox-listWrap {
22
+ position: relative;
23
+ overflow: hidden;
24
+ min-height: 0;
25
+ border: 1px solid var(--ds-color-neutral-border-subtle);
26
+ border-radius: 0.25rem;
27
+ background-color: var(--ds-color-neutral-background-default);
28
+ }
29
+
30
+ .ObCombobox-list {
31
+ height: 100%;
32
+ overflow-y: hidden;
33
+ overscroll-behavior: contain;
34
+ list-style: none;
35
+ margin: 0;
36
+ padding: 0;
37
+ }
38
+
39
+ .ObCombobox-input {
40
+ background-color: var(--ds-color-neutral-background-default);
41
+ }
42
+
43
+ /*
44
+ * The rows are rendered by the consumer; the divider and the hover and active
45
+ * states come from here, so a row only has to lay out its content.
46
+ */
47
+ .ObCombobox-listItem {
48
+ cursor: pointer;
49
+ box-sizing: border-box;
50
+ list-style: none;
51
+ border-bottom: 1px solid var(--ds-color-neutral-border-subtle);
52
+
53
+ &.active {
54
+ background-color: var(--ds-color-neutral-surface-active);
55
+ }
56
+
57
+ &:hover {
58
+ background-color: var(--ds-color-neutral-surface-hover);
59
+ }
60
+ }
61
+
62
+ /*
63
+ * The loading bar is a `LinearProgress overlay`: a zero-height wrapper at the
64
+ * top of the list wrap, with the bar laid over the first rows, so nothing moves
65
+ * when it appears. The track is square here so it hugs the list border.
66
+ */
67
+ .ObCombobox-progress .ObLinearProgress-root {
68
+ border-radius: 0;
69
+ }