Skip to content

Commit 525cd9f

Browse files
authored
feat(Combobox): add clear modelValue on cancel property (#2213)
* feat(Combobox): add clear model value prop * docs(Combobox): generate docs
1 parent 16264a1 commit 525cd9f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docs/content/meta/ComboboxRoot.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
'type': 'boolean',
101101
'required': false
102102
},
103+
{
104+
'name': 'resetModelValueOnClear',
105+
'description': '<p>When <code>true</code> the <code>modelValue</code> will be reset to <code>null</code> (or <code>[]</code> if <code>multiple</code>)</p>\n',
106+
'type': 'boolean',
107+
'required': false,
108+
'default': 'false'
109+
},
103110
{
104111
'name': 'resetSearchTermOnBlur',
105112
'description': '<p>Whether to reset the searchTerm when the Combobox input blurred</p>\n',

packages/core/src/Combobox/ComboboxCancel.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function handleClick() {
2323
if (rootContext.inputElement.value) {
2424
rootContext.inputElement.value.value = ''
2525
rootContext.inputElement.value.focus()
26+
if (rootContext.resetModelValueOnClear?.value) {
27+
rootContext.modelValue.value = rootContext.multiple.value ? [] : null
28+
}
2629
}
2730
}
2831
</script>

packages/core/src/Combobox/ComboboxRoot.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type ComboboxRootContext<T> = {
2929
ignoreFilter: Ref<boolean>
3030
openOnFocus: Ref<boolean>
3131
openOnClick: Ref<boolean>
32+
resetModelValueOnClear: Ref<boolean>
3233
}
3334
3435
export const [injectComboboxRootContext, provideComboboxRootContext]
@@ -72,6 +73,10 @@ export interface ComboboxRootProps<T = AcceptableValue> extends Omit<ListboxRoot
7273
* When `true`, disable the default filters
7374
*/
7475
ignoreFilter?: boolean
76+
/**
77+
* When `true` the `modelValue` will be reset to `null` (or `[]` if `multiple`)
78+
*/
79+
resetModelValueOnClear?: boolean
7580
}
7681
</script>
7782

@@ -88,6 +93,7 @@ const props = withDefaults(defineProps<ComboboxRootProps<T>>(), {
8893
resetSearchTermOnSelect: true,
8994
openOnFocus: false,
9095
openOnClick: false,
96+
resetModelValueOnClear: false,
9197
})
9298
const emits = defineEmits<ComboboxRootEmits<T>>()
9399
@@ -101,7 +107,7 @@ defineSlots<{
101107
}>()
102108
103109
const { primitiveElement, currentElement: parentElement } = usePrimitiveElement<GenericComponentInstance<typeof ListboxRoot>>()
104-
const { multiple, disabled, ignoreFilter, resetSearchTermOnSelect, openOnFocus, openOnClick, dir: propDir } = toRefs(props)
110+
const { multiple, disabled, ignoreFilter, resetSearchTermOnSelect, openOnFocus, openOnClick, dir: propDir, resetModelValueOnClear } = toRefs(props)
105111
106112
const dir = useDirection(propDir)
107113
@@ -236,6 +242,7 @@ provideComboboxRootContext({
236242
ignoreFilter,
237243
openOnFocus,
238244
openOnClick,
245+
resetModelValueOnClear,
239246
})
240247
</script>
241248

0 commit comments

Comments
 (0)