summaryrefslogtreecommitdiff
path: root/app/src/components/Select.vue
blob: f2d11876576e6f1e75c96fb34f0b82f5e39bd0dd (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
78
79
80
81
<template>
  <select />
</template>

<script>
import 'select2/dist/js/select2.min';
import $ from 'jquery';

export default {
  props: {
    options: {
      type: Array,
      required: true,
    },

    modelValue: {
      type: [Number, String],
      required: true,
    },

    config: {
      type: Object,
      default: () => ({}),
    },
  },

  emits: ['update:modelValue'],

  watch: {
    options (value) {
      $(this.$el)
        .empty().select2({ data: value });
    },

    modelValue (value) {
      $(this.$el)
        .val(value)
        .trigger('change');
    },
  },

  mounted () {
    $(this.$el)
      .select2({ ...this.config, data: this.options })
      .val(this.modelValue)
      .trigger('change')
      .on('change', (ev) => {
        this.$emit('update:modelValue', ev.target.value);
      });
  },

  unmounted () {
    $(this.$el)
      .off()
      .select2('destroy');
  },
};
</script>

<style lang="scss">
@import '~select2/dist/css/select2.min.css';

.select2 {
  &-selection {
    height: 2.4375rem !important;
    border-radius: 0 !important;
    border: 1px solid #cacaca !important;

    &__rendered {
      padding: .5rem;
      font-size: 1rem !important;
      line-height: 1.5 !important;
    }

    &__arrow {
      height: 2.4375rem !important;
      top: 0;
    }
  }
}
</style>