Skip to content

Commit 88ed152

Browse files
feat(ExpandableSection): added aria labeling (#12071)
Co-authored-by: Eric Olkowski <git.eric@thatblindgeye.dev>
1 parent 2ec8ac7 commit 88ed152

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface ExpandableSectionProps extends Omit<React.HTMLProps<HTMLDivElem
5656
* both are specified; used for uncontrolled expandable with dynamic toggle text).
5757
*/
5858
toggleTextExpanded?: string;
59+
/** Accessible name via human readable string for the expandable section toggle. */
60+
toggleAriaLabel?: string;
61+
/** Accessible name via space delimtted list of IDs for the expandable section toggle. */
62+
toggleAriaLabelledBy?: string;
5963
/** Truncates the expandable content to the specified number of lines when using the
6064
* "truncate" variant.
6165
*/
@@ -109,6 +113,8 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
109113
toggleText: '',
110114
toggleTextExpanded: '',
111115
toggleTextCollapsed: '',
116+
toggleAriaLabel: undefined,
117+
toggleAriaLabelledBy: undefined,
112118
// eslint-disable-next-line @typescript-eslint/no-unused-vars
113119
onToggle: (event, isExpanded): void => undefined,
114120
isDetached: false,
@@ -196,6 +202,8 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
196202
toggleTextExpanded,
197203
toggleTextCollapsed,
198204
toggleContent,
205+
toggleAriaLabel,
206+
toggleAriaLabelledBy,
199207
children,
200208
isExpanded,
201209
isDetached,
@@ -254,6 +262,8 @@ class ExpandableSection extends Component<ExpandableSectionProps, ExpandableSect
254262
</span>
255263
)
256264
})}
265+
aria-label={toggleAriaLabel}
266+
aria-labelledby={toggleAriaLabelledBy}
257267
>
258268
{toggleContent || computedToggleText}
259269
</Button>

packages/react-core/src/components/ExpandableSection/ExpandableSectionToggle.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface ExpandableSectionToggleProps extends Omit<React.HTMLProps<HTMLD
3030
onToggle?: (isExpanded: boolean) => void;
3131
/** Flag indicating that the expandable section and expandable toggle are detached from one another. */
3232
isDetached?: boolean;
33+
/** Accessible name via human readable string for the expandable section toggle. */
34+
toggleAriaLabel?: string;
35+
/** Accessible name via space delimtted list of IDs for the expandable section toggle. */
36+
toggleAriaLabelledBy?: string;
3337
}
3438

3539
export const ExpandableSectionToggle: React.FunctionComponent<ExpandableSectionToggleProps> = ({
@@ -42,6 +46,8 @@ export const ExpandableSectionToggle: React.FunctionComponent<ExpandableSectionT
4246
direction = 'down',
4347
hasTruncatedContent = false,
4448
isDetached,
49+
toggleAriaLabel,
50+
toggleAriaLabelledBy,
4551
...props
4652
}: ExpandableSectionToggleProps) => (
4753
<div
@@ -74,6 +80,8 @@ export const ExpandableSectionToggle: React.FunctionComponent<ExpandableSectionT
7480
</span>
7581
)
7682
})}
83+
aria-label={toggleAriaLabel}
84+
aria-labelledby={toggleAriaLabelledBy}
7785
>
7886
{children}
7987
</Button>

packages/react-core/src/components/ExpandableSection/__tests__/ExpandableSection.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,20 @@ test('Renders with class pf-m-detached when isDetached is true and direction is
191191

192192
expect(screen.getByText('Test content').parentElement).toHaveClass('pf-m-detached');
193193
});
194+
195+
test('Renders with aria-label when toggleAriaLabel is passed', () => {
196+
render(<ExpandableSection toggleAriaLabel="Test label"></ExpandableSection>);
197+
198+
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
199+
});
200+
201+
test('Renders with aria-labelledby when toggleAriaLabelledBy is passed', () => {
202+
render(
203+
<>
204+
<div id="test-id">Test label</div>
205+
<ExpandableSection toggleAriaLabelledBy="test-id"></ExpandableSection>
206+
</>
207+
);
208+
209+
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
210+
});

packages/react-core/src/components/ExpandableSection/__tests__/ExpandableSectionToggle.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,20 @@ test('Renders with class pf-m-detached when isDetached is true', () => {
3030

3131
expect(screen.getByTestId('test-id')).toHaveClass('pf-m-detached');
3232
});
33+
34+
test('Renders with aria-label when toggleAriaLabel is passed', () => {
35+
render(<ExpandableSectionToggle toggleAriaLabel="Test label"></ExpandableSectionToggle>);
36+
37+
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
38+
});
39+
40+
test('Renders with aria-labelledby when toggleAriaLabelledBy is passed', () => {
41+
render(
42+
<>
43+
<div id="test-id">Test label</div>
44+
<ExpandableSectionToggle toggleAriaLabelledBy="test-id"></ExpandableSectionToggle>
45+
</>
46+
);
47+
48+
expect(screen.getByRole('button')).toHaveAccessibleName('Test label');
49+
});

0 commit comments

Comments
 (0)