Segmented分段控制器

分段控制器。自 antd@4.20.0 版本开始提供该组件。

何时使用#

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

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

代码演示

最简单的用法。

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

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

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

Segmented 不可用。

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

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

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

expand codeexpand code
import { Avatar, Segmented } from 'infrad';
import { UserOutlined } from 'infra-design-icons';

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


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

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

export default () => (
  <>
    <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']} />
  </>
);

在 Segmented Item 选项中只设置 Icon。

expand codeexpand code
import { Segmented } from 'infrad';
import { AppstoreOutlined, BarsOutlined } from 'infra-design-icons';

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

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

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

export default () => (
  <Segmented block options={[123, 456, 'longtext-longtext-longtext-longtext']} />
);

受控的 Segmented。

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

import { Segmented } from 'infrad';

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

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

export default Demo;

动态加载数据。

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

import { Button, Segmented } from 'infrad';

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;

给 Segmented Item 设置 Icon。

expand codeexpand code
import { Segmented } from 'infrad';
import { AppstoreOutlined, BarsOutlined } from 'infra-design-icons';

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

API#

antd@4.20.0 版本开始提供该组件。

Segmented#

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