Skip to content

Commit

Permalink
fix(Combobox): default value not selected intermittently (#714)
Browse files Browse the repository at this point in the history
* fix: value not selected

* fix: searchTerm not triggering
  • Loading branch information
zernonia authored Feb 25, 2024
1 parent 02d49c4 commit c3b39c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/radix-vue/src/Combobox/ComboboxItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const { forwardRef } = useForwardExpose()
const isSelected = computed(() =>
rootContext.multiple.value && Array.isArray(rootContext.modelValue.value)
? rootContext.modelValue.value?.includes(props.value as never)
? rootContext.modelValue.value?.some(val => isEqual(val, props.value))
: isEqual(rootContext.modelValue?.value, props.value),
)
Expand Down
9 changes: 7 additions & 2 deletions packages/radix-vue/src/Combobox/ComboboxRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ const filteredOptions = computed(() => {
return props.filterFunction(options.value as ArrayOrWrapped<T>, searchTerm.value) as T[]
// The default filter only compares strings
return options.value.filter(i => typeof i === 'string' && i.toLowerCase().includes(searchTerm.value?.toLowerCase()))
const optionsWithTypeString = options.value.filter(i => typeof i === 'string') as string[]
if (optionsWithTypeString.length)
return optionsWithTypeString.filter(i => i.toLowerCase().includes(searchTerm.value?.toLowerCase())) as T[]
}
return options.value
})
Expand All @@ -186,8 +188,11 @@ const selectedElement = computed(() => {
return reactiveItems.value.find(i => i.value === selectedValue.value)?.ref
})
const stringifiedModelValue = computed(() => JSON.stringify(modelValue.value))
// nextTick() are required in the following watchers as we are waiting for DOM element to be mounted first the only apply following logic
watch(modelValue, async () => {
watch(stringifiedModelValue, async () => {
await nextTick()
await nextTick()
resetSearchTerm()
}, { immediate: true })
Expand Down

0 comments on commit c3b39c1

Please sign in to comment.