Skip to content

Commit 4332de4

Browse files
authored
fix: update ToggleGroup examples to properly toggle options (#12239)
- Update ToggleGroup Single select example to allow toggling off selected options - Update Table Basic example to remove 'Default' option and allow toggling - Update Table Actions example to remove 'Default actions toggle' option and allow toggling - Update Alert Async live region example to remove 'off' option and make single toggle Ensures all ToggleGroup instances follow aria-pressed semantics where clicking a selected option unselects it, and removes redundant 'default' or 'off' options that prevent proper toggle behavior. Fixes #12234
1 parent d2d4b0c commit 4332de4

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

packages/react-core/src/components/Alert/examples/AlertAsyncLiveRegion.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,7 @@ export const AsyncLiveRegionAlert: React.FunctionComponent = () => {
4242
text="Async alerts on"
4343
buttonId="async-alerts-on"
4444
isSelected={isActive}
45-
onChange={() => setIsActive(true)}
46-
/>
47-
<ToggleGroupItem
48-
text="Async alerts off"
49-
buttonId="async-alerts-off"
50-
isSelected={!isActive}
51-
onChange={() => setIsActive(false)}
45+
onChange={() => setIsActive(!isActive)}
5246
/>
5347
</ToggleGroup>
5448
<AlertGroup hasAnimations isLiveRegion>

packages/react-core/src/components/ToggleGroup/examples/ToggleGroupDefaultSingle.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export const ToggleGroupDefaultSingle: React.FunctionComponent = () => {
55
const [isSelected, setIsSelected] = useState('');
66
const handleItemClick = (event, _isSelected: boolean) => {
77
const id = event.currentTarget.id;
8-
setIsSelected(id);
8+
// Allow toggling off if clicking the already selected item
9+
setIsSelected(isSelected === id ? '' : id);
910
};
1011
return (
1112
<ToggleGroup aria-label="Default with single selectable">

packages/react-table/src/components/Table/examples/TableActions.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface Repository {
2323
singleAction: string;
2424
}
2525

26-
type ExampleType = 'defaultToggle' | 'customToggle';
26+
type ExampleType = 'customToggle';
2727

2828
export const TableActions: React.FunctionComponent = () => {
2929
// In real usage, this data would come from some external source like an API via props.
@@ -45,10 +45,11 @@ export const TableActions: React.FunctionComponent = () => {
4545
};
4646

4747
// This state is just for the ToggleGroup in this example and isn't necessary for Table usage.
48-
const [exampleChoice, setExampleChoice] = useState<ExampleType>('defaultToggle');
48+
const [exampleChoice, setExampleChoice] = useState<ExampleType | ''>('');
4949
const onExampleTypeChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => {
5050
const id = event.currentTarget.id;
51-
setExampleChoice(id as ExampleType);
51+
// Allow toggling off if clicking the already selected item
52+
setExampleChoice(exampleChoice === id ? '' : (id as ExampleType));
5253
};
5354

5455
const customActionsToggle = (props: CustomActionsToggleProps) => (
@@ -95,12 +96,6 @@ export const TableActions: React.FunctionComponent = () => {
9596
return (
9697
<Fragment>
9798
<ToggleGroup aria-label="Default uses kebab toggle">
98-
<ToggleGroupItem
99-
text="Default actions toggle"
100-
buttonId="defaultToggle"
101-
isSelected={exampleChoice === 'defaultToggle'}
102-
onChange={onExampleTypeChange}
103-
/>
10499
<ToggleGroupItem
105100
text="Custom actions toggle"
106101
buttonId="customToggle"

packages/react-table/src/components/Table/examples/TableBasic.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface Repository {
1010
lastCommit: string;
1111
}
1212

13-
type ExampleType = 'default' | 'compact' | 'compactBorderless';
13+
type ExampleType = 'compact' | 'compactBorderless';
1414

1515
export const TableBasic: React.FunctionComponent = () => {
1616
// In real usage, this data would come from some external source like an API via props.
@@ -29,21 +29,16 @@ export const TableBasic: React.FunctionComponent = () => {
2929
};
3030

3131
// This state is just for the ToggleGroup in this example and isn't necessary for Table usage.
32-
const [exampleChoice, setExampleChoice] = useState<ExampleType>('default');
32+
const [exampleChoice, setExampleChoice] = useState<ExampleType | ''>('');
3333
const onExampleTypeChange: ToggleGroupItemProps['onChange'] = (event, _isSelected) => {
3434
const id = event.currentTarget.id;
35-
setExampleChoice(id as ExampleType);
35+
// Allow toggling off if clicking the already selected item
36+
setExampleChoice(exampleChoice === id ? '' : (id as ExampleType));
3637
};
3738

3839
return (
3940
<Fragment>
4041
<ToggleGroup aria-label="Default with single selectable">
41-
<ToggleGroupItem
42-
text="Default"
43-
buttonId="default"
44-
isSelected={exampleChoice === 'default'}
45-
onChange={onExampleTypeChange}
46-
/>
4742
<ToggleGroupItem
4843
text="Compact"
4944
buttonId="compact"
@@ -59,7 +54,7 @@ export const TableBasic: React.FunctionComponent = () => {
5954
</ToggleGroup>
6055
<Table
6156
aria-label="Simple table"
62-
variant={exampleChoice !== 'default' ? 'compact' : undefined}
57+
variant={exampleChoice === 'compact' || exampleChoice === 'compactBorderless' ? 'compact' : undefined}
6358
borders={exampleChoice !== 'compactBorderless'}
6459
>
6560
<Caption>Simple table using composable components</Caption>

0 commit comments

Comments
 (0)