From ce28b041872eae242c8b93e90ce6de7c2b03c797 Mon Sep 17 00:00:00 2001 From: Sylvain Marroufin Date: Thu, 21 Jul 2022 15:46:05 +0200 Subject: [PATCH] chore(CommandPalette): improve customization options (#71) Co-authored-by: Benjamin Canac --- docs/pages/examples.vue | 16 +++---- .../components/navigation/CommandPalette.vue | 20 +++++++-- .../navigation/CommandPaletteGroup.vue | 42 ++++++++++++------- src/runtime/types/command-palette.d.ts | 4 ++ 4 files changed, 57 insertions(+), 25 deletions(-) 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 @@