aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/StudipProxiedCheckbox.vue
blob: 332815338be2a0967aa9d2089fd702595cb78126 (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
<script>
import { h } from 'vue';

let uuid = 0;
export default {
    name: 'studip-proxied-checkbox',
    emits: ['update:selected'],
    props: {
        name: String,
        id: String,
        value: {
            required: true
        },
        selected: {
            type: Array,
            required: true
        }
    },
    methods: {
        changeCollection () {
            const selected = new Set(this.selected);

            if (this.checked) {
                selected.delete(this.value);
            } else {
                selected.add(this.value);
            }

            this.$emit('update:selected', [...selected.values()]);
        }
    },
    computed: {
        proxiedId () {
            return this.id ?? `proxied-checkbox-${uuid++}`;
        },
        checked () {
            return this.selected.includes(this.value);
        },
    },
    render () {
        return h('input', {
            type: 'checkbox',
            name: this.name,
            id: this.proxiedId,
            value: this.value,
            checked: this.checked ? true : null,
            onChange: this.changeCollection,
        });
    }
};
</script>