@olenbetong/appframe-ds 0.6.0 → 0.7.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 +10 -0
- package/package.json +8 -6
- package/scripts/copyAssets.mjs +6 -1
- package/src/AfLookup/Lookup.tsx +4 -4
- package/src/AfLookup/{Combobox.tsx → LookupCombobox.tsx} +8 -8
- package/src/AfLookup/LookupEdit.tsx +2 -2
- package/src/AfLookup/index.ts +1 -1
- package/src/autocomplete/Autocomplete.css +222 -0
- package/src/autocomplete/Autocomplete.tsx +316 -0
- package/src/autocomplete/AutocompleteFrame.tsx +149 -0
- package/src/autocomplete/Combobox.tsx +260 -0
- package/src/autocomplete/index.ts +4 -0
- package/src/autocomplete/types.ts +200 -0
- package/src/autocomplete/utils.ts +183 -0
- package/src/index.ts +2 -0
- package/src/sortable/DropIndicator.css +51 -0
- package/src/sortable/DropIndicator.tsx +27 -0
- package/src/sortable/SortableItemList.css +21 -0
- package/src/sortable/SortableItemList.tsx +264 -0
- package/src/sortable/index.ts +8 -0
- package/src/sortable/reorderSortOrder.ts +126 -0
- package/src/sortable/sortOrder.ts +101 -0
- package/src/sortable/types.ts +9 -0
- package/src/sortable/useSortableContainer.ts +53 -0
- package/src/sortable/useSortableItem.ts +95 -0
- package/src/sortable/useSortableList.ts +117 -0
- /package/src/AfLookup/{Combobox.css → LookupCombobox.css} +0 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import "./Autocomplete.css";
|
|
2
|
+
|
|
3
|
+
import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
|
|
4
|
+
import { ChipRemovable } from "@digdir/designsystemet-react";
|
|
5
|
+
import { ChevronDownIcon } from "@navikt/aksel-icons";
|
|
6
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
7
|
+
import { useId, useMemo } from "react";
|
|
8
|
+
|
|
9
|
+
import { AutocompleteFrame } from "./AutocompleteFrame.js";
|
|
10
|
+
import type { AutocompleteBaseProps, AutocompleteChangeReason, AutocompleteValue } from "./types.js";
|
|
11
|
+
import {
|
|
12
|
+
areOptionsEqual,
|
|
13
|
+
defaultFilterOptions,
|
|
14
|
+
defaultGetOptionLabel,
|
|
15
|
+
groupOptions,
|
|
16
|
+
handleInputValueChange,
|
|
17
|
+
mapChangeReason,
|
|
18
|
+
useControlled,
|
|
19
|
+
} from "./utils.js";
|
|
20
|
+
|
|
21
|
+
export interface ComboboxProps<Option, Multiple extends boolean | undefined = false> extends AutocompleteBaseProps<
|
|
22
|
+
Option,
|
|
23
|
+
Multiple
|
|
24
|
+
> {
|
|
25
|
+
/**
|
|
26
|
+
* The selected option(s). Use together with `onChange` for a controlled
|
|
27
|
+
* component.
|
|
28
|
+
*/
|
|
29
|
+
value?: AutocompleteValue<Option, Multiple>;
|
|
30
|
+
/**
|
|
31
|
+
* The initially selected option(s) when uncontrolled.
|
|
32
|
+
*/
|
|
33
|
+
defaultValue?: AutocompleteValue<Option, Multiple>;
|
|
34
|
+
/**
|
|
35
|
+
* Called when the selection changes.
|
|
36
|
+
*/
|
|
37
|
+
onChange?: (
|
|
38
|
+
event: Event | undefined,
|
|
39
|
+
value: AutocompleteValue<Option, Multiple>,
|
|
40
|
+
reason: AutocompleteChangeReason,
|
|
41
|
+
) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A select-from-a-list input built on Base UI's `Combobox`, styled with
|
|
46
|
+
* Designsystemet tokens. The user must pick one of the options — free text is
|
|
47
|
+
* only used to filter the list.
|
|
48
|
+
*
|
|
49
|
+
* The API follows MUI's `Autocomplete`: `options`, `value`/`onChange`,
|
|
50
|
+
* `getOptionLabel`, `isOptionEqualToValue`, `filterOptions`, `renderOption`,
|
|
51
|
+
* `groupBy` and `multiple` all behave the same way. Designsystemet field props
|
|
52
|
+
* (`label`, `description`, `error`, `data-size`, `data-color`) are supported on
|
|
53
|
+
* top.
|
|
54
|
+
*
|
|
55
|
+
* Use `Autocomplete` instead when the input should accept free text and the
|
|
56
|
+
* options are only suggestions.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```jsx
|
|
60
|
+
* <Combobox
|
|
61
|
+
* label="Country"
|
|
62
|
+
* options={countries}
|
|
63
|
+
* getOptionLabel={(country) => country.name}
|
|
64
|
+
* isOptionEqualToValue={(a, b) => a.id === b.id}
|
|
65
|
+
* value={country}
|
|
66
|
+
* onChange={(_event, value) => setCountry(value)}
|
|
67
|
+
* />
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function Combobox<Option, Multiple extends boolean | undefined = false>(props: ComboboxProps<Option, Multiple>) {
|
|
71
|
+
let {
|
|
72
|
+
options,
|
|
73
|
+
multiple,
|
|
74
|
+
value: valueProp,
|
|
75
|
+
defaultValue,
|
|
76
|
+
onChange,
|
|
77
|
+
inputValue: inputValueProp,
|
|
78
|
+
onInputChange,
|
|
79
|
+
getOptionLabel = defaultGetOptionLabel,
|
|
80
|
+
getOptionKey,
|
|
81
|
+
getOptionDisabled,
|
|
82
|
+
isOptionEqualToValue,
|
|
83
|
+
groupBy,
|
|
84
|
+
filterOptions,
|
|
85
|
+
renderOption,
|
|
86
|
+
renderChipLabel,
|
|
87
|
+
noOptionsText,
|
|
88
|
+
loadingText,
|
|
89
|
+
loading,
|
|
90
|
+
limit = -1,
|
|
91
|
+
autoHighlight,
|
|
92
|
+
openOnFocus = true,
|
|
93
|
+
open,
|
|
94
|
+
defaultOpen,
|
|
95
|
+
onOpenChange,
|
|
96
|
+
modal,
|
|
97
|
+
disabled,
|
|
98
|
+
readOnly,
|
|
99
|
+
required,
|
|
100
|
+
name,
|
|
101
|
+
id: idProp,
|
|
102
|
+
className,
|
|
103
|
+
style,
|
|
104
|
+
controlClassName,
|
|
105
|
+
popupClassName,
|
|
106
|
+
fullWidth,
|
|
107
|
+
disableClearable,
|
|
108
|
+
placeholder,
|
|
109
|
+
inputRef,
|
|
110
|
+
"data-size": size,
|
|
111
|
+
"data-color": color,
|
|
112
|
+
} = props;
|
|
113
|
+
|
|
114
|
+
let generatedId = useId();
|
|
115
|
+
let id = idProp ?? generatedId;
|
|
116
|
+
|
|
117
|
+
let emptyValue = (multiple ? [] : null) as AutocompleteValue<Option, Multiple>;
|
|
118
|
+
let [value, setValue] = useControlled(valueProp, defaultValue ?? emptyValue);
|
|
119
|
+
let [inputValue, setInputValue] = useControlled(inputValueProp, "");
|
|
120
|
+
|
|
121
|
+
let filteredOptions = useMemo(() => {
|
|
122
|
+
let filter = (filterOptions ?? defaultFilterOptions) as (
|
|
123
|
+
options: readonly Option[],
|
|
124
|
+
state: { inputValue: string; getOptionLabel: (option: Option) => string },
|
|
125
|
+
) => Option[];
|
|
126
|
+
|
|
127
|
+
return filter(options, { inputValue, getOptionLabel });
|
|
128
|
+
}, [options, inputValue, filterOptions, getOptionLabel]);
|
|
129
|
+
|
|
130
|
+
let filteredItems = useMemo(
|
|
131
|
+
() => (groupBy ? groupOptions(filteredOptions, groupBy) : filteredOptions),
|
|
132
|
+
[filteredOptions, groupBy],
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
let selectedOptions = useMemo(() => (multiple ? ((value ?? []) as Option[]) : []), [multiple, value]) as Option[];
|
|
136
|
+
|
|
137
|
+
function handleValueChange(next: any, details: { event?: Event; reason: string }) {
|
|
138
|
+
let deselected = multiple && Array.isArray(next) && next.length < selectedOptions.length;
|
|
139
|
+
let reason = deselected ? "removeOption" : mapChangeReason(details.reason, next);
|
|
140
|
+
|
|
141
|
+
setValue(next as AutocompleteValue<Option, Multiple>);
|
|
142
|
+
onChange?.(details.event, next as AutocompleteValue<Option, Multiple>, reason);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function handleRemoveChip(option: Option) {
|
|
146
|
+
let next = selectedOptions.filter((item) => !areOptionsEqual(item, option, isOptionEqualToValue));
|
|
147
|
+
setValue(next as AutocompleteValue<Option, Multiple>);
|
|
148
|
+
onChange?.(undefined, next as AutocompleteValue<Option, Multiple>, "removeOption");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let chips =
|
|
152
|
+
multiple && selectedOptions.length > 0
|
|
153
|
+
? selectedOptions.map((option) => (
|
|
154
|
+
<ChipRemovable
|
|
155
|
+
key={getOptionKey?.(option) ?? getOptionLabel(option)}
|
|
156
|
+
data-size={size}
|
|
157
|
+
disabled={disabled || readOnly}
|
|
158
|
+
onClick={() => handleRemoveChip(option)}
|
|
159
|
+
>
|
|
160
|
+
{renderChipLabel?.(option) ?? getOptionLabel(option)}
|
|
161
|
+
</ChipRemovable>
|
|
162
|
+
))
|
|
163
|
+
: undefined;
|
|
164
|
+
|
|
165
|
+
function renderItem(option: Option, index: number) {
|
|
166
|
+
let selected = multiple
|
|
167
|
+
? selectedOptions.some((item) => areOptionsEqual(item, option, isOptionEqualToValue))
|
|
168
|
+
: value != null && areOptionsEqual(value as Option, option, isOptionEqualToValue);
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<BaseCombobox.Item
|
|
172
|
+
key={getOptionKey?.(option) ?? getOptionLabel(option)}
|
|
173
|
+
className="ObAutocomplete-item"
|
|
174
|
+
value={option}
|
|
175
|
+
index={index}
|
|
176
|
+
disabled={getOptionDisabled?.(option)}
|
|
177
|
+
>
|
|
178
|
+
{multiple && (
|
|
179
|
+
<BaseCombobox.ItemIndicator className="ObAutocomplete-itemIndicator">✓</BaseCombobox.ItemIndicator>
|
|
180
|
+
)}
|
|
181
|
+
<span>{renderOption?.(option, { index, inputValue, selected }) ?? getOptionLabel(option)}</span>
|
|
182
|
+
</BaseCombobox.Item>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function renderListItem(item: any, index: number) {
|
|
187
|
+
if (!groupBy) return renderItem(item as Option, index);
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<BaseCombobox.Group key={item.value} items={item.items}>
|
|
191
|
+
<BaseCombobox.GroupLabel className="ObAutocomplete-groupLabel">{item.value}</BaseCombobox.GroupLabel>
|
|
192
|
+
<BaseCombobox.Collection>
|
|
193
|
+
{(option: Option, optionIndex: number) => renderItem(option, optionIndex)}
|
|
194
|
+
</BaseCombobox.Collection>
|
|
195
|
+
</BaseCombobox.Group>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<BaseCombobox.Root
|
|
201
|
+
items={options as any}
|
|
202
|
+
filteredItems={filteredItems as any}
|
|
203
|
+
filter={null}
|
|
204
|
+
multiple={multiple as any}
|
|
205
|
+
value={value as any}
|
|
206
|
+
onValueChange={handleValueChange}
|
|
207
|
+
inputValue={inputValue}
|
|
208
|
+
onInputValueChange={(next, details) => handleInputValueChange(next, details, setInputValue, onInputChange)}
|
|
209
|
+
itemToStringLabel={getOptionLabel as (option: unknown) => string}
|
|
210
|
+
isItemEqualToValue={isOptionEqualToValue as ((option: unknown, value: unknown) => boolean) | undefined}
|
|
211
|
+
disabled={disabled}
|
|
212
|
+
readOnly={readOnly}
|
|
213
|
+
required={required}
|
|
214
|
+
name={name}
|
|
215
|
+
id={id}
|
|
216
|
+
open={open}
|
|
217
|
+
defaultOpen={defaultOpen}
|
|
218
|
+
onOpenChange={(nextOpen) => onOpenChange?.(nextOpen)}
|
|
219
|
+
openOnInputClick={openOnFocus}
|
|
220
|
+
autoHighlight={autoHighlight}
|
|
221
|
+
limit={limit}
|
|
222
|
+
modal={modal}
|
|
223
|
+
>
|
|
224
|
+
<AutocompleteFrame
|
|
225
|
+
id={id}
|
|
226
|
+
label={props.label}
|
|
227
|
+
description={props.description}
|
|
228
|
+
error={props.error}
|
|
229
|
+
className={className}
|
|
230
|
+
style={style}
|
|
231
|
+
controlClassName={controlClassName}
|
|
232
|
+
popupClassName={popupClassName}
|
|
233
|
+
fullWidth={fullWidth}
|
|
234
|
+
size={size}
|
|
235
|
+
color={color}
|
|
236
|
+
disabled={disabled}
|
|
237
|
+
readOnly={readOnly}
|
|
238
|
+
required={required}
|
|
239
|
+
placeholder={placeholder}
|
|
240
|
+
loading={loading}
|
|
241
|
+
disableClearable={disableClearable}
|
|
242
|
+
inputRef={inputRef}
|
|
243
|
+
chips={chips}
|
|
244
|
+
trigger={
|
|
245
|
+
<BaseCombobox.Trigger
|
|
246
|
+
className="ObAutocomplete-button ObAutocomplete-trigger"
|
|
247
|
+
disabled={disabled || readOnly}
|
|
248
|
+
aria-label={getLocalizedString("Open")}
|
|
249
|
+
>
|
|
250
|
+
<ChevronDownIcon aria-hidden fontSize="1.25em" />
|
|
251
|
+
</BaseCombobox.Trigger>
|
|
252
|
+
}
|
|
253
|
+
emptyContent={
|
|
254
|
+
loading ? (loadingText ?? getLocalizedString("Loading")) : (noOptionsText ?? getLocalizedString("No options"))
|
|
255
|
+
}
|
|
256
|
+
renderListItem={renderListItem}
|
|
257
|
+
/>
|
|
258
|
+
</BaseCombobox.Root>
|
|
259
|
+
);
|
|
260
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { ReactNode, Ref } from "react";
|
|
2
|
+
|
|
3
|
+
export type AutocompleteSize = "sm" | "md" | "lg";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolved type of the `value`/`onChange` payload, mirroring MUI's
|
|
7
|
+
* `AutocompleteValue`.
|
|
8
|
+
*/
|
|
9
|
+
export type AutocompleteValue<
|
|
10
|
+
Option,
|
|
11
|
+
Multiple extends boolean | undefined,
|
|
12
|
+
FreeSolo extends boolean | undefined = false,
|
|
13
|
+
> = Multiple extends true
|
|
14
|
+
? Array<FreeSolo extends true ? Option | string : Option>
|
|
15
|
+
: (FreeSolo extends true ? Option | string : Option) | null;
|
|
16
|
+
|
|
17
|
+
export type AutocompleteChangeReason = "selectOption" | "removeOption" | "createOption" | "clear" | "blur";
|
|
18
|
+
|
|
19
|
+
export type AutocompleteInputChangeReason = "input" | "reset" | "clear";
|
|
20
|
+
|
|
21
|
+
export interface AutocompleteRenderOptionState {
|
|
22
|
+
/**
|
|
23
|
+
* Index of the option in the filtered list.
|
|
24
|
+
*/
|
|
25
|
+
index: number;
|
|
26
|
+
/**
|
|
27
|
+
* Current text in the input.
|
|
28
|
+
*/
|
|
29
|
+
inputValue: string;
|
|
30
|
+
/**
|
|
31
|
+
* Whether the option is part of the current value.
|
|
32
|
+
*/
|
|
33
|
+
selected: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface AutocompleteFilterOptionsState<Option> {
|
|
37
|
+
inputValue: string;
|
|
38
|
+
getOptionLabel: (option: Option) => string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Props shared by `Autocomplete` and `Combobox`. Modelled on the MUI
|
|
43
|
+
* `Autocomplete` API, with Designsystemet field props (`label`,
|
|
44
|
+
* `description`, `error`, `data-size`, `data-color`) on top.
|
|
45
|
+
*/
|
|
46
|
+
export interface AutocompleteBaseProps<Option, Multiple extends boolean | undefined> {
|
|
47
|
+
/**
|
|
48
|
+
* The options to choose from.
|
|
49
|
+
*/
|
|
50
|
+
options: readonly Option[];
|
|
51
|
+
/**
|
|
52
|
+
* Whether several options can be selected. Selected options are rendered
|
|
53
|
+
* as chips above the input.
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
multiple?: Multiple;
|
|
57
|
+
/**
|
|
58
|
+
* Text rendered in the input when it is empty.
|
|
59
|
+
*/
|
|
60
|
+
placeholder?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Label for the field.
|
|
63
|
+
*/
|
|
64
|
+
label?: ReactNode;
|
|
65
|
+
/**
|
|
66
|
+
* Description rendered below the label.
|
|
67
|
+
*/
|
|
68
|
+
description?: ReactNode;
|
|
69
|
+
/**
|
|
70
|
+
* Validation message rendered below the input. Also puts the field in an
|
|
71
|
+
* invalid state.
|
|
72
|
+
*/
|
|
73
|
+
error?: ReactNode;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the string used as the label of an option, both in the list and
|
|
76
|
+
* in the input.
|
|
77
|
+
* @default (option) => option.label ?? String(option)
|
|
78
|
+
*/
|
|
79
|
+
getOptionLabel?: (option: Option) => string;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the React key for an option.
|
|
82
|
+
* @default getOptionLabel
|
|
83
|
+
*/
|
|
84
|
+
getOptionKey?: (option: Option) => React.Key;
|
|
85
|
+
/**
|
|
86
|
+
* Returns whether an option should be unselectable.
|
|
87
|
+
*/
|
|
88
|
+
getOptionDisabled?: (option: Option) => boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Compares an option to a value. Required when the value is not
|
|
91
|
+
* referentially equal to the option.
|
|
92
|
+
* @default Object.is
|
|
93
|
+
*/
|
|
94
|
+
isOptionEqualToValue?: (option: Option, value: Option) => boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Groups the options under headers. Options must already be sorted by
|
|
97
|
+
* group, the same way MUI's `groupBy` works.
|
|
98
|
+
*/
|
|
99
|
+
groupBy?: (option: Option) => string;
|
|
100
|
+
/**
|
|
101
|
+
* Filters the options against the current input value. Use
|
|
102
|
+
* `createFilterOptions` to tweak the default behaviour.
|
|
103
|
+
*/
|
|
104
|
+
filterOptions?: (options: readonly Option[], state: AutocompleteFilterOptionsState<Option>) => Option[];
|
|
105
|
+
/**
|
|
106
|
+
* Renders the content of an option in the list.
|
|
107
|
+
* @default getOptionLabel
|
|
108
|
+
*/
|
|
109
|
+
renderOption?: (option: Option, state: AutocompleteRenderOptionState) => ReactNode;
|
|
110
|
+
/**
|
|
111
|
+
* Renders the label of a selected chip when `multiple` is set.
|
|
112
|
+
* @default getOptionLabel
|
|
113
|
+
*/
|
|
114
|
+
renderChipLabel?: (option: Option) => ReactNode;
|
|
115
|
+
/**
|
|
116
|
+
* Rendered in the popup when there are no options to show.
|
|
117
|
+
*/
|
|
118
|
+
noOptionsText?: ReactNode;
|
|
119
|
+
/**
|
|
120
|
+
* Rendered in the popup instead of `noOptionsText` while `loading` is set
|
|
121
|
+
* and there are no options.
|
|
122
|
+
*/
|
|
123
|
+
loadingText?: ReactNode;
|
|
124
|
+
/**
|
|
125
|
+
* Shows a loading indicator in the input.
|
|
126
|
+
* @default false
|
|
127
|
+
*/
|
|
128
|
+
loading?: boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Hides the clear button.
|
|
131
|
+
* @default false
|
|
132
|
+
*/
|
|
133
|
+
disableClearable?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* The maximum number of options rendered in the list. `-1` renders all.
|
|
136
|
+
* @default -1
|
|
137
|
+
*/
|
|
138
|
+
limit?: number;
|
|
139
|
+
/**
|
|
140
|
+
* Highlights the first option as the user types.
|
|
141
|
+
* @default false
|
|
142
|
+
*/
|
|
143
|
+
autoHighlight?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Opens the popup when the input is clicked.
|
|
146
|
+
* @default true
|
|
147
|
+
*/
|
|
148
|
+
openOnFocus?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Makes the control fill the width of its container.
|
|
151
|
+
* @default true
|
|
152
|
+
*/
|
|
153
|
+
fullWidth?: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* The input value. Use together with `onInputChange` for a controlled input.
|
|
156
|
+
*/
|
|
157
|
+
inputValue?: string;
|
|
158
|
+
/**
|
|
159
|
+
* Called when the text in the input changes.
|
|
160
|
+
*/
|
|
161
|
+
onInputChange?: (event: Event | undefined, value: string, reason: AutocompleteInputChangeReason) => void;
|
|
162
|
+
/**
|
|
163
|
+
* Whether the popup is open. Use together with `onOpenChange`.
|
|
164
|
+
*/
|
|
165
|
+
open?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Whether the popup is open initially when uncontrolled.
|
|
168
|
+
*/
|
|
169
|
+
defaultOpen?: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Called when the popup opens or closes.
|
|
172
|
+
*/
|
|
173
|
+
onOpenChange?: (open: boolean) => void;
|
|
174
|
+
/**
|
|
175
|
+
* Traps interaction inside the popup while it is open.
|
|
176
|
+
* @default false
|
|
177
|
+
*/
|
|
178
|
+
modal?: boolean;
|
|
179
|
+
disabled?: boolean;
|
|
180
|
+
readOnly?: boolean;
|
|
181
|
+
required?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Name used when the field is submitted as part of a form.
|
|
184
|
+
*/
|
|
185
|
+
name?: string;
|
|
186
|
+
id?: string;
|
|
187
|
+
className?: string;
|
|
188
|
+
style?: React.CSSProperties;
|
|
189
|
+
/**
|
|
190
|
+
* Classes for the bordered control that wraps the chips and the input.
|
|
191
|
+
*/
|
|
192
|
+
controlClassName?: string;
|
|
193
|
+
/**
|
|
194
|
+
* Classes for the popup element.
|
|
195
|
+
*/
|
|
196
|
+
popupClassName?: string;
|
|
197
|
+
inputRef?: Ref<HTMLInputElement>;
|
|
198
|
+
"data-size"?: AutocompleteSize;
|
|
199
|
+
"data-color"?: string;
|
|
200
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AutocompleteChangeReason,
|
|
5
|
+
AutocompleteFilterOptionsState,
|
|
6
|
+
AutocompleteInputChangeReason,
|
|
7
|
+
} from "./types.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Default label resolver: uses `label`, then `name`, then the value itself.
|
|
11
|
+
*/
|
|
12
|
+
export function defaultGetOptionLabel(option: unknown): string {
|
|
13
|
+
if (option == null) return "";
|
|
14
|
+
if (typeof option === "string") return option;
|
|
15
|
+
if (typeof option === "object") {
|
|
16
|
+
let record = option as Record<string, unknown>;
|
|
17
|
+
if (typeof record.label === "string") return record.label;
|
|
18
|
+
if (typeof record.name === "string") return record.name;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return String(option);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CreateFilterOptionsConfig<Option> {
|
|
25
|
+
/**
|
|
26
|
+
* Removes diacritics before matching.
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
ignoreAccents?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Matches case insensitively.
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
ignoreCase?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Where in the option label the query has to match.
|
|
37
|
+
* @default "any"
|
|
38
|
+
*/
|
|
39
|
+
matchFrom?: "any" | "start";
|
|
40
|
+
/**
|
|
41
|
+
* The maximum number of options returned.
|
|
42
|
+
*/
|
|
43
|
+
limit?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Trims the query before matching.
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
trim?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the string the query is matched against.
|
|
51
|
+
*/
|
|
52
|
+
stringify?: (option: Option) => string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Creates a filter function for the `filterOptions` prop, equivalent to MUI's
|
|
57
|
+
* `createFilterOptions`.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```jsx
|
|
61
|
+
* let filterOptions = createFilterOptions({ matchFrom: "start", limit: 20 });
|
|
62
|
+
* <Combobox options={options} filterOptions={filterOptions} />
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function createFilterOptions<Option>(config: CreateFilterOptionsConfig<Option> = {}) {
|
|
66
|
+
let { ignoreAccents = true, ignoreCase = true, limit, matchFrom = "any", stringify, trim = false } = config;
|
|
67
|
+
|
|
68
|
+
return (options: readonly Option[], state: AutocompleteFilterOptionsState<Option>) => {
|
|
69
|
+
let input = trim ? state.inputValue.trim() : state.inputValue;
|
|
70
|
+
if (ignoreCase) input = input.toLowerCase();
|
|
71
|
+
if (ignoreAccents) input = stripDiacritics(input);
|
|
72
|
+
|
|
73
|
+
let filtered = input
|
|
74
|
+
? options.filter((option) => {
|
|
75
|
+
let candidate = (stringify ?? state.getOptionLabel)(option);
|
|
76
|
+
if (ignoreCase) candidate = candidate.toLowerCase();
|
|
77
|
+
if (ignoreAccents) candidate = stripDiacritics(candidate);
|
|
78
|
+
|
|
79
|
+
return matchFrom === "start" ? candidate.startsWith(input) : candidate.includes(input);
|
|
80
|
+
})
|
|
81
|
+
: [...options];
|
|
82
|
+
|
|
83
|
+
return typeof limit === "number" ? filtered.slice(0, limit) : filtered;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function stripDiacritics(value: string) {
|
|
88
|
+
return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const defaultFilterOptions = createFilterOptions<unknown>();
|
|
92
|
+
|
|
93
|
+
export interface OptionGroup<Option> {
|
|
94
|
+
/**
|
|
95
|
+
* Group header text. Named `value` because Base UI uses it as the group key.
|
|
96
|
+
*/
|
|
97
|
+
value: string;
|
|
98
|
+
items: Option[];
|
|
99
|
+
[key: string]: unknown;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Splits an already sorted list of options into consecutive groups, the same
|
|
104
|
+
* way MUI's `groupBy` works.
|
|
105
|
+
*/
|
|
106
|
+
export function groupOptions<Option>(options: readonly Option[], groupBy: (option: Option) => string) {
|
|
107
|
+
let groups: OptionGroup<Option>[] = [];
|
|
108
|
+
|
|
109
|
+
for (let option of options) {
|
|
110
|
+
let group = groupBy(option);
|
|
111
|
+
let last = groups[groups.length - 1];
|
|
112
|
+
|
|
113
|
+
if (last && last.value === group) {
|
|
114
|
+
last.items.push(option);
|
|
115
|
+
} else {
|
|
116
|
+
groups.push({ value: group, items: [option] });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return groups;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* State that is controlled when `controlled` is not `undefined`, and
|
|
125
|
+
* uncontrolled otherwise.
|
|
126
|
+
*/
|
|
127
|
+
export function useControlled<T>(controlled: T | undefined, defaultValue: T) {
|
|
128
|
+
let [uncontrolled, setUncontrolled] = useState(defaultValue);
|
|
129
|
+
let isControlled = useRef(controlled !== undefined).current;
|
|
130
|
+
let value = isControlled ? (controlled as T) : uncontrolled;
|
|
131
|
+
|
|
132
|
+
let setValue = useCallback(
|
|
133
|
+
(next: T) => {
|
|
134
|
+
if (!isControlled) setUncontrolled(next);
|
|
135
|
+
},
|
|
136
|
+
[isControlled],
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return [value, setValue] as const;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Compares two options, falling back to reference equality like MUI does.
|
|
144
|
+
*/
|
|
145
|
+
export function areOptionsEqual<Option>(
|
|
146
|
+
a: Option,
|
|
147
|
+
b: Option,
|
|
148
|
+
isOptionEqualToValue?: (option: Option, value: Option) => boolean,
|
|
149
|
+
) {
|
|
150
|
+
return isOptionEqualToValue ? isOptionEqualToValue(a, b) : Object.is(a, b);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Maps a Base UI change reason to the MUI-style reason.
|
|
155
|
+
*/
|
|
156
|
+
export function mapChangeReason(reason: string, next: unknown): AutocompleteChangeReason {
|
|
157
|
+
if (reason === "clear-press" || reason === "input-clear") return "clear";
|
|
158
|
+
if (reason === "chip-remove-press") return "removeOption";
|
|
159
|
+
if (next == null || (Array.isArray(next) && next.length === 0)) return "clear";
|
|
160
|
+
|
|
161
|
+
return "selectOption";
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function mapInputChangeReason(reason: string): AutocompleteInputChangeReason {
|
|
165
|
+
if (reason === "input-change") return "input";
|
|
166
|
+
if (reason === "input-clear" || reason === "clear-press") return "clear";
|
|
167
|
+
|
|
168
|
+
return "reset";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Keeps the input value state in sync and forwards the change to
|
|
173
|
+
* `onInputChange` with a MUI-style reason.
|
|
174
|
+
*/
|
|
175
|
+
export function handleInputValueChange(
|
|
176
|
+
next: string,
|
|
177
|
+
details: { event?: Event; reason: string },
|
|
178
|
+
setInputValue: (value: string) => void,
|
|
179
|
+
onInputChange?: (event: Event | undefined, value: string, reason: AutocompleteInputChangeReason) => void,
|
|
180
|
+
) {
|
|
181
|
+
setInputValue(next);
|
|
182
|
+
onInputChange?.(details.event, next, mapInputChangeReason(details.reason));
|
|
183
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "./AfLookup/index.js";
|
|
2
|
+
export * from "./autocomplete/index.js";
|
|
2
3
|
export * from "./binding/index.js";
|
|
3
4
|
export * from "./input/index.js";
|
|
4
5
|
export * from "./layout/index.js";
|
|
@@ -6,3 +7,4 @@ export * from "./list/index.js";
|
|
|
6
7
|
export * from "./page/index.js";
|
|
7
8
|
export * from "./paper/index.js";
|
|
8
9
|
export * from "./progress/index.js";
|
|
10
|
+
export * from "./sortable/index.js";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--ob-drop-indicator-thickness: 0.125rem;
|
|
3
|
+
--ob-drop-indicator-color: #3f3fa3;
|
|
4
|
+
--ob-drop-indicator-terminal-size: 0.75rem;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.ObDropIndicator-root {
|
|
8
|
+
position: absolute;
|
|
9
|
+
z-index: 10;
|
|
10
|
+
|
|
11
|
+
display: flex;
|
|
12
|
+
align-items: center;
|
|
13
|
+
|
|
14
|
+
pointer-events: none;
|
|
15
|
+
|
|
16
|
+
&::before {
|
|
17
|
+
content: "";
|
|
18
|
+
|
|
19
|
+
border: var(--ob-drop-indicator-thickness) solid var(--ob-drop-indicator-color);
|
|
20
|
+
border-radius: 50%;
|
|
21
|
+
|
|
22
|
+
width: var(--ob-drop-indicator-terminal-size);
|
|
23
|
+
height: var(--ob-drop-indicator-terminal-size);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
&::after {
|
|
27
|
+
content: "";
|
|
28
|
+
|
|
29
|
+
height: var(--ob-drop-indicator-thickness);
|
|
30
|
+
flex-grow: 1;
|
|
31
|
+
background-color: var(--ob-drop-indicator-color);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
&.ObDropIndicator-top,
|
|
35
|
+
&.ObDropIndicator-bottom {
|
|
36
|
+
height: var(--ob-drop-indicator-thickness);
|
|
37
|
+
|
|
38
|
+
left: calc(-1 * var(--ob-drop-indicator-terminal-size) / 2);
|
|
39
|
+
width: calc(100% + var(--ob-drop-indicator-terminal-size) / 2);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
&.ObDropIndicator-top {
|
|
43
|
+
top: var(--ob-drop-indicator-gap, 0rem);
|
|
44
|
+
transform: translateY(calc(var(--ob-drop-indicator-thickness) / -2));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
&.ObDropIndicator-bottom {
|
|
48
|
+
bottom: var(--ob-drop-indicator-gap, 0rem);
|
|
49
|
+
transform: translateY(calc(var(--ob-drop-indicator-thickness) / 2));
|
|
50
|
+
}
|
|
51
|
+
}
|