I am using the Dropdown component to allow users toggle between two views. This works fine. Both views contains forms the users can interact with. Now, i need to implement a "Changes unsaved, do you want to leave" dialog when a user changes the view mode using the dropdown. Thus, i added a command: () => {} to the options for the dropdown.
However, the v-model is changed regardless of the logic in the command callback. I then used a within the dropdown and used the click event to call the callback. This approach calls the callback function but the v-model still changes even when the callback does not change it.
Has anyone done anything similar to this? Is there a better way of doing this?
Snippet:
<Dropdown
v-model="mode"
:options="modeOptions"
optionLabel="name"
optionValue="value"
placeholder="Select Mode"
size="small"
>
<template #option="slotProps">
<div class="flex" @click="slotProps.option.command">
<div>{{ slotProps.option.name }}</div>
</div>
</template>
</Dropdown>
//script
const mode = ref('student')
const isEditing = ref(true)
const modeOptions = [
{
name: 'Student',
value: 'student',
command: () => {
if(isEditing.value){
//block mode changing
}else{
mode.value = 'student'
}
}
},
{
name: 'Teacher',
value: 'teacher',
command: () => {
if(isEditing.value){
//block mode changing
}else{
mode.value = 'teacher'
}
}
}
]