Skip to content

Commit 78bb02b

Browse files
authored
fix: allow data-* and aria-* attributes (#344)
* fix: allow data-* and aria-* attributes * fix: pick only data and aria attributes
1 parent 18e931f commit 78bb02b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Collapse.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React from 'react';
55
import useItems from './hooks/useItems';
66
import type { CollapseProps } from './interface';
77
import CollapsePanel from './Panel';
8+
import pickAttrs from 'rc-util/lib/pickAttrs';
89

910
function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
1011
let currentActiveKey = activeKey;
@@ -81,6 +82,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
8182
className={collapseClassName}
8283
style={style}
8384
role={accordion ? 'tablist' : undefined}
85+
{...pickAttrs(props, { aria: true, data: true })}
8486
>
8587
{mergedChildren}
8688
</div>

tests/index.spec.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('collapse', () => {
5555
expect(collapse.container.querySelectorAll('.rc-collapse-content')).toHaveLength(0);
5656
});
5757

58-
it('should render custom arrow icon corrctly', () => {
58+
it('should render custom arrow icon correctly', () => {
5959
expect(collapse.container.querySelector('.rc-collapse-header')?.textContent).toContain(
6060
'test>',
6161
);
@@ -190,23 +190,22 @@ describe('collapse', () => {
190190

191191
describe('prop: headerClass', () => {
192192
it('applies the passed headerClass to the header', () => {
193-
194193
const element = (
195-
<Collapse onChange={onChange} >
196-
<Panel header="collapse 1" key="1" headerClass='custom-class'>
194+
<Collapse onChange={onChange}>
195+
<Panel header="collapse 1" key="1" headerClass="custom-class">
197196
first
198197
</Panel>
199198
</Collapse>
200199
);
201200

202-
const {container} = render(element)
201+
const { container } = render(element);
203202
const header = container.querySelector('.rc-collapse-header');
204203

205204
expect(header.classList.contains('custom-class')).toBeTruthy();
206205
});
207206
});
208207

209-
it('shoule support extra whit number 0', () => {
208+
it('should support extra whit number 0', () => {
210209
const { container } = render(
211210
<Collapse onChange={onChange} activeKey={0}>
212211
<Panel header="collapse 0" key={0} extra={0}>
@@ -843,5 +842,22 @@ describe('collapse', () => {
843842
expect(container.querySelectorAll('.custom-icon')).toHaveLength(1);
844843
expect(container.querySelector('.custom-icon')?.innerHTML).toBe('p');
845844
});
845+
846+
it('should support data- and aria- attributes', () => {
847+
const { container } = render(
848+
<Collapse
849+
data-testid="1234"
850+
aria-label="test"
851+
items={[
852+
{
853+
label: 'title',
854+
} as any,
855+
]}
856+
/>,
857+
);
858+
859+
expect(container.querySelector('.rc-collapse')?.getAttribute('data-testid')).toBe('1234');
860+
expect(container.querySelector('.rc-collapse')?.getAttribute('aria-label')).toBe('test');
861+
});
846862
});
847863
});

0 commit comments

Comments
 (0)