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 'infrad';

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

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

Disabled 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',
      ]}
    />
  </>
);

Custom each Segmented Item by ReactNode.

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',
        },
      ]}
    />
  </>
);


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

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']} />
  </>
);

Set icon without label for Segmented Item.

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 property will make the Segmented fit to its parent width.

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

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

Controlled 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;

Load options dynamically.

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;

Set icon for Segmented Item.

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#

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