Add name and required to RadioButtonGroup.

Properties will be inherited in `RadioButton` unless the respective property is set there.

Fixes #1036.
This commit is contained in:
Harald Brunner 2022-01-22 18:49:45 +01:00 committed by brunnerh
commit 00cab46284
5 changed files with 1650 additions and 1505 deletions

File diff suppressed because it is too large Load diff

View file

@ -9523,6 +9523,28 @@
"constant": false, "constant": false,
"reactive": false "reactive": false
}, },
{
"name": "required",
"kind": "let",
"description": "Set to `true` to require the selection of a radio button",
"type": "boolean",
"value": "false",
"isFunction": false,
"isFunctionDeclaration": false,
"constant": false,
"reactive": false
},
{
"name": "name",
"kind": "let",
"description": "Specify a name attribute for the radio button inputs",
"type": "string",
"value": "\"\"",
"isFunction": false,
"isFunctionDeclaration": false,
"constant": false,
"reactive": false
},
{ {
"name": "legendText", "name": "legendText",
"kind": "let", "kind": "let",

View file

@ -35,13 +35,16 @@
import { getContext } from "svelte"; import { getContext } from "svelte";
import { writable } from "svelte/store"; import { writable } from "svelte/store";
const ctx = getContext("RadioButtonGroup"); const { add, update, selectedValue, groupName, groupRequired } = getContext(
const selectedValue = ctx "RadioButtonGroup"
? ctx.selectedValue ) ?? {
: writable(checked ? value : undefined); groupName: writable(name),
groupRequired: writable(required),
selectedValue: writable(checked ? value : undefined),
};
if (ctx) { if (add) {
ctx.add({ id, checked, disabled, value }); add({ id, checked, disabled, value });
} }
$: checked = $selectedValue === value; $: checked = $selectedValue === value;
@ -56,16 +59,16 @@
bind:this="{ref}" bind:this="{ref}"
type="radio" type="radio"
id="{id}" id="{id}"
name="{name}" name="{'name' in $$props ? name : $groupName}"
checked="{checked}" checked="{checked}"
disabled="{disabled}" disabled="{disabled}"
required="{required}" required="{'required' in $$props ? required : $groupRequired}"
value="{value}" value="{value}"
class:bx--radio-button="{true}" class:bx--radio-button="{true}"
on:change on:change
on:change="{() => { on:change="{() => {
if (ctx) { if (update) {
ctx.update(value); update(value);
} }
}}" }}"
/> />

View file

@ -8,6 +8,12 @@
/** Set to `true` to disable the radio buttons */ /** Set to `true` to disable the radio buttons */
export let disabled = false; export let disabled = false;
/** Set to `true` to require the selection of a radio button */
export let required = false;
/** Specify a name attribute for the radio button inputs */
export let name = "";
/** Specify the legend text */ /** Specify the legend text */
export let legendText = ""; export let legendText = "";
@ -42,9 +48,13 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
const selectedValue = writable(selected); const selectedValue = writable(selected);
const groupName = writable(name);
const groupRequired = writable(required);
setContext("RadioButtonGroup", { setContext("RadioButtonGroup", {
selectedValue, selectedValue,
groupName: { subscribe: groupName.subscribe },
groupRequired: { subscribe: groupRequired.subscribe },
add: ({ checked, value }) => { add: ({ checked, value }) => {
if (checked) { if (checked) {
selectedValue.set(value); selectedValue.set(value);
@ -67,6 +77,9 @@
selected = value; selected = value;
dispatch("change", value); dispatch("change", value);
}); });
$: $groupName = name;
$: $groupRequired = required;
</script> </script>
<!-- svelte-ignore a11y-mouse-events-have-key-events --> <!-- svelte-ignore a11y-mouse-events-have-key-events -->

View file

@ -16,6 +16,18 @@ export interface RadioButtonGroupProps extends RestProps {
*/ */
disabled?: boolean; disabled?: boolean;
/**
* Set to `true` to require the selection of a radio button
* @default false
*/
required?: boolean;
/**
* Specify a name attribute for the radio button inputs
* @default ""
*/
name?: string;
/** /**
* Specify the legend text * Specify the legend text
* @default "" * @default ""