@aleph-alpha/ui-library 2.9.0 → 2.11.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/config.js +10 -0
- package/dist/system/components/UiAutocomplete/UiAutocomplete.vue.d.ts +0 -1
- package/dist/system/components/UiAutocomplete/types.d.ts +2 -6
- package/dist/system/components/UiSelect/UiSelect.vue.d.ts +4 -5
- package/dist/system/components/UiSelect/UiSelectContent.vue.d.ts +0 -1
- package/dist/system/components/UiSelect/UiSelectItem.vue.d.ts +2 -9
- package/dist/system/components/UiSelect/UiSelectTrigger.vue.d.ts +0 -2
- package/dist/system/components/UiSelect/UiSelectValue.vue.d.ts +0 -3
- package/dist/system/components/UiSelect/types.d.ts +26 -6
- package/dist/system/lib.js +18370 -18530
- package/dist/system/patterns/UiDataTable/UiDataTableToolbar.vue.d.ts +2 -2
- package/docs/public-docs/component-lifecycle.md +177 -0
- package/docs/public-docs/quick-start.md +43 -4
- package/package.json +5 -3
- package/src/components/UiAutocomplete/UiAutocomplete.stories.ts +1 -1
- package/src/components/UiAutocomplete/UiAutocomplete.vue +9 -9
- package/src/components/UiAutocomplete/__tests__/UiAutocomplete.test.ts +76 -5
- package/src/components/UiAutocomplete/types.ts +2 -6
- package/src/components/UiSelect/UiSelect.stories.ts +4 -90
- package/src/components/UiSelect/UiSelect.vue +66 -144
- package/src/components/UiSelect/UiSelectContent.vue +10 -21
- package/src/components/UiSelect/UiSelectItem.vue +0 -82
- package/src/components/UiSelect/UiSelectTrigger.vue +1 -62
- package/src/components/UiSelect/UiSelectValue.vue +1 -47
- package/src/components/UiSelect/__tests__/UiSelect.test.ts +47 -98
- package/src/components/UiSelect/__tests__/UiSelectContent.test.ts +36 -3
- package/src/components/UiSelect/__tests__/UiSelectItem.test.ts +1 -1
- package/src/components/UiSelect/__tests__/UiSelectTrigger.test.ts +2 -2
- package/src/components/UiSelect/types.ts +29 -6
- package/src/patterns/UiDataTable/UiDataTableToolbar.vue +1 -1
- package/dist/system/components/UiSelect/context.d.ts +0 -28
- package/src/components/UiSelect/context.ts +0 -42
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import type { AcceptableValue } from 'reka-ui';
|
|
3
3
|
import { computed, ref } from 'vue';
|
|
4
4
|
import { Select as ShadcnSelect } from '@/components/core/select';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import type {
|
|
6
|
+
UiSelectProps,
|
|
7
|
+
UiSelectEmits,
|
|
8
|
+
UiSelectOptionalValue,
|
|
9
|
+
} from './types';
|
|
9
10
|
|
|
10
11
|
defineOptions({
|
|
11
12
|
name: 'UiSelect',
|
|
@@ -25,52 +26,44 @@ const props = withDefaults(defineProps<UiSelectProps>(), {
|
|
|
25
26
|
|
|
26
27
|
const emit = defineEmits<UiSelectEmits>();
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
function toArray(value: UiSelectOptionalValue): string[] {
|
|
30
|
+
if (Array.isArray(value)) return value;
|
|
31
|
+
if (typeof value === 'string' && value.length > 0) return [value];
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function toSelectValue(value: UiSelectOptionalValue): UiSelectOptionalValue {
|
|
36
|
+
if (props.multiple) {
|
|
37
|
+
if (value === undefined) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return toArray(value);
|
|
42
|
+
}
|
|
29
43
|
|
|
30
|
-
function handleSingleModelValueUpdate(value: AcceptableValue) {
|
|
31
44
|
if (typeof value === 'string') {
|
|
32
|
-
|
|
45
|
+
return value;
|
|
33
46
|
}
|
|
34
|
-
}
|
|
35
47
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
const singleDefaultValue = computed(() =>
|
|
40
|
-
typeof props.defaultValue === 'string' ? props.defaultValue : undefined,
|
|
41
|
-
);
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
42
50
|
|
|
43
|
-
|
|
51
|
+
const selectModelValue = computed(() => toSelectValue(props.modelValue));
|
|
44
52
|
|
|
45
|
-
|
|
46
|
-
if (Array.isArray(value)) return value;
|
|
47
|
-
if (typeof value === 'string' && value.length > 0) return [value];
|
|
48
|
-
return [];
|
|
49
|
-
}
|
|
53
|
+
const selectDefaultValue = computed(() => toSelectValue(props.defaultValue));
|
|
50
54
|
|
|
51
|
-
// Multi-select is treated as **controlled** when the consumer passes
|
|
52
|
-
// `modelValue` (even an empty array); otherwise we manage state
|
|
53
|
-
// internally seeded from `defaultValue`. This mirrors how Vue
|
|
54
|
-
// components are conventionally controlled and prevents internal
|
|
55
|
-
// state from drifting away from the prop when the parent doesn't
|
|
56
|
-
// echo `update:modelValue` back.
|
|
57
55
|
const isMultiControlled = computed(() => props.modelValue !== undefined);
|
|
58
|
-
|
|
59
|
-
// Uncontrolled storage. Only mutated when `isMultiControlled` is false.
|
|
60
56
|
const internalMultiValue = ref<string[]>(toArray(props.defaultValue));
|
|
61
|
-
|
|
62
|
-
// Effective selection used by the listbox, the trigger label, the
|
|
63
|
-
// hidden form inputs, and the provided context. When controlled it
|
|
64
|
-
// tracks the prop directly so the UI never leads the parent state.
|
|
65
57
|
const multiSelectedValues = computed<string[]>(() =>
|
|
66
58
|
isMultiControlled.value
|
|
67
59
|
? toArray(props.modelValue)
|
|
68
60
|
: internalMultiValue.value,
|
|
69
61
|
);
|
|
62
|
+
const formHiddenValues = computed<string[]>(() =>
|
|
63
|
+
props.multiple && props.name ? multiSelectedValues.value : [],
|
|
64
|
+
);
|
|
70
65
|
|
|
71
|
-
function
|
|
72
|
-
value: AcceptableValue | AcceptableValue[],
|
|
73
|
-
): string[] {
|
|
66
|
+
function toStringArray(value: AcceptableValue | AcceptableValue[]): string[] {
|
|
74
67
|
if (Array.isArray(value)) {
|
|
75
68
|
return value.filter((v): v is string => typeof v === 'string');
|
|
76
69
|
}
|
|
@@ -80,129 +73,58 @@ function normalizeMultiValue(
|
|
|
80
73
|
return [];
|
|
81
74
|
}
|
|
82
75
|
|
|
83
|
-
function
|
|
84
|
-
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!isMultiControlled.value) {
|
|
92
|
-
internalMultiValue.value = next;
|
|
76
|
+
function handleModelValueUpdate(value: AcceptableValue | AcceptableValue[]) {
|
|
77
|
+
if (props.multiple) {
|
|
78
|
+
const next = toStringArray(value);
|
|
79
|
+
if (!isMultiControlled.value) {
|
|
80
|
+
internalMultiValue.value = next;
|
|
81
|
+
}
|
|
82
|
+
emit('update:modelValue', next);
|
|
83
|
+
return;
|
|
93
84
|
}
|
|
94
|
-
emit('update:modelValue', next);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Open state for the multi-select popover. Mirrors the controlled `open`
|
|
98
|
-
// prop when provided, otherwise stays uncontrolled internally.
|
|
99
|
-
const internalOpen = ref<boolean>(props.defaultOpen ?? false);
|
|
100
|
-
const popoverOpen = computed({
|
|
101
|
-
get() {
|
|
102
|
-
return props.open ?? internalOpen.value;
|
|
103
|
-
},
|
|
104
|
-
set(value: boolean) {
|
|
105
|
-
internalOpen.value = value;
|
|
106
|
-
emit('update:open', value);
|
|
107
|
-
},
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// -------- Form integration (multi-select) --------
|
|
111
|
-
|
|
112
|
-
// Hidden `<input type="hidden">` values rendered for FormData
|
|
113
|
-
// submission. We only emit hidden inputs when `name` is set; without
|
|
114
|
-
// a name they wouldn't contribute to form data anyway.
|
|
115
|
-
const formHiddenValues = computed<string[]>(() =>
|
|
116
|
-
props.multiple && props.name ? multiSelectedValues.value : [],
|
|
117
|
-
);
|
|
118
85
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// Use getters instead of evaluating `props.multiple` once at setup
|
|
124
|
-
// time — descendants choose between the single-select and
|
|
125
|
-
// multi-select render paths via `ctx.mode`, and that decision must
|
|
126
|
-
// track the current prop value, not a stale snapshot. Without
|
|
127
|
-
// getters, toggling `:multiple` after mount would leave children
|
|
128
|
-
// rendering the original branch.
|
|
129
|
-
provideUiSelectContext({
|
|
130
|
-
get mode() {
|
|
131
|
-
return props.multiple ? 'multiple' : 'single';
|
|
132
|
-
},
|
|
133
|
-
get multiple() {
|
|
134
|
-
return props.multiple
|
|
135
|
-
? {
|
|
136
|
-
selectedValues: multiSelectedValues,
|
|
137
|
-
itemLabels,
|
|
138
|
-
}
|
|
139
|
-
: undefined;
|
|
140
|
-
},
|
|
141
|
-
});
|
|
86
|
+
if (typeof value === 'string') {
|
|
87
|
+
emit('update:modelValue', value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
142
90
|
</script>
|
|
143
91
|
|
|
144
92
|
<template>
|
|
145
93
|
<ShadcnSelect
|
|
146
|
-
|
|
147
|
-
:
|
|
148
|
-
:default-value="singleDefaultValue"
|
|
94
|
+
:model-value="selectModelValue"
|
|
95
|
+
:default-value="selectDefaultValue"
|
|
149
96
|
:default-open="props.defaultOpen"
|
|
150
97
|
:open="props.open"
|
|
151
98
|
:disabled="props.disabled"
|
|
152
|
-
:required="props.required"
|
|
153
|
-
:name="props.name"
|
|
99
|
+
:required="props.multiple ? false : props.required"
|
|
100
|
+
:name="props.multiple ? undefined : props.name"
|
|
154
101
|
:dir="props.dir"
|
|
155
|
-
|
|
102
|
+
:multiple="props.multiple"
|
|
103
|
+
@update:model-value="handleModelValueUpdate"
|
|
156
104
|
@update:open="emit('update:open', $event)"
|
|
157
105
|
>
|
|
158
106
|
<slot />
|
|
159
107
|
</ShadcnSelect>
|
|
160
108
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
109
|
+
<!--
|
|
110
|
+
Reka Select owns interaction and display in both modes. These inputs only
|
|
111
|
+
preserve native form submission/validation for multi-select arrays.
|
|
112
|
+
-->
|
|
113
|
+
<input
|
|
114
|
+
v-for="value in formHiddenValues"
|
|
115
|
+
:key="value"
|
|
116
|
+
type="hidden"
|
|
117
|
+
:name="props.name"
|
|
118
|
+
:value="value"
|
|
119
|
+
/>
|
|
120
|
+
<input
|
|
121
|
+
v-if="props.multiple && props.required"
|
|
122
|
+
type="text"
|
|
123
|
+
:value="multiSelectedValues.length > 0 ? 'has-value' : ''"
|
|
124
|
+
:required="true"
|
|
165
125
|
:disabled="props.disabled"
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
<ShadcnPopover v-model:open="popoverOpen">
|
|
171
|
-
<slot />
|
|
172
|
-
</ShadcnPopover>
|
|
173
|
-
|
|
174
|
-
<!--
|
|
175
|
-
Form integration for multi-select.
|
|
176
|
-
|
|
177
|
-
Native `<select multiple name="...">` submits one form entry per
|
|
178
|
-
selected option (same key, repeated values). We mirror that by
|
|
179
|
-
rendering one hidden input per selected value when `name` is set
|
|
180
|
-
so multi-select participates in standard FormData / form POSTs.
|
|
181
|
-
|
|
182
|
-
For `required`, we render a visually-hidden but focusable text
|
|
183
|
-
input that participates in native form validation. We can't use
|
|
184
|
-
`display:none`/`visibility:hidden` (browsers skip validating
|
|
185
|
-
those), so we use the standard sr-only clip-rect trick. The
|
|
186
|
-
input intentionally has no `name` so it doesn't appear in
|
|
187
|
-
submitted form data — its only job is to fire constraint
|
|
188
|
-
validation on submit when no value is selected.
|
|
189
|
-
-->
|
|
190
|
-
<input
|
|
191
|
-
v-for="value in formHiddenValues"
|
|
192
|
-
:key="value"
|
|
193
|
-
type="hidden"
|
|
194
|
-
:name="props.name"
|
|
195
|
-
:value="value"
|
|
196
|
-
/>
|
|
197
|
-
<input
|
|
198
|
-
v-if="props.required"
|
|
199
|
-
type="text"
|
|
200
|
-
:value="multiSelectedValues.length > 0 ? 'has-value' : ''"
|
|
201
|
-
:required="true"
|
|
202
|
-
:disabled="props.disabled"
|
|
203
|
-
tabindex="-1"
|
|
204
|
-
aria-hidden="true"
|
|
205
|
-
class="pointer-events-none absolute m-[-1px] size-px overflow-hidden border-0 p-0 whitespace-nowrap [clip:rect(0,0,0,0)]"
|
|
206
|
-
/>
|
|
207
|
-
</ShadcnListbox>
|
|
126
|
+
tabindex="-1"
|
|
127
|
+
aria-hidden="true"
|
|
128
|
+
class="pointer-events-none absolute m-[-1px] size-px overflow-hidden border-0 p-0 whitespace-nowrap [clip:rect(0,0,0,0)]"
|
|
129
|
+
/>
|
|
208
130
|
</template>
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
+
import { computed, useAttrs } from 'vue';
|
|
2
3
|
import { SelectContent as ShadcnSelectContent } from '@/components/core/select';
|
|
3
|
-
import { PopoverContent as ShadcnPopoverContent } from '@/components/core/popover';
|
|
4
|
-
import { ListboxContent as ShadcnListboxContent } from '@/components/core/listbox';
|
|
5
|
-
import { useUiSelectContext } from './context';
|
|
6
4
|
import type { UiSelectContentProps, UiSelectContentEmits } from './types';
|
|
7
5
|
|
|
8
6
|
defineOptions({
|
|
9
7
|
name: 'UiSelectContent',
|
|
8
|
+
inheritAttrs: false,
|
|
10
9
|
});
|
|
11
10
|
|
|
12
11
|
const props = withDefaults(defineProps<UiSelectContentProps>(), {
|
|
@@ -18,35 +17,25 @@ const props = withDefaults(defineProps<UiSelectContentProps>(), {
|
|
|
18
17
|
|
|
19
18
|
const emit = defineEmits<UiSelectContentEmits>();
|
|
20
19
|
|
|
21
|
-
const
|
|
20
|
+
const attrs = useAttrs();
|
|
21
|
+
|
|
22
|
+
const forwardedAttrs = computed(() => {
|
|
23
|
+
const { class: _class, ...delegatedAttrs } = attrs;
|
|
24
|
+
return delegatedAttrs;
|
|
25
|
+
});
|
|
22
26
|
</script>
|
|
23
27
|
|
|
24
28
|
<template>
|
|
25
29
|
<ShadcnSelectContent
|
|
26
|
-
v-
|
|
30
|
+
v-bind="forwardedAttrs"
|
|
27
31
|
:side="props.side"
|
|
28
32
|
:side-offset="props.sideOffset"
|
|
29
33
|
:align="props.align"
|
|
30
34
|
:position="props.position"
|
|
35
|
+
:class="props.class"
|
|
31
36
|
@escape-key-down="emit('escapeKeyDown', $event)"
|
|
32
37
|
@pointer-down-outside="emit('pointerDownOutside', $event)"
|
|
33
38
|
>
|
|
34
39
|
<slot />
|
|
35
40
|
</ShadcnSelectContent>
|
|
36
|
-
|
|
37
|
-
<ShadcnPopoverContent
|
|
38
|
-
v-else
|
|
39
|
-
data-slot="select-content"
|
|
40
|
-
:side="props.side"
|
|
41
|
-
:side-offset="props.sideOffset"
|
|
42
|
-
:align="props.align"
|
|
43
|
-
:force-mount="true"
|
|
44
|
-
class="min-w-(--reka-popper-anchor-width) w-(--reka-popper-anchor-width) max-w-(--reka-popper-available-width) p-1 data-[state=closed]:hidden data-[state=closed]:pointer-events-none"
|
|
45
|
-
@escape-key-down="emit('escapeKeyDown', $event)"
|
|
46
|
-
@pointer-down-outside="emit('pointerDownOutside', $event)"
|
|
47
|
-
>
|
|
48
|
-
<ShadcnListboxContent>
|
|
49
|
-
<slot />
|
|
50
|
-
</ShadcnListboxContent>
|
|
51
|
-
</ShadcnPopoverContent>
|
|
52
41
|
</template>
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
|
3
2
|
import { SelectItem as ShadcnSelectItem } from '@/components/core/select';
|
|
4
|
-
import {
|
|
5
|
-
ListboxItem as ShadcnListboxItem,
|
|
6
|
-
ListboxItemIndicator as ShadcnListboxItemIndicator,
|
|
7
|
-
} from '@/components/core/listbox';
|
|
8
|
-
import { UiIcon } from '@/components/UiIcon';
|
|
9
|
-
import { useUiSelectContext } from './context';
|
|
10
3
|
import type { UiSelectItemProps } from './types';
|
|
11
4
|
|
|
12
5
|
defineOptions({
|
|
@@ -17,62 +10,10 @@ const props = withDefaults(defineProps<UiSelectItemProps>(), {
|
|
|
17
10
|
disabled: false,
|
|
18
11
|
textValue: undefined,
|
|
19
12
|
});
|
|
20
|
-
|
|
21
|
-
const ctx = useUiSelectContext();
|
|
22
|
-
|
|
23
|
-
// ----- Multi-select label registration -----
|
|
24
|
-
// We expose the rendered slot's text to the trigger via the shared
|
|
25
|
-
// `itemLabels` registry so `UiSelectValue` can render comma-separated
|
|
26
|
-
// labels for the selected values without consumers having to provide a
|
|
27
|
-
// separate label map.
|
|
28
|
-
const labelEl = ref<HTMLElement | null>(null);
|
|
29
|
-
|
|
30
|
-
function readLabel(): string {
|
|
31
|
-
return labelEl.value?.textContent?.trim() || props.textValue || props.value;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function syncLabel() {
|
|
35
|
-
if (ctx.mode !== 'multiple' || !ctx.multiple) return;
|
|
36
|
-
ctx.multiple.itemLabels.value.set(props.value, readLabel());
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
onMounted(syncLabel);
|
|
40
|
-
|
|
41
|
-
// When `props.value` changes, the entry registered under the OLD key
|
|
42
|
-
// becomes orphaned — `syncLabel()` only writes to the NEW key, so
|
|
43
|
-
// without an explicit delete the registry would accumulate stale
|
|
44
|
-
// mappings. That manifests two ways for consumers:
|
|
45
|
-
// 1. The trigger can render a stale label for a value that's no
|
|
46
|
-
// longer a real option (because `UiSelectValue` happily reads
|
|
47
|
-
// whatever the registry still has under that key).
|
|
48
|
-
// 2. For dynamic option lists (e.g. `<UiSelectItem v-for>` driven
|
|
49
|
-
// by remote data), the map grows unbounded.
|
|
50
|
-
// Use the watcher's `(next, prev)` signature to drop the previous
|
|
51
|
-
// key before re-registering under the new one.
|
|
52
|
-
watch(
|
|
53
|
-
() => props.value,
|
|
54
|
-
(next, prev) => {
|
|
55
|
-
if (ctx.mode !== 'multiple' || !ctx.multiple) return;
|
|
56
|
-
if (prev !== undefined && prev !== next) {
|
|
57
|
-
ctx.multiple.itemLabels.value.delete(prev);
|
|
58
|
-
}
|
|
59
|
-
ctx.multiple.itemLabels.value.set(next, readLabel());
|
|
60
|
-
},
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
// `textValue` only affects the label string under the same key,
|
|
64
|
-
// so a plain re-sync is sufficient (no stale key to clean up).
|
|
65
|
-
watch(() => props.textValue, syncLabel);
|
|
66
|
-
|
|
67
|
-
onBeforeUnmount(() => {
|
|
68
|
-
if (ctx.mode !== 'multiple' || !ctx.multiple) return;
|
|
69
|
-
ctx.multiple.itemLabels.value.delete(props.value);
|
|
70
|
-
});
|
|
71
13
|
</script>
|
|
72
14
|
|
|
73
15
|
<template>
|
|
74
16
|
<ShadcnSelectItem
|
|
75
|
-
v-if="ctx.mode === 'single'"
|
|
76
17
|
:value="props.value"
|
|
77
18
|
:disabled="props.disabled"
|
|
78
19
|
:text-value="props.textValue"
|
|
@@ -85,27 +26,4 @@ onBeforeUnmount(() => {
|
|
|
85
26
|
</template>
|
|
86
27
|
<slot />
|
|
87
28
|
</ShadcnSelectItem>
|
|
88
|
-
|
|
89
|
-
<ShadcnListboxItem
|
|
90
|
-
v-else
|
|
91
|
-
:value="props.value"
|
|
92
|
-
:disabled="props.disabled"
|
|
93
|
-
:text-value="props.textValue"
|
|
94
|
-
class="hover-layer hover-layer-default focus:text-foreground data-[highlighted]:text-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-12 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50"
|
|
95
|
-
>
|
|
96
|
-
<span ref="labelEl" class="flex items-center gap-2">
|
|
97
|
-
<slot />
|
|
98
|
-
</span>
|
|
99
|
-
<span
|
|
100
|
-
class="!absolute right-2 flex items-center justify-center gap-1"
|
|
101
|
-
data-slot="select-item-end"
|
|
102
|
-
>
|
|
103
|
-
<slot name="right-icon" />
|
|
104
|
-
<ShadcnListboxItemIndicator>
|
|
105
|
-
<slot name="indicator-icon">
|
|
106
|
-
<UiIcon name="check" :size="16" />
|
|
107
|
-
</slot>
|
|
108
|
-
</ShadcnListboxItemIndicator>
|
|
109
|
-
</span>
|
|
110
|
-
</ShadcnListboxItem>
|
|
111
29
|
</template>
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed } from 'vue';
|
|
3
2
|
import { SelectTrigger as ShadcnSelectTrigger } from '@/components/core/select';
|
|
4
|
-
import { PopoverTrigger as ShadcnPopoverTrigger } from '@/components/core/popover';
|
|
5
|
-
import { UiIcon } from '@/components/UiIcon';
|
|
6
|
-
import { cn } from '@/lib/utils';
|
|
7
|
-
import { useUiSelectContext } from './context';
|
|
8
3
|
import type { UiSelectTriggerProps } from './types';
|
|
9
4
|
|
|
10
5
|
defineOptions({
|
|
@@ -14,76 +9,20 @@ defineOptions({
|
|
|
14
9
|
const props = withDefaults(defineProps<UiSelectTriggerProps>(), {
|
|
15
10
|
ariaLabel: undefined,
|
|
16
11
|
disabled: false,
|
|
17
|
-
size: 'default',
|
|
18
12
|
});
|
|
19
13
|
|
|
20
|
-
const ctx = useUiSelectContext();
|
|
21
|
-
|
|
22
|
-
// Mirror reka-ui SelectTrigger's `data-placeholder` attribute on our
|
|
23
|
-
// popover-backed multi-select trigger. Single-select gets this from
|
|
24
|
-
// SelectTrigger itself (when no value is selected). Without it the
|
|
25
|
-
// trigger keeps `bg-accent` and looks "active"/blue even when empty,
|
|
26
|
-
// because the `data-[placeholder]:bg-background-input-default` rule
|
|
27
|
-
// in `multiTriggerClasses` never matches.
|
|
28
|
-
const isMultiPlaceholder = computed(
|
|
29
|
-
() =>
|
|
30
|
-
ctx.mode === 'multiple' &&
|
|
31
|
-
(ctx.multiple?.selectedValues.value.length ?? 0) === 0,
|
|
32
|
-
);
|
|
33
|
-
const singleAriaLabel = computed(() => props.ariaLabel);
|
|
34
|
-
const multiAriaLabel = computed(
|
|
35
|
-
() =>
|
|
36
|
-
props.ariaLabel ??
|
|
37
|
-
(isMultiPlaceholder.value ? 'Select options' : undefined),
|
|
38
|
-
);
|
|
39
|
-
|
|
40
14
|
// shadcn trigger styles target only SVG icons (`[&_svg]...`) but our icons are spans (`data-slot="icon"`).
|
|
41
15
|
// Add equivalent sizing/behavior + rotate-on-open for the chevron.
|
|
42
16
|
const uiIconTriggerClasses =
|
|
43
17
|
'[&_[data-slot=icon]]:pointer-events-none [&_[data-slot=icon]]:shrink-0 [&_[data-slot=icon]]:transition-transform [&_[data-slot=icon]]:duration-200 [&[data-state=open]_[data-slot=icon]]:rotate-180';
|
|
44
|
-
|
|
45
|
-
// In multi-select mode, we render a Popover instead of reka-ui's Select, so
|
|
46
|
-
// we have to recreate the SelectTrigger button (incl. the styling that
|
|
47
|
-
// SelectTrigger.vue applies under the hood) and append the chevron icon
|
|
48
|
-
// ourselves to keep the visual parity with single-select.
|
|
49
|
-
//
|
|
50
|
-
// Two deliberate differences from single-select:
|
|
51
|
-
// - No `w-fit`: the trigger width must NOT grow with the selected
|
|
52
|
-
// labels. Consumers control the width via `min-w-*` / `w-*` classes
|
|
53
|
-
// (see stories). `max-w-full` keeps it from overflowing its parent.
|
|
54
|
-
// - No `whitespace-nowrap` on the trigger itself — we move single-line
|
|
55
|
-
// + ellipsis behavior down to the UiSelectValue span so it can
|
|
56
|
-
// truncate inside the flex row instead of forcing the button wider.
|
|
57
|
-
const multiTriggerClasses = computed(() =>
|
|
58
|
-
cn(
|
|
59
|
-
"border-input data-[placeholder]:text-muted-foreground data-[placeholder]:bg-background-input-default [&:not([data-placeholder])]:border-border-surface-focus-ring [&_svg:not([class*='text-'])]:text-muted-foreground focus:border-border-surface-focus-ring focus-visible:border-border-surface-focus-ring active:border-border-surface-focus-ring data-[state=open]:border-border-surface-focus-ring focus:ring-ring/50 focus-visible:ring-ring/50 focus:shadow-xs focus-visible:shadow-xs aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive flex max-w-full items-center justify-between gap-2 rounded-md border bg-accent px-3 py-2 text-sm transition-[color,box-shadow] outline-none focus:ring-[3px] focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 *:data-[slot=select-value]:min-w-0 *:data-[slot=select-value]:flex-1 *:data-[slot=select-value]:truncate [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([data-slot=icon]):not([class*='size-'])]:size-4",
|
|
60
|
-
props.size === 'default' ? 'h-9' : 'h-8',
|
|
61
|
-
uiIconTriggerClasses,
|
|
62
|
-
),
|
|
63
|
-
);
|
|
64
18
|
</script>
|
|
65
19
|
|
|
66
20
|
<template>
|
|
67
21
|
<ShadcnSelectTrigger
|
|
68
|
-
v-if="ctx.mode === 'single'"
|
|
69
22
|
:disabled="props.disabled"
|
|
70
|
-
:
|
|
71
|
-
:aria-label="singleAriaLabel"
|
|
23
|
+
:aria-label="props.ariaLabel"
|
|
72
24
|
:class="uiIconTriggerClasses"
|
|
73
25
|
>
|
|
74
26
|
<slot />
|
|
75
27
|
</ShadcnSelectTrigger>
|
|
76
|
-
|
|
77
|
-
<ShadcnPopoverTrigger
|
|
78
|
-
v-else
|
|
79
|
-
data-slot="select-trigger"
|
|
80
|
-
:data-size="props.size"
|
|
81
|
-
:data-placeholder="isMultiPlaceholder ? '' : undefined"
|
|
82
|
-
:disabled="props.disabled"
|
|
83
|
-
:aria-label="multiAriaLabel"
|
|
84
|
-
:class="multiTriggerClasses"
|
|
85
|
-
>
|
|
86
|
-
<slot />
|
|
87
|
-
<UiIcon name="chevron-down" :size="16" class="opacity-50" />
|
|
88
|
-
</ShadcnPopoverTrigger>
|
|
89
28
|
</template>
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed } from 'vue';
|
|
3
2
|
import { SelectValue as ShadcnSelectValue } from '@/components/core/select';
|
|
4
|
-
import { useUiSelectContext } from './context';
|
|
5
3
|
import type { UiSelectValueProps } from './types';
|
|
6
4
|
|
|
7
5
|
defineOptions({
|
|
@@ -11,54 +9,10 @@ defineOptions({
|
|
|
11
9
|
const props = withDefaults(defineProps<UiSelectValueProps>(), {
|
|
12
10
|
placeholder: undefined,
|
|
13
11
|
});
|
|
14
|
-
|
|
15
|
-
const ctx = useUiSelectContext();
|
|
16
|
-
|
|
17
|
-
// Multi-select: derive the displayed text from the registry of item
|
|
18
|
-
// labels populated by `UiSelectItem`. We keep the selection order
|
|
19
|
-
// (i.e. the order returned by Listbox) for consistent output.
|
|
20
|
-
const displayText = computed(() => {
|
|
21
|
-
if (ctx.mode !== 'multiple' || !ctx.multiple) return '';
|
|
22
|
-
const labels = ctx.multiple.itemLabels.value;
|
|
23
|
-
return ctx.multiple.selectedValues.value
|
|
24
|
-
.map((value) => labels.get(value) ?? value)
|
|
25
|
-
.join(', ');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
const hasSelection = computed(
|
|
29
|
-
() =>
|
|
30
|
-
ctx.mode === 'multiple' &&
|
|
31
|
-
(ctx.multiple?.selectedValues.value.length ?? 0) > 0,
|
|
32
|
-
);
|
|
33
12
|
</script>
|
|
34
13
|
|
|
35
14
|
<template>
|
|
36
|
-
<ShadcnSelectValue
|
|
37
|
-
v-if="ctx.mode === 'single'"
|
|
38
|
-
:placeholder="props.placeholder"
|
|
39
|
-
>
|
|
15
|
+
<ShadcnSelectValue :placeholder="props.placeholder">
|
|
40
16
|
<slot />
|
|
41
17
|
</ShadcnSelectValue>
|
|
42
|
-
|
|
43
|
-
<!--
|
|
44
|
-
Multi-select display: render as a block-level truncating span.
|
|
45
|
-
`truncate` (= `overflow:hidden; text-overflow:ellipsis; white-space:nowrap`)
|
|
46
|
-
works only when this element participates in the parent flex row with
|
|
47
|
-
`min-w-0` + `flex-1` (applied via `UiSelectTrigger` via the
|
|
48
|
-
`*:data-[slot=select-value]:*` selectors), which is why we avoid flex
|
|
49
|
-
layout on the span itself here.
|
|
50
|
-
-->
|
|
51
|
-
<span
|
|
52
|
-
v-else
|
|
53
|
-
data-slot="select-value"
|
|
54
|
-
:data-placeholder="hasSelection ? undefined : ''"
|
|
55
|
-
class="block truncate"
|
|
56
|
-
>
|
|
57
|
-
<slot
|
|
58
|
-
v-if="$slots.default"
|
|
59
|
-
:selected="ctx.multiple?.selectedValues.value ?? []"
|
|
60
|
-
/>
|
|
61
|
-
<template v-else-if="hasSelection">{{ displayText }}</template>
|
|
62
|
-
<template v-else>{{ props.placeholder }}</template>
|
|
63
|
-
</span>
|
|
64
18
|
</template>
|