Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 1 | <template> |
| 2 | <div class="input-password-toggle-container"> |
| 3 | <slot></slot> |
| 4 | <b-button |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 5 | :title="togglePasswordLabel" |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 6 | variant="link" |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 7 | class="input-action-btn btn-icon-only" |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 8 | :class="{ isVisible: isVisible }" |
| 9 | @click="toggleVisibility" |
| 10 | > |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 11 | <icon-view-off v-if="isVisible" /> |
| 12 | <icon-view v-else /> |
SurenNeware | 6e2cb97 | 2020-12-24 20:58:16 +0530 | [diff] [blame] | 13 | <span class="sr-only">{{ togglePasswordLabel }}</span> |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 14 | </b-button> |
| 15 | </div> |
| 16 | </template> |
| 17 | |
| 18 | <script> |
| 19 | import IconView from '@carbon/icons-vue/es/view/20'; |
| 20 | import IconViewOff from '@carbon/icons-vue/es/view--off/20'; |
Ed Tanous | dbd37e0 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 21 | import i18n from '@/i18n'; |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 22 | |
| 23 | export default { |
| 24 | name: 'InputPasswordToggle', |
| 25 | components: { IconView, IconViewOff }, |
| 26 | data() { |
| 27 | return { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 28 | isVisible: false, |
Ed Tanous | dbd37e0 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 29 | togglePasswordLabel: i18n.global.t('global.ariaLabel.showPassword'), |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 30 | }; |
| 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 Pandey | e881499 | 2021-01-04 12:05:30 +0530 | [diff] [blame] | 39 | if (inputEl && inputEl.nodeName === 'INPUT') { |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 40 | inputEl.type = this.isVisible ? 'text' : 'password'; |
| 41 | } |
Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 42 | |
| 43 | this.isVisible |
Ed Tanous | dbd37e0 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 44 | ? (this.togglePasswordLabel = i18n.global.t( |
| 45 | 'global.ariaLabel.hidePassword', |
| 46 | )) |
| 47 | : (this.togglePasswordLabel = i18n.global.t( |
| 48 | 'global.ariaLabel.showPassword', |
| 49 | )); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 50 | }, |
| 51 | }, |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 52 | }; |
| 53 | </script> |
| 54 | |
| 55 | <style lang="scss" scoped> |
Ed Tanous | 9c72979 | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 56 | @import '@/assets/styles/bmc/helpers/_index.scss'; |
| 57 | @import '@/assets/styles/bootstrap/_helpers.scss'; |
| 58 | |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 59 | .input-password-toggle-container { |
| 60 | position: relative; |
| 61 | } |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 62 | </style> |