Skip to content

Commit

Permalink
fix: allow data-* and aria-* attributes (#344)
Browse files Browse the repository at this point in the history
* fix: allow data-* and aria-* attributes

* fix: pick only data and aria attributes
  • Loading branch information
destimon authored Mar 16, 2024
1 parent 18e931f commit 78bb02b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import useItems from './hooks/useItems';
import type { CollapseProps } from './interface';
import CollapsePanel from './Panel';
import pickAttrs from 'rc-util/lib/pickAttrs';

function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
let currentActiveKey = activeKey;
Expand Down Expand Up @@ -81,6 +82,7 @@ const Collapse = React.forwardRef<HTMLDivElement, CollapseProps>((props, ref) =>
className={collapseClassName}
style={style}
role={accordion ? 'tablist' : undefined}
{...pickAttrs(props, { aria: true, data: true })}
>
{mergedChildren}
</div>
Expand Down
28 changes: 22 additions & 6 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('collapse', () => {
expect(collapse.container.querySelectorAll('.rc-collapse-content')).toHaveLength(0);
});

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

describe('prop: headerClass', () => {
it('applies the passed headerClass to the header', () => {

const element = (
<Collapse onChange={onChange} >
<Panel header="collapse 1" key="1" headerClass='custom-class'>
<Collapse onChange={onChange}>
<Panel header="collapse 1" key="1" headerClass="custom-class">
first
</Panel>
</Collapse>
);

const {container} = render(element)
const { container } = render(element);
const header = container.querySelector('.rc-collapse-header');

expect(header.classList.contains('custom-class')).toBeTruthy();
});
});

it('shoule support extra whit number 0', () => {
it('should support extra whit number 0', () => {
const { container } = render(
<Collapse onChange={onChange} activeKey={0}>
<Panel header="collapse 0" key={0} extra={0}>
Expand Down Expand Up @@ -843,5 +842,22 @@ describe('collapse', () => {
expect(container.querySelectorAll('.custom-icon')).toHaveLength(1);
expect(container.querySelector('.custom-icon')?.innerHTML).toBe('p');
});

it('should support data- and aria- attributes', () => {
const { container } = render(
<Collapse
data-testid="1234"
aria-label="test"
items={[
{
label: 'title',
} as any,
]}
/>,
);

expect(container.querySelector('.rc-collapse')?.getAttribute('data-testid')).toBe('1234');
expect(container.querySelector('.rc-collapse')?.getAttribute('aria-label')).toBe('test');
});
});
});

0 comments on commit 78bb02b

Please sign in to comment.