blob: 519a808fbbb9131d44ffa5963fe0543d61800af8 [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
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -08005 :aria-label="$t('global.ariaLabel.showPassword')"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08006 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>
17import IconView from '@carbon/icons-vue/es/view/20';
18import IconViewOff from '@carbon/icons-vue/es/view--off/20';
19
20export 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 Wolmers5ea16782020-07-27 17:50:43 -050052 padding: 0.4rem 1rem;
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080053 svg {
54 margin-left: 0;
Derick Montague3f6710a2020-03-04 00:59:47 -060055 vertical-align: sub;
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080056 }
57}
58</style>