docs: add CommandPalette async search example

This commit is contained in:
Benjamin Canac
2023-05-17 15:13:34 +02:00
parent c834f401cd
commit 2bdeb04f56
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<script setup>
const groups = computed(() => {
return [{
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 } })
return users.map(user => ({ id: user.id, label: user.name, suffix: user.email }))
}
}].filter(Boolean)
})
</script>
<template>
<UCommandPalette :groups="groups" :autoselect="false" />
</template>

View File

@@ -261,6 +261,43 @@ You can also highlight the matches in the command by setting the `fuse.fuseOptio
Try it yourself in this documentation's search by pressing :badge-shortcut{value="meta"} :badge-shortcut{value="K" class="ml-1"}.
::
## Async search
You can also pass an `async` function to the `search` property of a group to perform an async search. The function will receive the query as its first argument and should return an array of commands.
::component-example
---
padding: false
---
#default
:command-palette-example-async{class="h-[274px]"}
#code
```vue
<script setup>
const groups = computed(() => {
return [{
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 } })
return users.map(user => ({ id: user.id, label: user.name, suffix: user.email }))
}
}].filter(Boolean)
})
</script>
<template>
<UCommandPalette :groups="groups" />
</template>
```
::
## Themes
Our theming system provides a lot of flexibility to customize the component. Here is some examples of what you can do.