Collapse
A content area which can be collapsed and expanded.
When To Use#
Can be used to group or hide complex regions to keep the page clean.
Accordion
is a special kind ofCollapse
, which allows only one panel to be expanded at a time.
Examples
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return (
<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>
);
};
export default App;
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => (
<Collapse accordion>
<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>
);
export default App;
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return (
<Collapse onChange={onChange}>
<Panel header="This is panel header 1" key="1">
<Collapse defaultActiveKey="1">
<Panel header="This is panel nest panel" key="1">
<p>{text}</p>
</Panel>
</Collapse>
</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>
);
};
export default App;
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 App: React.FC = () => (
<Collapse bordered={false} defaultActiveKey={['1']}>
<Panel header="This is panel header 1" key="1">
{text}
</Panel>
<Panel header="This is panel header 2" key="2">
{text}
</Panel>
<Panel header="This is panel header 3" key="3">
{text}
</Panel>
</Collapse>
);
export default App;
TypeScript
JavaScript
import { CaretRightOutlined } from 'infra-design-icons';
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => (
<Collapse
bordered={false}
defaultActiveKey={['1']}
expandIcon={({ isActive }) => <CaretRightOutlined rotate={isActive ? 90 : 0} />}
className="site-collapse-custom-collapse"
>
<Panel header="This is panel header 1" key="1" className="site-collapse-custom-panel">
<p>{text}</p>
</Panel>
<Panel header="This is panel header 2" key="2" className="site-collapse-custom-panel">
<p>{text}</p>
</Panel>
<Panel header="This is panel header 3" key="3" className="site-collapse-custom-panel">
<p>{text}</p>
</Panel>
</Collapse>
);
export default App;
[data-theme='compact'] .site-collapse-custom-collapse .site-collapse-custom-panel,
.site-collapse-custom-collapse .site-collapse-custom-panel {
margin-bottom: 24px;
overflow: hidden;
background: #f7f7f7;
border: 0px;
border-radius: 2px;
}
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return (
<Collapse defaultActiveKey={['1']} onChange={onChange}>
<Panel header="This is panel header with arrow icon" key="1">
<p>{text}</p>
</Panel>
<Panel showArrow={false} header="This is panel header with no arrow icon" key="2">
<p>{text}</p>
</Panel>
</Collapse>
);
};
export default App;
Expand Icon Position:
start
TypeScript
JavaScript
import { SettingOutlined } from 'infra-design-icons';
import { Collapse, Select } from 'infrad';
import React, { useState } from 'react';
const { Panel } = Collapse;
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();
}}
/>
);
return (
<>
<Collapse
defaultActiveKey={['1']}
onChange={onChange}
expandIconPosition={expandIconPosition}
>
<Panel header="This is panel header 1" key="1" extra={genExtra()}>
<div>{text}</div>
</Panel>
<Panel header="This is panel header 2" key="2" extra={genExtra()}>
<div>{text}</div>
</Panel>
<Panel header="This is panel header 3" key="3" extra={genExtra()}>
<div>{text}</div>
</Panel>
</Collapse>
<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;
TypeScript
JavaScript
import { Collapse } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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 = () => (
<Collapse defaultActiveKey={['1']} ghost>
<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>
);
export default App;
TypeScript
JavaScript
import { Collapse, Space } from 'infrad';
import React from 'react';
const { Panel } = Collapse;
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']}>
<Panel header="This panel can only be collapsed by clicking text" key="1">
<p>{text}</p>
</Panel>
</Collapse>
<Collapse collapsible="disabled">
<Panel header="This panel can't be collapsed" key="1">
<p>{text}</p>
</Panel>
</Collapse>
</Space>
);
export default App;
API#
Collapse#
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 | 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 |
onChange | Callback function executed when active panel is changed | function | - |
Collapse.Panel#
Property | Description | Type | Default | Version |
---|---|---|---|---|
collapsible | Specify whether the panel be collapsible or the trigger area of collapsible | header | disabled | - | 4.9.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 | boolean | true |