blob: d2c0d4a6957ab2eb99dbb868d13b979d4e6862a4 [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
Dixsie Wolmers30f11f82020-11-10 16:07:56 -06005 :title="togglePasswordLabel"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08006 variant="link"
Dixsie Wolmers30f11f82020-11-10 16:07:56 -06007 class="input-action-btn btn-icon-only"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08008 :class="{ isVisible: isVisible }"
9 @click="toggleVisibility"
10 >
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060011 <icon-view-off v-if="isVisible" />
12 <icon-view v-else />
SurenNeware6e2cb972020-12-24 20:58:16 +053013 <span class="sr-only">{{ togglePasswordLabel }}</span>
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080014 </b-button>
15 </div>
16</template>
17
18<script>
19import IconView from '@carbon/icons-vue/es/view/20';
20import IconViewOff from '@carbon/icons-vue/es/view--off/20';
21
22export default {
23 name: 'InputPasswordToggle',
24 components: { IconView, IconViewOff },
25 data() {
26 return {
Derick Montague602e98a2020-10-21 16:20:00 -050027 isVisible: false,
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060028 togglePasswordLabel: this.$t('global.ariaLabel.showPassword'),
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080029 };
30 },
31 methods: {
32 toggleVisibility() {
33 const firstChild = this.$children[0];
34 const inputEl = firstChild ? firstChild.$el : null;
35
36 this.isVisible = !this.isVisible;
37
Sukanya Pandeye8814992021-01-04 12:05:30 +053038 if (inputEl && inputEl.nodeName === 'INPUT') {
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080039 inputEl.type = this.isVisible ? 'text' : 'password';
40 }
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060041
42 this.isVisible
43 ? (this.togglePasswordLabel = this.$t('global.ariaLabel.hidePassword'))
44 : (this.togglePasswordLabel = this.$t('global.ariaLabel.showPassword'));
Derick Montague602e98a2020-10-21 16:20:00 -050045 },
46 },
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080047};
48</script>
49
50<style lang="scss" scoped>
51.input-password-toggle-container {
52 position: relative;
53}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080054</style>