aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/ColourSelector.vue
blob: 43520b327928600606eb7ed0475c1941205828a7 (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
<template>
    <table class="colour-selector">
        <tbody>
            <tr ref="row">
                <td v-for="colour in colours" :key="`colour-${colour.id}`"
                    class="colour colour-selector"
                >
                    <input type="radio"
                           :name="inputName"
                           :id="`colour-${colour.id}`"
                           :value="colour.id"
                           v-model="selectedColor"
                           :aria-label="colour.label ?? null"
                    >
                    <label :for="`colour-${colour.id}`"
                           :class="colour.class ?? null"
                           :style="{backgroundColor: colour.colour ?? null}">
                    </label>
                </td>
            </tr>
        </tbody>
    </table>
</template>
<script setup>
import {onMounted, ref} from "vue";

const selectedColor = defineModel({
    type: Number,
    default: () => null,
});

const row = ref(null);

const props = defineProps({
    autofocus: {
        type: Boolean,
        default: false,
    },
    colours: {
        type: Array,
        required: true
    },
    inputName: {
        type: String,
        default: 'colour_id'
    },
});

if (props.autofocus) {
    onMounted(() => {
        row.value.querySelector('input[type="radio"]:checked')?.focus();
    });
}
</script>