mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
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:
parent
108eb5286c
commit
00cab46284
5 changed files with 1650 additions and 1505 deletions
3085
COMPONENT_INDEX.md
3085
COMPONENT_INDEX.md
File diff suppressed because it is too large
Load diff
|
@ -9523,6 +9523,28 @@
|
|||
"constant": 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",
|
||||
"kind": "let",
|
||||
|
|
|
@ -35,13 +35,16 @@
|
|||
import { getContext } from "svelte";
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
const ctx = getContext("RadioButtonGroup");
|
||||
const selectedValue = ctx
|
||||
? ctx.selectedValue
|
||||
: writable(checked ? value : undefined);
|
||||
const { add, update, selectedValue, groupName, groupRequired } = getContext(
|
||||
"RadioButtonGroup"
|
||||
) ?? {
|
||||
groupName: writable(name),
|
||||
groupRequired: writable(required),
|
||||
selectedValue: writable(checked ? value : undefined),
|
||||
};
|
||||
|
||||
if (ctx) {
|
||||
ctx.add({ id, checked, disabled, value });
|
||||
if (add) {
|
||||
add({ id, checked, disabled, value });
|
||||
}
|
||||
|
||||
$: checked = $selectedValue === value;
|
||||
|
@ -56,16 +59,16 @@
|
|||
bind:this="{ref}"
|
||||
type="radio"
|
||||
id="{id}"
|
||||
name="{name}"
|
||||
name="{'name' in $$props ? name : $groupName}"
|
||||
checked="{checked}"
|
||||
disabled="{disabled}"
|
||||
required="{required}"
|
||||
required="{'required' in $$props ? required : $groupRequired}"
|
||||
value="{value}"
|
||||
class:bx--radio-button="{true}"
|
||||
on:change
|
||||
on:change="{() => {
|
||||
if (ctx) {
|
||||
ctx.update(value);
|
||||
if (update) {
|
||||
update(value);
|
||||
}
|
||||
}}"
|
||||
/>
|
||||
|
|
|
@ -8,6 +8,12 @@
|
|||
/** Set to `true` to disable the radio buttons */
|
||||
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 */
|
||||
export let legendText = "";
|
||||
|
||||
|
@ -42,9 +48,13 @@
|
|||
|
||||
const dispatch = createEventDispatcher();
|
||||
const selectedValue = writable(selected);
|
||||
const groupName = writable(name);
|
||||
const groupRequired = writable(required);
|
||||
|
||||
setContext("RadioButtonGroup", {
|
||||
selectedValue,
|
||||
groupName: { subscribe: groupName.subscribe },
|
||||
groupRequired: { subscribe: groupRequired.subscribe },
|
||||
add: ({ checked, value }) => {
|
||||
if (checked) {
|
||||
selectedValue.set(value);
|
||||
|
@ -67,6 +77,9 @@
|
|||
selected = value;
|
||||
dispatch("change", value);
|
||||
});
|
||||
|
||||
$: $groupName = name;
|
||||
$: $groupRequired = required;
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
||||
|
|
|
@ -16,6 +16,18 @@ export interface RadioButtonGroupProps extends RestProps {
|
|||
*/
|
||||
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
|
||||
* @default ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue