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 |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 5 | :aria-label="$t('global.ariaLabel.showPassword')" |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 6 | variant="link" |
| 7 | :class="{ isVisible: isVisible }" |
| 8 | @click="toggleVisibility" |
| 9 | > |
| 10 | <icon-view-off v-if="isVisible" aria-hidden="true" /> |
| 11 | <icon-view v-else aria-hidden="true" /> |
| 12 | </b-button> |
| 13 | </div> |
| 14 | </template> |
| 15 | |
| 16 | <script> |
| 17 | import IconView from '@carbon/icons-vue/es/view/20'; |
| 18 | import IconViewOff from '@carbon/icons-vue/es/view--off/20'; |
| 19 | |
| 20 | export default { |
| 21 | name: 'InputPasswordToggle', |
| 22 | components: { IconView, IconViewOff }, |
| 23 | data() { |
| 24 | return { |
| 25 | isVisible: false |
| 26 | }; |
| 27 | }, |
| 28 | methods: { |
| 29 | toggleVisibility() { |
| 30 | const firstChild = this.$children[0]; |
| 31 | const inputEl = firstChild ? firstChild.$el : null; |
| 32 | |
| 33 | this.isVisible = !this.isVisible; |
| 34 | |
| 35 | if (inputEl.nodeName === 'INPUT') { |
| 36 | inputEl.type = this.isVisible ? 'text' : 'password'; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | }; |
| 41 | </script> |
| 42 | |
| 43 | <style lang="scss" scoped> |
| 44 | .input-password-toggle-container { |
| 45 | position: relative; |
| 46 | } |
| 47 | |
| 48 | .btn { |
| 49 | position: absolute; |
| 50 | right: 0; |
| 51 | top: 0; |
Dixsie Wolmers | 5ea1678 | 2020-07-27 17:50:43 -0500 | [diff] [blame] | 52 | padding: 0.4rem 1rem; |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 53 | svg { |
| 54 | margin-left: 0; |
Derick Montague | 3f6710a | 2020-03-04 00:59:47 -0600 | [diff] [blame] | 55 | vertical-align: sub; |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | </style> |