Skip to content

Commit

Permalink
feat(comp:select): set first option active on search and options change
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Aug 30, 2022
1 parent 06c02b6 commit ec664bf
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions packages/components/select/src/composables/usePanelActiveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@ export function usePanelActiveState(
const activeIndex = ref(0)
const setActiveIndex = (index: number) => {
activeIndex.value = index
setActiveValue(flattenedOptions.value[index]?.key)
const key = flattenedOptions.value[index]?.key
key !== activeValue.value && setActiveValue(flattenedOptions.value[index]?.key)
}

watch(
() => props.activeValue,
value => {
const targetIndex = value ? keyIndexMap.value.get(value) ?? -1 : -1
const enabledIndex = getEnabledActiveIndex(flattenedOptions.value, targetIndex === -1 ? 0 : targetIndex, 1)

enabledIndex !== activeIndex.value && setActiveIndex(enabledIndex)
},
)
watch([() => props.activeValue, flattenedOptions], ([value, options]) => {
const targetIndex = value ? keyIndexMap.value.get(value) ?? -1 : -1
setActiveIndex(getEnabledActiveIndex(options, targetIndex === -1 ? 0 : targetIndex, 1))
})

const scrollToActivated = () => {
scrollTo({ index: activeIndex.value })
Expand Down

0 comments on commit ec664bf

Please sign in to comment.