@weni/unnnic-system 3.36.0 → 3.36.1-alpha.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/dist/components/MultiSelect/MultiSelectOption.css +1 -1
- package/dist/components/MultiSelect/MultiSelectOption.vue.mjs +2 -2
- package/dist/components/MultiSelect/MultiSelectOption.vue2.mjs +41 -15
- package/dist/components/MultiSelect/MultiSelectTreeOptions.css +1 -0
- package/dist/components/MultiSelect/MultiSelectTreeOptions.vue.mjs +7 -0
- package/dist/components/MultiSelect/MultiSelectTreeOptions.vue2.mjs +50 -0
- package/dist/components/MultiSelect/index.css +1 -1
- package/dist/components/MultiSelect/index.vue.mjs +2 -2
- package/dist/components/MultiSelect/index.vue2.mjs +126 -114
- package/dist/composables/useMultiSelectRows.mjs +74 -0
- package/dist/composables/useMultiSelectSelection.mjs +44 -0
- package/dist/composables/useSelectBase.mjs +44 -39
- package/dist/composables/useSelectKeyboard.mjs +70 -35
- package/dist/composables/useTreeExpansion.mjs +24 -0
- package/dist/lib/tree-selection.mjs +66 -0
- package/dist/locales/en.json.mjs +9 -5
- package/dist/style.css +4 -2
- package/dist/unnnic.umd.js +19 -19
- package/package.json +2 -2
- package/src/components/MultiSelect/MultiSelectOption.vue +66 -3
- package/src/components/MultiSelect/MultiSelectTreeOptions.vue +73 -0
- package/src/components/MultiSelect/__tests__/MultiSelect.spec.js +556 -0
- package/src/components/MultiSelect/__tests__/MultiSelectOption.spec.js +55 -1
- package/src/components/MultiSelect/__tests__/__snapshots__/MultiSelectOption.spec.js.snap +5 -0
- package/src/components/MultiSelect/index.vue +57 -50
- package/src/components/Select/_select-shared.scss +8 -0
- package/src/components/Select/types.ts +1 -0
- package/src/composables/useMultiSelectRows.ts +121 -0
- package/src/composables/useMultiSelectSelection.ts +97 -0
- package/src/composables/useSelectBase.ts +40 -16
- package/src/composables/useSelectKeyboard.ts +92 -18
- package/src/composables/useTreeExpansion.ts +44 -0
- package/src/lib/__tests__/tree-selection.spec.ts +141 -0
- package/src/lib/tree-selection.ts +156 -0
- package/src/locales/en.json +3 -1
- package/src/stories/MultiSelect.stories.js +62 -1
|
@@ -48,16 +48,28 @@
|
|
|
48
48
|
{{ $t('without_results') }}
|
|
49
49
|
</p>
|
|
50
50
|
<template v-else>
|
|
51
|
-
<
|
|
51
|
+
<UnnnicMultiSelectTreeOptions
|
|
52
|
+
v-if="isTreeMode"
|
|
53
|
+
:groups="optionGroups"
|
|
54
|
+
:itemLabel="props.itemLabel"
|
|
55
|
+
:itemValue="props.itemValue"
|
|
56
|
+
:focusedIndex="focusedOptionIndex"
|
|
57
|
+
@select="toggleOption"
|
|
58
|
+
@expand="toggleExpanded"
|
|
59
|
+
/>
|
|
60
|
+
<div
|
|
61
|
+
v-else
|
|
62
|
+
class="unnnic-multi-select__options"
|
|
63
|
+
>
|
|
52
64
|
<UnnnicMultiSelectOption
|
|
53
|
-
v-for="
|
|
54
|
-
:key="String(option[props.itemValue])"
|
|
55
|
-
:data-option-index="index"
|
|
56
|
-
:label="String(option[props.itemLabel] ?? '')"
|
|
57
|
-
:active="
|
|
58
|
-
:focused="focusedOptionIndex === index"
|
|
59
|
-
:disabled="option.disabled"
|
|
60
|
-
@update:model-value="
|
|
65
|
+
v-for="row in visibleRows"
|
|
66
|
+
:key="String(row.option[props.itemValue])"
|
|
67
|
+
:data-option-index="row.index"
|
|
68
|
+
:label="String(row.option[props.itemLabel] ?? '')"
|
|
69
|
+
:active="row.checkState"
|
|
70
|
+
:focused="focusedOptionIndex === row.index"
|
|
71
|
+
:disabled="row.option.disabled"
|
|
72
|
+
@update:model-value="toggleOption(row.option, $event)"
|
|
61
73
|
/>
|
|
62
74
|
</div>
|
|
63
75
|
<div
|
|
@@ -85,9 +97,13 @@ import { Popover, PopoverTrigger, PopoverContent } from '../ui/popover';
|
|
|
85
97
|
import UnnnicInput from '../Input/Input.vue';
|
|
86
98
|
import UnnnicIconLoading from '../IconLoading/IconLoading.vue';
|
|
87
99
|
import UnnnicMultiSelectOption from './MultiSelectOption.vue';
|
|
100
|
+
import UnnnicMultiSelectTreeOptions from './MultiSelectTreeOptions.vue';
|
|
88
101
|
|
|
89
102
|
import { useSelectBase } from '../../composables/useSelectBase';
|
|
90
103
|
import { useSelectKeyboard } from '../../composables/useSelectKeyboard';
|
|
104
|
+
import { useMultiSelectSelection } from '../../composables/useMultiSelectSelection';
|
|
105
|
+
import { useMultiSelectRows } from '../../composables/useMultiSelectRows';
|
|
106
|
+
import { filterTreeOptions } from '../../lib/tree-selection';
|
|
91
107
|
import type { SelectBaseProps, SelectOption } from '../Select/types';
|
|
92
108
|
|
|
93
109
|
defineOptions({
|
|
@@ -131,26 +147,41 @@ const emit = defineEmits<{
|
|
|
131
147
|
const multiSelectInputRef = useTemplateRef<HTMLElement>('multiSelectInputRef');
|
|
132
148
|
const contentRef = useTemplateRef<HTMLDivElement>('contentRef');
|
|
133
149
|
|
|
134
|
-
const base = useSelectBase(props, 'multi', multiSelectInputRef, contentRef
|
|
150
|
+
const base = useSelectBase(props, 'multi', multiSelectInputRef, contentRef, {
|
|
151
|
+
filterOptions: filterTreeOptions,
|
|
152
|
+
});
|
|
135
153
|
|
|
136
154
|
const openPopover = base.openPopover;
|
|
137
155
|
const popoverContentCustomStyles = base.popoverContentCustomStyles;
|
|
138
156
|
const inputWidthString = base.inputWidthString;
|
|
139
157
|
const filteredOptions = base.filteredOptions;
|
|
140
158
|
|
|
159
|
+
const {
|
|
160
|
+
isTreeMode,
|
|
161
|
+
getCheckState,
|
|
162
|
+
toggleOption,
|
|
163
|
+
toggle,
|
|
164
|
+
displayItems: selectedItems,
|
|
165
|
+
} = useMultiSelectSelection(props, emit);
|
|
166
|
+
|
|
167
|
+
const {
|
|
168
|
+
optionGroups,
|
|
169
|
+
visibleRows,
|
|
170
|
+
toggleExpanded,
|
|
171
|
+
resetExpanded,
|
|
172
|
+
handleArrowRight,
|
|
173
|
+
handleArrowLeft,
|
|
174
|
+
} = useMultiSelectRows(props, {
|
|
175
|
+
filteredOptions: base.filteredOptions,
|
|
176
|
+
isSearchActive: base.isSearchActive,
|
|
177
|
+
matchesSearch: base.matchesSearch,
|
|
178
|
+
getCheckState,
|
|
179
|
+
});
|
|
180
|
+
|
|
141
181
|
function setOpenPopover(value: boolean) {
|
|
142
182
|
base.openPopover.value = value;
|
|
143
183
|
}
|
|
144
184
|
|
|
145
|
-
const selectedItems = computed<SelectOption[]>(() => {
|
|
146
|
-
if (props.returnObject) return props.modelValue as SelectOption[];
|
|
147
|
-
|
|
148
|
-
const valueIds = props.modelValue.slice();
|
|
149
|
-
return props.options.filter((option) =>
|
|
150
|
-
valueIds.includes(option[props.itemValue]),
|
|
151
|
-
);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
185
|
const inputValue = computed(() =>
|
|
155
186
|
selectedItems.value.map((item) => item[props.itemLabel]).join(', '),
|
|
156
187
|
);
|
|
@@ -159,42 +190,16 @@ function handleSearch(value: string) {
|
|
|
159
190
|
emit('update:search', value);
|
|
160
191
|
}
|
|
161
192
|
|
|
162
|
-
function getActivatedOptionStatus(option: SelectOption) {
|
|
163
|
-
if (props.returnObject) {
|
|
164
|
-
return (props.modelValue as SelectOption[]).some(
|
|
165
|
-
(item) => item[props.itemValue] === option[props.itemValue],
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
return props.modelValue.includes(option[props.itemValue]);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function handleSelectOption(option: SelectOption, selected: boolean) {
|
|
172
|
-
if (selected) {
|
|
173
|
-
const value = props.returnObject
|
|
174
|
-
? [...props.modelValue, option]
|
|
175
|
-
: [...props.modelValue, option[props.itemValue]];
|
|
176
|
-
emit('update:modelValue', value);
|
|
177
|
-
} else {
|
|
178
|
-
const value = props.returnObject
|
|
179
|
-
? (props.modelValue as SelectOption[]).filter(
|
|
180
|
-
(item) => item[props.itemValue] !== option[props.itemValue],
|
|
181
|
-
)
|
|
182
|
-
: props.modelValue.filter((item) => item !== option[props.itemValue]);
|
|
183
|
-
emit('update:modelValue', value);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
193
|
const keyboard = useSelectKeyboard<SelectOption>(
|
|
188
|
-
() =>
|
|
189
|
-
|
|
190
|
-
if (option.disabled) return;
|
|
191
|
-
const active = getActivatedOptionStatus(option);
|
|
192
|
-
handleSelectOption(option, !active);
|
|
193
|
-
},
|
|
194
|
+
() => visibleRows.value.map((row) => row.option),
|
|
195
|
+
toggle,
|
|
194
196
|
{
|
|
195
197
|
closeOnSelect: false,
|
|
196
198
|
closePopover: () => setOpenPopover(false),
|
|
197
199
|
openPopoverRef: base.openPopover,
|
|
200
|
+
toggleOnSpace: isTreeMode,
|
|
201
|
+
onArrowRight: handleArrowRight,
|
|
202
|
+
onArrowLeft: handleArrowLeft,
|
|
198
203
|
},
|
|
199
204
|
);
|
|
200
205
|
|
|
@@ -241,6 +246,7 @@ watch(base.openPopover, (isOpen) => {
|
|
|
241
246
|
if (!isOpen) {
|
|
242
247
|
handleSearch('');
|
|
243
248
|
infiniteScrollLoading.value = false;
|
|
249
|
+
resetExpanded();
|
|
244
250
|
return;
|
|
245
251
|
}
|
|
246
252
|
|
|
@@ -281,6 +287,7 @@ defineExpose({
|
|
|
281
287
|
|
|
282
288
|
:deep(.unnnic-multi-select__input) {
|
|
283
289
|
@include shared.select-input-trigger;
|
|
290
|
+
@include shared.select-input-ellipsis;
|
|
284
291
|
}
|
|
285
292
|
|
|
286
293
|
:deep(.unnnic-multi-select__input-search) {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rows MultiSelect renders and navigates: one group per root, followed by its
|
|
3
|
+
* children while expanded. A flat list produces one leaf row per option.
|
|
4
|
+
*/
|
|
5
|
+
import { computed, toValue, type MaybeRefOrGetter } from 'vue';
|
|
6
|
+
import type { CheckboxValue } from '../components/Checkbox/Checkbox.vue';
|
|
7
|
+
import type { SelectOption } from '../components/Select/types';
|
|
8
|
+
import { getVisibleChildren, type OptionMatcher } from '../lib/tree-selection';
|
|
9
|
+
import { useTreeExpansion } from './useTreeExpansion';
|
|
10
|
+
|
|
11
|
+
export interface OptionRow {
|
|
12
|
+
option: SelectOption;
|
|
13
|
+
parent: SelectOption | null;
|
|
14
|
+
level: 1 | 2;
|
|
15
|
+
hasChildren: boolean;
|
|
16
|
+
isExpanded: boolean;
|
|
17
|
+
checkState: CheckboxValue;
|
|
18
|
+
index: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UseMultiSelectRowsProps {
|
|
22
|
+
itemValue: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UseMultiSelectRowsSources {
|
|
26
|
+
filteredOptions: MaybeRefOrGetter<SelectOption[]>;
|
|
27
|
+
isSearchActive: MaybeRefOrGetter<boolean>;
|
|
28
|
+
matchesSearch: OptionMatcher;
|
|
29
|
+
getCheckState: (option: SelectOption) => CheckboxValue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useMultiSelectRows(
|
|
33
|
+
props: UseMultiSelectRowsProps,
|
|
34
|
+
{
|
|
35
|
+
filteredOptions,
|
|
36
|
+
isSearchActive,
|
|
37
|
+
matchesSearch,
|
|
38
|
+
getCheckState,
|
|
39
|
+
}: UseMultiSelectRowsSources,
|
|
40
|
+
) {
|
|
41
|
+
const expansion = useTreeExpansion(isSearchActive);
|
|
42
|
+
|
|
43
|
+
function getValue(option: SelectOption) {
|
|
44
|
+
return option[props.itemValue];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const optionGroups = computed(() => {
|
|
48
|
+
let index = 0;
|
|
49
|
+
|
|
50
|
+
return toValue(filteredOptions).map((option): OptionRow[] => {
|
|
51
|
+
const children = getVisibleChildren(option, matchesSearch);
|
|
52
|
+
const hasChildren = children.length > 0;
|
|
53
|
+
const isExpanded = hasChildren && expansion.isExpanded(getValue(option));
|
|
54
|
+
|
|
55
|
+
const root: OptionRow = {
|
|
56
|
+
option,
|
|
57
|
+
parent: null,
|
|
58
|
+
level: 1,
|
|
59
|
+
hasChildren,
|
|
60
|
+
isExpanded,
|
|
61
|
+
checkState: getCheckState(option),
|
|
62
|
+
index: index++,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (!isExpanded) return [root];
|
|
66
|
+
|
|
67
|
+
return [
|
|
68
|
+
root,
|
|
69
|
+
...children.map(
|
|
70
|
+
(child): OptionRow => ({
|
|
71
|
+
option: child,
|
|
72
|
+
parent: option,
|
|
73
|
+
level: 2,
|
|
74
|
+
hasChildren: false,
|
|
75
|
+
isExpanded: false,
|
|
76
|
+
checkState: getCheckState(child),
|
|
77
|
+
index: index++,
|
|
78
|
+
}),
|
|
79
|
+
),
|
|
80
|
+
];
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const visibleRows = computed(() => optionGroups.value.flat());
|
|
85
|
+
|
|
86
|
+
function toggleExpanded(option: SelectOption, expanded?: boolean) {
|
|
87
|
+
expansion.setExpanded(getValue(option), expanded);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** ArrowRight: expand a collapsed parent, or move into its first child. */
|
|
91
|
+
function handleArrowRight(index: number): number | void {
|
|
92
|
+
const row = visibleRows.value[index];
|
|
93
|
+
if (!row?.hasChildren) return;
|
|
94
|
+
if (row.isExpanded) return index + 1;
|
|
95
|
+
|
|
96
|
+
toggleExpanded(row.option, true);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** ArrowLeft: collapse an expanded parent, or move from a child to it. */
|
|
100
|
+
function handleArrowLeft(index: number): number | void {
|
|
101
|
+
const row = visibleRows.value[index];
|
|
102
|
+
if (!row) return;
|
|
103
|
+
|
|
104
|
+
if (row.isExpanded) {
|
|
105
|
+
toggleExpanded(row.option, false);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!row.parent) return;
|
|
110
|
+
return visibleRows.value.findIndex((item) => item.option === row.parent);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
optionGroups,
|
|
115
|
+
visibleRows,
|
|
116
|
+
toggleExpanded,
|
|
117
|
+
resetExpanded: expansion.reset,
|
|
118
|
+
handleArrowRight,
|
|
119
|
+
handleArrowLeft,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MultiSelect selection for flat and tree options: adapts the component model
|
|
3
|
+
* (values or objects, per `returnObject`) to the pure rules in tree-selection.
|
|
4
|
+
*/
|
|
5
|
+
import { computed } from 'vue';
|
|
6
|
+
import type { SelectOption } from '../components/Select/types';
|
|
7
|
+
import {
|
|
8
|
+
flattenTreeOptions,
|
|
9
|
+
getCheckState as getOptionCheckState,
|
|
10
|
+
getDisplayOptions,
|
|
11
|
+
isTreeOptions,
|
|
12
|
+
toggleOptionValue,
|
|
13
|
+
withImpliedChildren,
|
|
14
|
+
} from '../lib/tree-selection';
|
|
15
|
+
|
|
16
|
+
type ModelItem = SelectOption | unknown;
|
|
17
|
+
|
|
18
|
+
export interface UseMultiSelectSelectionProps {
|
|
19
|
+
options: SelectOption[];
|
|
20
|
+
modelValue: ModelItem[];
|
|
21
|
+
itemValue: string;
|
|
22
|
+
returnObject: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useMultiSelectSelection(
|
|
26
|
+
props: UseMultiSelectSelectionProps,
|
|
27
|
+
emit: (event: 'update:modelValue', value: ModelItem[]) => void,
|
|
28
|
+
) {
|
|
29
|
+
const isTreeMode = computed(() => isTreeOptions(props.options));
|
|
30
|
+
|
|
31
|
+
function getValue(option: SelectOption) {
|
|
32
|
+
return option[props.itemValue];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A single value is still accepted, as earlier versions tolerated it. */
|
|
36
|
+
const modelItems = computed<ModelItem[]>(() =>
|
|
37
|
+
Array.isArray(props.modelValue) ? props.modelValue : [props.modelValue],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const modelValues = computed(() =>
|
|
41
|
+
props.returnObject
|
|
42
|
+
? (modelItems.value as SelectOption[]).map(getValue)
|
|
43
|
+
: modelItems.value,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const selectedValues = computed(() =>
|
|
47
|
+
withImpliedChildren(props.options, modelValues.value, getValue),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const selectedSet = computed(() => new Set(selectedValues.value));
|
|
51
|
+
|
|
52
|
+
/** Keeps the received objects, so values unknown to `options` survive. */
|
|
53
|
+
function toModelItems(values: unknown[]): ModelItem[] {
|
|
54
|
+
if (!props.returnObject) return values;
|
|
55
|
+
|
|
56
|
+
const items = [
|
|
57
|
+
...flattenTreeOptions(props.options),
|
|
58
|
+
...(modelItems.value as SelectOption[]),
|
|
59
|
+
];
|
|
60
|
+
const itemByValue = new Map(items.map((item) => [getValue(item), item]));
|
|
61
|
+
return values.map((value) => itemByValue.get(value));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getCheckState(option: SelectOption) {
|
|
65
|
+
return getOptionCheckState(option, selectedSet.value, getValue);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function toggleOption(option: SelectOption, checked: boolean) {
|
|
69
|
+
const values = toggleOptionValue(
|
|
70
|
+
props.options,
|
|
71
|
+
selectedValues.value,
|
|
72
|
+
option,
|
|
73
|
+
checked,
|
|
74
|
+
getValue,
|
|
75
|
+
);
|
|
76
|
+
emit('update:modelValue', toModelItems(values));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function toggle(option: SelectOption) {
|
|
80
|
+
if (option.disabled) return;
|
|
81
|
+
toggleOption(option, getCheckState(option) !== true);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Closed-field items. Flat `returnObject` lists show the model as received,
|
|
86
|
+
* so selected objects missing from `options` (e.g. with infinite scroll)
|
|
87
|
+
* still show.
|
|
88
|
+
*/
|
|
89
|
+
const displayItems = computed<SelectOption[]>(() => {
|
|
90
|
+
if (props.returnObject && !isTreeMode.value) {
|
|
91
|
+
return props.modelValue as SelectOption[];
|
|
92
|
+
}
|
|
93
|
+
return getDisplayOptions(props.options, selectedSet.value, getValue);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return { isTreeMode, getCheckState, toggleOption, toggle, displayItems };
|
|
97
|
+
}
|
|
@@ -11,6 +11,16 @@ import type {
|
|
|
11
11
|
|
|
12
12
|
export type SelectVariant = 'single' | 'multi';
|
|
13
13
|
|
|
14
|
+
export type SelectOptionMatcher = (option: SelectOption) => boolean;
|
|
15
|
+
|
|
16
|
+
export interface UseSelectBaseOptions {
|
|
17
|
+
/** Picks the options kept by a search. Defaults to the matching options. */
|
|
18
|
+
filterOptions?: (
|
|
19
|
+
options: SelectOption[],
|
|
20
|
+
matches: SelectOptionMatcher,
|
|
21
|
+
) => SelectOption[];
|
|
22
|
+
}
|
|
23
|
+
|
|
14
24
|
export interface UseSelectBaseProps {
|
|
15
25
|
options: SelectOption[];
|
|
16
26
|
itemLabel: string;
|
|
@@ -33,28 +43,40 @@ export function useSelectBase(
|
|
|
33
43
|
variant: SelectVariant,
|
|
34
44
|
triggerRef: SelectTriggerRef,
|
|
35
45
|
contentRef: SelectContentRef,
|
|
46
|
+
{
|
|
47
|
+
filterOptions = (options, matches) => options.filter(matches),
|
|
48
|
+
}: UseSelectBaseOptions = {},
|
|
36
49
|
) {
|
|
37
50
|
const openPopover = ref(false);
|
|
38
51
|
|
|
39
52
|
const { width: inputWidth } = useElementSize(triggerRef);
|
|
40
53
|
const inputWidthString = computed(() => `${inputWidth.value}px`);
|
|
41
54
|
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
const isSearchActive = computed(
|
|
56
|
+
() => props.enableSearch && Boolean(props.search),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
/** Every option matches while the search is inactive. */
|
|
60
|
+
function matchesSearch(option: SelectOption) {
|
|
61
|
+
if (!isSearchActive.value) return true;
|
|
62
|
+
const searchLower = props.search.toLowerCase();
|
|
63
|
+
|
|
64
|
+
const matchLabel = String(option[props.itemLabel] ?? '')
|
|
65
|
+
.toLowerCase()
|
|
66
|
+
.includes(searchLower);
|
|
67
|
+
|
|
68
|
+
const matchValue = String(option[props.itemValue] ?? '')
|
|
69
|
+
.toLowerCase()
|
|
70
|
+
.includes(searchLower);
|
|
71
|
+
|
|
72
|
+
return matchLabel || matchValue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const filteredOptions = computed(() =>
|
|
76
|
+
isSearchActive.value
|
|
77
|
+
? filterOptions(props.options, matchesSearch)
|
|
78
|
+
: props.options,
|
|
79
|
+
);
|
|
58
80
|
|
|
59
81
|
function calcPopoverHeight(params: SelectPopoverHeightParams): string {
|
|
60
82
|
const {
|
|
@@ -114,6 +136,8 @@ export function useSelectBase(
|
|
|
114
136
|
inputWidthString,
|
|
115
137
|
calculatedPopoverHeight,
|
|
116
138
|
popoverContentCustomStyles,
|
|
139
|
+
isSearchActive,
|
|
140
|
+
matchesSearch,
|
|
117
141
|
filteredOptions,
|
|
118
142
|
};
|
|
119
143
|
}
|
|
@@ -1,24 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared keyboard navigation for Select and MultiSelect
|
|
2
|
+
* Shared keyboard navigation for Select and MultiSelect
|
|
3
|
+
* (ArrowUp, ArrowDown, Enter, optional ArrowRight, ArrowLeft and Space).
|
|
3
4
|
* Binds keydown to document when popover is open and unbinds when closed.
|
|
4
5
|
*/
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
ref,
|
|
8
|
+
nextTick,
|
|
9
|
+
watch,
|
|
10
|
+
onBeforeUnmount,
|
|
11
|
+
toValue,
|
|
12
|
+
type MaybeRefOrGetter,
|
|
13
|
+
type Ref,
|
|
14
|
+
} from 'vue';
|
|
6
15
|
import type { SelectOption } from '../components/Select/types';
|
|
7
16
|
|
|
8
17
|
const DATA_OPTION_INDEX = 'data-option-index';
|
|
9
18
|
|
|
10
|
-
export interface UseSelectKeyboardOptions
|
|
19
|
+
export interface UseSelectKeyboardOptions<
|
|
20
|
+
T extends SelectOption = SelectOption,
|
|
21
|
+
> {
|
|
11
22
|
closeOnSelect?: boolean;
|
|
12
23
|
closePopover: () => void;
|
|
13
24
|
openPopoverRef: Ref<boolean>;
|
|
25
|
+
/** May return the index to focus next. */
|
|
26
|
+
onArrowRight?: (index: number, option: T) => number | void;
|
|
27
|
+
/** May return the index to focus next. */
|
|
28
|
+
onArrowLeft?: (index: number, option: T) => number | void;
|
|
29
|
+
toggleOnSpace?: MaybeRefOrGetter<boolean>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function isEditableTarget(event: KeyboardEvent) {
|
|
33
|
+
const target = event.target;
|
|
34
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
35
|
+
if (target.closest('input, textarea, button, [contenteditable="true"]')) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return target.isContentEditable;
|
|
14
39
|
}
|
|
15
40
|
|
|
16
41
|
export function useSelectKeyboard<T extends SelectOption>(
|
|
17
42
|
getFilteredOptions: () => T[],
|
|
18
43
|
onSelectOption: (option: T) => void,
|
|
19
|
-
options: UseSelectKeyboardOptions
|
|
44
|
+
options: UseSelectKeyboardOptions<T>,
|
|
20
45
|
) {
|
|
21
|
-
const {
|
|
46
|
+
const {
|
|
47
|
+
closeOnSelect = true,
|
|
48
|
+
closePopover,
|
|
49
|
+
openPopoverRef,
|
|
50
|
+
onArrowRight,
|
|
51
|
+
onArrowLeft,
|
|
52
|
+
toggleOnSpace = false,
|
|
53
|
+
} = options;
|
|
22
54
|
const focusedOptionIndex = ref(-1);
|
|
23
55
|
|
|
24
56
|
function scrollToOption(
|
|
@@ -34,30 +66,64 @@ export function useSelectKeyboard<T extends SelectOption>(
|
|
|
34
66
|
});
|
|
35
67
|
}
|
|
36
68
|
|
|
37
|
-
function
|
|
38
|
-
const
|
|
39
|
-
|
|
69
|
+
function selectFocusedOption() {
|
|
70
|
+
const list = getFilteredOptions();
|
|
71
|
+
const option = list[focusedOptionIndex.value];
|
|
72
|
+
if (focusedOptionIndex.value < 0 || !option) return;
|
|
73
|
+
if (option.disabled) return;
|
|
74
|
+
onSelectOption(option);
|
|
75
|
+
if (closeOnSelect) closePopover();
|
|
76
|
+
}
|
|
40
77
|
|
|
41
|
-
|
|
78
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
42
79
|
const list = getFilteredOptions();
|
|
43
80
|
|
|
44
81
|
if (event.key === 'ArrowUp') {
|
|
82
|
+
event.preventDefault();
|
|
45
83
|
if (focusedOptionIndex.value <= 0) return;
|
|
46
84
|
focusedOptionIndex.value--;
|
|
47
85
|
scrollToOption(focusedOptionIndex.value);
|
|
48
|
-
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (event.key === 'ArrowDown') {
|
|
90
|
+
event.preventDefault();
|
|
49
91
|
if (focusedOptionIndex.value >= list.length - 1) return;
|
|
50
92
|
focusedOptionIndex.value++;
|
|
51
93
|
scrollToOption(focusedOptionIndex.value);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (event.key === 'Enter') {
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
selectFocusedOption();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const horizontalHandler =
|
|
104
|
+
event.key === 'ArrowRight'
|
|
105
|
+
? onArrowRight
|
|
106
|
+
: event.key === 'ArrowLeft'
|
|
107
|
+
? onArrowLeft
|
|
108
|
+
: undefined;
|
|
109
|
+
|
|
110
|
+
if (horizontalHandler) {
|
|
111
|
+
if (isEditableTarget(event)) return;
|
|
112
|
+
event.preventDefault();
|
|
57
113
|
const option = list[focusedOptionIndex.value];
|
|
58
|
-
if (option
|
|
59
|
-
|
|
60
|
-
if (
|
|
114
|
+
if (!option) return;
|
|
115
|
+
const nextIndex = horizontalHandler(focusedOptionIndex.value, option);
|
|
116
|
+
if (typeof nextIndex === 'number' && nextIndex >= 0) {
|
|
117
|
+
focusedOptionIndex.value = nextIndex;
|
|
118
|
+
scrollToOption(nextIndex);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (event.key === ' ' && toValue(toggleOnSpace)) {
|
|
124
|
+
if (isEditableTarget(event)) return;
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
selectFocusedOption();
|
|
61
127
|
}
|
|
62
128
|
}
|
|
63
129
|
|
|
@@ -79,6 +145,14 @@ export function useSelectKeyboard<T extends SelectOption>(
|
|
|
79
145
|
if (isOpen) nextTick(bindKeydown);
|
|
80
146
|
else unbindKeydown();
|
|
81
147
|
});
|
|
148
|
+
watch(
|
|
149
|
+
() => getFilteredOptions().length,
|
|
150
|
+
(length) => {
|
|
151
|
+
if (focusedOptionIndex.value >= length) {
|
|
152
|
+
focusedOptionIndex.value = length - 1;
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
);
|
|
82
156
|
onBeforeUnmount(unbindKeydown);
|
|
83
157
|
}
|
|
84
158
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which tree parents are expanded. While searching, every parent starts
|
|
3
|
+
* expanded and the user may still collapse it; that search-only state is
|
|
4
|
+
* discarded when the search starts or ends, so the manual expansion is back
|
|
5
|
+
* once the search is cleared.
|
|
6
|
+
*/
|
|
7
|
+
import { ref, toValue, watch, type MaybeRefOrGetter } from 'vue';
|
|
8
|
+
|
|
9
|
+
export function useTreeExpansion(isSearchActive: MaybeRefOrGetter<boolean>) {
|
|
10
|
+
const expandedValues = ref(new Set<unknown>());
|
|
11
|
+
const searchCollapsedValues = ref(new Set<unknown>());
|
|
12
|
+
|
|
13
|
+
watch(
|
|
14
|
+
() => toValue(isSearchActive),
|
|
15
|
+
() => {
|
|
16
|
+
searchCollapsedValues.value = new Set();
|
|
17
|
+
},
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
function isExpanded(value: unknown) {
|
|
21
|
+
return toValue(isSearchActive)
|
|
22
|
+
? !searchCollapsedValues.value.has(value)
|
|
23
|
+
: expandedValues.value.has(value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function setExpanded(value: unknown, expanded = !isExpanded(value)) {
|
|
27
|
+
const searching = toValue(isSearchActive);
|
|
28
|
+
const target = searching ? searchCollapsedValues : expandedValues;
|
|
29
|
+
const shouldInclude = searching ? !expanded : expanded;
|
|
30
|
+
const next = new Set(target.value);
|
|
31
|
+
|
|
32
|
+
if (shouldInclude) next.add(value);
|
|
33
|
+
else next.delete(value);
|
|
34
|
+
|
|
35
|
+
target.value = next;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function reset() {
|
|
39
|
+
expandedValues.value = new Set();
|
|
40
|
+
searchCollapsedValues.value = new Set();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { isExpanded, setExpanded, reset };
|
|
44
|
+
}
|