Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clsx } from 'clsx';
import CSSMotion from '@rc-component/motion';
import { isNonNullable, isReactRenderable, KeyCode } from '@rc-component/util';
import { isNonNullable, isReactRenderable, KeyCode, useId } from '@rc-component/util';
import React from 'react';
import type { CollapsePanelProps } from './interface';
import PanelContent from './PanelContent';
Expand Down Expand Up @@ -32,6 +32,8 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop

const ifExtraExist = isNonNullable(extra) && typeof extra !== 'boolean';

const panelId = useId();

const collapsibleProps = {
onClick: () => {
onItemClick?.(panelKey);
Expand All @@ -43,6 +45,8 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
role: accordion ? 'tab' : 'button',
['aria-expanded']: isActive,
// The panel content is not mounted while collapsed, so only reference it when expanded
['aria-controls']: isActive ? panelId : undefined,
['aria-disabled']: disabled,
tabIndex: disabled ? -1 : 0,
};
Expand Down Expand Up @@ -117,6 +121,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
styles={styles}
isActive={isActive}
forceRender={forceRender}
id={panelId}
role={accordion ? 'tabpanel' : undefined}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions src/PanelContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const PanelContent = React.forwardRef<
style,
children,
isActive,
id,
role,
classNames: customizeClassNames,
styles,
Expand Down Expand Up @@ -42,6 +43,7 @@ const PanelContent = React.forwardRef<
className,
)}
style={style}
id={id}
role={role}
>
<div className={clsx(`${prefixCls}-body`, customizeClassNames?.body)} style={styles?.body}>
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`collapse props items should work with nested 1`] = `
<div
Expand Down
29 changes: 29 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,35 @@ describe('collapse', () => {
);
});

it('should link the header to its panel with aria-controls', () => {
const { container } = render(
<Collapse defaultActiveKey="1">
<Panel header="collapse 1" key="1">
first
</Panel>
</Collapse>,
);
const panelId = container.querySelector('.rc-collapse-panel')!.id;

expect(panelId).toBeTruthy();
expect(container.querySelector('.rc-collapse-header')).toHaveAttribute(
'aria-controls',
panelId,
);
});

it('should not set aria-controls on a collapsed header', () => {
const { container } = render(
<Collapse>
<Panel header="collapse 1" key="1">
first
</Panel>
</Collapse>,
);

expect(container.querySelector('.rc-collapse-header')).not.toHaveAttribute('aria-controls');
});

describe('wrapped in Fragment', () => {
const expandIcon = () => <span>test{'>'}</span>;
const element = (
Expand Down
Loading