summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/confirmation_input.vue
blob: 8a5ff70515d9427c1e05ca7cc299c4a10c253f79 (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
<script>
  import _ from 'underscore';
  import { __, sprintf } from '~/locale';

  export default {
    props: {
      id: {
        type: String,
        required: true,
      },
      confirmationValue: {
        type: String,
        required: true,
      },
      shouldEscapeConfirmationValue: {
        type: Boolean,
        required: false,
        default: true,
      },
    },
    computed: {
      inputLabel() {
        let value = this.confirmationValue;
        if (this.shouldEscapeConfirmationValue) {
          value = _.escape(value);
        }

        return sprintf(
          __('Type %{value} to confirm:'),
          { value: `<code>${value}</code>` },
          false,
        );
      },
    },
    mounted() {
      this.$on('clear', this.clear);
    },
    beforeDestroy() {
      this.$off('clear', this.clear);
    },
    methods: {
      clear() {
        this.$refs.input.value = '';
        this.$emit('confirmed', '' === this.confirmationValue);
      },
      onInput(event) {
        const input = event.target;
        this.$emit(
          'confirmed',
          input.value === this.confirmationValue,
        );
      },
    },
  };
</script>

<template>
  <div>
    <label
      v-html="inputLabel"
      :for="id"></label>
    <input
      ref="input"
      :id="id"
      type="text"
      class="form-control"
      @input="onInput"
    />
  </div>
</template>