VSelect
issue with v-click-outside
on parent element
#20610
Unanswered
frederikheld
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I have tried this I hope it helps you <template>
<div>
<div
v-if="isInEditMode"
v-click-outside="{
handler: leaveEditMode,
include: [dropdownRef]
}"
>
<v-select
ref="dropdownRef"
v-model="inputValue"
:items="items"
@blur="onBlur"
/>
</div>
<div v-else @click="enterEditMode">{{ inputValue }}</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const isInEditMode = ref(false)
const inputValue = ref('Option 1')
const items = ref(['Option 1', 'Option 2', 'Option 3'])
const dropdownRef = ref(null)
const enterEditMode = () => {
isInEditMode.value = true
}
const leaveEditMode = () => {
isInEditMode.value = false
}
const onValueSelect = () => {
// Handle selection and leave edit mode
leaveEditMode()
}
const onBlur = () => {
// Additional safety check to leave edit mode when focus is lost
if (isInEditMode.value) {
leaveEditMode()
}
}
</script> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey everyone,
I'd like to have a
div
showing a value and if the user clicks on it, it goes into edit mode showing anVInput
instead of the text. The user can hit Enter to save changes or click outside of thatdiv
to leave the edit mode. This works well for all kinds ofVInput
, esp.VTextField
that I'm using a lot. But it does not work forVSelect
as clicks on the list of options will be registered by thev-click-outside
directive of the parent element.See the following code (I left out some of the details of entering/leaving the edit mode to focus on the click-outside part):
The issue is that the
VSelect
teleports the options list to somewhere outside of that div element. I tried to use theinclude
property ofv-click-outside
but I could not figure out what to put in there to make it work:I also looked if the
VSelect
has a property that turns off the teleport to make the list render as an actual child of thediv
but this doesn't seem to be possible either.I also built my own list using the
#item
slot in the hopes to somehow hook into the event handling, but no success eitherIs there any way to get the result that I'm looking for?
Beta Was this translation helpful? Give feedback.
All reactions