Segmented

Segmented Controls. This component is available since antd@4.20.0.

When To Use#

  • When displaying multiple options and user can select a single option;

  • When switching the selected option, the content of the associated area changes.

Examples

The most basic usage.

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

const Demo = () => <Segmented options={['Daily', 'Weekly', 'Monthly', 'Quarterly', 'Yearly']} />;

export default Demo;
.code-box-demo {
  overflow-x: auto;
}

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

Disabled Segmented.

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

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

export default Demo;

Custom each Segmented Item by ReactNode.

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

const Demo = () => (
  <>
    <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',
        },
      ]}
    />
  </>
);

export default Demo;


There are three sizes of an Segmented: large (40px), default (32px) and small (24px).

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

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

export default Demo;

Set icon without label for Segmented Item.

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

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

export default Demo;

block property will make the Segmented fit to its parent width.

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

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

export default Demo;

Controlled Segmented.

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

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

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

export default Demo;

Load options dynamically.

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;

Set icon for Segmented Item.

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

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

export default Demo;

API#

This component is available since antd@4.20.0

PropertyDescriptionTypeDefaultVersion
blockOption to fit width to its parent\'s widthbooleanfalse
defaultValueDefault selected valuestring | number
disabledDisable all segmentsbooleanfalse
onChangeThe callback function that is triggered when the state changesfunction(value: string | number)
optionsSet children optionalstring[] | number[] | Array<{ label: ReactNode value: string icon? ReactNode disabled?: boolean className?: string }>[]
sizeThe size of the Segmented.large | middle | small-
valueCurrently selected valuestring | number
PopoverStatistic