Skip to content

Commit

Permalink
feat(CommandPalette): handle filter attribute in groups (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Oct 26, 2023
1 parent 68f024f commit 8ba2a79
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
24 changes: 11 additions & 13 deletions docs/components/content/examples/CommandPaletteExampleAsync.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<script setup>
const groups = computed(() => {
return [{
key: 'users',
label: q => q && `Users matching “${q}”...`,
search: async (q) => {
if (!q) {
return []
}
const groups = [{
key: 'users',
label: q => q && `Users matching “${q}”...`,
search: async (q) => {
if (!q) {
return []
}
const users = await $fetch('https://jsonplaceholder.typicode.com/users', { params: { q } })
const users = await $fetch('https://jsonplaceholder.typicode.com/users', { params: { q } })
return users.map(user => ({ id: user.id, label: user.name, suffix: user.email }))
}
}].filter(Boolean)
})
return users.map(user => ({ id: user.id, label: user.name, suffix: user.email }))
}
}]
</script>

<template>
Expand Down
30 changes: 30 additions & 0 deletions docs/components/content/examples/CommandPaletteExampleFilter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script setup>
const people = [
{ id: 1, label: 'Wade Cooper', child: true },
{ id: 2, label: 'Arlene Mccoy' },
{ id: 3, label: 'Devon Webb', child: true },
{ id: 4, label: 'Tom Cook' },
{ id: 5, label: 'Tanya Fox', child: true },
{ id: 6, label: 'Hellen Schmidt' },
{ id: 7, label: 'Caroline Schultz', child: true },
{ id: 8, label: 'Mason Heaney' },
{ id: 9, label: 'Claudie Smitham', child: true },
{ id: 10, label: 'Emil Schaefer' }
]
const groups = [{
key: 'users',
commands: people,
filter: (q, commands) => {
if (!q) {
return commands?.filter(command => !command.child)
}
return commands
}
}]
</script>
<template>
<UCommandPalette :groups="groups" :autoselect="false" />
</template>
13 changes: 13 additions & 0 deletions docs/content/5.navigation/2.command-palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ componentProps:
The `loading` state will automatically be enabled when a `search` function is loading. You can disable this behavior by setting the `loading-icon` prop to `null` or globally in `ui.commandPalette.default.loadingIcon`.
::

## Filter search

You can also pass a function to the `filter` property of a group to filter displayed commands after the search happened. The function will receive the query as its first argument, the array of commands as second argument and should return an array of commands.

::component-example
---
padding: false
component: 'command-palette-example-filter'
componentProps:
class: 'h-[274px]'
---
::

## Slots

### `<group>-icon`
Expand Down
17 changes: 14 additions & 3 deletions src/runtime/components/navigation/CommandPalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ export default defineComponent({
}
for (const key in groupedCommands) {
const group = props.groups.find(group => group.key === key)
const commands = groupedCommands[key].slice(0, options.value.resultLimit).map((result) => {
let commands = groupedCommands[key].map((result) => {
const { item, ...data } = result
return {
Expand All @@ -246,12 +247,22 @@ export default defineComponent({
} as Command
})
groups.push({ ...group, commands })
if (group.filter && typeof group.filter === 'function') {
commands = group.filter(query.value, commands)
}
groups.push({ ...group, commands: commands.slice(0, options.value.resultLimit) })
}
for (const group of props.groups) {
if (group.search && searchResults.value[group.key]?.length) {
groups.push({ ...group, commands: (searchResults.value[group.key] || []).slice(0, options.value.resultLimit) })
let commands = (searchResults.value[group.key] || [])
if (group.filter && typeof group.filter === 'function') {
commands = group.filter(query.value, commands)
}
groups.push({ ...group, commands: commands.slice(0, options.value.resultLimit) })
}
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/types/command-palette.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export interface Group {
inactive?: string
commands?: Command[]
search?: Function
filter?: Function
[key: string]: any
}

1 comment on commit 8ba2a79

@vercel
Copy link

@vercel vercel bot commented on 8ba2a79 Oct 26, 2023

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.nuxt.com
ui-git-dev-nuxt-js.vercel.app
ui-nuxt-js.vercel.app

Please sign in to comment.