Segmented分段控制器

分段控制器。自 [email protected] 版本开始提供该组件。

何时使用#

  • 用于展示多个选项并允许用户选择其中单个选项;

  • 当切换选中选项时,关联区域的内容会发生变化。

代码演示

最简单的用法。

expand codeexpand code
import { Segmented } from 'antd';

ReactDOM.render(
  <Segmented options={['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']} />,
  mountNode,
);
.code-box-demo {
  overflow-x: auto;
}

.code-box-demo .ant-segmented {
  margin-bottom: 10px;
}

Segmented 不可用。

expand codeexpand code
import { Segmented } from 'antd';

ReactDOM.render(
  <>
    <Segmented options={['Map', 'Transit', 'Satellite']} disabled />
    <br />
    <Segmented
      options={[
        'Daily',
        { label: 'Weekly', value: 'Weekly', disabled: true },
        'Monthly',
        { label: 'Quarterly', value: 'Quarterly', disabled: true },
        'Yearly',
      ]}
    />
  </>,
  mountNode,
);

给 Segmented Item 设置 Icon。

expand codeexpand code
import { Segmented } from 'antd';
import { AppstoreOutlined, BarsOutlined } from '@ant-design/icons';

ReactDOM.render(
  <Segmented
    options={[
      {
        label: 'List',
        value: 'List',
        icon: <BarsOutlined />,
      },
      {
        label: 'Kanban',
        value: 'Kanban',
        icon: <AppstoreOutlined />,
      },
    ]}
  />,
  mountNode,
);


我们为 <Segmented /> 组件定义了三种尺寸(大、默认、小),高度分别为 40px32px24px

expand codeexpand code
import { Segmented } from 'antd';

ReactDOM.render(
  <>
    <Segmented size="large" options={['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']} />
    <br />
    <Segmented options={['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']} />
    <br />
    <Segmented size="small" options={['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']} />
  </>,
  mountNode,
);

block 属性使其适合父元素宽度。

expand codeexpand code
import { Segmented } from 'antd';

ReactDOM.render(
  <Segmented block options={[123, 456, 'longtext-longtext-longtext-longtext']} />,
  mountNode,
);

受控的 Segmented。

expand codeexpand code
import React, { useState } from 'react';
import { Segmented } from 'antd';

const Demo: React.FC = () => {
  const [value, setValue] = useState('Map');

  return (
    <Segmented
      options={['Map', 'Transit', 'Satellite']}
      value={value}
      onChange={e => setValue(e.target.value)}
    />
  );
};

ReactDOM.render(<Demo />, mountNode);

动态加载数据。

expand codeexpand code
import React, { useState } from 'react';
import { Segmented, Button } from 'antd';

const defaultOptions = ['Daily', 'Weekly', 'Monthly'];

const Demo: React.FC = () => {
  const [options, setOptions] = useState(defaultOptions);
  const [moreLoaded, setMoreLoaded] = useState(false);

  const handleLoadOptions = () => {
    setOptions([...defaultOptions, 'Quarterly', 'Yearly']);
    setMoreLoaded(true);
  };

  return (
    <>
      <Segmented options={options} />
      <br />
      <Button type="primary" disabled={moreLoaded} onClick={handleLoadOptions}>
        Load more options
      </Button>
    </>
  );
};

export default Demo;

使用 ReactNode 自定义渲染每一个 Segmented Item。

expand codeexpand code
import { Avatar, Segmented } from 'antd';
import { UserOutlined } from '@ant-design/icons';

ReactDOM.render(
  <>
    <Segmented
      options={[
        {
          label: (
            <>
              <Avatar src="https://joeschmoe.io/api/v1/random" />
              <div>User 1</div>
            </>
          ),
          value: 'user1',
        },
        {
          label: (
            <>
              <Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
              <div>User 2</div>
            </>
          ),
          value: 'user2',
        },
        {
          label: (
            <>
              <Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
              <div>User 3</div>
            </>
          ),
          value: 'user3',
        },
      ]}
    />
    <br />
    <Segmented
      options={[
        {
          label: (
            <>
              <div>Spring</div>
              <div>Jan-Mar</div>
            </>
          ),
          value: 'spring',
        },
        {
          label: (
            <>
              <div>Summer</div>
              <div>Apr-Jun</div>
            </>
          ),
          value: 'summer',
        },
        {
          label: (
            <>
              <div>Autumn</div>
              <div>Jul-Sept</div>
            </>
          ),
          value: 'autumn',
        },
        {
          label: (
            <>
              <div>Winter</div>
              <div>Oct-Dec</div>
            </>
          ),
          value: 'winter',
        },
      ]}
    />
  </>,
  mountNode,
);

在 Segmented Item 选项中只设置 Icon。

expand codeexpand code
import { Segmented } from 'antd';
import { AppstoreOutlined, BarsOutlined } from '@ant-design/icons';

export default () => (
  <Segmented
    options={[
      {
        value: 'List',
        icon: <BarsOutlined />,
      },
      {
        value: 'Kanban',
        icon: <AppstoreOutlined />,
      },
    ]}
  />
);

API#

[email protected] 版本开始提供该组件。

Segmented#

参数说明类型默认值版本
block将宽度调整为父元素宽度的选项booleanfalse
defaultValue默认选中的值string | number
disabled是否禁用booleanfalse
onChange选项变化时的回调函数function(e:Event)
options数据化配置选项内容string[] | number[] | Array<{ label: string value: string icon? ReactNode disabled?: boolean className?: string }>[]
size控件尺寸large | middle | small-
value当前选中的值string | number