Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Make custom filter-function for CommandDialog Component #284

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/www/__registry__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ export const Index = {
component: () => import('../src/lib/registry/default/example/CommandDialogDemo.vue').then(m => m.default),
files: ['../src/lib/registry/default/example/CommandDialogDemo.vue'],
},
CommandDialogUseFuseDemo: {
name: 'CommandDialogUseFuseDemo',
type: 'components:example',
registryDependencies: ['command'],
component: () => import('../src/lib/registry/default/example/CommandDialogUseFuseDemo.vue').then(m => m.default),
files: ['../src/lib/registry/default/example/CommandDialogUseFuseDemo.vue'],
},
ContextMenuDemo: {
name: 'ContextMenuDemo',
type: 'components:example',
Expand Down Expand Up @@ -1265,6 +1272,13 @@ export const Index = {
component: () => import('../src/lib/registry/new-york/example/CommandDialogDemo.vue').then(m => m.default),
files: ['../src/lib/registry/new-york/example/CommandDialogDemo.vue'],
},
CommandDialogUseFuseDemo: {
name: 'CommandDialogUseFuseDemo',
type: 'components:example',
registryDependencies: ['command'],
component: () => import('../src/lib/registry/new-york/example/CommandDialogUseFuseDemo.vue').then(m => m.default),
files: ['../src/lib/registry/new-york/example/CommandDialogUseFuseDemo.vue'],
},
ContextMenuDemo: {
name: 'ContextMenuDemo',
type: 'components:example',
Expand Down
1 change: 1 addition & 0 deletions apps/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@unovis/vue": "^1.3.3",
"@vee-validate/zod": "^4.12.5",
"@vueuse/core": "^10.7.2",
"@vueuse/integrations": "^10.7.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"codesandbox": "^2.2.3",
Expand Down
99 changes: 99 additions & 0 deletions apps/www/src/content/docs/components/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,106 @@ watch(CmdJ, (v) => {
</div>
</template>
```
### Dialog with custom filter (Fuse.js)

<ComponentPreview name="CommandDialogUseFuseDemo" />

For this Demo we will use the [useFuse](https://vueuse.org/integrations/useFuse/) integration. Don't forget to install `fuse.js` and `@vueuse/integrations`:

```bash
pnpm install fuse.js @vueuse/integrations
```

Code Example:
```vue
<script setup lang="ts">
import { useMagicKeys } from '@vueuse/core'

import { ref, watch } from 'vue'
import { useFuse } from '@vueuse/integrations/useFuse'
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
} from '@/lib/registry/default/ui/command'

const open = ref(false)

const { Meta_M, Ctrl_M } = useMagicKeys({
passive: false,
onEventFired(e) {
if (e.key === 'm' && (e.metaKey || e.ctrlKey))
e.preventDefault()
},
})

watch([Meta_M, Ctrl_M], (v) => {
if (v[0] || v[1])
handleOpenChange()
})

function handleOpenChange() {
open.value = !open.value
}

function customFiltering(val: string[], term: string) {
const { results } = useFuse(term, val, {
matchAllWhenSearchEmpty: true,
fuseOptions: {
shouldSort: false,
},
})

return results.value?.map(v => v.item)
}
</script>

<template>
<div>
<p class="text-sm text-muted-foreground">
Press
<kbd
class="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground opacity-100"
>
<span class="text-xs">⌘</span>M
</kbd>
</p>
<CommandDialog v-model:open="open" :filter-function="customFiltering">
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Suggestions">
<CommandItem value="calendar">
Calendar
</CommandItem>
<CommandItem value="search-emoji">
Search Emoji
</CommandItem>
<CommandItem value="calculator">
Calculator
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Settings">
<CommandItem value="profile">
Profile
</CommandItem>
<CommandItem value="billing">
Billing
</CommandItem>
<CommandItem value="settings">
Settings
</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
</div>
</template>
```
### Combobox

You can use the `<Command />` component as a combobox. See the [Combobox](/docs/components/combobox) page for more information.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<script setup lang="ts">
import { useMagicKeys } from '@vueuse/core'

import { ref, watch } from 'vue'
import { useFuse } from '@vueuse/integrations/useFuse'
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
} from '@/lib/registry/default/ui/command'

const open = ref(false)

const { Meta_M, Ctrl_M } = useMagicKeys({
passive: false,
onEventFired(e) {
if (e.key === 'm' && (e.metaKey || e.ctrlKey))
e.preventDefault()
},
})

watch([Meta_M, Ctrl_M], (v) => {
if (v[0] || v[1])
handleOpenChange()
})

function handleOpenChange() {
open.value = !open.value
}

function customFiltering(val: string[], term: string) {
const { results } = useFuse(term, val, {
matchAllWhenSearchEmpty: true,
fuseOptions: {
shouldSort: false,
},
})

return results.value?.map(v => v.item)
}
</script>

<template>
<div>
<p class="text-sm text-muted-foreground">
Press
<kbd
class="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground opacity-100"
>
<span class="text-xs">⌘</span>M
</kbd>
</p>
<CommandDialog v-model:open="open" :filter-function="customFiltering">
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Suggestions">
<CommandItem value="calendar">
Calendar
</CommandItem>
<CommandItem value="search-emoji">
Search Emoji
</CommandItem>
<CommandItem value="calculator">
Calculator
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Settings">
<CommandItem value="profile">
Profile
</CommandItem>
<CommandItem value="billing">
Billing
</CommandItem>
<CommandItem value="settings">
Settings
</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
</div>
</template>
20 changes: 16 additions & 4 deletions apps/www/src/lib/registry/default/ui/command/CommandDialog.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<script setup lang="ts">
<script setup lang="ts" generic="T">
import { computed } from 'vue'
import { useForwardPropsEmits } from 'radix-vue'
import type { DialogRootEmits, DialogRootProps } from 'radix-vue'
import Command from './Command.vue'
import { Dialog, DialogContent } from '@/lib/registry/default/ui/dialog'

const props = defineProps<DialogRootProps>()
type ArrayOrWrapped<T> = T extends any[] ? T : Array<T>
interface CommandDialogProps extends DialogRootProps {
filterFunction?: (val: ArrayOrWrapped<T>, term: string) => ArrayOrWrapped<T>
}

const props = defineProps<CommandDialogProps>()
const emits = defineEmits<DialogRootEmits>()

const forwarded = useForwardPropsEmits(props, emits)
const delegatedProps = computed(() => {
const { filterFunction: _, ...delegated } = props

return delegated
})

const forwarded = useForwardPropsEmits(delegatedProps, emits)
</script>

<template>
<Dialog v-bind="forwarded">
<DialogContent class="overflow-hidden p-0 shadow-lg">
<Command class="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
<Command :filter-function="props.filterFunction" class="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
Copy link
Collaborator

Choose a reason for hiding this comment

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

@romanhrynevych can you check this line, it has TypeScript error

<slot />
</Command>
</DialogContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<script setup lang="ts">
import { useMagicKeys } from '@vueuse/core'

import { ref, watch } from 'vue'
import { useFuse } from '@vueuse/integrations/useFuse'
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
} from '@/lib/registry/default/ui/command'

const open = ref(false)

const { Meta_M, Ctrl_M } = useMagicKeys({
passive: false,
onEventFired(e) {
if (e.key === 'm' && (e.metaKey || e.ctrlKey))
e.preventDefault()
},
})

watch([Meta_M, Ctrl_M], (v) => {
if (v[0] || v[1])
handleOpenChange()
})

function handleOpenChange() {
open.value = !open.value
}

function customFiltering(val: string[], term: string) {
const { results } = useFuse(term, val, {
matchAllWhenSearchEmpty: true,
fuseOptions: {
shouldSort: false,
},
})

return results.value?.map(v => v.item)
}
</script>

<template>
<div>
<p class="text-sm text-muted-foreground">
Press
<kbd
class="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground opacity-100"
>
<span class="text-xs">⌘</span>M
</kbd>
</p>
<CommandDialog v-model:open="open" :filter-function="customFiltering">
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Suggestions">
<CommandItem value="calendar">
Calendar
</CommandItem>
<CommandItem value="search-emoji">
Search Emoji
</CommandItem>
<CommandItem value="calculator">
Calculator
</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="Settings">
<CommandItem value="profile">
Profile
</CommandItem>
<CommandItem value="billing">
Billing
</CommandItem>
<CommandItem value="settings">
Settings
</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
</div>
</template>
22 changes: 17 additions & 5 deletions apps/www/src/lib/registry/new-york/ui/command/CommandDialog.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<script setup lang="ts">
<script setup lang="ts" generic="T">
import { computed } from 'vue'
import { useForwardPropsEmits } from 'radix-vue'
import type { DialogRootEmits, DialogRootProps } from 'radix-vue'
import Command from './Command.vue'
import { Dialog, DialogContent } from '@/lib/registry/new-york/ui/dialog'
import { Dialog, DialogContent } from '@/lib/registry/default/ui/dialog'

const props = defineProps<DialogRootProps>()
type ArrayOrWrapped<T> = T extends any[] ? T : Array<T>
interface CommandDialogProps extends DialogRootProps {
filterFunction?: (val: ArrayOrWrapped<T>, term: string) => ArrayOrWrapped<T>
}

const props = defineProps<CommandDialogProps>()
const emits = defineEmits<DialogRootEmits>()

const forwarded = useForwardPropsEmits(props, emits)
const delegatedProps = computed(() => {
const { filterFunction: _, ...delegated } = props

return delegated
})

const forwarded = useForwardPropsEmits(delegatedProps, emits)
</script>

<template>
<Dialog v-bind="forwarded">
<DialogContent class="overflow-hidden p-0 shadow-lg">
<Command class="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
<Command :filter-function="props.filterFunction" class="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
<slot />
</Command>
</DialogContent>
Expand Down