A content area which can be collapsed and expanded.
Accordion
is a special kind of Collapse
, which allows only one panel to be expanded at a time.// works when >= 5.6.0, recommended ✅const text = `A dog is a type of domesticated animal.Known for its loyalty and faithfulness,it can be found as a welcome guest in many households across the world.`;const items: CollapseProps['items'] = [{key: '1',label: 'This is panel header 1',children: <p>{text}</p>,},{key: '2',label: 'This is panel header 2',children: <p>{text}</p>,},{key: '3',label: 'This is panel header 3',children: <p>{text}</p>,},];<Collapse items={items} defaultActiveKey={['1']} />;// works when <5.6.0 , deprecated when >=5.6.0 🙅🏻♀️<Collapse defaultActiveKey={['1']} onChange={onChange}><Panel header="This is panel header 1" key="1"><p>{text}</p></Panel><Panel header="This is panel header 2" key="2"><p>{text}</p></Panel><Panel header="This is panel header 3" key="3"><p>{text}</p></Panel></Collapse>;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: <p>{text}</p>,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
},
];
const App: React.FC = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return <Collapse items={items} defaultActiveKey={['1']} onChange={onChange} />;
};
export default App;
import { Collapse, Divider } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const App: React.FC = () => (
<>
<Divider orientation="left">Default Size</Divider>
<Collapse
items={[{ key: '1', label: 'This is default size panel header', children: <p>{text}</p> }]}
/>
<Divider orientation="left">Small Size</Divider>
<Collapse
size="small"
items={[{ key: '1', label: 'This is small size panel header', children: <p>{text}</p> }]}
/>
<Divider orientation="left">Large Size</Divider>
<Collapse
size="large"
items={[{ key: '1', label: 'This is large size panel header', children: <p>{text}</p> }]}
/>
</>
);
export default App;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: <p>{text}</p>,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
},
];
const App: React.FC = () => <Collapse accordion items={items} />;
export default App;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const itemsNest: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel nest panel',
children: <p>{text}</p>,
},
];
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: <Collapse defaultActiveKey="1" items={itemsNest} />,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
},
];
const App: React.FC = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return <Collapse onChange={onChange} items={items} />;
};
export default App;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = (
<p style={{ paddingLeft: 24 }}>
A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found
as a welcome guest in many households across the world.
</p>
);
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: text,
},
{
key: '2',
label: 'This is panel header 2',
children: text,
},
{
key: '3',
label: 'This is panel header 3',
children: text,
},
];
const App: React.FC = () => <Collapse items={items} bordered={false} defaultActiveKey={['1']} />;
export default App;
import { CaretRightOutlined } from '@ant-design/icons';
import type { CollapseProps } from 'antd';
import { Collapse, theme } from 'antd';
import type { CSSProperties } from 'react';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const getItems: (panelStyle: CSSProperties) => CollapseProps['items'] = (panelStyle) => [
{
key: '1',
label: 'This is panel header 1',
children: <p>{text}</p>,
style: panelStyle,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
style: panelStyle,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
style: panelStyle,
},
];
const App: React.FC = () => {
const { token } = theme.useToken();
const panelStyle = {
marginBottom: 24,
background: token.colorFillAlter,
borderRadius: token.borderRadiusLG,
border: 'none',
};
return (
<Collapse
bordered={false}
defaultActiveKey={['1']}
expandIcon={({ isActive }) => <CaretRightOutlined rotate={isActive ? 90 : 0} />}
style={{ background: token.colorBgContainer }}
items={getItems(panelStyle)}
/>
);
};
export default App;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header with arrow icon',
children: <p>{text}</p>,
},
{
key: '2',
label: 'This is panel header with no arrow icon',
children: <p>{text}</p>,
showArrow: false,
},
];
const App: React.FC = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return <Collapse defaultActiveKey={['1']} onChange={onChange} items={items} />;
};
export default App;
import { SettingOutlined } from '@ant-design/icons';
import type { CollapseProps } from 'antd';
import { Collapse, Select } from 'antd';
import React, { useState } from 'react';
const { Option } = Select;
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
type ExpandIconPosition = 'start' | 'end';
const App: React.FC = () => {
const [expandIconPosition, setExpandIconPosition] = useState<ExpandIconPosition>('start');
const onPositionChange = (newExpandIconPosition: ExpandIconPosition) => {
setExpandIconPosition(newExpandIconPosition);
};
const onChange = (key: string | string[]) => {
console.log(key);
};
const genExtra = () => (
<SettingOutlined
onClick={(event) => {
// If you don't want click extra trigger collapse, you can prevent this:
event.stopPropagation();
}}
/>
);
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: <div>{text}</div>,
extra: genExtra(),
},
{
key: '2',
label: 'This is panel header 2',
children: <div>{text}</div>,
extra: genExtra(),
},
{
key: '3',
label: 'This is panel header 3',
children: <div>{text}</div>,
extra: genExtra(),
},
];
return (
<>
<Collapse
defaultActiveKey={['1']}
onChange={onChange}
expandIconPosition={expandIconPosition}
items={items}
/>
<br />
<span>Expand Icon Position: </span>
<Select value={expandIconPosition} style={{ margin: '0 8px' }} onChange={onPositionChange}>
<Option value="start">start</Option>
<Option value="end">end</Option>
</Select>
</>
);
};
export default App;
import type { CollapseProps } from 'antd';
import { Collapse } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const items: CollapseProps['items'] = [
{
key: '1',
label: 'This is panel header 1',
children: <p>{text}</p>,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
},
];
const App: React.FC = () => <Collapse defaultActiveKey={['1']} ghost items={items} />;
export default App;
import { Collapse, Space } from 'antd';
import React from 'react';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const App: React.FC = () => (
<Space direction="vertical">
<Collapse
collapsible="header"
defaultActiveKey={['1']}
items={[
{
key: '1',
label: 'This panel can only be collapsed by clicking text',
children: <p>{text}</p>,
},
]}
/>
<Collapse
collapsible="icon"
defaultActiveKey={['1']}
items={[
{
key: '1',
label: 'This panel can only be collapsed by clicking icon',
children: <p>{text}</p>,
},
]}
/>
<Collapse
collapsible="disabled"
items={[
{
key: '1',
label: "This panel can't be collapsed",
children: <p>{text}</p>,
},
]}
/>
</Space>
);
export default App;
#components-collapse-demo-collapsible .ant-space {
width: 100%;
}
Property | Description | Type | Default | Version |
---|---|---|---|---|
accordion | If true, Collapse renders as Accordion | boolean | false | |
activeKey | Key of the active panel | string[] | string number[] | number | No default value. In accordion mode, it's the key of the first panel | |
bordered | Toggles rendering of the border around the collapse block | boolean | true | |
collapsible | Specify whether the panels of children be collapsible or the trigger area of collapsible | header | icon | disabled | - | 4.9.0 |
defaultActiveKey | Key of the initial active panel | string[] | string number[] | number | - | |
destroyInactivePanel | Destroy Inactive Panel | boolean | false | |
expandIcon | Allow to customize collapse icon | (panelProps) => ReactNode | - | |
expandIconPosition | Set expand icon position | start | end | - | 4.21.0 |
ghost | Make the collapse borderless and its background transparent | boolean | false | 4.4.0 |
size | Set the size of collapse | large | middle | small | middle | 5.2.0 |
onChange | Callback function executed when active panel is changed | function | - | |
items | collapse items content | ItemType | - | 5.6.0 |
Property | Description | Type | Default | Version |
---|---|---|---|---|
collapsible | Specify whether the panel be collapsible or the trigger area of collapsible | header | icon | disabled | - | 4.9.0 (icon: 4.24.0) |
extra | The extra element in the corner | ReactNode | - | |
forceRender | Forced render of content on panel, instead of lazy rendering after clicking on header | boolean | false | |
header | Title of the panel | ReactNode | - | |
key | Unique key identifying the panel from among its siblings | string | number | - | |
showArrow | If false, panel will not show arrow icon. If false, collapsible can't be set as icon | boolean | true |