Skip to content

Commit 82f86b7

Browse files
authored
fix(select): close options on selected item click (#61)
1 parent a2ebf2b commit 82f86b7

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

packages/anu-vue/src/components/select/ASelect.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom'
22
import { onClickOutside } from '@vueuse/core'
33
import type { PropType } from 'vue'
4-
import { Teleport, computed, defineComponent, onBeforeUnmount, onMounted, ref, watch } from 'vue'
4+
import { Teleport, computed, defineComponent, onBeforeUnmount, onMounted, ref } from 'vue'
55
import { isObject } from '@/utils/helpers'
66
import { ABaseInput, useBaseInputProp } from '@/components/base-input'
77

@@ -103,15 +103,6 @@ export const ASelect = defineComponent({
103103
)
104104

105105
// !SECTION
106-
107-
// 👉 watch: modelValue
108-
watch(
109-
() => props.modelValue,
110-
() => {
111-
isOptionsVisible.value = false
112-
},
113-
)
114-
115106
// TODO: You can use it as utility in another components
116107
// TODO: Add some style to indicate currently selected item
117108
const handleInputClick = () => {
@@ -128,6 +119,10 @@ export const ASelect = defineComponent({
128119
emit('input', value)
129120
emit('update:modelValue', value)
130121
}
122+
const closeOptions = (event: MouseEvent) => {
123+
if (event.target !== refFloating.value)
124+
isOptionsVisible.value = false
125+
}
131126

132127
// 👉 Value
133128
const selectedValue = computed(() => {
@@ -157,6 +152,7 @@ export const ASelect = defineComponent({
157152
</ABaseInput>
158153
<Teleport to="body">
159154
<ul
155+
onClick={closeOptions}
160156
v-show={isOptionsVisible.value}
161157
ref={refFloating}
162158
class={['a-select-options-container absolute bg-[hsl(var(--a-layer))]', props.optionsWrapperClasses]}>

0 commit comments

Comments
 (0)