- 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
This commit is contained in:
brunnerh 2023-09-30 21:25:56 +02:00
commit a81328feef
7 changed files with 1528 additions and 1612 deletions

File diff suppressed because it is too large Load diff

View file

@ -4788,7 +4788,7 @@
{ {
"name": "company", "name": "company",
"kind": "let", "kind": "let",
"description": "Specify the company name. \nAlternatively, use the named slot \"company\" (e.g., `<span slot=\"company\">...</span>`)", "description": "Specify the company name.\n\nAlternatively, use the named slot \"company\" (e.g., `<span slot=\"company\">...</span>`)",
"type": "string", "type": "string",
"isFunction": false, "isFunction": false,
"isFunctionDeclaration": false, "isFunctionDeclaration": false,
@ -9463,7 +9463,6 @@
"kind": "let", "kind": "let",
"description": "Specify a name attribute for the radio button input", "description": "Specify a name attribute for the radio button input",
"type": "string", "type": "string",
"value": "\"\"",
"isFunction": false, "isFunction": false,
"isFunctionDeclaration": false, "isFunctionDeclaration": false,
"isRequired": false, "isRequired": false,
@ -9528,9 +9527,9 @@
"kind": "let", "kind": "let",
"description": "Set to `true` to require the selection of a radio button", "description": "Set to `true` to require the selection of a radio button",
"type": "boolean", "type": "boolean",
"value": "false",
"isFunction": false, "isFunction": false,
"isFunctionDeclaration": false, "isFunctionDeclaration": false,
"isRequired": false,
"constant": false, "constant": false,
"reactive": false "reactive": false
}, },
@ -9539,9 +9538,9 @@
"kind": "let", "kind": "let",
"description": "Specify a name attribute for the radio button inputs", "description": "Specify a name attribute for the radio button inputs",
"type": "string", "type": "string",
"value": "\"\"",
"isFunction": false, "isFunction": false,
"isFunctionDeclaration": false, "isFunctionDeclaration": false,
"isRequired": false,
"constant": false, "constant": false,
"reactive": false "reactive": false
}, },

View file

@ -26,21 +26,24 @@
/** Set an id for the input element */ /** Set an id for the input element */
export let id = "ccs-" + Math.random().toString(36); export let id = "ccs-" + Math.random().toString(36);
/** Specify a name attribute for the radio button input */ /**
export let name = ""; * Specify a name attribute for the radio button input
* @type {string}
*/
export let name = undefined;
/** Obtain a reference to the input HTML element */ /** Obtain a reference to the input HTML element */
export let ref = null; export let ref = null;
import { getContext } from "svelte"; import { getContext } from "svelte";
import { writable } from "svelte/store"; import { readable } from "svelte/store";
const { add, update, selectedValue, groupName, groupRequired } = getContext( const { add, update, selectedValue, groupName, groupRequired } = getContext(
"RadioButtonGroup" "RadioButtonGroup"
) ?? { ) ?? {
groupName: writable(name), groupName: readable(undefined),
groupRequired: writable(required), groupRequired: readable(undefined),
selectedValue: writable(checked ? value : undefined), selectedValue: readable(checked ? value : undefined),
}; };
if (add) { if (add) {
@ -59,10 +62,10 @@
bind:this="{ref}" bind:this="{ref}"
type="radio" type="radio"
id="{id}" id="{id}"
name="{'name' in $$props ? name : $groupName}" name="{$groupName ?? name}"
checked="{checked}" checked="{checked}"
disabled="{disabled}" disabled="{disabled}"
required="{'required' in $$props ? required : $groupRequired}" required="{$groupRequired ?? required}"
value="{value}" value="{value}"
class:bx--radio-button="{true}" class:bx--radio-button="{true}"
on:change on:change

View file

@ -8,11 +8,17 @@
/** 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; * Set to `true` to require the selection of a radio button
* @type {boolean}
*/
export let required = undefined;
/** Specify a name attribute for the radio button inputs */ /**
export let name = ""; * Specify a name attribute for the radio button inputs
* @type {string}
*/
export let name = undefined;
/** Specify the legend text */ /** Specify the legend text */
export let legendText = ""; export let legendText = "";
@ -44,7 +50,7 @@
onMount, onMount,
setContext, setContext,
} from "svelte"; } from "svelte";
import { writable } from "svelte/store"; import { readonly, writable } from "svelte/store";
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
const selectedValue = writable(selected); const selectedValue = writable(selected);
@ -53,8 +59,8 @@
setContext("RadioButtonGroup", { setContext("RadioButtonGroup", {
selectedValue, selectedValue,
groupName: { subscribe: groupName.subscribe }, groupName: readonly(groupName),
groupRequired: { subscribe: groupRequired.subscribe }, groupRequired: readonly(groupRequired),
add: ({ checked, value }) => { add: ({ checked, value }) => {
if (checked) { if (checked) {
selectedValue.set(value); selectedValue.set(value);

View file

@ -54,7 +54,7 @@ export interface RadioButtonProps extends RestProps {
/** /**
* Specify a name attribute for the radio button input * Specify a name attribute for the radio button input
* @default "" * @default undefined
*/ */
name?: string; name?: string;

View file

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

View file

@ -30,6 +30,7 @@ export interface HeaderProps extends RestProps {
/** /**
* Specify the company name. * Specify the company name.
*
* Alternatively, use the named slot "company" (e.g., `<span slot="company">...</span>`) * Alternatively, use the named slot "company" (e.g., `<span slot="company">...</span>`)
* @default undefined * @default undefined
*/ */