@olenbetong/appframe-ds 1.7.1 → 1.7.2

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,168 +1,168 @@
1
- import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
2
- import { FieldDescription, Label, Spinner, ValidationMessage } from "@digdir/designsystemet-react";
3
- import { XMarkIcon } from "@navikt/aksel-icons";
4
- import clsx from "clsx";
5
- import { type ReactNode, type Ref, useEffect, useRef, useState } from "react";
6
-
7
- import type { AutocompleteSize } from "./types.js";
8
- import { getLocalizedString } from "../i18n.js";
9
-
10
- export interface AutocompleteFrameProps {
11
- id?: string;
12
- label?: ReactNode;
13
- description?: ReactNode;
14
- error?: ReactNode;
15
- className?: string;
16
- style?: React.CSSProperties;
17
- controlClassName?: string;
18
- popupClassName?: string;
19
- fullWidth?: boolean;
20
- size?: AutocompleteSize;
21
- color?: string;
22
- disabled?: boolean;
23
- readOnly?: boolean;
24
- required?: boolean;
25
- placeholder?: string;
26
- loading?: boolean;
27
- disableClearable?: boolean;
28
- inputRef?: Ref<HTMLInputElement>;
29
- /**
30
- * Accessible name for the input. Use when the field has no visible label.
31
- */
32
- "aria-label"?: string;
33
- /**
34
- * Chips for the selected values, rendered above the input.
35
- */
36
- chips?: ReactNode;
37
- /**
38
- * The trigger button, which differs between `Autocomplete` and `Combobox`.
39
- */
40
- trigger?: ReactNode;
41
- /**
42
- * Rendered in the popup when the filtered list is empty.
43
- */
44
- emptyContent?: ReactNode;
45
- /**
46
- * Renders an entry of the filtered list. Receives either an option or, when
47
- * `groupBy` is used, a group.
48
- */
49
- renderListItem: (item: any, index: number) => ReactNode;
50
- onInputKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
51
- onInputBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
52
- }
53
-
54
- /**
55
- * Shared chrome for `Autocomplete` and `Combobox`: the Designsystemet field
56
- * (label, description, validation message), the bordered control holding the
57
- * chips and the input, and the popup with the option list.
58
- *
59
- * All the parts used here are shared between the Base UI `Autocomplete` and
60
- * `Combobox` namespaces, so they can be imported from `combobox` regardless of
61
- * which root the caller renders.
62
- */
63
- export function AutocompleteFrame({
64
- id,
65
- label,
66
- description,
67
- error,
68
- className,
69
- style,
70
- controlClassName,
71
- popupClassName,
72
- fullWidth = true,
73
- size,
74
- color,
75
- disabled,
76
- readOnly,
77
- required,
78
- placeholder,
79
- loading,
80
- disableClearable,
81
- inputRef,
82
- "aria-label": ariaLabel,
83
- chips,
84
- trigger,
85
- emptyContent,
86
- renderListItem,
87
- onInputKeyDown,
88
- onInputBlur,
89
- }: AutocompleteFrameProps) {
90
- let rootRef = useRef<HTMLDivElement>(null);
91
- let [container, setContainer] = useState<HTMLElement | null>(null);
92
-
93
- /*
94
- * A modal dialog is painted in the top layer, above everything else in the
95
- * document, so a popup portalled to the body ends up behind its backdrop and
96
- * is invisible. Portal into the dialog itself when there is one.
97
- */
98
- useEffect(() => {
99
- setContainer(rootRef.current?.closest("dialog") ?? null);
100
- }, []);
101
-
102
- return (
103
- // Designsystemet's `Field` is a custom element that expects to own the
104
- // layout, so the root here is a plain grid instead.
105
- <div
106
- ref={rootRef}
107
- className={clsx("ObAutocomplete-root", className)}
108
- style={style}
109
- data-size={size}
110
- data-color={color}
111
- data-full-width={fullWidth}
112
- >
113
- {label && (
114
- <Label htmlFor={id} data-size={size}>
115
- {label}
116
- {required && <span aria-hidden> *</span>}
117
- </Label>
118
- )}
119
- {description && <FieldDescription data-size={size}>{description}</FieldDescription>}
120
- <div
121
- className={clsx("ObAutocomplete-control", controlClassName)}
122
- data-invalid={error ? true : undefined}
123
- data-disabled={disabled ? true : undefined}
124
- >
125
- {chips && <div className="ObAutocomplete-chips">{chips}</div>}
126
- <BaseCombobox.InputGroup className="ObAutocomplete-inputGroup">
127
- <BaseCombobox.Input
128
- id={id}
129
- ref={inputRef}
130
- className="ObAutocomplete-input"
131
- placeholder={placeholder}
132
- aria-label={ariaLabel}
133
- disabled={disabled}
134
- readOnly={readOnly}
135
- required={required}
136
- aria-invalid={error ? true : undefined}
137
- onKeyDown={onInputKeyDown}
138
- onBlur={onInputBlur}
139
- />
140
- {loading && (
141
- <span className="ObAutocomplete-spinner">
142
- <Spinner aria-label={getLocalizedString("Loading")} data-size="xs" />
143
- </span>
144
- )}
145
- {!disableClearable && (
146
- <BaseCombobox.Clear
147
- className="ObAutocomplete-button ObAutocomplete-clear"
148
- disabled={disabled || readOnly}
149
- aria-label={getLocalizedString("Clear")}
150
- >
151
- <XMarkIcon aria-hidden fontSize="1.25em" />
152
- </BaseCombobox.Clear>
153
- )}
154
- {trigger}
155
- </BaseCombobox.InputGroup>
156
- </div>
157
- {error && <ValidationMessage data-size={size}>{error}</ValidationMessage>}
158
- <BaseCombobox.Portal container={container ?? undefined}>
159
- <BaseCombobox.Positioner className="ObAutocomplete-positioner" sideOffset={4}>
160
- <BaseCombobox.Popup className={clsx("ObAutocomplete-popup", popupClassName)} data-size={size}>
161
- <BaseCombobox.Empty className="ObAutocomplete-empty">{emptyContent}</BaseCombobox.Empty>
162
- <BaseCombobox.List className="ObAutocomplete-list">{renderListItem}</BaseCombobox.List>
163
- </BaseCombobox.Popup>
164
- </BaseCombobox.Positioner>
165
- </BaseCombobox.Portal>
166
- </div>
167
- );
168
- }
1
+ import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
2
+ import { FieldDescription, Label, Spinner, ValidationMessage } from "@digdir/designsystemet-react";
3
+ import { XMarkIcon } from "@navikt/aksel-icons";
4
+ import clsx from "clsx";
5
+ import { type ReactNode, type Ref, useEffect, useRef, useState } from "react";
6
+
7
+ import type { AutocompleteSize } from "./types.js";
8
+ import { getLocalizedString } from "../i18n.js";
9
+
10
+ export interface AutocompleteFrameProps {
11
+ id?: string;
12
+ label?: ReactNode;
13
+ description?: ReactNode;
14
+ error?: ReactNode;
15
+ className?: string;
16
+ style?: React.CSSProperties;
17
+ controlClassName?: string;
18
+ popupClassName?: string;
19
+ fullWidth?: boolean;
20
+ size?: AutocompleteSize;
21
+ color?: string;
22
+ disabled?: boolean;
23
+ readOnly?: boolean;
24
+ required?: boolean;
25
+ placeholder?: string;
26
+ loading?: boolean;
27
+ disableClearable?: boolean;
28
+ inputRef?: Ref<HTMLInputElement>;
29
+ /**
30
+ * Accessible name for the input. Use when the field has no visible label.
31
+ */
32
+ "aria-label"?: string;
33
+ /**
34
+ * Chips for the selected values, rendered above the input.
35
+ */
36
+ chips?: ReactNode;
37
+ /**
38
+ * The trigger button, which differs between `Autocomplete` and `Combobox`.
39
+ */
40
+ trigger?: ReactNode;
41
+ /**
42
+ * Rendered in the popup when the filtered list is empty.
43
+ */
44
+ emptyContent?: ReactNode;
45
+ /**
46
+ * Renders an entry of the filtered list. Receives either an option or, when
47
+ * `groupBy` is used, a group.
48
+ */
49
+ renderListItem: (item: any, index: number) => ReactNode;
50
+ onInputKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
51
+ onInputBlur?: (event: React.FocusEvent<HTMLInputElement>) => void;
52
+ }
53
+
54
+ /**
55
+ * Shared chrome for `Autocomplete` and `Combobox`: the Designsystemet field
56
+ * (label, description, validation message), the bordered control holding the
57
+ * chips and the input, and the popup with the option list.
58
+ *
59
+ * All the parts used here are shared between the Base UI `Autocomplete` and
60
+ * `Combobox` namespaces, so they can be imported from `combobox` regardless of
61
+ * which root the caller renders.
62
+ */
63
+ export function AutocompleteFrame({
64
+ id,
65
+ label,
66
+ description,
67
+ error,
68
+ className,
69
+ style,
70
+ controlClassName,
71
+ popupClassName,
72
+ fullWidth = true,
73
+ size,
74
+ color,
75
+ disabled,
76
+ readOnly,
77
+ required,
78
+ placeholder,
79
+ loading,
80
+ disableClearable,
81
+ inputRef,
82
+ "aria-label": ariaLabel,
83
+ chips,
84
+ trigger,
85
+ emptyContent,
86
+ renderListItem,
87
+ onInputKeyDown,
88
+ onInputBlur,
89
+ }: AutocompleteFrameProps) {
90
+ let rootRef = useRef<HTMLDivElement>(null);
91
+ let [container, setContainer] = useState<HTMLElement | null>(null);
92
+
93
+ /*
94
+ * A modal dialog is painted in the top layer, above everything else in the
95
+ * document, so a popup portalled to the body ends up behind its backdrop and
96
+ * is invisible. Portal into the dialog itself when there is one.
97
+ */
98
+ useEffect(() => {
99
+ setContainer(rootRef.current?.closest("dialog") ?? null);
100
+ }, []);
101
+
102
+ return (
103
+ // Designsystemet's `Field` is a custom element that expects to own the
104
+ // layout, so the root here is a plain grid instead.
105
+ <div
106
+ ref={rootRef}
107
+ className={clsx("ObAutocomplete-root", className)}
108
+ style={style}
109
+ data-size={size}
110
+ data-color={color}
111
+ data-full-width={fullWidth}
112
+ >
113
+ {label && (
114
+ <Label htmlFor={id} data-size={size}>
115
+ {label}
116
+ {required && <span aria-hidden> *</span>}
117
+ </Label>
118
+ )}
119
+ {description && <FieldDescription data-size={size}>{description}</FieldDescription>}
120
+ <div
121
+ className={clsx("ObAutocomplete-control", controlClassName)}
122
+ data-invalid={error ? true : undefined}
123
+ data-disabled={disabled ? true : undefined}
124
+ >
125
+ {chips && <div className="ObAutocomplete-chips">{chips}</div>}
126
+ <BaseCombobox.InputGroup className="ObAutocomplete-inputGroup">
127
+ <BaseCombobox.Input
128
+ id={id}
129
+ ref={inputRef}
130
+ className="ObAutocomplete-input"
131
+ placeholder={placeholder}
132
+ aria-label={ariaLabel}
133
+ disabled={disabled}
134
+ readOnly={readOnly}
135
+ required={required}
136
+ aria-invalid={error ? true : undefined}
137
+ onKeyDown={onInputKeyDown}
138
+ onBlur={onInputBlur}
139
+ />
140
+ {loading && (
141
+ <span className="ObAutocomplete-spinner">
142
+ <Spinner aria-label={getLocalizedString("Loading")} data-size="xs" />
143
+ </span>
144
+ )}
145
+ {!disableClearable && (
146
+ <BaseCombobox.Clear
147
+ className="ObAutocomplete-button ObAutocomplete-clear"
148
+ disabled={disabled || readOnly}
149
+ aria-label={getLocalizedString("Clear")}
150
+ >
151
+ <XMarkIcon aria-hidden fontSize="1.25em" />
152
+ </BaseCombobox.Clear>
153
+ )}
154
+ {trigger}
155
+ </BaseCombobox.InputGroup>
156
+ </div>
157
+ {error && <ValidationMessage data-size={size}>{error}</ValidationMessage>}
158
+ <BaseCombobox.Portal container={container ?? undefined}>
159
+ <BaseCombobox.Positioner className="ObAutocomplete-positioner" sideOffset={4}>
160
+ <BaseCombobox.Popup className={clsx("ObAutocomplete-popup", popupClassName)} data-size={size}>
161
+ <BaseCombobox.Empty className="ObAutocomplete-empty">{emptyContent}</BaseCombobox.Empty>
162
+ <BaseCombobox.List className="ObAutocomplete-list">{renderListItem}</BaseCombobox.List>
163
+ </BaseCombobox.Popup>
164
+ </BaseCombobox.Positioner>
165
+ </BaseCombobox.Portal>
166
+ </div>
167
+ );
168
+ }