summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/listbox_input/init_listbox_inputs.js
blob: ad89b78b52150d82c5208930183c8795ad0542f7 (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
import Vue from 'vue';
import ListboxInput from '~/vue_shared/components/listbox_input/listbox_input.vue';

export const initListboxInputs = () => {
  const els = [...document.querySelectorAll('.js-listbox-input')];

  els.forEach((el, index) => {
    const { label, description, name, defaultToggleText, value = null } = el.dataset;
    const { id } = el;
    const items = JSON.parse(el.dataset.items);

    return new Vue({
      el,
      name: `ListboxInputRoot${index + 1}`,
      data() {
        return {
          selected: value,
        };
      },
      render(createElement) {
        return createElement(ListboxInput, {
          on: {
            select: (newValue) => {
              this.selected = newValue;
            },
          },
          props: {
            label,
            description,
            name,
            defaultToggleText,
            selected: this.selected,
            items,
          },
          attrs: {
            id,
          },
        });
      },
    });
  });
};