| 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'; | 
|  | 21 |  | 
|  | 22 | export default { | 
|  | 23 | name: 'InputPasswordToggle', | 
|  | 24 | components: { IconView, IconViewOff }, | 
|  | 25 | data() { | 
|  | 26 | return { | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 27 | isVisible: false, | 
| Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 28 | togglePasswordLabel: this.$t('global.ariaLabel.showPassword'), | 
| Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 29 | }; | 
|  | 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 Pandey | e881499 | 2021-01-04 12:05:30 +0530 | [diff] [blame] | 38 | if (inputEl && inputEl.nodeName === 'INPUT') { | 
| Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 39 | inputEl.type = this.isVisible ? 'text' : 'password'; | 
|  | 40 | } | 
| Dixsie Wolmers | 30f11f8 | 2020-11-10 16:07:56 -0600 | [diff] [blame] | 41 |  | 
|  | 42 | this.isVisible | 
|  | 43 | ? (this.togglePasswordLabel = this.$t('global.ariaLabel.hidePassword')) | 
|  | 44 | : (this.togglePasswordLabel = this.$t('global.ariaLabel.showPassword')); | 
| Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 45 | }, | 
|  | 46 | }, | 
| Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 47 | }; | 
|  | 48 | </script> | 
|  | 49 |  | 
|  | 50 | <style lang="scss" scoped> | 
|  | 51 | .input-password-toggle-container { | 
|  | 52 | position: relative; | 
|  | 53 | } | 
| Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 54 | </style> |