chore(CommandPalette): support selected with modelValue

This commit is contained in:
Benjamin Canac
2022-07-18 15:08:10 +02:00
parent 9e0edc27ab
commit 8a57ab60c7
2 changed files with 34 additions and 6 deletions

View File

@@ -1,5 +1,10 @@
<template> <template>
<Combobox @update:modelValue="onSelect"> <Combobox
:model-value="modelValue"
:multiple="multiple"
:nullable="nullable"
@update:model-value="onSelect"
>
<div class="flex flex-col flex-1 min-h-0 divide-y divide-gray-100 dark:divide-gray-800"> <div class="flex flex-col flex-1 min-h-0 divide-y divide-gray-100 dark:divide-gray-800">
<div class="relative flex items-center"> <div class="relative flex items-center">
<UIcon :name="inputIcon" class="pointer-events-none absolute top-3.5 left-5 h-5 w-5 u-text-gray-400" aria-hidden="true" /> <UIcon :name="inputIcon" class="pointer-events-none absolute top-3.5 left-5 h-5 w-5 u-text-gray-400" aria-hidden="true" />
@@ -40,6 +45,18 @@ import type { Group, Command } from '../../types/command-palette'
import CommandPaletteGroup from './CommandPaletteGroup.vue' import CommandPaletteGroup from './CommandPaletteGroup.vue'
const props = defineProps({ const props = defineProps({
modelValue: {
type: [String, Number, Object, Array],
default: null
},
multiple: {
type: Boolean,
default: false
},
nullable: {
type: Boolean,
default: false
},
groups: { groups: {
type: Array as PropType<Group[]>, type: Array as PropType<Group[]>,
default: () => [] default: () => []
@@ -66,7 +83,9 @@ const props = defineProps({
} }
}) })
const emit = defineEmits(['select', 'close']) const router = useRouter()
const emit = defineEmits(['update:modelValue', 'close'])
const query = ref('') const query = ref('')
const comboboxInput = ref<ComponentPublicInstance<HTMLInputElement>>() const comboboxInput = ref<ComponentPublicInstance<HTMLInputElement>>()
@@ -111,7 +130,13 @@ function onSelect (option) {
return return
} }
emit('select', option, { query: query.value }) if (option.click) {
option.click()
} else if (option.to) {
router.push(option.to)
}
emit('update:modelValue', option)
// waiting for modal to be closed // waiting for modal to be closed
setTimeout(() => { setTimeout(() => {

View File

@@ -8,12 +8,12 @@
<ComboboxOption <ComboboxOption
v-for="(command, index) of group.commands" v-for="(command, index) of group.commands"
:key="`${group.key}-${index}`" :key="`${group.key}-${index}`"
v-slot="{ active }" v-slot="{ active, selected }"
:value="command" :value="command"
:disabled="command.disabled" :disabled="command.disabled"
as="template" as="template"
> >
<li :class="['flex justify-between select-none items-center rounded-md px-3 py-2 u-text-gray-400 gap-3', active && 'bg-gray-100 dark:bg-gray-800 u-text-gray-900', command.disabled ? 'cursor-not-allowed' : 'cursor-pointer']"> <li :class="['flex justify-between select-none items-center rounded-md px-3 py-2 u-text-gray-400 gap-3 relative', active && 'bg-gray-100 dark:bg-gray-800 u-text-gray-900', command.disabled ? 'cursor-not-allowed' : 'cursor-pointer']">
<div class="flex items-center flex-1 gap-3 min-w-0"> <div class="flex items-center flex-1 gap-3 min-w-0">
<UIcon v-if="command.icon" :name="command.icon" :class="['h-4 w-4', command.iconClass]" class="flex-shrink-0" aria-hidden="true" /> <UIcon v-if="command.icon" :name="command.icon" :class="['h-4 w-4', command.iconClass]" class="flex-shrink-0" aria-hidden="true" />
<UAvatar <UAvatar
@@ -32,7 +32,10 @@
</div> </div>
</div> </div>
<span v-if="active && command.suffix" class="flex-none u-text-gray-500">{{ command.suffix }}</span> <span v-if="selected" class="absolute inset-y-0 right-0 flex items-center pr-2 u-text-gray-900">
<UIcon name="heroicons-outline:check" class="h-5 w-5" aria-hidden="true" />
</span>
<span v-else-if="active && command.suffix" class="flex-none u-text-gray-500">{{ command.suffix }}</span>
<span v-else-if="command.shortcuts?.length" class="flex-none text-xs font-semibold u-text-gray-500"> <span v-else-if="command.shortcuts?.length" class="flex-none text-xs font-semibold u-text-gray-500">
<kbd v-for="shortcut of command.shortcuts" :key="shortcut" class="font-sans">{{ shortcut }}</kbd> <kbd v-for="shortcut of command.shortcuts" :key="shortcut" class="font-sans">{{ shortcut }}</kbd>
</span> </span>