summaryrefslogtreecommitdiff
path: root/packages/client/src/components/form/radios.vue
blob: ff5d51f9c71dc463b36e350459c8d9e6eec91e6b (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
82
83
<script lang="ts">
import { defineComponent, h } from 'vue';
import MkRadio from './radio.vue';

export default defineComponent({
	components: {
		MkRadio
	},
	props: {
		modelValue: {
			required: false
		},
	},
	data() {
		return {
			value: this.modelValue,
		}
	},
	watch: {
		value() {
			this.$emit('update:modelValue', this.value);
		}
	},
	render() {
		let options = this.$slots.default();
		const label = this.$slots.label && this.$slots.label();
		const caption = this.$slots.caption && this.$slots.caption();

		// なぜかFragmentになることがあるため
		if (options.length === 1 && options[0].props == null) options = options[0].children;

		return h('div', {
			class: 'novjtcto'
		}, [
			...(label ? [h('div', {
				class: 'label'
			}, [label])] : []),
			h('div', {
				class: 'body'
			}, options.map(option => h(MkRadio, {
					key: option.key,
					value: option.props.value,
					modelValue: this.value,
					'onUpdate:modelValue': value => this.value = value,
				}, option.children)),
			),
			...(caption ? [h('div', {
				class: 'caption'
			}, [caption])] : []),
		]);
	}
});
</script>

<style lang="scss">
.novjtcto {
	> .label {
		font-size: 0.85em;
		padding: 0 0 8px 0;
		user-select: none;

		&:empty {
			display: none;
		}
	}

	> .body {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
		grid-gap: 12px;
	}

	> .caption {
		font-size: 0.85em;
		padding: 8px 0 0 0;
		color: var(--fgTransparentWeak);

		&:empty {
			display: none;
		}
	}
}
</style>