React collapsible panel component with accordion support and extensive customization options
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The Panel component provides backward compatibility for existing codebases. This approach is deprecated and will be removed in v4.0.0. Use the items prop instead for new implementations.
declare const Panel: React.ForwardRefExoticComponent<CollapsePanelProps & React.RefAttributes<HTMLDivElement>>;
interface CollapsePanelProps extends React.DOMAttributes<HTMLDivElement> {
id?: string;
header?: string | React.ReactNode;
prefixCls?: string;
headerClass?: string;
showArrow?: boolean;
className?: string;
classNames?: Partial<Record<SemanticName, string>>;
style?: object;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
isActive?: boolean;
openMotion?: CSSMotionProps;
destroyInactivePanel?: boolean;
accordion?: boolean;
forceRender?: boolean;
extra?: string | React.ReactNode;
onItemClick?: (panelKey: React.Key) => void;
expandIcon?: (props: CollapsePanelProps) => React.ReactNode;
panelKey?: React.Key;
role?: string;
collapsible?: CollapsibleType;
children?: React.ReactNode;
}
type CollapsibleType = 'header' | 'icon' | 'disabled';
type SemanticName = 'header' | 'title' | 'body' | 'icon';header: string | React.ReactNode - Panel header contentchildren: React.ReactNode - Panel body contentextra: string | React.ReactNode - Additional content in header areaid: string - HTML id attribute for the panelpanelKey: React.Key - Unique key for the panel (falls back to React key)className: string - Custom CSS class for panel containerstyle: object - Inline styles for panel containerheaderClass: string - Custom CSS class for panel headerclassNames: Partial<Record<SemanticName, string>> - Semantic CSS classesstyles: Partial<Record<SemanticName, React.CSSProperties>> - Semantic inline stylesprefixCls: string - CSS class prefix (inherited from parent Collapse)showArrow: boolean - Show/hide expand arrow (default: true)collapsible: CollapsibleType - Panel collapsible behaviorforceRender: boolean - Always render content even when inactivedestroyInactivePanel: boolean - Destroy content when panel is inactiveonItemClick: (panelKey: React.Key) => void - Panel click handlerrole: string - ARIA role overrideisActive: boolean - Panel active stateaccordion: boolean - Accordion mode flagopenMotion: CSSMotionProps - Animation configurationexpandIcon: (props: CollapsePanelProps) => React.ReactNode - Custom expand iconimport Collapse, { Panel } from "rc-collapse";
const LegacyExample = () => {
return (
<Collapse defaultActiveKey={['1']}>
<Panel header="Panel 1" key="1">
Content of panel 1
</Panel>
<Panel header="Panel 2" key="2">
Content of panel 2
</Panel>
</Collapse>
);
};import Collapse, { Panel } from "rc-collapse";
const CustomHeaderPanel = () => {
const customHeader = (
<div style={{ display: 'flex', alignItems: 'center' }}>
<Icon type="user" />
<span style={{ marginLeft: 8 }}>User Profile</span>
</div>
);
return (
<Collapse>
<Panel header={customHeader} key="profile">
User profile content here
</Panel>
</Collapse>
);
};import Collapse, { Panel } from "rc-collapse";
const PanelWithExtra = () => {
const extraContent = (
<button
onClick={(e) => {
e.stopPropagation();
console.log('Extra button clicked');
}}
>
Settings
</button>
);
return (
<Collapse>
<Panel
header="Panel with Extra"
key="extra"
extra={extraContent}
>
Panel content with extra button in header
</Panel>
</Collapse>
);
};import Collapse, { Panel } from "rc-collapse";
const DisabledPanel = () => {
return (
<Collapse>
<Panel header="Normal Panel" key="normal">
This panel can be toggled
</Panel>
<Panel
header="Disabled Panel"
key="disabled"
collapsible="disabled"
>
This panel cannot be toggled
</Panel>
</Collapse>
);
};import Collapse, { Panel } from "rc-collapse";
const StyledPanel = () => {
return (
<Collapse>
<Panel
header="Styled Panel"
key="styled"
className="custom-panel"
headerClass="custom-header"
classNames={{
header: 'semantic-header',
title: 'semantic-title',
body: 'semantic-body',
}}
styles={{
header: { backgroundColor: '#f0f0f0' },
body: { padding: '20px' },
}}
>
Panel with custom styling
</Panel>
</Collapse>
);
};import Collapse, { Panel } from "rc-collapse";
const LegacyApproach = () => {
return (
<Collapse accordion defaultActiveKey="1">
<Panel header="First Panel" key="1" className="panel-1">
First panel content
</Panel>
<Panel
header="Second Panel"
key="2"
extra={<button>Action</button>}
collapsible="disabled"
>
Second panel content
</Panel>
</Collapse>
);
};import Collapse from "rc-collapse";
const ModernApproach = () => {
const items = [
{
key: '1',
label: 'First Panel',
children: 'First panel content',
className: 'panel-1',
},
{
key: '2',
label: 'Second Panel',
children: 'Second panel content',
extra: <button>Action</button>,
collapsible: 'disabled',
},
];
return (
<Collapse
accordion
defaultActiveKey="1"
items={items}
/>
);
};Install with Tessl CLI
npx tessl i tessl/npm-rc-collapse