Skip to content

Commit

Permalink
fix(SelectMenu): fixes non-strings and nested searchable attributes (#…
Browse files Browse the repository at this point in the history
…967)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
DarkGhostHunter and benjamincanac authored Nov 21, 2023
1 parent 73d0fa7 commit 37fdf22
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script setup>
const options = [
{ id: 1, name: 'Wade Cooper', favoriteColors: ['red', 'yellow'] },
{ id: 2, name: 'Arlene Mccoy', favoriteColors: ['blue', 'yellow'] },
{ id: 3, name: 'Devon Webb', favoriteColors: ['green', 'blue'] },
{ id: 4, name: 'Tom Cook', favoriteColors: ['blue', 'red'] },
{ id: 5, name: 'Tanya Fox', favoriteColors: ['green', 'red'] },
{ id: 5, name: 'Hellen Schmidt', favoriteColors: ['green', 'yellow'] }
]
const selected = ref(options[1])
</script>

<template>
<USelectMenu
v-model="selected"
:options="options"
class="w-full lg:w-96"
placeholder="Select an user"
searchable
searchable-placeholder="Search by name or favorite colors"
option-attribute="name"
by="id"
:search-attributes="['name', 'favoriteColors']"
>
<template #option="{ option: person }">
<span v-for="color in person.favoriteColors" :key="color.id" class="h-2 w-2 rounded-full" :class="`bg-${color}-500 dark:bg-${color}-400`" />
<span class="truncate">{{ person.name }}</span>
</template>
</USelectMenu>
</template>
14 changes: 14 additions & 0 deletions docs/content/3.forms/4.select-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ props:
---
::

#### Search Attributes

Use the `search-attributes` with an array of property names to search on each option object.

Nested attributes can be accessed using `dot.notation`. When the property value is an array or object, these are cast to string so these can be searched within.

::component-example
---
component: 'select-menu-example-search-attributes'
componentProps:
class: 'w-full lg:w-96'
---
::

#### Clear on close :u-badge{label="New" class="align-middle ml-2 !rounded-full" variant="subtle"}

By default, the search query will be kept after the menu is closed. To clear it on close, set the `clear-search-on-close` prop.
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ import UAvatar from '../elements/Avatar.vue'
import { useUI } from '../../composables/useUI'
import { usePopper } from '../../composables/usePopper'
import { useFormGroup } from '../../composables/useFormGroup'
import { mergeConfig } from '../../utils'
import { get, mergeConfig } from '../../utils'
import { useInjectButtonGroup } from '../../composables/useButtonGroup'
import type { SelectSize, SelectColor, SelectVariant, PopperOptions, Strategy } from '../../types'
// @ts-expect-error
Expand Down Expand Up @@ -422,7 +422,13 @@ export default defineComponent({
return (props.options as any[]).filter((option: any) => {
return (props.searchAttributes?.length ? props.searchAttributes : [props.optionAttribute]).some((searchAttribute: any) => {
return ['string', 'number'].includes(typeof option) ? option.toString().search(new RegExp(query.value, 'i')) !== -1 : (option[searchAttribute] && option[searchAttribute].search(new RegExp(query.value, 'i')) !== -1)
if (['string', 'number'].includes(typeof option)) {
return String(option).search(new RegExp(query.value, 'i')) !== -1
}
const child = get(option, searchAttribute)
return child !== null && child !== undefined && String(child).search(new RegExp(query.value, 'i')) !== -1
})
})
})
Expand Down

1 comment on commit 37fdf22

@vercel
Copy link

@vercel vercel bot commented on 37fdf22 Nov 21, 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-git-dev-nuxt-js.vercel.app
ui-nuxt-js.vercel.app
ui.nuxt.com

Please sign in to comment.