JGuang

Segmented

Segmented · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Segmented

Display multiple options and allow users to select a single option.

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

  • Basic · basic
  • Vertical Direction · vertical
  • Block Segmented · block
  • Round shape · shape
  • Disabled · disabled
  • Controlled mode · controlled
  • Custom Render · custom
  • Dynamic · dynamic
  • Three sizes of Segmented · size
  • With Icon · with-icon
  • With Icon only · icon-only
  • With name · with-name
  • Custom semantic dom styling · style-class

API

Common props ref:Common props

This component is available since antd@4.20.0

Segmented

PropertyDescriptionTypeDefaultVersionGlobal Config
blockOption to fit width to its parent's widthbooleanfalse×
classNamesCustomize class for each semantic structure inside the Segmented component. Supports object or function.Record<SemanticDOM, string> | (info: { props }) => Record<SemanticDOM, string>-6.0.0
defaultValueDefault selected valuestring | numberValue of first item in options×
disabledDisable all segmentsbooleanfalse×
onChangeThe callback function that is triggered when the state changesfunction(value: string | number)×
optionsSet children optionalstring[] | number[] | SegmentedItemType[][]×
orientationOrientationhorizontal | verticalhorizontal×
sizeThe size of the Segmented.large | medium | smallmedium×
stylesCustomize inline style for each semantic structure inside the Segmented component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props }) => Record<SemanticDOM, CSSProperties>-6.0.0
verticalOrientation. Simultaneously existing with orientation, orientation takes prioritybooleanfalse5.21.0×
valueCurrently selected valuestring | number×
shapeshape of Segmenteddefault | rounddefault5.24.0×
nameThe name property of all input[type="radio"] children. if not set, it will fallback to a randomly generated namestring5.23.0×

SegmentedItemType

PropertyDescriptionTypeDefaultVersion
disabledDisabled state of segmented itembooleanfalse
classNameThe additional css classstring-
iconDisplay icon for Segmented itemReactNode-
labelDisplay text for Segmented itemReactNode-
tooltiptooltip for Segmented itemstring | TooltipProps-
valueValue for Segmented itemstring | number-

Semantic DOM

Semantic nodeNative descriptionVersion
rootRoot element with inline-block layout, padding, background, border radius, transition and container styles6.0.0
itemOption element with relative positioning, text alignment, cursor style, transition, selected state background and hover styles6.0.0
labelLabel content element with min height, line height, padding, text ellipsis and content layout styles6.0.0
iconIcon element with icon size, color and text spacing styles6.0.0

Semantic description source: vendor/components/segmented/demo/_semantic.tsx

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

TokenTypeNative description
itemColorstringText color of item
itemHoverColorstringText color of item when hover
itemHoverBgstringBackground color of item when hover
itemActiveBgstringBackground color of item when active
itemSelectedBgstringBackground color of item when selected
itemSelectedColorstringText color of item when selected
trackPaddingstring | numberPadding of Segmented container
trackBgstringBackground of Segmented container

Declaration source: vendor/components/segmented/style/index.ts

import type { CSSObject } from '@ant-design/cssinjs';

import { unit } from '@ant-design/cssinjs';

import { genFocusOutline, genFocusStyle, resetComponent, textEllipsis } from '../../style';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

export interface ComponentToken {
  /**
   * @desc 选项文本颜色
   * @descEN Text color of item
   */
  itemColor: string;
  /**
   * @desc 选项悬浮态文本颜色
   * @descEN Text color of item when hover
   */
  itemHoverColor: string;
  /**
   * @desc 选项悬浮态背景颜色
   * @descEN Background color of item when hover
   */
  itemHoverBg: string;
  /**
   * @desc 选项激活态背景颜色
   * @descEN Background color of item when active
   */
  itemActiveBg: string;
  /**
   * @desc 选项选中时背景颜色
   * @descEN Background color of item when selected
   */
  itemSelectedBg: string;
  /**
   * @desc 选项选中时文字颜色
   * @descEN Text color of item when selected
   */
  itemSelectedColor: string;
  /**
   * @desc Segmented 控件容器的 padding
   * @descEN Padding of Segmented container
   */
  trackPadding: string | number;
  /**
   * @desc Segmented 控件容器背景色
   * @descEN Background of Segmented container
   */
  trackBg: string;
}

interface SegmentedToken extends FullToken<'Segmented'> {
  segmentedPaddingHorizontal: number | string;
  segmentedPaddingHorizontalSM: number | string;
}

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/segmented/index.tsx
  • vendor/components/segmented/style/index.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.