Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update checked for radio buttons without group. #1880

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -2967,7 +2967,7 @@ None.
| selected | No | <code>let</code> | Yes | <code>string &#124; number</code> | <code>undefined</code> | Set the selected radio button value |
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the radio buttons |
| required | No | <code>let</code> | No | <code>boolean</code> | <code>undefined</code> | Set to `true` to require the selection of a radio button |
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the radio button inputs |
| name | Yes | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the radio button inputs |
| legendText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the legend text |
| hideLegend | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the legend |
| labelPosition | No | <code>let</code> | No | <code>"right" &#124; "left"</code> | <code>"right"</code> | Specify the label position |
Expand Down
2 changes: 1 addition & 1 deletion docs/src/COMPONENT_API.json
Original file line number Diff line number Diff line change
Expand Up @@ -9540,7 +9540,7 @@
"type": "string",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"isRequired": true,
"constant": false,
"reactive": false
},
Expand Down
5 changes: 4 additions & 1 deletion docs/src/pages/_layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
bind:theme="{$theme}"
on:update="{(e) => {
const theme = e.detail.theme;
document.documentElement.style.setProperty("color-scheme", ["white", "g10"].includes(theme) ? "light" : "dark");
document.documentElement.style.setProperty(
'color-scheme',
['white', 'g10'].includes(theme) ? 'light' : 'dark'
);
}}"
>
<Header
Expand Down
6 changes: 3 additions & 3 deletions docs/src/pages/components/RadioButton.svx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ Use the `selected` prop to bind and update the selected value.

## Skeleton

<RadioButtonGroup legendText="Storage tier (disk)">
<RadioButtonGroup legendText="Storage tier (disk)" name="skeleton">
<RadioButtonSkeleton />
<RadioButtonSkeleton />
<RadioButtonSkeleton />
</RadioButtonGroup>

## Skeleton (vertical)

<RadioButtonGroup orientation="vertical" legendText="Storage tier (disk)">
<RadioButtonGroup orientation="vertical" legendText="Storage tier (disk)" name="skeleton-vertical">
<RadioButtonSkeleton />
<RadioButtonSkeleton />
<RadioButtonSkeleton />
</RadioButtonGroup>
</RadioButtonGroup>
6 changes: 5 additions & 1 deletion docs/src/pages/framed/Theme/Theme.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

<Theme bind:theme />

<RadioButtonGroup legendText="Carbon theme" bind:selected="{theme}">
<RadioButtonGroup
legendText="Carbon theme"
name="theme"
bind:selected="{theme}"
>
{#each ["white", "g10", "g80", "g90", "g100"] as value}
<RadioButton labelText="{value}" value="{value}" />
{/each}
Expand Down
6 changes: 5 additions & 1 deletion docs/src/pages/framed/Theme/ThemePersist.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

<Theme bind:theme persist persistKey="__carbon-theme" />

<RadioButtonGroup legendText="Carbon theme" bind:selected="{theme}">
<RadioButtonGroup
legendText="Carbon theme"
name="theme"
bind:selected="{theme}"
>
{#each ["white", "g10", "g80", "g90", "g100"] as value}
<RadioButton labelText="{value}" value="{value}" />
{/each}
Expand Down
9 changes: 6 additions & 3 deletions docs/src/pages/framed/_reset.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
// NOTE: we *do not* want to persist the theme as this can
// conflict with how the iframe is displayed in the docs.
// Instead, we want the theme to be overridden in the standalone page.
if ([ "white", "g10", "g80", "g90", "g100" ].includes(current_theme)) {
document.documentElement.setAttribute("theme", current_theme)
document.documentElement.style.setProperty("color-scheme", ["white", "g10"].includes(current_theme) ? "light" : "dark");
if (["white", "g10", "g80", "g90", "g100"].includes(current_theme)) {
document.documentElement.setAttribute("theme", current_theme);
document.documentElement.style.setProperty(
"color-scheme",
["white", "g10"].includes(current_theme) ? "light" : "dark"
);
}
}
</script>
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<RadioButtonGroup
style="margin-top: var(--cds-spacing-08)"
legendText="Carbon themes"
name="theme"
bind:selected="{$theme}"
>
{#each ["white", "g10", "g80", "g90", "g100"] as value}
Expand Down
23 changes: 19 additions & 4 deletions src/RadioButton/RadioButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}">
Expand Down
2 changes: 1 addition & 1 deletion src/RadioButtonGroup/RadioButtonGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
2 changes: 1 addition & 1 deletion tests/Form.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Checkbox id="checkbox-2" labelText="Checkbox Label" disabled />
</FormGroup>
<FormGroup legendText="Radio buttons">
<RadioButtonGroup selected="default-selected">
<RadioButtonGroup selected="default-selected" name="radio-group">
<RadioButton
id="radio-1"
value="standard"
Expand Down
8 changes: 7 additions & 1 deletion tests/RadioButton.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<RadioButtonGroup
legendText="Storage tier (disk)"
name="group-1"
selected="standard"
on:change="{(e) => {
console.log(e.detail); // string
Expand All @@ -16,6 +17,7 @@

<RadioButtonGroup
legendText="Storage tier (disk)"
name="group-2"
labelPosition="left"
selected="standard"
>
Expand All @@ -24,7 +26,11 @@
<RadioButton labelText="Pro (128 GB)" value="pro" />
</RadioButtonGroup>

<RadioButtonGroup orientation="vertical" legendText="Storage tier (disk)">
<RadioButtonGroup
orientation="vertical"
legendText="Storage tier (disk)"
name="group-3"
>
<RadioButtonSkeleton />
<RadioButtonSkeleton />
<RadioButtonSkeleton />
Expand Down
2 changes: 1 addition & 1 deletion types/RadioButtonGroup/RadioButtonGroup.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface RadioButtonGroupProps extends RestProps {
* Specify a name attribute for the radio button inputs
* @default undefined
*/
name?: string;
name: string;

/**
* Specify the legend text
Expand Down