I have a simple vue component that can be clicked, but I want the click itself to be conditional, so I can display the component, but prevent the user from clicking it.
I tried the disabled directive, but didn't work.
Component
<template>
<!-- the listeners make sure the whole component is clickable -->
<div class="col hb_click" v-on="$listeners" :class="{disabled:disabled}" :disabled="disabled">
<div class="">
<!-- Display icon with label at bottom -->
<div class="row">
<div class="col d-flex align-items-center justify-content-center">
<button
type="button"
class="btn btn-link px-0"
>
<i :class="icon"></i>
</button>
</div>
</div>
<div class="row">
<div class="col d-flex align-items-center justify-content-center">
<div class="hb_label">{{ label }}</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "HB",
props: {
label: String,
icon: String,
disabled:{
type: Boolean,
default: false,
}
},
data() {
return {};
},
};
</script>
<style scoped>
i {
font-size: 1.3rem;
}
.hb_label{
font-size: 1rem;
}
.hb_click {
cursor: pointer;
}
.hb_click:hover{
background-color: rgb(222, 216, 215);
}
.disabled{
color: lightgray!important;
}
</style>
Vue
<HB label="Templates" icon="fas fa-search" @click="shouldWork()" :disabled="false"></HB>
<HB label="Templates" icon="fas fa-search" @click="shouldNotWork()" :disabled="true"></HB>