diff --git a/docs/pages/examples.vue b/docs/pages/examples.vue index 5555eca1..f6c137d9 100644 --- a/docs/pages/examples.vue +++ b/docs/pages/examples.vue @@ -176,7 +176,7 @@ - + @@ -208,7 +208,7 @@ - + @@ -251,11 +251,11 @@ const isModalOpen = ref(false) const people = [ - { id: 1, label: 'Durward Reynolds', disabled: false }, - { id: 2, label: 'Kenton Towne', disabled: false }, - { id: 3, label: 'Therese Wunsch', disabled: false }, - { id: 4, label: 'Benedict Kessler', disabled: true }, - { id: 5, label: 'Katelyn Rohan', disabled: false } + { id: 1, name: 'Durward Reynolds', disabled: false }, + { id: 2, name: 'Kenton Towne', disabled: false }, + { id: 3, name: 'Therese Wunsch', disabled: false }, + { id: 4, name: 'Benedict Kessler', disabled: true }, + { id: 5, name: 'Katelyn Rohan', disabled: false, static: '1' } ] const form = reactive({ email: '', @@ -271,6 +271,8 @@ const form = reactive({ const { $toast } = useNuxtApp() +const customQuery = query => computed(() => query.value ? `${query.value} | =1` : '') + function toggleModalIsOpen () { isModalOpen.value = !isModalOpen.value } diff --git a/src/runtime/components/navigation/CommandPalette.vue b/src/runtime/components/navigation/CommandPalette.vue index f71e0226..4d6fbacf 100644 --- a/src/runtime/components/navigation/CommandPalette.vue +++ b/src/runtime/components/navigation/CommandPalette.vue @@ -21,7 +21,11 @@ - + + + + + @@ -77,6 +81,14 @@ const props = defineProps({ type: String, default: 'heroicons-outline:search' }, + groupAttribute: { + type: String, + default: 'label' + }, + commandAttribute: { + type: String, + default: 'label' + }, options: { type: Object as PropType>>, default: () => ({}) @@ -94,14 +106,14 @@ onMounted(() => { const options: ComputedRef>> = computed(() => defu({}, props.options, { fuseOptions: { - keys: ['label'] + keys: [props.commandAttribute] }, resultLimit: 12, matchAllWhenSearchEmpty: true })) const fuse = props.groups.reduce((acc, group) => { - const fuse = useFuse(query, group.commands, defu({}, group.options || {}, options.value)) + const fuse = useFuse(group.customQuery ? group.customQuery(query) : query, group.commands, defu({}, group.options || {}, options.value)) acc[group.key] = fuse return acc }, {}) @@ -124,7 +136,7 @@ function activateFirstOption () { } function onSelect (option: Command | Command[]) { - emit('update:modelValue', option) + emit('update:modelValue', option, { query: query.value }) // Clear input after selection if (!props.multiple) { diff --git a/src/runtime/components/navigation/CommandPaletteGroup.vue b/src/runtime/components/navigation/CommandPaletteGroup.vue index 2d095952..a4d1d031 100644 --- a/src/runtime/components/navigation/CommandPaletteGroup.vue +++ b/src/runtime/components/navigation/CommandPaletteGroup.vue @@ -1,7 +1,7 @@ - - {{ group.label }} + + {{ group[groupAttribute] }} @@ -13,32 +13,38 @@ :disabled="command.disabled" as="template" > - + - + - - {{ command.prefix }} - {{ command.label }} + + + {{ command.prefix }} + {{ command[commandAttribute] }} + {{ command.suffix }} + - - - - {{ command.suffix }} - - {{ shortcut }} + + + {{ group.active }} + + + {{ shortcut }} + + {{ group.inactive }} + @@ -54,6 +60,14 @@ defineProps({ group: { type: Object as PropType, required: true + }, + groupAttribute: { + type: String, + required: true + }, + commandAttribute: { + type: String, + required: true } }) diff --git a/src/runtime/types/command-palette.d.ts b/src/runtime/types/command-palette.d.ts index 00fd87aa..aa992590 100644 --- a/src/runtime/types/command-palette.d.ts +++ b/src/runtime/types/command-palette.d.ts @@ -1,3 +1,4 @@ +import type { Ref, ComputedRef } from 'vue' import type { UseFuseOptions } from '@vueuse/integrations/useFuse' export interface Command { @@ -15,6 +16,9 @@ export interface Command { export interface Group { key: string label: string + active?: string + inactive?: string commands: Command[] + customQuery?: (query: Ref) => ComputedRef options?: Partial> }