Skip to content

Commit b24d1f8

Browse files
committed
feat(AccordionItemPanel): optional region role
1 parent 51a38ae commit b24d1f8

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
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

src/components/AccordionItemPanel.spec.tsx

Lines changed: 45 additions & 0 deletions
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

Lines changed: 10 additions & 2 deletions
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,19 @@ const AccordionItemPanel = ({
1516
assertValidHtmlId(id);
1617
}
1718

19+
const attrs = {
20+
...panelAttributes,
21+
'aria-labelledby': region ? panelAttributes['aria-labelledby'] : undefined,
22+
};
23+
console.log(region)
24+
1825
return (
1926
<div
2027
data-accordion-component="AccordionItemPanel"
2128
className={className}
2229
{...rest}
23-
{...panelAttributes}
30+
{...attrs}
31+
role={region ? 'region': undefined}
2432
/>
2533
);
2634
};

0 commit comments

Comments
 (0)