Skip to content

Commit

Permalink
fix(tile-group): add name and required props (#1818)
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym authored Oct 3, 2023
1 parent 5ef4dc1 commit 836b360
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 45 deletions.
15 changes: 9 additions & 6 deletions COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -3019,11 +3019,12 @@ None.
| checked | No | <code>let</code> | Yes | <code>boolean</code> | <code>false</code> | Set to `true` to check the tile |
| light | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to enable the light variant |
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the tile |
| required | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to mark the field as required |
| value | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the value of the radio input |
| tabindex | No | <code>let</code> | No | <code>string</code> | <code>"0"</code> | Specify the tabindex |
| iconDescription | No | <code>let</code> | No | <code>string</code> | <code>"Tile checkmark"</code> | Specify the ARIA label for the radio tile checkmark icon |
| id | No | <code>let</code> | No | <code>string</code> | <code>"ccs-" + Math.random().toString(36)</code> | Set an id for the input element |
| name | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify a name attribute for the input |
| name | No | <code>let</code> | No | <code>string</code> | <code>undefined</code> | Specify a name attribute for the radio tile input |

### Slots

Expand Down Expand Up @@ -4234,11 +4235,13 @@ export type CarbonTheme = "white" | "g10" | "g80" | "g90" | "g100";

### Props

| Prop name | Required | Kind | Reactive | Type | Default value | Description |
| :-------- | :------- | :--------------- | :------- | -------------------- | ---------------------- | --------------------------------------- |
| selected | No | <code>let</code> | Yes | <code>string</code> | <code>undefined</code> | Specify the selected tile value |
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the tile group |
| legend | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the legend text |
| Prop name | Required | Kind | Reactive | Type | Default value | Description |
| :-------- | :------- | :--------------- | :------- | -------------------- | ---------------------- | -------------------------------------------------------- |
| selected | No | <code>let</code> | Yes | <code>string</code> | <code>undefined</code> | Specify the selected tile value |
| disabled | No | <code>let</code> | No | <code>boolean</code> | <code>false</code> | Set to `true` to disable the tile group |
| 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 |
| legend | No | <code>let</code> | No | <code>string</code> | <code>""</code> | Specify the legend text |

### Slots

Expand Down
37 changes: 35 additions & 2 deletions docs/src/COMPONENT_API.json
Original file line number Diff line number Diff line change
Expand Up @@ -9679,6 +9679,18 @@
"constant": false,
"reactive": false
},
{
"name": "required",
"kind": "let",
"description": "Set to `true` to mark the field as required",
"type": "boolean",
"value": "false",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "value",
"kind": "let",
Expand Down Expand Up @@ -9730,9 +9742,8 @@
{
"name": "name",
"kind": "let",
"description": "Specify a name attribute for the input",
"description": "Specify a name attribute for the radio tile input",
"type": "string",
"value": "\"\"",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
Expand Down Expand Up @@ -13110,6 +13121,28 @@
"constant": false,
"reactive": false
},
{
"name": "required",
"kind": "let",
"description": "Set to `true` to require the selection of a radio button",
"type": "boolean",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "name",
"kind": "let",
"description": "Specify a name attribute for the radio button inputs",
"type": "string",
"isFunction": false,
"isFunctionDeclaration": false,
"isRequired": false,
"constant": false,
"reactive": false
},
{
"name": "legend",
"kind": "let",
Expand Down
10 changes: 5 additions & 5 deletions docs/src/pages/components/RadioButton.svx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It's recommended that you provide a legend for accessibility.

Use `hideLegend` to visually hide the legend text.

<RadioButtonGroup hideLegend legendText="Storage tier (disk)" selected="standard">
<RadioButtonGroup hideLegend legendText="Storage tier (disk)" name="plan-legend" selected="standard">
<RadioButton labelText="Free (1 GB)" value="free" />
<RadioButton labelText="Standard (10 GB)" value="standard" />
<RadioButton labelText="Pro (128 GB)" value="pro" />
Expand All @@ -33,7 +33,7 @@ Use `hideLegend` to visually hide the legend text.

Use the named "legendText" slot to customize the legend text.

<RadioButtonGroup name="plan" selected="standard">
<RadioButtonGroup name="plan-legend-slot" selected="standard">
<div slot="legendText" style:display="flex">
Storage tier (disk)
<Tooltip>
Expand All @@ -55,23 +55,23 @@ Use the `selected` prop to bind and update the selected value.

## Left-aligned label text

<RadioButtonGroup labelPosition="left" legendText="Storage tier (disk)" name="plan" selected="standard">
<RadioButtonGroup labelPosition="left" legendText="Storage tier (disk)" name="plan-left-aligned" selected="standard">
<RadioButton labelText="Free (1 GB)" value="free" />
<RadioButton labelText="Standard (10 GB)" value="standard" />
<RadioButton labelText="Pro (128 GB)" value="pro" />
</RadioButtonGroup>

## Disabled buttons

<RadioButtonGroup labelPosition="left" legendText="Storage tier (disk)" name="plan" selected="standard">
<RadioButtonGroup labelPosition="left" legendText="Storage tier (disk)" name="plan-disabled" selected="standard">
<RadioButton disabled labelText="Free (1 GB)" value="free" />
<RadioButton labelText="Standard (10 GB)" value="standard" />
<RadioButton disabled labelText="Pro (128 GB)" value="pro" />
</RadioButtonGroup>

## Vertical orientation

<RadioButtonGroup orientation="vertical" legendText="Storage tier (disk)" name="plan" selected="standard">
<RadioButtonGroup orientation="vertical" legendText="Storage tier (disk)" name="plan-vertical" selected="standard">
<RadioButton labelText="Free (1 GB)" value="free" />
<RadioButton labelText="Standard (10 GB)" value="standard" />
<RadioButton labelText="Pro (128 GB)" value="pro" />
Expand Down
24 changes: 12 additions & 12 deletions docs/src/pages/components/RadioTile.svx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ components: ["TileGroup", "RadioTile"]

## Default

<TileGroup legend="Service pricing tiers">
<RadioTile value="0" checked>
<TileGroup legend="Service pricing tiers" name="plan" selected="standard">
<RadioTile value="lite">
Lite plan
</RadioTile>
<RadioTile value="1">
<RadioTile value="standard">
Standard plan
</RadioTile>
<RadioTile value="2">
<RadioTile value="plus">
Plus plan
</RadioTile>
</TileGroup>
Expand All @@ -33,28 +33,28 @@ Binding to the `selected` prop is a more concise approach to managing state.

## Light variant

<TileGroup legend="Service pricing tiers">
<RadioTile light value="0" checked>
<TileGroup legend="Service pricing tiers" name="plan-light" selected="standard">
<RadioTile light value="lite">
Lite plan
</RadioTile>
<RadioTile light value="1">
<RadioTile light value="standard">
Standard plan
</RadioTile>
<RadioTile light value="2">
<RadioTile light value="plus">
Plus plan
</RadioTile>
</TileGroup>

## Disabled state

<TileGroup legend="Service pricing tiers">
<RadioTile value="0" checked>
<TileGroup legend="Service pricing tiers" name="plan-disabled" selected="standard">
<RadioTile value="lite" disabled>
Lite plan
</RadioTile>
<RadioTile value="1" disabled>
<RadioTile value="standard">
Standard plan
</RadioTile>
<RadioTile value="2" disabled>
<RadioTile value="plus" disabled>
Plus plan
</RadioTile>
</TileGroup>
4 changes: 2 additions & 2 deletions docs/src/pages/framed/RadioTile/RadioTileReactive.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const values = ["Lite plan", "Standard plan", "Plus plan"];
let selected = values[0];
let selected = values[1];
</script>

<TileGroup legend="Service pricing tiers" bind:selected>
<TileGroup legend="Service pricing tiers" name="plan" bind:selected>
{#each values as value}
<RadioTile value="{value}">{value}</RadioTile>
{/each}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
const values = ["Lite plan", "Standard plan", "Plus plan"];
let selected = values[0];
let selected = values[1];
</script>

<TileGroup
legend="Service pricing tiers"
name="plan"
on:select="{({ detail }) => (selected = detail)}"
>
{#each values as value}
<RadioTile value="{value}" checked="{selected === value}">{value}</RadioTile
>
<RadioTile value="{value}" checked="{selected === value}">
{value}
</RadioTile>
{/each}
</TileGroup>

Expand Down
22 changes: 18 additions & 4 deletions src/Tile/RadioTile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
/** Set to `true` to disable the tile */
export let disabled = false;
/** Set to `true` to mark the field as required */
export let required = false;
/** Specify the value of the radio input */
export let value = "";
Expand All @@ -20,13 +23,23 @@
/** Set an id for the input element */
export let id = "ccs-" + Math.random().toString(36);
/** Specify a name attribute for the input */
export let name = "";
/**
* Specify a name attribute for the radio tile input
* @type {string}
*/
export let name = undefined;
import { getContext } from "svelte";
import { readable } from "svelte/store";
import CheckmarkFilled from "../icons/CheckmarkFilled.svelte";
const { add, update, selectedValue } = getContext("TileGroup");
const { add, update, selectedValue, groupName, groupRequired } = getContext(
"TileGroup"
) ?? {
groupName: readable(undefined),
groupRequired: readable(undefined),
selectedValue: readable(checked ? value : undefined),
};
add({ value, checked });
Expand All @@ -36,11 +49,12 @@
<input
type="radio"
id="{id}"
name="{name}"
name="{$groupName ?? name}"
value="{value}"
checked="{checked}"
tabindex="{disabled ? undefined : tabindex}"
disabled="{disabled}"
required="{$groupRequired ?? required}"
class:bx--tile-input="{true}"
on:change
on:change="{() => {
Expand Down
20 changes: 19 additions & 1 deletion src/Tile/TileGroup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@
/** Set to `true` to disable the tile group */
export let disabled = 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
* @type {string}
*/
export let name = undefined;
/** Specify the legend text */
export let legend = "";
import { createEventDispatcher, setContext } from "svelte";
import { writable } from "svelte/store";
import { writable, readonly } from "svelte/store";
const dispatch = createEventDispatcher();
const selectedValue = writable(selected);
const groupName = writable(name);
const groupRequired = writable(required);
setContext("TileGroup", {
selectedValue,
groupName: readonly(groupName),
groupRequired: readonly(groupRequired),
add: ({ checked, value }) => {
if (checked) {
selectedValue.set(value);
Expand All @@ -32,6 +48,8 @@
$: selected = $selectedValue;
$: selectedValue.set(selected);
$: $groupName = name;
$: $groupRequired = required;
</script>

<fieldset disabled="{disabled}" class:bx--tile-group="{true}" {...$$restProps}>
Expand Down
10 changes: 2 additions & 8 deletions tests/RadioTile.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
import { TileGroup, RadioTile } from "../types";
</script>

<TileGroup legend="Service pricing tiers">
<RadioTile value="0" checked>Lite plan</RadioTile>
<TileGroup name="plan" required legend="Service pricing tiers">
<RadioTile light value="0" checked>Lite plan</RadioTile>
<RadioTile value="1">Standard plan</RadioTile>
<RadioTile value="2">Plus plan</RadioTile>
</TileGroup>

<TileGroup legend="Service pricing tiers">
<RadioTile light value="0" checked>Lite plan</RadioTile>
<RadioTile light value="1">Standard plan</RadioTile>
<RadioTile light value="2">Plus plan</RadioTile>
</TileGroup>
10 changes: 8 additions & 2 deletions types/Tile/RadioTile.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export interface RadioTileProps extends RestProps {
*/
disabled?: boolean;

/**
* Set to `true` to mark the field as required
* @default false
*/
required?: boolean;

/**
* Specify the value of the radio input
* @default ""
Expand All @@ -47,8 +53,8 @@ export interface RadioTileProps extends RestProps {
id?: string;

/**
* Specify a name attribute for the input
* @default ""
* Specify a name attribute for the radio tile input
* @default undefined
*/
name?: string;

Expand Down
12 changes: 12 additions & 0 deletions types/Tile/TileGroup.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export interface TileGroupProps extends RestProps {
*/
disabled?: boolean;

/**
* Set to `true` to require the selection of a radio button
* @default undefined
*/
required?: boolean;

/**
* Specify a name attribute for the radio button inputs
* @default undefined
*/
name?: string;

/**
* Specify the legend text
* @default ""
Expand Down

0 comments on commit 836b360

Please sign in to comment.