-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathRadioButtonGroup.svelte
120 lines (103 loc) · 2.65 KB
/
RadioButtonGroup.svelte
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<script>
/**
* @event {string | number} change
*/
/**
* Set the selected radio button value
* @type {string | number}
*/
export let selected = undefined;
/** Set to `true` to disable the radio buttons */
export let disabled = false;
/**
* Set to `true` to require the selection of a radio button
* @type {boolean}
*/
export let required = undefined;
/**
* Specify a name attribute for the radio button inputs
* @type {string}
*/
export let name;
/** Specify the legend text */
export let legendText = "";
/** Set to `true` to visually hide the legend */
export let hideLegend = false;
/**
* Specify the label position
* @type {"right" | "left"}
*/
export let labelPosition = "right";
/**
* Specify the orientation of the radio buttons
* @type {"horizontal" | "vertical"}
*/
export let orientation = "horizontal";
/**
* Set an id for the container div element
* @type {string}
*/
export let id = undefined;
import {
beforeUpdate,
createEventDispatcher,
onMount,
setContext,
} from "svelte";
import { readonly, writable } from "svelte/store";
const dispatch = createEventDispatcher();
const selectedValue = writable(selected);
const groupName = writable(name);
const groupRequired = writable(required);
setContext("RadioButtonGroup", {
selectedValue,
groupName: readonly(groupName),
groupRequired: readonly(groupRequired),
add: ({ checked, value }) => {
if (checked) {
selectedValue.set(value);
}
},
update: (value) => {
selected = value;
},
});
onMount(() => {
$selectedValue = selected;
});
beforeUpdate(() => {
$selectedValue = selected;
});
selectedValue.subscribe((value) => {
selected = value;
dispatch("change", value);
});
$: $groupName = name;
$: $groupRequired = required;
</script>
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
id="{id}"
class:bx--form-item="{true}"
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
>
<fieldset
class:bx--radio-button-group="{true}"
class:bx--radio-button-group--vertical="{orientation === 'vertical'}"
class:bx--radio-button-group--label-left="{labelPosition === 'left'}"
class:bx--radio-button-group--label-right="{labelPosition === 'right'}"
disabled="{disabled}"
>
{#if legendText || $$slots.legendText}
<legend class:bx--label="{true}" class:bx--visually-hidden="{hideLegend}">
<slot name="legendText">{legendText}</slot>
</legend>
{/if}
<slot />
</fieldset>
</div>