Skip to content

Commit 06a04d0

Browse files
authored
feat: improve a11y behavior (#354)
1 parent 5b50798 commit 06a04d0

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

assets/index.less

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
margin: 0 16px 0 auto;
6060
}
6161
}
62-
.@{prefixCls}-header-collapsible-only {
62+
.@{prefixCls}-collapsible-header {
6363
cursor: default;
6464
.@{prefixCls}-header-text {
6565
cursor: pointer;
@@ -68,7 +68,7 @@
6868
cursor: pointer;
6969
}
7070
}
71-
.@{prefixCls}-icon-collapsible-only {
71+
.@{prefixCls}-collapsible-icon {
7272
cursor: default;
7373
.@{prefixCls}-expand-icon {
7474
cursor: pointer;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@rc-component/father-plugin": "^1.0.1",
5555
"@testing-library/jest-dom": "^6.1.4",
5656
"@testing-library/react": "^14.1.2",
57+
"@testing-library/user-event": "^14.5.2",
5758
"@types/classnames": "^2.2.9",
5859
"@types/jest": "^29.4.0",
5960
"@types/react": "^18.0.0",

src/Panel.tsx

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,39 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
2929
} = props;
3030

3131
const disabled = collapsible === 'disabled';
32-
const collapsibleHeader = collapsible === 'header';
33-
const collapsibleIcon = collapsible === 'icon';
3432

3533
const ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean';
3634

37-
const handleItemClick = () => {
38-
onItemClick?.(panelKey!);
39-
};
40-
41-
const handleKeyDown = (e: React.KeyboardEvent) => {
42-
if (e.key === 'Enter' || e.keyCode === KeyCode.ENTER || e.which === KeyCode.ENTER) {
43-
handleItemClick();
44-
}
35+
const collapsibleProps = {
36+
onClick: () => {
37+
onItemClick?.(panelKey);
38+
},
39+
onKeyDown: (e: React.KeyboardEvent) => {
40+
if (e.key === 'Enter' || e.keyCode === KeyCode.ENTER || e.which === KeyCode.ENTER) {
41+
onItemClick?.(panelKey);
42+
}
43+
},
44+
role: accordion ? 'tab' : 'button',
45+
['aria-expanded']: isActive,
46+
['aria-disabled']: disabled,
47+
tabIndex: disabled ? -1 : 0,
4548
};
4649

4750
// ======================== Icon ========================
48-
let iconNode = typeof expandIcon === 'function' ? expandIcon(props) : <i className="arrow" />;
49-
if (iconNode) {
50-
iconNode = (
51-
<div
52-
className={`${prefixCls}-expand-icon`}
53-
onClick={['header', 'icon'].includes(collapsible as string) ? handleItemClick : undefined}
54-
>
55-
{iconNode}
56-
</div>
57-
);
58-
}
51+
const iconNodeInner =
52+
typeof expandIcon === 'function' ? expandIcon(props) : <i className="arrow" />;
53+
const iconNode = iconNodeInner && (
54+
<div
55+
className={`${prefixCls}-expand-icon`}
56+
{...(['header', 'icon'].includes(collapsible) ? collapsibleProps : {})}
57+
>
58+
{iconNodeInner}
59+
</div>
60+
);
5961

6062
const collapsePanelClassNames = classNames(
63+
`${prefixCls}-item`,
6164
{
62-
[`${prefixCls}-item`]: true,
6365
[`${prefixCls}-item-active`]: isActive,
6466
[`${prefixCls}-item-disabled`]: disabled,
6567
},
@@ -68,37 +70,28 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop
6870

6971
const headerClassName = classNames(
7072
headerClass,
73+
`${prefixCls}-header`,
7174
{
72-
[`${prefixCls}-header`]: true,
73-
[`${prefixCls}-header-collapsible-only`]: collapsibleHeader,
74-
[`${prefixCls}-icon-collapsible-only`]: collapsibleIcon,
75+
[`${prefixCls}-collapsible-${collapsible}`]: !!collapsible,
7576
},
7677
customizeClassNames.header,
7778
);
7879

7980
// ======================== HeaderProps ========================
8081
const headerProps: React.HTMLAttributes<HTMLDivElement> = {
8182
className: headerClassName,
82-
'aria-expanded': isActive,
83-
'aria-disabled': disabled,
84-
onKeyDown: handleKeyDown,
8583
style: styles.header,
86-
role: accordion ? 'tab' : 'button',
84+
...(['header', 'icon'].includes(collapsible) ? {} : collapsibleProps),
8785
};
8886

89-
if (!collapsibleHeader && !collapsibleIcon) {
90-
headerProps.onClick = handleItemClick;
91-
headerProps.tabIndex = disabled ? -1 : 0;
92-
}
93-
9487
// ======================== Render ========================
9588
return (
9689
<div {...resetProps} ref={ref} className={collapsePanelClassNames}>
9790
<div {...headerProps}>
9891
{showArrow && iconNode}
9992
<span
10093
className={`${prefixCls}-header-text`}
101-
onClick={collapsible === 'header' ? handleItemClick : undefined}
94+
{...(collapsible === 'header' ? collapsibleProps : {})}
10295
>
10396
{header}
10497
</span>

tests/__snapshots__/index.spec.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports[`collapse props items should work with nested 1`] = `
1010
<div
1111
aria-disabled="true"
1212
aria-expanded="false"
13-
class="rc-collapse-header"
13+
class="rc-collapse-header rc-collapse-collapsible-disabled"
1414
role="button"
1515
tabindex="-1"
1616
>

0 commit comments

Comments
 (0)