summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/toggles/index.js
blob: 046b9fc7dcd349f1279bcf3301f1a75cd03b93c5 (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
import { kebabCase } from 'lodash';
import Vue from 'vue';
import { GlToggle } from '@gitlab/ui';
import { parseBoolean } from '~/lib/utils/common_utils';

export const initToggle = (el) => {
  if (!el) {
    return false;
  }

  const {
    name,
    isChecked,
    disabled,
    isLoading,
    label,
    help,
    labelPosition,
    ...dataset
  } = el.dataset;

  return new Vue({
    el,
    props: {
      disabled: {
        type: Boolean,
        required: false,
        default: parseBoolean(disabled),
      },
      isLoading: {
        type: Boolean,
        required: false,
        default: parseBoolean(isLoading),
      },
    },
    data() {
      return {
        value: parseBoolean(isChecked),
      };
    },
    render(h) {
      return h(GlToggle, {
        props: {
          name,
          value: this.value,
          disabled: this.disabled,
          isLoading: this.isLoading,
          label,
          help,
          labelPosition,
        },
        class: el.className,
        attrs: Object.fromEntries(
          Object.entries(dataset).map(([key, value]) => [`data-${kebabCase(key)}`, value]),
        ),
        on: {
          change: (newValue) => {
            this.value = newValue;
            this.$emit('change', newValue);
          },
        },
      });
    },
  });
};