toggle_button.vue 1.8 KB
Newer Older
1
<script>
2 3
  import { s__ } from '../../locale';
  import icon from './icon.vue';
4 5
  import loadingIcon from './loading_icon.vue';

6 7 8 9 10
  const ICON_ON = 'status_success_borderless';
  const ICON_OFF = 'status_failed_borderless';
  const LABEL_ON = s__('ToggleButton|Toggle Status: ON');
  const LABEL_OFF = s__('ToggleButton|Toggle Status: OFF');

11
  export default {
F
Filipa Lacerda 已提交
12 13 14 15 16 17 18 19 20 21
    components: {
      icon,
      loadingIcon,
    },

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

22 23 24 25
    props: {
      name: {
        type: String,
        required: false,
M
Mike Greiling 已提交
26
        default: null,
27 28 29
      },
      value: {
        type: Boolean,
30
        required: false,
M
Mike Greiling 已提交
31
        default: null,
32 33 34 35 36 37 38 39 40 41 42 43 44
      },
      disabledInput: {
        type: Boolean,
        required: false,
        default: false,
      },
      isLoading: {
        type: Boolean,
        required: false,
        default: false,
      },
    },

45 46 47 48 49 50 51 52 53
    computed: {
      toggleIcon() {
        return this.value ? ICON_ON : ICON_OFF;
      },
      ariaLabel() {
        return this.value ? LABEL_ON : LABEL_OFF;
      },
    },

54 55 56 57 58 59 60 61 62 63 64
    methods: {
      toggleFeature() {
        if (!this.disabledInput) this.$emit('change', !this.value);
      },
    },
  };
</script>

<template>
  <label class="toggle-wrapper">
    <input
65
      v-if="name"
66 67 68 69 70 71 72
      type="hidden"
      :name="name"
      :value="value"
    />
    <button
      type="button"
      class="project-feature-toggle"
73
      :aria-label="ariaLabel"
74 75 76 77 78 79 80 81
      :class="{
        'is-checked': value,
        'is-disabled': disabledInput,
        'is-loading': isLoading
      }"
      @click="toggleFeature"
    >
      <loadingIcon class="loading-icon" />
82 83 84 85 86
      <span class="toggle-icon">
        <icon
          css-classes="toggle-icon-svg"
          :name="toggleIcon"/>
      </span>
87 88 89
    </button>
  </label>
</template>