Skip to content

Commit c9aded3

Browse files
authored
Merge pull request #333 from springload/feat/optional-region-role-on-accordion-panel
Feature: optional region role on accordion panel
2 parents 51a38ae + c805a59 commit c9aded3

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ Class(es) to apply to the 'button' element.
155155
156156
Class(es) to apply to element.
157157
158+
#### region: `boolean`
159+
160+
Make the element have a region role.
161+
158162
---
159163
160164
### AccordionItemState

integration/src/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ReactDOM.render(
1515
<AccordionItemHeading>
1616
<AccordionItemButton>Heading One</AccordionItemButton>
1717
</AccordionItemHeading>
18-
<AccordionItemPanel>
18+
<AccordionItemPanel region>
1919
Sunt in reprehenderit cillum ex proident qui culpa fugiat
2020
pariatur aliqua nostrud consequat consequat enim quis sit
2121
consectetur ad aute ea ex eiusmod id esse culpa et pariatur
@@ -26,7 +26,7 @@ ReactDOM.render(
2626
<AccordionItemHeading>
2727
<AccordionItemButton>Heading Two</AccordionItemButton>
2828
</AccordionItemHeading>
29-
<AccordionItemPanel>
29+
<AccordionItemPanel region>
3030
Velit tempor dolore commodo voluptate id do nulla do ut
3131
proident cillum ad cillum voluptate deserunt fugiat ut sed
3232
cupidatat ut consectetur consequat incididunt sed in culpa
@@ -37,7 +37,7 @@ ReactDOM.render(
3737
<AccordionItemHeading>
3838
<AccordionItemButton>Heading Three</AccordionItemButton>
3939
</AccordionItemHeading>
40-
<AccordionItemPanel>
40+
<AccordionItemPanel region>
4141
Lorem ipsum esse occaecat voluptate duis incididunt amet
4242
eiusmod sunt commodo sunt enim anim ea culpa ut tempor
4343
dolore fugiat exercitation aliquip commodo dolore elit esse

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-accessible-accordion",
3-
"version": "3.3.4",
3+
"version": "4.0.0",
44
"description": "Accessible Accordion component for React",
55
"main": "dist/umd/index.js",
66
"module": "dist/es/index.js",

src/components/AccordionItemPanel.spec.tsx

+45
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,49 @@ describe('AccordionItem', () => {
6666
expect(getByText('Hello World')).toBeTruthy();
6767
});
6868
});
69+
70+
describe('role region', () => {
71+
it('enables aria-labelledby', () => {
72+
const { getByRole } = render(
73+
<Accordion>
74+
<AccordionItem>
75+
<AccordionItemPanel region>
76+
Hello World
77+
</AccordionItemPanel>
78+
</AccordionItem>
79+
</Accordion>,
80+
);
81+
expect(
82+
getByRole('region').getAttribute('aria-labelledby'),
83+
).toBeTruthy();
84+
});
85+
86+
it('disables aria-labelledby when absent', () => {
87+
const { getByText, queryByRole } = render(
88+
<Accordion>
89+
<AccordionItem>
90+
<AccordionItemPanel>Hello World</AccordionItemPanel>
91+
</AccordionItem>
92+
</Accordion>,
93+
);
94+
expect(queryByRole('region')).toBeNull();
95+
expect(
96+
getByText('Hello World').getAttribute('aria-labelledby'),
97+
).toBeFalsy();
98+
});
99+
100+
it('disables aria-labelledby when false', () => {
101+
const { getByText, queryByRole } = render(
102+
<Accordion>
103+
<AccordionItem>
104+
<AccordionItemPanel region={false}>Hello World</AccordionItemPanel>
105+
</AccordionItem>
106+
</Accordion>,
107+
);
108+
expect(queryByRole('region')).toBeNull();
109+
expect(
110+
getByText('Hello World').getAttribute('aria-labelledby'),
111+
).toBeFalsy();
112+
});
113+
});
69114
});

src/components/AccordionItemPanel.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { DivAttributes } from '../helpers/types';
33
import { assertValidHtmlId } from '../helpers/uuid';
44
import { Consumer as ItemConsumer, ItemContext } from './ItemContext';
55

6-
type Props = DivAttributes & { className?: string };
6+
type Props = DivAttributes & { region?: boolean; className?: string };
77

88
const AccordionItemPanel = ({
99
className = 'accordion__panel',
10+
region,
1011
id,
1112
...rest
1213
}: Props): JSX.Element => {
@@ -15,12 +16,18 @@ const AccordionItemPanel = ({
1516
assertValidHtmlId(id);
1617
}
1718

19+
const attrs = {
20+
...panelAttributes,
21+
'aria-labelledby': region ? panelAttributes['aria-labelledby'] : undefined,
22+
};
23+
1824
return (
1925
<div
2026
data-accordion-component="AccordionItemPanel"
2127
className={className}
2228
{...rest}
23-
{...panelAttributes}
29+
{...attrs}
30+
role={region ? 'region': undefined}
2431
/>
2532
);
2633
};

0 commit comments

Comments
 (0)