@zooui/nuxt 0.1.1 → 0.3.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/README.md +14 -2
- package/components/ConfirmButton.vue +124 -0
- package/components/ConfirmDialog.vue +201 -0
- package/components/DateField.vue +176 -42
- package/components/FileDropOverlay.vue +275 -0
- package/components/FileList.vue +155 -0
- package/components/FilterSelect.vue +340 -0
- package/components/OptionalDate.vue +180 -0
- package/components/OrderList.vue +495 -0
- package/components/RepeatingList.vue +133 -21
- package/components/SelectMenu.vue +325 -0
- package/components/confirm.ts +163 -0
- package/components/row-snapshot.ts +48 -0
- package/components/select-typeahead.ts +79 -0
- package/composables/useConfirm.ts +68 -0
- package/nuxt.config.ts +5 -1
- package/package.json +2 -1
- package/utils/confirm-queue.ts +85 -0
- package/utils/date-entry.ts +189 -8
- package/utils/file-drop.ts +151 -0
- package/utils/filter-match.ts +62 -0
- package/utils/order-list.ts +42 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// The files a form holds, one row each: a file-type icon, the name, the size in the locale's words, and Remove. A row
|
|
3
|
+
// is a `File` just picked or dropped, or a file already on the server (`{ id, name, size, type }`), in the order given,
|
|
4
|
+
// so an edit form lists what was sent before and what is being added in one list. Nuxt UI's UFileUpload lists only the
|
|
5
|
+
// `File`s it holds itself; this lists whatever the form has. Written on Nuxt UI's tokens.
|
|
6
|
+
//
|
|
7
|
+
// <ZFileList :items="[...saved, ...added]" :remove="(item) => (item instanceof File ? drop(item) : api.delete(item.id))" />
|
|
8
|
+
//
|
|
9
|
+
// `remove` may return a promise (a server delete): the row shows a spinner and its button is disabled until it
|
|
10
|
+
// settles. A rejected one leaves the row as it was and is handed to @remove-error, for the product's toast.
|
|
11
|
+
//
|
|
12
|
+
// With `preview`, an image `File` shows a thumbnail through an object URL, made on the client when the row first shows
|
|
13
|
+
// and revoked when the row goes or the list unmounts; a stored file shows its `thumbnail` URL, if it has one.
|
|
14
|
+
import { useLocale } from '@nuxt/ui/composables'
|
|
15
|
+
import { formatFileSize, type FileListItem } from '../utils/file-drop'
|
|
16
|
+
|
|
17
|
+
const props = withDefaults(
|
|
18
|
+
defineProps<{
|
|
19
|
+
/** The rows, in order: `File`s and files already stored (`{ id, name, size, type? }`). */
|
|
20
|
+
items: readonly FileListItem[]
|
|
21
|
+
/** Removes one row; may return a promise, shown as the row's spinner. Without it, no Remove buttons. */
|
|
22
|
+
remove?: (item: FileListItem, index: number) => unknown
|
|
23
|
+
/** The sizes' locale (BCP 47). Unset: Nuxt UI's locale. */
|
|
24
|
+
locale?: string
|
|
25
|
+
/** The Remove button's word; its accessible name is this and the file's name: "Remove agenda.pdf". */
|
|
26
|
+
removeLabel?: string
|
|
27
|
+
/** Sizes in steps of 1024 (`1.2 KB`); `false` for decimal units (`1.2 kB`, 1 kB = 1000 bytes). */
|
|
28
|
+
binary?: boolean
|
|
29
|
+
/** Show a thumbnail in place of the icon: an image `File`'s own, or a stored file's `thumbnail` URL. */
|
|
30
|
+
preview?: boolean
|
|
31
|
+
}>(),
|
|
32
|
+
{ remove: undefined, locale: undefined, removeLabel: 'Remove', binary: true, preview: false },
|
|
33
|
+
)
|
|
34
|
+
const emit = defineEmits<{
|
|
35
|
+
/** A `remove` promise rejected: the row stays. */
|
|
36
|
+
removeError: [error: unknown, item: FileListItem]
|
|
37
|
+
}>()
|
|
38
|
+
defineSlots<{
|
|
39
|
+
/** Before the row's icon or thumbnail: a checkbox, say. */
|
|
40
|
+
leading?(props: { item: FileListItem; index: number; removing: boolean }): unknown
|
|
41
|
+
/** After the size, before Remove: a badge, say. */
|
|
42
|
+
trailing?(props: { item: FileListItem; index: number; removing: boolean }): unknown
|
|
43
|
+
}>()
|
|
44
|
+
|
|
45
|
+
const { locale: uiLocale } = useLocale()
|
|
46
|
+
const lang = computed(() => props.locale ?? uiLocale.value.code)
|
|
47
|
+
|
|
48
|
+
// `File` isn't one of the globals a template can see, so the test is made here.
|
|
49
|
+
const isStored = (item: FileListItem) => !(item instanceof File)
|
|
50
|
+
|
|
51
|
+
// A stored file is keyed by its id; a File, which has none, by identity.
|
|
52
|
+
const fileKeys = new WeakMap<File, string>()
|
|
53
|
+
let counter = 0
|
|
54
|
+
function keyOf(item: FileListItem) {
|
|
55
|
+
if (!(item instanceof File)) return `id:${item.id}`
|
|
56
|
+
let k = fileKeys.get(item)
|
|
57
|
+
if (!k) fileKeys.set(item, (k = `file:${++counter}`))
|
|
58
|
+
return k
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The icon for a type, by its MIME type or, without one, its extension. */
|
|
62
|
+
function iconOf(item: FileListItem) {
|
|
63
|
+
const type = (item.type ?? '').toLowerCase()
|
|
64
|
+
const ext = item.name.toLowerCase().split('.').pop() ?? ''
|
|
65
|
+
if (type.startsWith('image/') || /^(png|jpe?g|gif|webp|heic|svg|bmp|tiff?)$/.test(ext)) return 'i-lucide-file-image'
|
|
66
|
+
if (type.startsWith('video/') || /^(mp4|mov|webm|avi|mkv)$/.test(ext)) return 'i-lucide-file-video'
|
|
67
|
+
if (type.startsWith('audio/') || /^(mp3|wav|m4a|flac|ogg)$/.test(ext)) return 'i-lucide-file-audio'
|
|
68
|
+
if (/spreadsheet|excel|csv/.test(type) || /^(xlsx?|csv|ods|numbers)$/.test(ext)) return 'i-lucide-file-spreadsheet'
|
|
69
|
+
if (/zip|compressed|tar|gzip|7z|rar/.test(type) || /^(zip|tar|gz|tgz|7z|rar)$/.test(ext)) return 'i-lucide-file-archive'
|
|
70
|
+
if (type === 'application/pdf' || type.startsWith('text/') || /word|document|rtf/.test(type) || /^(pdf|txt|md|docx?|rtf|odt|pages)$/.test(ext)) return 'i-lucide-file-text'
|
|
71
|
+
return 'i-lucide-file'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Thumbnails. An object URL holds its File in memory until revoked, so one is made only on the client (never during
|
|
75
|
+
// SSR, where it would leak and mismatch), only for an image File in a row that is shown, and revoked as soon as that
|
|
76
|
+
// File leaves `items`, `preview` is turned off, or the list unmounts.
|
|
77
|
+
const onClient = ref(false)
|
|
78
|
+
onMounted(() => { onClient.value = true })
|
|
79
|
+
const objectUrls = new Map<File, string>()
|
|
80
|
+
/** Thumbnails that failed to load (a type the browser can't draw, such as HEIC): those rows show the icon. */
|
|
81
|
+
const broken = ref(new Set<string>())
|
|
82
|
+
|
|
83
|
+
function thumbnailOf(item: FileListItem): string | undefined {
|
|
84
|
+
if (!props.preview || broken.value.has(keyOf(item))) return undefined
|
|
85
|
+
if (!(item instanceof File)) return item.thumbnail || undefined
|
|
86
|
+
if (!onClient.value || !item.type.toLowerCase().startsWith('image/')) return undefined
|
|
87
|
+
let url = objectUrls.get(item)
|
|
88
|
+
if (!url) objectUrls.set(item, (url = URL.createObjectURL(item)))
|
|
89
|
+
return url
|
|
90
|
+
}
|
|
91
|
+
function revoke(keep: ReadonlySet<FileListItem>) {
|
|
92
|
+
for (const [file, url] of objectUrls) {
|
|
93
|
+
if (keep.has(file)) continue
|
|
94
|
+
URL.revokeObjectURL(url)
|
|
95
|
+
objectUrls.delete(file)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
watch([() => props.items, () => props.preview], ([items, preview]) => revoke(preview ? new Set(items) : new Set()), { deep: 1 })
|
|
99
|
+
onBeforeUnmount(() => revoke(new Set()))
|
|
100
|
+
|
|
101
|
+
const removing = ref(new Set<string>())
|
|
102
|
+
async function onRemove(item: FileListItem, index: number) {
|
|
103
|
+
if (!props.remove) return
|
|
104
|
+
const key = keyOf(item)
|
|
105
|
+
if (removing.value.has(key)) return
|
|
106
|
+
removing.value = new Set(removing.value).add(key)
|
|
107
|
+
try {
|
|
108
|
+
await props.remove(item, index)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
emit('removeError', error, item)
|
|
111
|
+
} finally {
|
|
112
|
+
const next = new Set(removing.value)
|
|
113
|
+
next.delete(key)
|
|
114
|
+
removing.value = next
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
</script>
|
|
118
|
+
|
|
119
|
+
<template>
|
|
120
|
+
<ul v-if="items.length" class="flex flex-col gap-1" data-file-list>
|
|
121
|
+
<li
|
|
122
|
+
v-for="(item, index) in items"
|
|
123
|
+
:key="keyOf(item)"
|
|
124
|
+
:aria-busy="removing.has(keyOf(item)) || undefined"
|
|
125
|
+
class="flex items-center gap-2 rounded-md bg-elevated px-3 py-2"
|
|
126
|
+
:data-file-row="item.name"
|
|
127
|
+
:data-stored="isStored(item) ? '' : undefined"
|
|
128
|
+
>
|
|
129
|
+
<slot name="leading" :item="item" :index="index" :removing="removing.has(keyOf(item))" />
|
|
130
|
+
<img
|
|
131
|
+
v-if="thumbnailOf(item)"
|
|
132
|
+
:src="thumbnailOf(item)"
|
|
133
|
+
alt=""
|
|
134
|
+
class="size-8 shrink-0 rounded-sm bg-default object-cover"
|
|
135
|
+
data-file-thumbnail
|
|
136
|
+
@error="broken = new Set(broken).add(keyOf(item))"
|
|
137
|
+
>
|
|
138
|
+
<UIcon v-else :name="iconOf(item)" class="size-4 shrink-0 text-dimmed" aria-hidden="true" />
|
|
139
|
+
<span class="min-w-0 flex-1 truncate text-sm text-default">{{ item.name }}</span>
|
|
140
|
+
<span class="shrink-0 text-xs text-muted tabular-nums">{{ formatFileSize(item.size, lang, { binary }) }}</span>
|
|
141
|
+
<slot name="trailing" :item="item" :index="index" :removing="removing.has(keyOf(item))" />
|
|
142
|
+
<UButton
|
|
143
|
+
v-if="remove"
|
|
144
|
+
type="button"
|
|
145
|
+
size="xs"
|
|
146
|
+
variant="ghost"
|
|
147
|
+
color="error"
|
|
148
|
+
icon="i-lucide-x"
|
|
149
|
+
:loading="removing.has(keyOf(item))"
|
|
150
|
+
:aria-label="`${removeLabel} ${item.name}`"
|
|
151
|
+
@click="onRemove(item, index)"
|
|
152
|
+
/>
|
|
153
|
+
</li>
|
|
154
|
+
</ul>
|
|
155
|
+
</template>
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
<script setup lang="ts" generic="T extends object, VK extends (keyof T & string) | undefined = undefined">
|
|
2
|
+
// A faceted filter for a list or table toolbar: a button naming the filter, with a chip per chosen value (up to
|
|
3
|
+
// `maxShown`, then one chip saying how many), that opens a searchable list of options to check, each with an
|
|
4
|
+
// optional count of the records it matches and an optional icon, and All and Clear under them.
|
|
5
|
+
//
|
|
6
|
+
// <ZFilterSelect v-model="statuses" label="Status" :options="statusOptions" value-key="value" />
|
|
7
|
+
// <ZFilterSelect v-model="owners" label="Owner" :options="people" value-key="id" label-key="name" count-key="open" />
|
|
8
|
+
//
|
|
9
|
+
// The list is Nuxt UI's UListbox (reka-ui's Listbox: role="listbox", aria-multiselectable, an aria-activedescendant
|
|
10
|
+
// search box) in a UPopover. v-model is an array of values: each option's `valueKey` field, or the option itself
|
|
11
|
+
// without one (then compared by identity). Values the options no longer hold are kept, not dropped.
|
|
12
|
+
//
|
|
13
|
+
// SEARCH is literal and case-insensitive (utils/filter-match.ts): what is typed is text, never a pattern, so `(` or
|
|
14
|
+
// `.` match themselves. The options keep their order, and each label is drawn as segments with the matches in <mark>,
|
|
15
|
+
// never through v-html. Checking or unchecking an option clears the search, so the next pick
|
|
16
|
+
// starts from the whole list. All checks every option on screen (the matches, while searching) and leaves the search
|
|
17
|
+
// as it is; Clear empties the filter.
|
|
18
|
+
//
|
|
19
|
+
// KEYBOARD. Focus opens in the search box. The arrow keys, Home and End move through the options; Enter checks one.
|
|
20
|
+
// Space checks one too, from the search box, while the box is empty or once the arrows have moved since the last
|
|
21
|
+
// character typed; otherwise it is a space in the search. Escape closes the panel, and focus goes back to the
|
|
22
|
+
// button. Without `searchable`, focus opens in the list itself and Space is reka-ui's own. Each change is
|
|
23
|
+
// announced politely ("Status: 2 selected."), and the button's name carries the chips ("Status, Open, Blocked" or
|
|
24
|
+
// "Status, 3 selected").
|
|
25
|
+
//
|
|
26
|
+
// `ui` takes classes for the parts, as Nuxt UI's components do, merged over the defaults with tailwind-merge (the
|
|
27
|
+
// same merge Nuxt UI's own `ui` goes through), so an override replaces the default it conflicts with rather than
|
|
28
|
+
// sitting beside it. A root `class` lands on the wrapper.
|
|
29
|
+
import type { ListboxItem } from '@nuxt/ui'
|
|
30
|
+
import { useLocale } from '@nuxt/ui/composables'
|
|
31
|
+
import { twMerge } from 'tailwind-merge'
|
|
32
|
+
import { filterSelectMatches, filterSelectSegments } from '../utils/filter-match'
|
|
33
|
+
|
|
34
|
+
type Value = VK extends keyof T ? T[VK] : T
|
|
35
|
+
type Part = 'trigger' | 'badge' | 'content' | 'search' | 'list' | 'item' | 'itemIcon' | 'itemLabel' | 'itemCount' | 'footer'
|
|
36
|
+
|
|
37
|
+
defineOptions({ inheritAttrs: false })
|
|
38
|
+
|
|
39
|
+
const props = withDefaults(
|
|
40
|
+
defineProps<{
|
|
41
|
+
/** The filter's name: the button's text, and the list's accessible name. "Status". */
|
|
42
|
+
label: string
|
|
43
|
+
/** The options, in the order they are listed. */
|
|
44
|
+
options: T[]
|
|
45
|
+
/** The option field that is its value in v-model. Unset: the option itself. */
|
|
46
|
+
valueKey?: VK
|
|
47
|
+
/** The option field shown as its label. Unset: `label`. */
|
|
48
|
+
labelKey?: keyof T & string
|
|
49
|
+
/** The option field holding how many records it matches, shown beside it. Unset: `count`. */
|
|
50
|
+
countKey?: keyof T & string
|
|
51
|
+
/** The option field holding its icon. Unset: `icon`. A product's own icons are the product's to bundle. */
|
|
52
|
+
iconKey?: keyof T & string
|
|
53
|
+
/** The option field holding classes for its icon ("text-success"). Unset: `iconClass`. Without any, `text-dimmed`. */
|
|
54
|
+
iconClassKey?: keyof T & string
|
|
55
|
+
/** How many chosen values the button names, a chip each, before it shows one "N selected" chip. 0: always a count. */
|
|
56
|
+
maxShown?: number
|
|
57
|
+
/** Whether the panel has an All button. Without it, the footer shows only while something is chosen. */
|
|
58
|
+
showSelectAll?: boolean
|
|
59
|
+
/** Classes for the parts, merged over the defaults. */
|
|
60
|
+
ui?: Partial<Record<Part, string>>
|
|
61
|
+
/** The button's leading icon; '' for none. */
|
|
62
|
+
icon?: string
|
|
63
|
+
/** Whether the panel has a search box. */
|
|
64
|
+
searchable?: boolean
|
|
65
|
+
/** Unset: "Search". The search box is named "Search <label>" for assistive tech. */
|
|
66
|
+
searchPlaceholder?: string
|
|
67
|
+
/** The button's size, as UButton's. */
|
|
68
|
+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
|
69
|
+
disabled?: boolean
|
|
70
|
+
/** UPopover's `portal`. */
|
|
71
|
+
portal?: boolean | string
|
|
72
|
+
allLabel?: string
|
|
73
|
+
clearLabel?: string
|
|
74
|
+
/** Shown when nothing matches the search. Unset: "No matches". */
|
|
75
|
+
emptyLabel?: string
|
|
76
|
+
/** The count chip's words, also read in the button's name; "3 selected" unless given. */
|
|
77
|
+
selectedLabel?: (count: number) => string
|
|
78
|
+
/** What a screen reader hears when the selection changes; "Status: 2 selected." unless given. */
|
|
79
|
+
announce?: (change: { label: string; count: number }) => string
|
|
80
|
+
}>(),
|
|
81
|
+
{
|
|
82
|
+
valueKey: undefined,
|
|
83
|
+
labelKey: undefined,
|
|
84
|
+
countKey: undefined,
|
|
85
|
+
iconKey: undefined,
|
|
86
|
+
iconClassKey: undefined,
|
|
87
|
+
maxShown: 2,
|
|
88
|
+
showSelectAll: true,
|
|
89
|
+
ui: undefined,
|
|
90
|
+
icon: 'i-lucide-list-filter',
|
|
91
|
+
searchable: true,
|
|
92
|
+
searchPlaceholder: 'Search',
|
|
93
|
+
size: 'sm',
|
|
94
|
+
disabled: false,
|
|
95
|
+
portal: true,
|
|
96
|
+
allLabel: 'All',
|
|
97
|
+
clearLabel: 'Clear',
|
|
98
|
+
emptyLabel: 'No matches',
|
|
99
|
+
selectedLabel: undefined,
|
|
100
|
+
announce: undefined,
|
|
101
|
+
},
|
|
102
|
+
)
|
|
103
|
+
const model = defineModel<Value[]>({ default: () => [] })
|
|
104
|
+
defineSlots<{
|
|
105
|
+
/** The option's label, in place of its highlighted text. */
|
|
106
|
+
'option-label'?(props: { option: T; segments: ReturnType<typeof filterSelectSegments>; selected: boolean }): unknown
|
|
107
|
+
}>()
|
|
108
|
+
|
|
109
|
+
const uid = useId()
|
|
110
|
+
const { code: localeCode } = useLocale()
|
|
111
|
+
const open = ref(false)
|
|
112
|
+
const query = ref('')
|
|
113
|
+
const panel = ref<HTMLElement | null>(null)
|
|
114
|
+
|
|
115
|
+
const field = (option: T, key: string) => (option as Record<string, unknown>)[key]
|
|
116
|
+
const valueOf = (option: T) => (props.valueKey ? field(option, props.valueKey) : option) as Value
|
|
117
|
+
const labelOf = (option: T) => String(field(option, props.labelKey ?? 'label') ?? '')
|
|
118
|
+
const countOf = (option: T) => {
|
|
119
|
+
const n = field(option, props.countKey ?? 'count')
|
|
120
|
+
return typeof n === 'number' ? n : undefined
|
|
121
|
+
}
|
|
122
|
+
const iconOf = (option: T) => {
|
|
123
|
+
const name = field(option, props.iconKey ?? 'icon')
|
|
124
|
+
return typeof name === 'string' && name ? name : undefined
|
|
125
|
+
}
|
|
126
|
+
const iconClassOf = (option: T) => {
|
|
127
|
+
const value = field(option, props.iconClassKey ?? 'iconClass')
|
|
128
|
+
return typeof value === 'string' && value ? value : undefined
|
|
129
|
+
}
|
|
130
|
+
const isDisabled = (option: T) => field(option, 'disabled') === true
|
|
131
|
+
/** A part's classes: the defaults, then the caller's `ui` for it, conflicts going to the caller. */
|
|
132
|
+
const cls = (part: Part, ...defaults: (string | undefined)[]) => twMerge(...defaults, props.ui?.[part])
|
|
133
|
+
/** An option's icon: the default color only when the option brings none; the option's own over `ui.itemIcon`. */
|
|
134
|
+
const iconClasses = (option: T) => {
|
|
135
|
+
const own = iconClassOf(option)
|
|
136
|
+
return twMerge('size-4 shrink-0', own ? undefined : 'text-dimmed', props.ui?.itemIcon, own)
|
|
137
|
+
}
|
|
138
|
+
// Values are compared the same way here and in reka-ui (given as UListbox's `by`): a field's value by ===, an option
|
|
139
|
+
// by identity — of the raw object, since a model held in ref() hands back reactive proxies of the options.
|
|
140
|
+
const same = (a: unknown, b: unknown) => Object.is(toRaw(a), toRaw(b))
|
|
141
|
+
const has = (value: Value) => model.value.some((v) => same(v, value))
|
|
142
|
+
const isSelected = (option: T) => has(valueOf(option))
|
|
143
|
+
|
|
144
|
+
const shown = computed(() => props.options.filter((o) => filterSelectMatches(labelOf(o), query.value)))
|
|
145
|
+
const checkable = computed(() => shown.value.filter((o) => !isDisabled(o)))
|
|
146
|
+
const allSelected = computed(() => checkable.value.every(isSelected))
|
|
147
|
+
|
|
148
|
+
const countFormat = computed(() => new Intl.NumberFormat(localeCode.value))
|
|
149
|
+
const formatCount = (n: number) => countFormat.value.format(n)
|
|
150
|
+
|
|
151
|
+
/** The chosen values' labels in the options' order; values no option holds follow, as written. */
|
|
152
|
+
const chosenLabels = computed(() => [
|
|
153
|
+
...props.options.filter(isSelected).map(labelOf),
|
|
154
|
+
...model.value.filter((v) => !props.options.some((o) => same(valueOf(o), v))).map((v) => String(v)),
|
|
155
|
+
])
|
|
156
|
+
/** The button's chips: one per value up to `maxShown`, otherwise one saying how many. */
|
|
157
|
+
const chips = computed(() => {
|
|
158
|
+
const n = model.value.length
|
|
159
|
+
if (!n) return []
|
|
160
|
+
return n <= props.maxShown ? chosenLabels.value : [props.selectedLabel?.(n) ?? `${n} selected`]
|
|
161
|
+
})
|
|
162
|
+
// The button's name, the chips said in words: "Status, Open, Blocked" or "Status, 3 selected". It starts with the
|
|
163
|
+
// visible label, so speech input still finds the button by what it shows.
|
|
164
|
+
const buttonName = computed(() => (chips.value.length ? `${props.label}, ${chips.value.join(', ')}` : undefined))
|
|
165
|
+
|
|
166
|
+
// A chip cut short by the cap carries its whole text as a title. Measured once drawn, and again as the pointer or
|
|
167
|
+
// focus reaches the button, since a web font arriving late can change what fits.
|
|
168
|
+
const chipBox = ref<HTMLElement | null>(null)
|
|
169
|
+
const cutShort = ref<boolean[]>([])
|
|
170
|
+
function measureChips() {
|
|
171
|
+
cutShort.value = [...(chipBox.value?.children ?? [])].map((el) => el.scrollWidth > el.clientWidth)
|
|
172
|
+
}
|
|
173
|
+
watch([chips, chipBox], measureChips, { flush: 'post' })
|
|
174
|
+
|
|
175
|
+
function selectAll() {
|
|
176
|
+
model.value = [...model.value, ...checkable.value.map(valueOf).filter((v) => !has(v))]
|
|
177
|
+
refocus()
|
|
178
|
+
}
|
|
179
|
+
function clear() {
|
|
180
|
+
model.value = []
|
|
181
|
+
refocus()
|
|
182
|
+
}
|
|
183
|
+
/** After All or Clear, focus goes back into the list's search box (or the list), not to a button that may go disabled. */
|
|
184
|
+
function refocus() {
|
|
185
|
+
panel.value?.querySelector<HTMLElement>('input, [role="listbox"]')?.focus()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Space checks the highlighted option from the search box while the box is empty, or once the arrow keys have moved
|
|
189
|
+
// since the last character typed; otherwise it is part of the search.
|
|
190
|
+
let navigated = false
|
|
191
|
+
const NAVIGATION = new Set(['ArrowDown', 'ArrowUp', 'Home', 'End', 'PageUp', 'PageDown'])
|
|
192
|
+
function onKeydown(e: KeyboardEvent) {
|
|
193
|
+
if (NAVIGATION.has(e.key)) {
|
|
194
|
+
navigated = true
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
if (e.key !== ' ' || !(e.target instanceof HTMLInputElement)) return
|
|
198
|
+
if (query.value && !navigated) return
|
|
199
|
+
const option = panel.value?.querySelector<HTMLElement>('[role="option"][data-highlighted]')
|
|
200
|
+
if (!option) return
|
|
201
|
+
e.preventDefault()
|
|
202
|
+
option.click()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// The list is named for the filter, and the search box says which list it drives. UListbox puts neither attribute on
|
|
206
|
+
// the elements that need them, so they are set once the panel is drawn.
|
|
207
|
+
watch(
|
|
208
|
+
[panel, () => props.label],
|
|
209
|
+
([el]) => {
|
|
210
|
+
const list = el?.querySelector<HTMLElement>('[role="listbox"]')
|
|
211
|
+
if (!list) return
|
|
212
|
+
list.setAttribute('aria-label', props.label)
|
|
213
|
+
list.id = `${uid}-list`
|
|
214
|
+
el?.querySelector('input')?.setAttribute('aria-controls', list.id)
|
|
215
|
+
},
|
|
216
|
+
{ flush: 'post' },
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
/** A check from the list (click, Enter or Space): the new selection, and the search cleared for the next pick. */
|
|
220
|
+
function onPick(values: Value[]) {
|
|
221
|
+
model.value = values
|
|
222
|
+
query.value = ''
|
|
223
|
+
navigated = false
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// A closed panel forgets its search.
|
|
227
|
+
watch(open, (isOpen) => {
|
|
228
|
+
if (!isOpen) query.value = ''
|
|
229
|
+
navigated = false
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
// The polite announcement of the selection. Cleared first and set again in a later task, so the same words twice are
|
|
233
|
+
// heard twice; a newer announcement replaces one still pending.
|
|
234
|
+
const announcement = ref('')
|
|
235
|
+
let announceTimer: ReturnType<typeof setTimeout> | undefined
|
|
236
|
+
watch(model, (values) => {
|
|
237
|
+
const count = values.length
|
|
238
|
+
const text = props.announce?.({ label: props.label, count }) ?? `${props.label}: ${count ? count : 'none'} selected.`
|
|
239
|
+
announcement.value = ''
|
|
240
|
+
clearTimeout(announceTimer)
|
|
241
|
+
announceTimer = setTimeout(() => { announcement.value = text }, 100)
|
|
242
|
+
})
|
|
243
|
+
onBeforeUnmount(() => clearTimeout(announceTimer))
|
|
244
|
+
|
|
245
|
+
const searchInput = computed(() => props.searchable
|
|
246
|
+
? { placeholder: props.searchPlaceholder, icon: 'i-lucide-search', autocomplete: 'off', 'aria-label': `Search ${props.label}`, 'data-search': '', class: props.ui?.search }
|
|
247
|
+
: false)
|
|
248
|
+
</script>
|
|
249
|
+
|
|
250
|
+
<template>
|
|
251
|
+
<div class="inline-flex" data-filter-select v-bind="$attrs">
|
|
252
|
+
<UPopover v-model:open="open" :portal="portal" :content="{ side: 'bottom', align: 'start', sideOffset: 6 }" :ui="{ content: cls('content', 'w-64') }">
|
|
253
|
+
<UButton
|
|
254
|
+
type="button"
|
|
255
|
+
variant="outline"
|
|
256
|
+
color="neutral"
|
|
257
|
+
:size="size"
|
|
258
|
+
:icon="icon || undefined"
|
|
259
|
+
:disabled="disabled"
|
|
260
|
+
:aria-label="buttonName"
|
|
261
|
+
:class="ui?.trigger"
|
|
262
|
+
data-trigger
|
|
263
|
+
@pointerenter="measureChips"
|
|
264
|
+
@focus="measureChips"
|
|
265
|
+
>
|
|
266
|
+
<span>{{ label }}</span>
|
|
267
|
+
<!-- Not UBadges: their block padding and line height would make a button with chips taller than one without,
|
|
268
|
+
and a theme may restyle badges (Forest sets them in the mono face). These take the button's own line
|
|
269
|
+
height and font, so the button is the same height whatever it holds and the values read as words. The
|
|
270
|
+
row is capped, and a grid rather than a flex row: its tracks grow each chip toward its whole width and give
|
|
271
|
+
what is left to the longer ones, so a short chip ("Low") stays whole and only a long one truncates. -->
|
|
272
|
+
<span v-if="chips.length" ref="chipBox" class="grid min-w-0 max-w-40 auto-cols-[minmax(0,max-content)] grid-flow-col gap-1" data-badges>
|
|
273
|
+
<span
|
|
274
|
+
v-for="(chip, i) in chips"
|
|
275
|
+
:key="i"
|
|
276
|
+
:class="cls('badge', 'min-w-0 truncate rounded-sm bg-elevated px-1.5 text-[0.875em] text-default tabular-nums')"
|
|
277
|
+
:title="cutShort[i] ? chip : undefined"
|
|
278
|
+
data-badge
|
|
279
|
+
>{{ chip }}</span>
|
|
280
|
+
</span>
|
|
281
|
+
</UButton>
|
|
282
|
+
|
|
283
|
+
<template #content>
|
|
284
|
+
<div ref="panel" data-filter-panel @keydown="onKeydown" @input="navigated = false">
|
|
285
|
+
<UListbox
|
|
286
|
+
:model-value="(model as never)"
|
|
287
|
+
:search-term="query"
|
|
288
|
+
:items="(shown as ListboxItem[])"
|
|
289
|
+
multiple
|
|
290
|
+
:value-key="valueKey"
|
|
291
|
+
:by="same"
|
|
292
|
+
:filter="searchInput"
|
|
293
|
+
ignore-filter
|
|
294
|
+
:autofocus="searchable"
|
|
295
|
+
:ui="{ root: 'rounded-none ring-0', content: cls('list', 'max-h-72'), item: ui?.item }"
|
|
296
|
+
@update:model-value="onPick($event as Value[])"
|
|
297
|
+
@update:search-term="query = $event"
|
|
298
|
+
>
|
|
299
|
+
<template #item="{ item }">
|
|
300
|
+
<span
|
|
301
|
+
aria-hidden="true"
|
|
302
|
+
:class="[
|
|
303
|
+
'grid size-4 shrink-0 place-items-center rounded-sm border',
|
|
304
|
+
isSelected(item as T) ? 'border-primary bg-primary text-inverted' : 'border-accented bg-default',
|
|
305
|
+
]"
|
|
306
|
+
data-check
|
|
307
|
+
>
|
|
308
|
+
<UIcon v-if="isSelected(item as T)" name="i-lucide-check" class="size-3" />
|
|
309
|
+
</span>
|
|
310
|
+
<UIcon v-if="iconOf(item as T)" :name="iconOf(item as T)!" :class="iconClasses(item as T)" aria-hidden="true" data-icon />
|
|
311
|
+
<span :class="cls('itemLabel', 'min-w-0 flex-1 truncate')" data-label>
|
|
312
|
+
<slot name="option-label" :option="(item as T)" :segments="filterSelectSegments(labelOf(item as T), query)" :selected="isSelected(item as T)">
|
|
313
|
+
<template v-for="(segment, i) in filterSelectSegments(labelOf(item as T), query)" :key="i">
|
|
314
|
+
<mark v-if="segment.match" class="rounded-xs bg-primary/15 font-semibold text-highlighted">{{ segment.text }}</mark>
|
|
315
|
+
<template v-else>{{ segment.text }}</template>
|
|
316
|
+
</template>
|
|
317
|
+
</slot>
|
|
318
|
+
</span>
|
|
319
|
+
<span v-if="countOf(item as T) !== undefined" :class="cls('itemCount', 'ms-auto shrink-0 text-xs tabular-nums text-muted')" data-count>
|
|
320
|
+
{{ formatCount(countOf(item as T)!) }}
|
|
321
|
+
</span>
|
|
322
|
+
</template>
|
|
323
|
+
<template #empty>{{ emptyLabel }}</template>
|
|
324
|
+
</UListbox>
|
|
325
|
+
|
|
326
|
+
<!-- Without All, the footer is there only while Clear has something to clear. -->
|
|
327
|
+
<div v-if="showSelectAll || model.length" :class="cls('footer', 'flex gap-1 border-t border-default p-1')" data-footer>
|
|
328
|
+
<UButton v-if="showSelectAll" type="button" size="xs" variant="ghost" color="neutral" icon="i-lucide-check-check" class="flex-1 justify-center" :disabled="!checkable.length || allSelected" data-all @click="selectAll">
|
|
329
|
+
{{ allLabel }}
|
|
330
|
+
</UButton>
|
|
331
|
+
<UButton type="button" size="xs" variant="ghost" color="neutral" icon="i-lucide-x" class="flex-1 justify-center" :disabled="!model.length" data-clear @click="clear">
|
|
332
|
+
{{ clearLabel }}
|
|
333
|
+
</UButton>
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
</template>
|
|
337
|
+
</UPopover>
|
|
338
|
+
<p class="sr-only" role="status" aria-live="polite" data-announce>{{ announcement }}</p>
|
|
339
|
+
</div>
|
|
340
|
+
</template>
|