blob: fadb99afa79687d789a6dcfb047a4988bc441866 [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';
Ed Tanousdbd37e02024-03-23 14:56:34 -070021import i18n from '@/i18n';
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080022
23export default {
24 name: 'InputPasswordToggle',
25 components: { IconView, IconViewOff },
26 data() {
27 return {
Derick Montague602e98a2020-10-21 16:20:00 -050028 isVisible: false,
Ed Tanousdbd37e02024-03-23 14:56:34 -070029 togglePasswordLabel: i18n.global.t('global.ariaLabel.showPassword'),
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080030 };
31 },
32 methods: {
33 toggleVisibility() {
34 const firstChild = this.$children[0];
35 const inputEl = firstChild ? firstChild.$el : null;
36
37 this.isVisible = !this.isVisible;
38
Sukanya Pandeye8814992021-01-04 12:05:30 +053039 if (inputEl && inputEl.nodeName === 'INPUT') {
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080040 inputEl.type = this.isVisible ? 'text' : 'password';
41 }
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060042
43 this.isVisible
Ed Tanousdbd37e02024-03-23 14:56:34 -070044 ? (this.togglePasswordLabel = i18n.global.t(
45 'global.ariaLabel.hidePassword',
46 ))
47 : (this.togglePasswordLabel = i18n.global.t(
48 'global.ariaLabel.showPassword',
49 ));
Derick Montague602e98a2020-10-21 16:20:00 -050050 },
51 },
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080052};
53</script>
54
55<style lang="scss" scoped>
Ed Tanous9c729792024-03-23 14:56:34 -070056@import '@/assets/styles/bmc/helpers/_index.scss';
57@import '@/assets/styles/bootstrap/_helpers.scss';
58
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080059.input-password-toggle-container {
60 position: relative;
61}
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080062</style>