@weni/unnnic-system 3.36.0 → 3.36.1-alpha.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.
- package/dist/assets/scss/tailwind.css +1 -1
- 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/components/SelectTime/index.css +1 -1
- package/dist/components/SelectTime/index.vue.mjs +55 -42
- 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 +6 -4
- 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/components/SelectTime/__tests__/SelectTime.spec.js +88 -0
- package/src/components/SelectTime/index.vue +31 -7
- 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
- package/src/stories/SelectTime.stories.js +5 -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,88 @@
|
|
|
1
|
+
import { mount } from '@vue/test-utils';
|
|
2
|
+
import { describe, it, expect } from 'vitest';
|
|
3
|
+
import SelectTime from '../index.vue';
|
|
4
|
+
|
|
5
|
+
function mountSelectTime(props = {}) {
|
|
6
|
+
return mount(SelectTime, {
|
|
7
|
+
props: {
|
|
8
|
+
modelValue: '',
|
|
9
|
+
...props,
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('SelectTime', () => {
|
|
15
|
+
it('uses a native time input when enable24hTime is off', () => {
|
|
16
|
+
const wrapper = mountSelectTime({ modelValue: '24:00' });
|
|
17
|
+
const input = wrapper.find('input');
|
|
18
|
+
|
|
19
|
+
expect(input.attributes('type')).toBe('time');
|
|
20
|
+
expect(wrapper.text()).not.toContain('24:00');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('clears 24:00 on blur when enable24hTime is off', () => {
|
|
24
|
+
const wrapper = mountSelectTime({ modelValue: '24:00' });
|
|
25
|
+
|
|
26
|
+
wrapper.vm.handleBlur();
|
|
27
|
+
|
|
28
|
+
expect(wrapper.emitted('update:modelValue').at(-1)).toEqual(['']);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('enable24hTime', () => {
|
|
32
|
+
it('renders 24:00 in a text input and lists it as an option', () => {
|
|
33
|
+
const wrapper = mountSelectTime({
|
|
34
|
+
modelValue: '24:00',
|
|
35
|
+
enable24hTime: true,
|
|
36
|
+
});
|
|
37
|
+
const input = wrapper.find('input');
|
|
38
|
+
|
|
39
|
+
expect(input.attributes('type')).toBe('text');
|
|
40
|
+
expect(input.element.value).toBe('24:00');
|
|
41
|
+
expect(wrapper.text()).toContain('24:00');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('keeps 24:00 on blur', () => {
|
|
45
|
+
const wrapper = mountSelectTime({
|
|
46
|
+
modelValue: '24:00',
|
|
47
|
+
enable24hTime: true,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
wrapper.vm.handleBlur();
|
|
51
|
+
|
|
52
|
+
expect(wrapper.emitted('update:modelValue').at(-1)).toEqual(['24:00']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('formats 2400 as 24:00', () => {
|
|
56
|
+
const wrapper = mountSelectTime({
|
|
57
|
+
modelValue: '2400',
|
|
58
|
+
enable24hTime: true,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
wrapper.vm.handleBlur();
|
|
62
|
+
|
|
63
|
+
expect(wrapper.emitted('update:modelValue').at(-1)).toEqual(['24:00']);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('formats 24 as 24:00', () => {
|
|
67
|
+
const wrapper = mountSelectTime({
|
|
68
|
+
modelValue: '24',
|
|
69
|
+
enable24hTime: true,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
wrapper.vm.handleBlur();
|
|
73
|
+
|
|
74
|
+
expect(wrapper.emitted('update:modelValue').at(-1)).toEqual(['24:00']);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('clears 24:30 because only 24:00 is valid past 23:59', () => {
|
|
78
|
+
const wrapper = mountSelectTime({
|
|
79
|
+
modelValue: '24:30',
|
|
80
|
+
enable24hTime: true,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
wrapper.vm.handleBlur();
|
|
84
|
+
|
|
85
|
+
expect(wrapper.emitted('update:modelValue').at(-1)).toEqual(['']);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
<TextInput
|
|
14
14
|
:modelValue="modelValue"
|
|
15
15
|
:iconRight="active ? 'keyboard_arrow_up' : 'keyboard_arrow_down'"
|
|
16
|
-
nativeType="time"
|
|
16
|
+
:nativeType="enable24hTime ? 'text' : 'time'"
|
|
17
17
|
:placeholder="placeholder"
|
|
18
18
|
:disabled="disabled"
|
|
19
19
|
class="unnnic-select-time__input"
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
class="unnnic-select-time__options"
|
|
30
30
|
>
|
|
31
31
|
<section
|
|
32
|
-
v-for="hour in
|
|
32
|
+
v-for="hour in timeOptions"
|
|
33
33
|
:ref="`hour-${hour.value}`"
|
|
34
34
|
:key="hour.value"
|
|
35
35
|
:class="{
|
|
@@ -82,6 +82,10 @@ export default {
|
|
|
82
82
|
type: Boolean,
|
|
83
83
|
default: false,
|
|
84
84
|
},
|
|
85
|
+
enable24hTime: {
|
|
86
|
+
type: Boolean,
|
|
87
|
+
default: false,
|
|
88
|
+
},
|
|
85
89
|
},
|
|
86
90
|
emits: ['update:modelValue'],
|
|
87
91
|
data() {
|
|
@@ -89,10 +93,21 @@ export default {
|
|
|
89
93
|
active: false,
|
|
90
94
|
};
|
|
91
95
|
},
|
|
96
|
+
computed: {
|
|
97
|
+
timeOptions() {
|
|
98
|
+
const options = this.options.filter((hour) => hour.value !== '24:00');
|
|
99
|
+
|
|
100
|
+
if (!this.enable24hTime) {
|
|
101
|
+
return options;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return [...options, { label: '24:00', value: '24:00' }];
|
|
105
|
+
},
|
|
106
|
+
},
|
|
92
107
|
methods: {
|
|
93
108
|
handleInput(text) {
|
|
94
109
|
const { value } =
|
|
95
|
-
this.
|
|
110
|
+
this.timeOptions.find((hour) => hour.value.includes(text)) || {};
|
|
96
111
|
|
|
97
112
|
if (value) {
|
|
98
113
|
const hourRef = this.$refs[`hour-${value}`][0];
|
|
@@ -112,11 +127,15 @@ export default {
|
|
|
112
127
|
let formattedTime = '';
|
|
113
128
|
|
|
114
129
|
if (cleanValue.length === 4) {
|
|
115
|
-
// Format: 0101 -> 01:01, 1310 -> 13:10
|
|
130
|
+
// Format: 0101 -> 01:01, 1310 -> 13:10, 2400 -> 24:00
|
|
116
131
|
const hours = cleanValue.substring(0, 2);
|
|
117
132
|
const minutes = cleanValue.substring(2, 4);
|
|
133
|
+
const hoursNumber = parseInt(hours, 10);
|
|
134
|
+
const minutesNumber = parseInt(minutes, 10);
|
|
135
|
+
const isEndOfDay =
|
|
136
|
+
this.enable24hTime && hoursNumber === 24 && minutesNumber === 0;
|
|
118
137
|
|
|
119
|
-
if (
|
|
138
|
+
if ((hoursNumber <= 23 && minutesNumber <= 59) || isEndOfDay) {
|
|
120
139
|
formattedTime = `${hours}:${minutes}`;
|
|
121
140
|
}
|
|
122
141
|
} else if (cleanValue.length === 3) {
|
|
@@ -128,9 +147,14 @@ export default {
|
|
|
128
147
|
formattedTime = `${hours}:${minutes}`;
|
|
129
148
|
}
|
|
130
149
|
} else if (cleanValue.length === 2) {
|
|
131
|
-
// Format: 12 -> 12:00
|
|
150
|
+
// Format: 12 -> 12:00, 24 -> 24:00 when enable24hTime is on
|
|
132
151
|
const hours = cleanValue;
|
|
133
|
-
|
|
152
|
+
const hoursNumber = parseInt(hours, 10);
|
|
153
|
+
const isEndOfDay = hoursNumber === 24;
|
|
154
|
+
|
|
155
|
+
if (!isEndOfDay || this.enable24hTime) {
|
|
156
|
+
formattedTime = `${hours}:00`;
|
|
157
|
+
}
|
|
134
158
|
} else if (cleanValue.length === 1) {
|
|
135
159
|
// Format: 1 -> 01:00
|
|
136
160
|
const hours = `0${cleanValue}`;
|
|
@@ -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
|
}
|