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
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;
}
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;
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;
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;
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;
import { Segmented } from 'antd';
const Demo = () => <Segmented block options={[123, 456, 'longtext-longtext-longtext-longtext']} />;
export default Demo;
TypeScript
JavaScript
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;
TypeScript
JavaScript
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;
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
Property | Description | Type | Default | Version |
---|---|---|---|---|
block | Option to fit width to its parent\'s width | boolean | false | |
defaultValue | Default selected value | string | number | ||
disabled | Disable all segments | boolean | false | |
onChange | The callback function that is triggered when the state changes | function(value: string | number) | ||
options | Set children optional | string[] | number[] | Array<{ label: ReactNode value: string icon? ReactNode disabled?: boolean className?: string }> | [] | |
size | The size of the Segmented. | large | middle | small | - | |
value | Currently selected value | string | number |