blob: bf3e4ca55fdd365b880492d5cea4623091b3f8da [file] [log] [blame]
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08001<template>
2 <div class="input-password-toggle-container">
3 <slot></slot>
4 <b-button
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -08005 :aria-label="$t('global.ariaLabel.showPassword')"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08006 variant="link"
SurenNewaredc618a82020-08-17 18:42:20 +05307 class="input-action-btn"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08008 :class="{ isVisible: isVisible }"
9 @click="toggleVisibility"
10 >
Dixsie Wolmersd2ac76d2020-08-26 17:23:45 -050011 <icon-view-off
12 v-if="isVisible"
13 aria-hidden="true"
14 :title="$t('global.ariaLabel.hidePassword')"
15 />
16 <icon-view
17 v-else
18 aria-hidden="true"
19 :title="$t('global.ariaLabel.showPassword')"
20 />
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080021 </b-button>
22 </div>
23</template>
24
25<script>
26import IconView from '@carbon/icons-vue/es/view/20';
27import IconViewOff from '@carbon/icons-vue/es/view--off/20';
28
29export default {
30 name: 'InputPasswordToggle',
31 components: { IconView, IconViewOff },
32 data() {
33 return {
Derick Montague602e98a2020-10-21 16:20:00 -050034 isVisible: false,
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080035 };
36 },
37 methods: {
38 toggleVisibility() {
39 const firstChild = this.$children[0];
40 const inputEl = firstChild ? firstChild.$el : null;
41
42 this.isVisible = !this.isVisible;
43
44 if (inputEl.nodeName === 'INPUT') {
45 inputEl.type = this.isVisible ? 'text' : 'password';
46 }
Derick Montague602e98a2020-10-21 16:20:00 -050047 },
48 },
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080049};
50</script>
51
52<style lang="scss" scoped>
53.input-password-toggle-container {
54 position: relative;
55}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080056</style>