@weni/unnnic-system 3.36.1-alpha.1 → 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/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/composables/useSelectBase.mjs +39 -44
- package/dist/composables/useSelectKeyboard.mjs +35 -70
- package/dist/locales/en.json.mjs +5 -9
- package/dist/style.css +2 -4
- 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/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/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,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
|
-
}
|