@weni/unnnic-system 3.36.1-alpha.0 → 3.36.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 +15 -41
- package/dist/components/MultiSelect/index.css +1 -1
- package/dist/components/MultiSelect/index.vue.mjs +2 -2
- package/dist/components/MultiSelect/index.vue2.mjs +114 -126
- package/dist/components/SelectTime/index.css +1 -1
- package/dist/components/SelectTime/index.vue.mjs +55 -42
- package/dist/composables/useSelectBase.mjs +39 -44
- package/dist/composables/useSelectKeyboard.mjs +35 -70
- package/dist/locales/en.json.mjs +5 -9
- package/dist/style.css +4 -6
- package/dist/unnnic.umd.js +19 -19
- package/package.json +2 -2
- package/src/components/MultiSelect/MultiSelectOption.vue +3 -66
- package/src/components/MultiSelect/__tests__/MultiSelect.spec.js +0 -556
- package/src/components/MultiSelect/__tests__/MultiSelectOption.spec.js +1 -55
- package/src/components/MultiSelect/__tests__/__snapshots__/MultiSelectOption.spec.js.snap +0 -5
- package/src/components/MultiSelect/index.vue +50 -57
- package/src/components/Select/_select-shared.scss +0 -8
- package/src/components/Select/types.ts +0 -1
- package/src/components/SelectTime/__tests__/SelectTime.spec.js +88 -0
- package/src/components/SelectTime/index.vue +31 -7
- package/src/composables/useSelectBase.ts +16 -40
- package/src/composables/useSelectKeyboard.ts +18 -92
- package/src/locales/en.json +1 -3
- package/src/stories/MultiSelect.stories.js +1 -62
- package/src/stories/SelectTime.stories.js +5 -1
- package/dist/components/MultiSelect/MultiSelectTreeOptions.css +0 -1
- package/dist/components/MultiSelect/MultiSelectTreeOptions.vue.mjs +0 -7
- package/dist/components/MultiSelect/MultiSelectTreeOptions.vue2.mjs +0 -50
- package/dist/composables/useMultiSelectRows.mjs +0 -74
- package/dist/composables/useMultiSelectSelection.mjs +0 -44
- package/dist/composables/useTreeExpansion.mjs +0 -24
- package/dist/lib/tree-selection.mjs +0 -66
- package/src/components/MultiSelect/MultiSelectTreeOptions.vue +0 -73
- package/src/composables/useMultiSelectRows.ts +0 -121
- package/src/composables/useMultiSelectSelection.ts +0 -97
- package/src/composables/useTreeExpansion.ts +0 -44
- package/src/lib/__tests__/tree-selection.spec.ts +0 -141
- package/src/lib/tree-selection.ts +0 -156
|
@@ -1,121 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import type { SelectOption } from '@/components/Select/types';
|
|
3
|
-
import {
|
|
4
|
-
filterTreeOptions,
|
|
5
|
-
getCheckState,
|
|
6
|
-
getDisplayOptions,
|
|
7
|
-
getDrivingChildren,
|
|
8
|
-
getVisibleChildren,
|
|
9
|
-
isTreeOptions,
|
|
10
|
-
toggleOptionValue,
|
|
11
|
-
withImpliedChildren,
|
|
12
|
-
} from '@/lib/tree-selection';
|
|
13
|
-
|
|
14
|
-
const getValue = (option: SelectOption) => option.value;
|
|
15
|
-
|
|
16
|
-
const delay: SelectOption = { label: 'Delay', value: 'delay' };
|
|
17
|
-
const tracking: SelectOption = { label: 'Tracking', value: 'tracking' };
|
|
18
|
-
const delivery: SelectOption = {
|
|
19
|
-
label: 'Delivery',
|
|
20
|
-
value: 'delivery',
|
|
21
|
-
children: [
|
|
22
|
-
delay,
|
|
23
|
-
tracking,
|
|
24
|
-
{ label: 'Locked', value: 'locked', disabled: true },
|
|
25
|
-
],
|
|
26
|
-
};
|
|
27
|
-
const payment: SelectOption = {
|
|
28
|
-
label: 'Payment',
|
|
29
|
-
value: 'payment',
|
|
30
|
-
children: [{ label: 'Pix', value: 'pix' }],
|
|
31
|
-
};
|
|
32
|
-
const other: SelectOption = { label: 'Other', value: 'other' };
|
|
33
|
-
const options = [delivery, payment, other];
|
|
34
|
-
|
|
35
|
-
const toggle = (values: unknown[], option: SelectOption, checked: boolean) =>
|
|
36
|
-
toggleOptionValue(options, values, option, checked, getValue);
|
|
37
|
-
|
|
38
|
-
describe('tree-selection', () => {
|
|
39
|
-
it('detects tree options', () => {
|
|
40
|
-
expect(isTreeOptions(options)).toBe(true);
|
|
41
|
-
expect(isTreeOptions([other])).toBe(false);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('drives a parent by its enabled children, or all when none is enabled', () => {
|
|
45
|
-
expect(getDrivingChildren(delivery).map(getValue)).toEqual([
|
|
46
|
-
'delay',
|
|
47
|
-
'tracking',
|
|
48
|
-
]);
|
|
49
|
-
|
|
50
|
-
const locked = { value: 'p', children: [{ value: 'c', disabled: true }] };
|
|
51
|
-
expect(getDrivingChildren(locked).map(getValue)).toEqual(['c']);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('search', () => {
|
|
55
|
-
const matches = (option: SelectOption) =>
|
|
56
|
-
String(option.label).toLowerCase().includes('track');
|
|
57
|
-
|
|
58
|
-
it('keeps roots that match or have a matching child', () => {
|
|
59
|
-
expect(filterTreeOptions(options, matches)).toEqual([delivery]);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('shows only matching children under a non-matching root', () => {
|
|
63
|
-
expect(getVisibleChildren(delivery, matches).map(getValue)).toEqual([
|
|
64
|
-
'tracking',
|
|
65
|
-
]);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('shows every child under a matching root', () => {
|
|
69
|
-
expect(getVisibleChildren(delivery, () => true)).toHaveLength(3);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('adds the enabled children implied by a parent value', () => {
|
|
74
|
-
expect(withImpliedChildren(options, ['delivery'], getValue)).toEqual([
|
|
75
|
-
'delivery',
|
|
76
|
-
'delay',
|
|
77
|
-
'tracking',
|
|
78
|
-
]);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
describe('getCheckState', () => {
|
|
82
|
-
const stateOf = (option: SelectOption, values: unknown[]) =>
|
|
83
|
-
getCheckState(option, new Set(values), getValue);
|
|
84
|
-
|
|
85
|
-
it('returns the leaf selection', () => {
|
|
86
|
-
expect(stateOf(other, ['other'])).toBe(true);
|
|
87
|
-
expect(stateOf(other, [])).toBe(false);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('returns true, less or false for a parent', () => {
|
|
91
|
-
expect(stateOf(delivery, ['delay', 'tracking'])).toBe(true);
|
|
92
|
-
expect(stateOf(delivery, ['delay'])).toBe('less');
|
|
93
|
-
expect(stateOf(delivery, [])).toBe(false);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe('toggleOptionValue', () => {
|
|
98
|
-
it('checks a parent with its enabled children', () => {
|
|
99
|
-
expect(toggle([], delivery, true)).toEqual([
|
|
100
|
-
'delay',
|
|
101
|
-
'tracking',
|
|
102
|
-
'delivery',
|
|
103
|
-
]);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('unchecks a parent and its children', () => {
|
|
107
|
-
expect(
|
|
108
|
-
toggle(['delay', 'tracking', 'delivery'], delivery, false),
|
|
109
|
-
).toEqual([]);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('adds the parent once its last driving child is checked', () => {
|
|
113
|
-
expect(toggle(['delay'], tracking, true)).toEqual([
|
|
114
|
-
'delay',
|
|
115
|
-
'tracking',
|
|
116
|
-
'delivery',
|
|
117
|
-
]);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('removes the parent when a child is unchecked', () => {
|
|
121
|
-
expect(toggle(['delay', 'tracking', 'delivery'], delay, false)).toEqual([
|
|
122
|
-
'tracking',
|
|
123
|
-
]);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('toggles a leaf and keeps unknown values', () => {
|
|
127
|
-
expect(toggle(['unknown'], other, true)).toEqual(['unknown', 'other']);
|
|
128
|
-
expect(toggle(['unknown', 'other'], other, false)).toEqual(['unknown']);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('displays a full parent instead of its children, in tree order', () => {
|
|
133
|
-
const selected = new Set(['other', 'pix', 'payment', 'delay']);
|
|
134
|
-
|
|
135
|
-
expect(getDisplayOptions(options, selected, getValue)).toEqual([
|
|
136
|
-
delay,
|
|
137
|
-
payment,
|
|
138
|
-
other,
|
|
139
|
-
]);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure selection rules for options with one level of children.
|
|
3
|
-
* Rules work on option values (`option[itemValue]`); converting them from and
|
|
4
|
-
* to a component model is the caller's job. Options without children are
|
|
5
|
-
* leaves, so a flat list follows the same rules.
|
|
6
|
-
*/
|
|
7
|
-
import type { CheckboxValue } from '../components/Checkbox/Checkbox.vue';
|
|
8
|
-
import type { SelectOption } from '../components/Select/types';
|
|
9
|
-
|
|
10
|
-
export type GetOptionValue = (option: SelectOption) => unknown;
|
|
11
|
-
export type OptionMatcher = (option: SelectOption) => boolean;
|
|
12
|
-
|
|
13
|
-
export function getOptionChildren(option: SelectOption): SelectOption[] {
|
|
14
|
-
return Array.isArray(option.children) ? option.children : [];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function hasOptionChildren(option: SelectOption) {
|
|
18
|
-
return getOptionChildren(option).length > 0;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function isTreeOptions(options: SelectOption[]) {
|
|
22
|
-
return options.some(hasOptionChildren);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function flattenTreeOptions(options: SelectOption[]) {
|
|
26
|
-
return options.flatMap((option) => [option, ...getOptionChildren(option)]);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Children that decide whether a parent is fully checked. Disabled children
|
|
31
|
-
* are skipped so they cannot keep a parent partial forever, unless every
|
|
32
|
-
* child is disabled.
|
|
33
|
-
*/
|
|
34
|
-
export function getDrivingChildren(parent: SelectOption) {
|
|
35
|
-
const children = getOptionChildren(parent);
|
|
36
|
-
const enabled = children.filter((child) => !child.disabled);
|
|
37
|
-
return enabled.length ? enabled : children;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Roots kept by a search: the matching ones and those with a matching child. */
|
|
41
|
-
export function filterTreeOptions(
|
|
42
|
-
options: SelectOption[],
|
|
43
|
-
matches: OptionMatcher,
|
|
44
|
-
) {
|
|
45
|
-
return options.filter(
|
|
46
|
-
(option) => matches(option) || getOptionChildren(option).some(matches),
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Children shown under a kept root: all of them when the root matches. */
|
|
51
|
-
export function getVisibleChildren(
|
|
52
|
-
option: SelectOption,
|
|
53
|
-
matches: OptionMatcher,
|
|
54
|
-
) {
|
|
55
|
-
const children = getOptionChildren(option);
|
|
56
|
-
return matches(option) ? children : children.filter(matches);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* A parent value received without its children (e.g. restored from a URL)
|
|
61
|
-
* implies its enabled children, so they count as selected.
|
|
62
|
-
*/
|
|
63
|
-
export function withImpliedChildren(
|
|
64
|
-
options: SelectOption[],
|
|
65
|
-
values: unknown[],
|
|
66
|
-
getValue: GetOptionValue,
|
|
67
|
-
) {
|
|
68
|
-
const explicit = new Set(values);
|
|
69
|
-
const implied = options
|
|
70
|
-
.filter((option) => explicit.has(getValue(option)))
|
|
71
|
-
.flatMap(getOptionChildren)
|
|
72
|
-
.filter((child) => !child.disabled && !explicit.has(getValue(child)))
|
|
73
|
-
.map(getValue);
|
|
74
|
-
|
|
75
|
-
return [...values, ...implied];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function getCheckState(
|
|
79
|
-
option: SelectOption,
|
|
80
|
-
selected: Set<unknown>,
|
|
81
|
-
getValue: GetOptionValue,
|
|
82
|
-
): CheckboxValue {
|
|
83
|
-
const isSelected = (item: SelectOption) => selected.has(getValue(item));
|
|
84
|
-
const children = getOptionChildren(option);
|
|
85
|
-
|
|
86
|
-
if (!children.length) return isSelected(option);
|
|
87
|
-
if (getDrivingChildren(option).every(isSelected)) return true;
|
|
88
|
-
return children.some(isSelected) ? 'less' : false;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function findParent(
|
|
92
|
-
options: SelectOption[],
|
|
93
|
-
option: SelectOption,
|
|
94
|
-
getValue: GetOptionValue,
|
|
95
|
-
) {
|
|
96
|
-
const value = getValue(option);
|
|
97
|
-
return options.find((root) =>
|
|
98
|
-
getOptionChildren(root).some((child) => getValue(child) === value),
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** The parent value is present only while all its driving children are. */
|
|
103
|
-
function syncParentValue(
|
|
104
|
-
parent: SelectOption,
|
|
105
|
-
values: unknown[],
|
|
106
|
-
getValue: GetOptionValue,
|
|
107
|
-
) {
|
|
108
|
-
const parentValue = getValue(parent);
|
|
109
|
-
const selected = new Set(values);
|
|
110
|
-
const rest = values.filter((value) => value !== parentValue);
|
|
111
|
-
const isFull = getDrivingChildren(parent).every((child) =>
|
|
112
|
-
selected.has(getValue(child)),
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
return isFull ? [...rest, parentValue] : rest;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Checks or unchecks an option. A parent toggles its enabled children; a
|
|
120
|
-
* child or leaf toggles itself. `values` must already include implied
|
|
121
|
-
* children; values unknown to `options` are kept.
|
|
122
|
-
*/
|
|
123
|
-
export function toggleOptionValue(
|
|
124
|
-
options: SelectOption[],
|
|
125
|
-
values: unknown[],
|
|
126
|
-
option: SelectOption,
|
|
127
|
-
checked: boolean,
|
|
128
|
-
getValue: GetOptionValue,
|
|
129
|
-
) {
|
|
130
|
-
const isParent = hasOptionChildren(option);
|
|
131
|
-
const parent = isParent ? option : findParent(options, option, getValue);
|
|
132
|
-
const toggled = isParent
|
|
133
|
-
? getOptionChildren(option).filter((child) => !child.disabled)
|
|
134
|
-
: [option];
|
|
135
|
-
const toggledValues = toggled.map(getValue);
|
|
136
|
-
|
|
137
|
-
const next = values.filter((value) => !toggledValues.includes(value));
|
|
138
|
-
if (checked) next.push(...toggledValues);
|
|
139
|
-
|
|
140
|
-
return parent ? syncParentValue(parent, next, getValue) : next;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** Selected options in tree order, where a full parent replaces its children. */
|
|
144
|
-
export function getDisplayOptions(
|
|
145
|
-
options: SelectOption[],
|
|
146
|
-
selected: Set<unknown>,
|
|
147
|
-
getValue: GetOptionValue,
|
|
148
|
-
) {
|
|
149
|
-
const isSelected = (item: SelectOption) => selected.has(getValue(item));
|
|
150
|
-
|
|
151
|
-
return options.flatMap((option) => {
|
|
152
|
-
if (!hasOptionChildren(option)) return isSelected(option) ? [option] : [];
|
|
153
|
-
if (getCheckState(option, selected, getValue) === true) return [option];
|
|
154
|
-
return getOptionChildren(option).filter(isSelected);
|
|
155
|
-
});
|
|
156
|
-
}
|