跳转到内容

Collapse 折叠面板

用于常见问题解答、手风琴和可展开部分的可折叠内容面板。

import { Collapse } from 'asterui'

基本折叠

简单的可折叠面板。

What is AsterUI?
AsterUI is a React component library built on DaisyUI and Tailwind CSS.
Run npm install asterui to get started.
Yes, AsterUI is open source and free to use.
import { Collapse } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'What is AsterUI?',
      children: 'AsterUI is a React component library built on DaisyUI and Tailwind CSS.',
    },
    {
      key: '2',
      label: 'How do I install it?',
      children: 'Run npm install asterui to get started.',
    },
    {
      key: '3',
      label: 'Is it free?',
      children: 'Yes, AsterUI is open source and free to use.',
    },
  ]
  
  return (
      <Collapse items={items} defaultActiveKey={['1']} />
    )
}

export default App

手风琴

一次只能打开一个面板。

Section 1
Content for section 1. Click another section to close this one.
Content for section 2. Only one section can be open.
Content for section 3. This is accordion behavior.
import { Collapse } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'Section 1',
      children: 'Content for section 1. Click another section to close this one.',
    },
    {
      key: '2',
      label: 'Section 2',
      children: 'Content for section 2. Only one section can be open.',
    },
    {
      key: '3',
      label: 'Section 3',
      children: 'Content for section 3. This is accordion behavior.',
    },
  ]
  
  return (
      <Collapse items={items} accordion defaultActiveKey="1" />
    )
}

export default App

无边框

没有边框的折叠。

Content without borders for a cleaner look.
Great for FAQ sections or sidebars.
import { Collapse } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'Panel 1',
      children: 'Content without borders for a cleaner look.',
    },
    {
      key: '2',
      label: 'Panel 2',
      children: 'Great for FAQ sections or sidebars.',
    },
  ]
  
  return (
      <Collapse items={items} bordered={false} />
    )
}

export default App

嵌套内容

带丰富嵌套内容的折叠。

Follow these steps to get started:

  1. Install the package
  2. Import components
  3. Start building
Learn about advanced patterns and customization options.
import { Collapse, Button, Space } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'Getting Started',
      children: (
        <Space direction="vertical" size="sm">
          <p>Follow these steps to get started:</p>
          <ol className="list-decimal list-inside">
            <li>Install the package</li>
            <li>Import components</li>
            <li>Start building</li>
          </ol>
          <Button color="primary" size="sm">Read Docs</Button>
        </Space>
      ),
    },
    {
      key: '2',
      label: 'Advanced Usage',
      children: 'Learn about advanced patterns and customization options.',
    },
  ]
  
  return (
      <Collapse items={items} />
    )
}

export default App

幽灵模式

透明背景以获得简约外观。

Ghost mode removes the background color.
Great for minimalist designs.
import { Collapse } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'Transparent Background',
      children: 'Ghost mode removes the background color.',
    },
    {
      key: '2',
      label: 'Clean Look',
      children: 'Great for minimalist designs.',
    },
  ]
  
  return (
      <Collapse items={items} ghost bordered={false} />
    )
}

export default App

加减图标

使用加减图标代替箭头。

This panel uses plus/minus icons instead of arrows.
Click the plus icon to expand.
import { Collapse } from 'asterui'

function App() {
  const items = [
    {
      key: '1',
      label: 'Plus/Minus Icon',
      children: 'This panel uses plus/minus icons instead of arrows.',
    },
    {
      key: '2',
      label: 'Another Panel',
      children: 'Click the plus icon to expand.',
    },
  ]
  
  return (
      <Collapse items={items} icon="plus" />
    )
}

export default App
属性描述类型默认值
items折叠面板项CollapseItemType[]-
activeKey受控的激活面板键string | number | (string | number)[]-
defaultActiveKey初始激活面板键string | number | (string | number)[]-
accordion一次只打开一个面板booleanfalse
bordered在面板周围显示边框booleantrue
ghost透明背景样式booleanfalse
size尺寸变体'sm' | 'md' | 'lg''md'
icon展开指示器的图标类型'arrow' | 'plus' | 'none''arrow'
expandIconPlacement图标位置'start' | 'end''start'
onChange改变处理函数(activeKey: (string | number)[]) => void-
data-testid组件的测试 IDstring'collapse'
className额外的 CSS 类名string-
属性描述类型默认值
key面板的唯一标识符string | number-
label面板标题内容React.ReactNode-
children面板主体内容React.ReactNode-
extra标题角落的额外元素React.ReactNode-
showArrow是否显示展开图标booleantrue
collapsible可折叠模式'header' | 'icon' | 'disabled'-
className此面板的自定义类名string-
  • 面板标题可通过 Enter 和 Space 键访问
  • 使用 aria-expanded 指示面板状态
  • 对禁用面板使用 aria-disabled
  • 面板公开 data-state 用于测试和样式