I would like to show and hide an icon on my input based on the prop type.
If the type is password it will show the icon to make the password visible/hidden; if the type is not password it should hide the icon changing the v-show to false.
Here is the HTML with a v-show showPwIcon to make the icon visible
<input
ref="textField"
v-model="textField"
:placeholder="placeholder"
:type="type"
>
<span v-show="showPwIcon" @click="switchVisibility">
<i :class="[pwIcon ? 'ri-eye-line' : 'ri-eye-off-line']"></i>
</span>
And here the JS, without the CSS for example purpose only, with the v-show showPwIcon set to false by default:
export default {
props:{
placeholder: {
type: String,
value: ''
},
type: {
type: String,
value: '',
required: true
}
},
data() {
return {
textField: '',
showPwIcon: false,
pwIcon: true
}
},
watch: {
type: function (password) {
const inputType = this.$refs.textField.type
if (inputType === 'password') {
this.showPwIcon = true
} else {
this.showPwIcon = false
}
}
},
methods: {
switchVisibility() {
this.$emit('input', {
type: this.$refs.textField.type = this.$refs.textField.type === 'password' ? 'text' : 'password'
})
if (this.$refs.textField.type === 'password') {
this.pwIcon = true
} else {
this.pwIcon = false
}
}
}
}
With the current code I'm having this issue:
With type text it's not showing the icon
<text-field-icon
placeholder="Placeholder here"
type="text"
/>
The type is password but the v-show is not true.
<text-field-icon
placeholder="Placeholder here"
type="password"
/>
I am trying to get this work watching the prop type on the input but I'm not able to get the input type with this.$refs.textField.type
Sample code https://codesandbox.io/s/vue-tailwind-template-dteq4
Any thoughts?