Skip to content

Commit 8bb4817

Browse files
committed
- RadioButtonGroup values for name/required override those of RadioButton
- Fix reactivity when RadioButton is used without group - Use `readable` when writing is not required - Use `readonly` to make intent clearer. - Remove default values for `required` and `name` - More consistent with other input types - Means that `name` attribute will not be added for no reason
1 parent 448c3d3 commit 8bb4817

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

COMPONENT_INDEX.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2944,7 +2944,7 @@ None.
29442944
| labelText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the label text |
29452945
| hideLabel | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the label text |
29462946
| id | No | <code>let</code> | No | <code>string</code> | <code>"ccs-" + Math.random().toString(36)</code> | Set an id for the input element |
2947-
| name | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify a name attribute for the radio button input |
2947+
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the radio button input |
29482948

29492949
### Slots
29502950

@@ -2966,8 +2966,8 @@ None.
29662966
| :------------ | :------- | :--------------- | :------- | ------------------------------------------- | ------------------------- | -------------------------------------------------------- |
29672967
| selected | No | <code>let</code> | Yes | <code>string</code> | <code>undefined</code> | Set the selected radio button value |
29682968
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the radio buttons |
2969-
| required | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to require the selection of a radio button |
2970-
| name | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify a name attribute for the radio button inputs |
2969+
| required | No | <code>let</code> | No | <code>boolean</code> | <code>undefined</code> | Set to `true` to require the selection of a radio button |
2970+
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the radio button inputs |
29712971
| legendText | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the legend text |
29722972
| hideLegend | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to visually hide the legend |
29732973
| labelPosition | No | <code>let</code> | No | <code>"right" &#124; "left"</code> | <code>"right"</code> | Specify the label position |

docs/src/COMPONENT_API.json

-3
Original file line numberDiff line numberDiff line change
@@ -9463,7 +9463,6 @@
94639463
"kind": "let",
94649464
"description": "Specify a name attribute for the radio button input",
94659465
"type": "string",
9466-
"value": "\"\"",
94679466
"isFunction": false,
94689467
"isFunctionDeclaration": false,
94699468
"isRequired": false,
@@ -9528,7 +9527,6 @@
95289527
"kind": "let",
95299528
"description": "Set to `true` to require the selection of a radio button",
95309529
"type": "boolean",
9531-
"value": "false",
95329530
"isFunction": false,
95339531
"isFunctionDeclaration": false,
95349532
"isRequired": false,
@@ -9540,7 +9538,6 @@
95409538
"kind": "let",
95419539
"description": "Specify a name attribute for the radio button inputs",
95429540
"type": "string",
9543-
"value": "\"\"",
95449541
"isFunction": false,
95459542
"isFunctionDeclaration": false,
95469543
"isRequired": false,

src/RadioButton/RadioButton.svelte

+11-8
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@
2626
/** Set an id for the input element */
2727
export let id = "ccs-" + Math.random().toString(36);
2828
29-
/** Specify a name attribute for the radio button input */
30-
export let name = "";
29+
/**
30+
* Specify a name attribute for the radio button input
31+
* @type {string}
32+
*/
33+
export let name = undefined;
3134
3235
/** Obtain a reference to the input HTML element */
3336
export let ref = null;
3437
3538
import { getContext } from "svelte";
36-
import { writable } from "svelte/store";
39+
import { readable } from "svelte/store";
3740
3841
const { add, update, selectedValue, groupName, groupRequired } = getContext(
3942
"RadioButtonGroup"
4043
) ?? {
41-
groupName: writable(name),
42-
groupRequired: writable(required),
43-
selectedValue: writable(checked ? value : undefined),
44+
groupName: readable(undefined),
45+
groupRequired: readable(undefined),
46+
selectedValue: readable(checked ? value : undefined),
4447
};
4548
4649
if (add) {
@@ -59,10 +62,10 @@
5962
bind:this="{ref}"
6063
type="radio"
6164
id="{id}"
62-
name="{'name' in $$props ? name : $groupName}"
65+
name="{$groupName ?? name}"
6366
checked="{checked}"
6467
disabled="{disabled}"
65-
required="{'required' in $$props ? required : $groupRequired}"
68+
required="{$groupRequired ?? required}"
6669
value="{value}"
6770
class:bx--radio-button="{true}"
6871
on:change

src/RadioButtonGroup/RadioButtonGroup.svelte

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
/** Set to `true` to disable the radio buttons */
99
export let disabled = false;
1010
11-
/** Set to `true` to require the selection of a radio button */
12-
export let required = false;
11+
/**
12+
* Set to `true` to require the selection of a radio button
13+
* @type {boolean}
14+
*/
15+
export let required = undefined;
1316
14-
/** Specify a name attribute for the radio button inputs */
15-
export let name = "";
17+
/**
18+
* Specify a name attribute for the radio button inputs
19+
* @type {string}
20+
*/
21+
export let name = undefined;
1622
1723
/** Specify the legend text */
1824
export let legendText = "";
@@ -44,7 +50,7 @@
4450
onMount,
4551
setContext,
4652
} from "svelte";
47-
import { writable } from "svelte/store";
53+
import { readonly, writable } from "svelte/store";
4854
4955
const dispatch = createEventDispatcher();
5056
const selectedValue = writable(selected);
@@ -53,8 +59,8 @@
5359
5460
setContext("RadioButtonGroup", {
5561
selectedValue,
56-
groupName: { subscribe: groupName.subscribe },
57-
groupRequired: { subscribe: groupRequired.subscribe },
62+
groupName: readonly(groupName),
63+
groupRequired: readonly(groupRequired),
5864
add: ({ checked, value }) => {
5965
if (checked) {
6066
selectedValue.set(value);

types/RadioButton/RadioButton.svelte.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export interface RadioButtonProps extends RestProps {
5454

5555
/**
5656
* Specify a name attribute for the radio button input
57-
* @default ""
57+
* @default undefined
5858
*/
5959
name?: string;
6060

types/RadioButtonGroup/RadioButtonGroup.svelte.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export interface RadioButtonGroupProps extends RestProps {
1818

1919
/**
2020
* Set to `true` to require the selection of a radio button
21-
* @default false
21+
* @default undefined
2222
*/
2323
required?: boolean;
2424

2525
/**
2626
* Specify a name attribute for the radio button inputs
27-
* @default ""
27+
* @default undefined
2828
*/
2929
name?: string;
3030

0 commit comments

Comments
 (0)