summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/toggle_button.vue
blob: ddc9ddbc3a3a52d67f659d802ccfac223bc1d550 (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
<script>
  import loadingIcon from './loading_icon.vue';

  export default {
    props: {
      name: {
        type: String,
        required: false,
        default: '',
      },
      value: {
        type: Boolean,
        required: true,
      },
      disabledInput: {
        type: Boolean,
        required: false,
        default: false,
      },
      isLoading: {
        type: Boolean,
        required: false,
        default: false,
      },
      enabledText: {
        type: String,
        required: false,
        default: 'Enabled',
      },
      disabledText: {
        type: String,
        required: false,
        default: 'Disabled',
      },
    },

    components: {
      loadingIcon,
    },

    model: {
      prop: 'value',
      event: 'change',
    },

    methods: {
      toggleFeature() {
        if (!this.disabledInput) this.$emit('change', !this.value);
      },
    },
  };
</script>

<template>
  <label class="toggle-wrapper">
    <input
      type="hidden"
      :name="name"
      :value="value"
    />
    <button
      type="button"
      aria-label="Toggle"
      class="project-feature-toggle"
      :data-enabled-text="enabledText"
      :data-disabled-text="disabledText"
      :class="{
        'is-checked': value,
        'is-disabled': disabledInput,
        'is-loading': isLoading
      }"
      @click="toggleFeature"
    >
      <loadingIcon class="loading-icon" />
    </button>
  </label>
</template>