summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/form/input_copy_toggle_visibility.vue
blob: 69548f0e7a8d332d2ddf551d7669dba6976c6970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<script>
import { GlFormInputGroup, GlFormGroup, GlButton, GlTooltipDirective } from '@gitlab/ui';

import { __ } from '~/locale';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';

export default {
  name: 'InputCopyToggleVisibility',
  i18n: {
    toggleVisibilityLabelHide: __('Click to hide'),
    toggleVisibilityLabelReveal: __('Click to reveal'),
  },
  components: {
    GlFormInputGroup,
    GlFormGroup,
    GlButton,
    ClipboardButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    value: {
      type: String,
      required: false,
      default: '',
    },
    initialVisibility: {
      type: Boolean,
      required: false,
      default: false,
    },
    showToggleVisibilityButton: {
      type: Boolean,
      required: false,
      default: true,
    },
    showCopyButton: {
      type: Boolean,
      required: false,
      default: true,
    },
    copyButtonTitle: {
      type: String,
      required: false,
      default: __('Copy'),
    },
    formInputGroupProps: {
      type: Object,
      required: false,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      valueIsVisible: this.initialVisibility,
    };
  },
  computed: {
    toggleVisibilityLabel() {
      return this.valueIsVisible
        ? this.$options.i18n.toggleVisibilityLabelHide
        : this.$options.i18n.toggleVisibilityLabelReveal;
    },
    toggleVisibilityIcon() {
      return this.valueIsVisible ? 'eye-slash' : 'eye';
    },
    computedValueIsVisible() {
      return !this.showToggleVisibilityButton || this.valueIsVisible;
    },
    displayedValue() {
      return this.computedValueIsVisible ? this.value : '*'.repeat(this.value.length || 20);
    },
  },
  methods: {
    handleToggleVisibilityButtonClick() {
      this.valueIsVisible = !this.valueIsVisible;

      this.$emit('visibility-change', this.valueIsVisible);
    },
    handleCopyButtonClick() {
      this.$emit('copy');
    },
    handleFormInputCopy(event) {
      if (this.computedValueIsVisible) {
        return;
      }

      event.clipboardData.setData('text/plain', this.value);
      event.preventDefault();
    },
  },
};
</script>
<template>
  <gl-form-group v-bind="$attrs">
    <gl-form-input-group
      :value="displayedValue"
      input-class="gl-font-monospace! gl-cursor-default!"
      select-on-click
      readonly
      v-bind="formInputGroupProps"
      @copy="handleFormInputCopy"
    >
      <template v-if="showToggleVisibilityButton || showCopyButton" #append>
        <gl-button
          v-if="showToggleVisibilityButton"
          v-gl-tooltip.hover="toggleVisibilityLabel"
          :aria-label="toggleVisibilityLabel"
          :icon="toggleVisibilityIcon"
          @click.stop="handleToggleVisibilityButtonClick"
        />
        <clipboard-button
          v-if="showCopyButton"
          :text="value"
          :title="copyButtonTitle"
          @click="handleCopyButtonClick"
        />
      </template>
    </gl-form-input-group>
    <template v-for="slot in Object.keys($slots)" #[slot]>
      <slot :name="slot"></slot>
    </template>
  </gl-form-group>
</template>