Update checked for radio buttons without group.

Make radio button group `name` a required property.
This commit is contained in:
brunnerh 2024-01-03 04:05:51 +01:00
commit 6f51bdbf1b
7 changed files with 33 additions and 12 deletions

View file

@ -54,6 +54,22 @@
}
$: checked = $selectedValue === value;
function onChange() {
if (update) {
update(value);
} else {
checked = ref.checked;
if (name != null && name != "") {
Array.from(document.getElementsByName(name))
.filter((element) => element !== ref)
.forEach((element) =>
element.dispatchEvent(new CustomEvent("carbon:checked-change"))
);
}
}
}
</script>
<div
@ -72,10 +88,9 @@
value="{value}"
class:bx--radio-button="{true}"
on:change
on:change="{() => {
if (update) {
update(value);
}
on:change="{onChange}"
on:carbon:checked-change="{(e) => {
checked = e.currentTarget.checked;
}}"
/>
<label class:bx--radio-button__label="{true}" for="{id}">

View file

@ -22,7 +22,7 @@
* Specify a name attribute for the radio button inputs
* @type {string}
*/
export let name = undefined;
export let name;
/** Specify the legend text */
export let legendText = "";