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

  export default {
    data() {
      return {
        enteredValue: '',
      };
    },
    computed: {
      id() {
        if (!this.$parent.id) {
          throw new Error('Extending component needs to override this!');
        }
        return `${this.$parent.id}-input`;
      },
      // eslint-disable-next-line vue/return-in-computed-property
      confirmationValue() {
        throw new Error('Extending component needs to override this!');
      },
      inputLabel() {
        let value = this.confirmationValue;
        if (this.shouldEscapeConfirmationValue) {
          value = _.escape(value);
        }

        return sprintf(
          __('Type %{value} to confirm:'),
          { value: `<code>${value}</code>` },
          false,
        );
      },
      shouldEscapeConfirmationValue() {
        return true;
      },
    },
    mounted() {
      this.$parent.$on('clearInputs', this.clear);
    },
    beforeDestroy() {
      this.$parent.$off('clearInputs', this.clear);
    },
    created() {
      this.onInput();
    },
    methods: {
      clear() {
        this.enteredValue = '';
      },
      onInput() {
        this.$parent.$emit(
          'toggleCanSubmit',
          this.enteredValue === this.confirmationValue,
        );
      },
    },
  };
</script>

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