:class on <input> element breaks Bootstrap 5 dropdown behaviour
Vue version
3.2.45
Link to minimal reproduction
Steps to reproduce
Create a simple bootstrap dropdown, but add a :class
property to the input
element which changes a class (e.g. fw-bold
) depending on the selected element.
Click to open the dropdown, click on an item which would prompt the above class to change.
See SFC playground example, which has two dropdowns - one with the :class, and one without.
Relevant template snippet
<div class="dropdown m-4">
<input readonly class=" btn dropdown-toggle btn-outline-secondary" :value="val"
:class="{ 'fw-bold': makeBold() }"
data-bs-toggle="dropdown">
<ul class="dropdown-menu filter-dropdown">
<li v-for="s in vals">
<div class="dropdown-item" @click="updateSource(s)">{{ s }}</div>
</li>
</ul>
</div>
What is expected?
The input element re-renders and the dropdown is closed.
What is actually happening?
The input element displays correctly, but the dropdown does not close.
System Info
SFC Playground with Bootstrap 5.2.3
Any additional comments?
This may be in the no-mans-land between Bootstrap and Vue :)
Bootstrap adds a class called show
that vue is unaware of, and it seems to make the close behavior depend on that.
When you select "zero", which makes Vue add another class to the element, Vue overrides the classes with the ones it knows - and show
is not one of them, so it's effectively being removed.
And bootstrap then doesn't close the dropdown, because apparently, that missing class on the input means it's not in need of closing, or something.
You can verify this by: 1) opening the dropdown 2) removing the show class on the input 3) selecting any entry. It will also not close.
This is a wontfix. Two libraries changing the same element is generally a very bad idea. Vue can't reliably to its job (and neither can bootstrap as we see) when other code is messing in its turf.
...Vue overrides the classes with the ones it knows - and
show
is not one of them, so it's effectively being removed.
That's more than a little unexpected. Is there a reason that Vue can't leave those classes it doesn't know about untouched? Would having and additional (but less flexible) variant of the :class
attribute help?
I think I can solve my particular problem by using a computed CSS style to 'bold' the text rather than the bootstrap utility fw-bold
class.
Edit: Yes, that works.