Skip to content

Commit

Permalink
fix(CommandPalette): reactivity issue + improve results
Browse files Browse the repository at this point in the history
Resolves #95, resolves #96
  • Loading branch information
benjamincanac committed Oct 24, 2022
1 parent 72dc0d0 commit ec9f670
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
29 changes: 13 additions & 16 deletions docs/pages/examples.vue
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,15 @@
<UCommandPalette
v-model="form.persons"
multiple
:placeholder="false"
:options="{
fuseOptions: {
includeMatches: true
}
}"
:groups="[{
key: 'persons',
commands: people,
customQuery,
options: {
fuseOptions: {
includeMatches: true,
useExtendedSearch: true,
keys: ['name']
}
}
commands: people
}]"
command-attribute="name"
/>
Expand Down Expand Up @@ -275,13 +273,14 @@
<script setup>
const isModalOpen = ref(false)
const people = [
const people = ref([
{ 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' }
]
{ id: 5, name: 'Katelyn Rohan', disabled: false }
])
const form = reactive({
email: '',
description: '',
Expand All @@ -290,8 +289,8 @@ const form = reactive({
notifications: [],
terms: false,
personId: null,
person: ref(people[0]),
persons: ref([people[0]])
person: ref(people.value[0]),
persons: ref([people.value[0]])
})
const { $toast } = useNuxtApp()
Expand Down Expand Up @@ -323,8 +322,6 @@ function openContextMenu () {
isContextMenuOpen.value = true
}
const customQuery = query => computed(() => query.value ? `${query.value} | =1` : '')
function toggleModalIsOpen () {
isModalOpen.value = !isModalOpen.value
}
Expand Down
43 changes: 24 additions & 19 deletions src/runtime/components/navigation/CommandPalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</CommandPaletteGroup>
</ComboboxOptions>

<div v-else class="flex flex-col items-center justify-center flex-1 px-6 py-14 sm:px-14">
<div v-else-if="placeholder" class="flex flex-col items-center justify-center flex-1 px-6 py-14 sm:px-14">
<Icon :name="emptyIcon" class="w-6 h-6 mx-auto u-text-gray-400" aria-hidden="true" />
<p class="mt-4 text-sm text-center u-text-gray-900">
{{ query ? "We couldn't find any items with that term. Please try again." : "We couldn't find any items." }}
Expand All @@ -45,6 +45,7 @@ import { ref, computed, onMounted } from 'vue'
import { Combobox, ComboboxInput, ComboboxOptions } from '@headlessui/vue'
import type { ComputedRef, PropType, ComponentPublicInstance } from 'vue'
import { useFuse } from '@vueuse/integrations/useFuse'
import { groupBy, map } from 'lodash-es'
import { defu } from 'defu'
import type { UseFuseOptions } from '@vueuse/integrations/useFuse'
import type { Group, Command } from '../../types/command-palette'
Expand Down Expand Up @@ -108,6 +109,10 @@ const props = defineProps({
autoselect: {
type: Boolean,
default: true
},
placeholder: {
type: Boolean,
default: true
}
})
Expand All @@ -117,13 +122,6 @@ const query = ref('')
const comboboxInput = ref<ComponentPublicInstance<HTMLInputElement>>()
const comboboxApi = ref(null)
defineExpose({
updateQuery: (q) => {
query.value = q
},
comboboxApi
})
onMounted(() => {
if (props.autoselect) {
activateFirstOption()
Expand All @@ -149,15 +147,14 @@ const options: ComputedRef<Partial<UseFuseOptions<Command>>> = computed(() => de
matchAllWhenSearchEmpty: true
}))
const fuse = props.groups.reduce((acc, group) => {
// FIXME: useFuse is not watching data correctly, so we need to add an id
const fuse = useFuse(group.customQuery ? group.customQuery(query) : query, group.commands, defu({}, group.options || {}, options.value))
acc[group.key] = fuse
return acc
}, {})
const commands = computed(() => props.groups.reduce((acc, group) => {
return acc.concat(group.commands.map(command => ({ ...command, group: group.key })))
}, [] as Command[]))
const groups = computed(() => props.groups.map((group) => {
const commands = fuse[group.key].results.value.map((result) => {
const { results } = useFuse(query, commands, options)
const groups = computed(() => map(groupBy(results.value, command => command.item.group), (results, key) => {
const commands = results.map((result) => {
const { item, ...data } = result
return {
Expand All @@ -167,10 +164,10 @@ const groups = computed(() => props.groups.map((group) => {
})
return {
...group,
commands: commands.slice(0, group.options?.resultLimit || options.value.resultLimit)
...props.groups.find(group => group.key === key),
commands: commands.slice(0, options.value.resultLimit)
}
}).filter(group => group.commands.length))
}))
// Methods
Expand Down Expand Up @@ -200,6 +197,14 @@ function onClear () {
emit('close')
}
}
defineExpose({
updateQuery: (q) => {
query.value = q
},
comboboxApi,
results
})
</script>

<script lang="ts">
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/types/command-palette.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Ref, ComputedRef } from 'vue'
import type { UseFuseOptions } from '@vueuse/integrations/useFuse'
import type { Avatar } from './avatar'

Expand All @@ -19,6 +18,5 @@ export interface Group {
active?: string
inactive?: string
commands: Command[]
customQuery?: (query: Ref<string>) => ComputedRef<string>
options?: Partial<UseFuseOptions<Command>>
}

1 comment on commit ec9f670

@vercel
Copy link

@vercel vercel bot commented on ec9f670 Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxtlabs.vercel.app
nuxthq-ui.vercel.app
ui-nuxtlabs.vercel.app

Please sign in to comment.