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

feat(SelectMenu): allows to clear search query on close #968

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 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,23 @@ props:
---
::

#### 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.

::component-card
---
baseProps:
class: 'w-full lg:w-40'
placeholder: 'Select a person'
searchable: true
searchablePlaceholder: 'Search a person...'
options: ['Wade Cooper', 'Arlene Mccoy', 'Devon Webb', 'Tom Cook', 'Tanya Fox', 'Hellen Schmidt', 'Caroline Schultz', 'Mason Heaney', 'Claudie Smitham', 'Emil Schaefer']
props:
clearSearchOnClose: true
---
::

### Async search

Pass a function to the `searchable` prop to customize the search behavior and filter options according to your needs. The function will receive the query as its first argument and should return an array.
Expand Down
18 changes: 15 additions & 3 deletions src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@
aria-hidden="true"
/>
<span v-else-if="option.chip" :class="uiMenu.option.chip.base" :style="{ background: `#${option.chip}` }" />

<span class="truncate">{{ ['string', 'number'].includes(typeof option) ? option : option[optionAttribute] }}</span>
</slot>
</div>

<span v-if="selected" :class="[uiMenu.option.selectedIcon.wrapper, uiMenu.option.selectedIcon.padding]">
<UIcon :name="selectedIcon" :class="uiMenu.option.selectedIcon.base" aria-hidden="true" />
</span>
</li>
</component>

<component :is="searchable ? 'HComboboxOption' : 'HListboxOption'" v-if="creatable && queryOption && !filteredOptions.length" v-slot="{ active, selected }" :value="queryOption" as="template">
<li :class="[uiMenu.option.base, uiMenu.option.rounded, uiMenu.option.padding, uiMenu.option.size, uiMenu.option.color, active ? uiMenu.option.active : uiMenu.option.inactive]">
<div :class="uiMenu.option.container">
Expand Down Expand Up @@ -239,6 +239,10 @@ export default defineComponent({
type: String,
default: 'Search...'
},
clearSearchOnClose: {
type: Boolean,
default: () => configMenu.default.clearOnClose
},
debounce: {
type: Number,
default: 200
Expand Down Expand Up @@ -423,10 +427,17 @@ export default defineComponent({
return query.value === '' ? null : { [props.optionAttribute]: query.value }
})

function clearOnClose () {
if (props.clearSearchOnClose) {
query.value = ''
}
}

watch(container, (value) => {
if (value) {
emit('open')
} else {
clearOnClose()
emit('close')
emitFormBlur()
}
Expand All @@ -438,6 +449,7 @@ export default defineComponent({
// explicitly set input text because `ComboboxInput` `displayValue` is not reactive
searchInput.value.$el.value = ''
}

emit('update:modelValue', event)
emit('change', event)
emitFormChange()
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/ui.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ export const selectMenu = {
placement: 'bottom-end'
},
default: {
selectedIcon: 'i-heroicons-check-20-solid'
selectedIcon: 'i-heroicons-check-20-solid',
clearOnClose: false
},
arrow: {
..._popperArrow,
Expand Down