JGuang

级联选择

Cascader · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

级联选择

级联选择框。

何时使用

  • 需要从一组相关联的数据集合进行选择,例如省市区,公司层级,事物分类等。
  • 从一个较大的数据集合中进行选择时,用多级分类进行分隔,方便选择。
  • 比起 Select 组件,可以在同一个浮层中完成选择,有较好的体验。

代码演示

  • 基本 · basic
  • 默认值 · default-value
  • 可以自定义显示 · custom-trigger
  • 移入展开 · hover
  • 禁用选项 · disabled-option
  • 选择即改变 · change-on-select
  • 多选 · multiple
  • 自定义回填方式 · showCheckedStrategy
  • 大小 · size
  • 自定义已选项 · custom-render
  • 搜索 · search
  • 动态加载选项 · lazy
  • 自定义字段名 · fields-name
  • 前后缀 · suffix
  • 扩展菜单 · custom-dropdown
  • 弹出位置 · placement
  • 形态变体 · variant
  • 自定义状态 · status
  • 自定义语义结构的样式和类 · style-class
  • 面板使用 · panel

API

通用属性参考:通用属性

<Cascader options={options} onChange={onChange} />
参数说明类型默认值版本全局配置
allowClear支持清除boolean | { clearIcon?: ReactNode }true5.8.0: 支持对象形式clearIcon: 6.4.0
autoClearSearchValue是否在选中项后清空搜索框,只在 multiple 为 true 时有效booleantrue5.9.0×
bordered是否带边框,请使用 variant 替代booleantrue-×
changeOnSelect单选时生效(multiple 下始终都可以选择),点选每级菜单选项值都会发生变化。booleanfalse×
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-5.25.0
defaultOpen是否默认展示浮层boolean-×
defaultValue默认的选中项string[] | number[][]×
disabled禁用booleanfalse×
displayRender选择后展示的渲染函数(label, selectedOptions) => ReactNodelabel => label.join(/)multiple: 4.18.0×
tagRender自定义 tag 内容 render,仅在多选时生效({ label: string, onClose: function, value: string }) => ReactNode-×
popupClassName自定义浮层类名,使用 classNames.popup.root 替换string-4.23.0×
dropdownClassName自定义浮层类名,请使用 classNames.popup.root 替代string--×
dropdownRender自定义下拉框内容,请使用 popupRender 替换(menus: ReactElement) => ReactNode-4.4.0×
popupRender自定义下拉框内容(menus: ReactElement) => ReactNode-×
dropdownStyle下拉菜单的 style 属性,使用 styles.popup.root 替换CSSProperties-×
expandIcon自定义次级菜单展开图标ReactNode-4.4.06.3.0
expandTrigger次级菜单的展开方式,可选 'click' 和 'hover'stringclick×
fieldNames自定义 options 中 label value children 的字段object{ label: label, value: value, children: children }×
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。示例function(triggerNode)() => document.body×
loadData用于动态加载选项,无法与 showSearch 一起使用(selectedOptions) => void-×
loadingIcon自定义的加载图标ReactNode-6.3.0
maxTagCount最多显示多少个 tag,响应式模式会对性能产生损耗number | responsive-4.17.0×
maxTagPlaceholder隐藏 tag 时显示的内容ReactNode | function(omittedValues)-4.17.0×
maxTagTextLength最大显示的 tag 文本长度number-4.17.0×
notFoundContent当下拉列表为空时显示的内容ReactNode暂无数据×
open控制浮层显隐boolean-4.17.0×
options可选项数据源Option[]-×
placeholder输入框占位文本string-×
placement浮层预设位置bottomLeft bottomRight topLeft topRightbottomLeft4.17.0×
prefix自定义前缀ReactNode-5.22.0×
showArrow是否显示箭头图标,请使用 suffixIcon={null} 替代booleantrue-×
showSearch在选择框中显示搜索框boolean | ObjectfalsesearchIcon: 6.4.0
size输入框大小large | medium | smallmedium×
status设置校验状态'error' | 'warning'-4.19.0×
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-5.25.0
suffixIcon自定义的选择框后缀图标ReactNode-6.4.0
value指定选中项string[] | number[]-×
variant形态变体outlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.05.19.0
onChange选择完成后的回调(value, selectedOptions) => void-×
onClear清除内容时回调() => void--×
onDropdownVisibleChange显示/隐藏浮层的回调,请使用 onOpenChange 替换(value) => void-4.17.0×
onOpenChange显示/隐藏浮层的回调(open: boolean) => void-×
onPopupVisibleChange显示或隐藏浮层的回调,请使用 onOpenChange 替代(value) => void--×
multiple支持多选节点boolean-4.17.0×
removeIcon自定义的多选框清除图标ReactNode-6.4.0
showCheckedStrategy定义选中项回填的方式(仅在 multiple 为 true 时生效)。Cascader.SHOW_CHILD: 只显示选中的子节点。Cascader.SHOW_PARENT: 只显示父节点(当父节点下所有子节点都选中时)。Cascader.SHOW_PARENT | Cascader.SHOW_CHILDCascader.SHOW_PARENT4.20.0×
searchValue设置搜索的值,需要与 showSearch 配合使用string-4.17.0×
onSearch监听搜索,返回输入的值(search: string) => void-4.17.0×
dropdownMenuColumnStyle下拉菜单列的样式,请使用 styles.popup.listItem 替换CSSProperties-×
popupMenuColumnStyle下拉菜单列的样式,请使用 styles.popup.listItem 替换CSSProperties-×
optionRender自定义渲染下拉选项(option: Option) => React.ReactNode-5.16.0×

showSearch

showSearch 为对象时,其中的字段:

参数说明类型默认值版本
autoClearSearchValue是否在选中项后清空搜索框,只在 multiple 为 true 时有效booleantrue5.9.0
filter接收 inputValue path 两个参数,当 path 符合筛选条件时,应返回 true,反之则返回 falsefunction(inputValue, path): boolean-
limit搜索结果展示数量number | false50
matchInputWidth搜索结果列表是否与输入框同宽(效果)booleantrue
render用于渲染 filter 后的选项function(inputValue, path): ReactNode-
sort用于排序 filter 后的选项function(a, b, inputValue)-
searchValue设置搜索的值,需要与 showSearch 配合使用string-4.17.0
onSearch监听搜索,返回输入的值(search: string) => void-4.17.0
searchIcon自定义的搜索图标ReactNode-6.3.0

Option

interface Option {
  value: string | number;
  label?: React.ReactNode;
  disabled?: boolean;
  children?: Option[];
  // 标记是否为叶子节点,设置了 `loadData` 时有效
  // 设为 `false` 时会强制标记为父节点,即使当前节点没有 children,也会显示展开图标
  isLeaf?: boolean;
}

方法

名称描述版本
blur()移除焦点
focus()获取焦点

注意,如果需要获得中国省市区数据,可以参考 china-division。

Semantic DOM

语义节点原生说明版本
root根元素,包含相对定位、行内 flex 布局、光标样式、过渡动画、边框等选择器容器的基础样式—
prefix前缀元素,包含前缀内容的布局和样式—
content多选容器,包含已选项的布局、间距、换行相关样式—
placeholder占位符元素,包含占位符文本的字体样式和颜色—
clear清除按钮元素,包含清除按钮的布局、样式和交互效果—
input输入框元素,包含搜索输入框的样式、光标控制、字体继承等搜索相关样式,去除了边框样式—
suffix后缀元素,包含后缀内容的布局和样式,如清除按钮、箭头图标等—
popup.root弹出菜单元素,包含弹出层的定位、层级、背景、边框、阴影等弹出容器样式—
popup.list弹出菜单列表元素,包含选项列表的布局、滚动、最大高度等列表容器样式—
popup.listItem弹出菜单条目元素,包含选项项的内边距、悬浮效果、选中状态、禁用状态等选项交互样式—
item多选项元素,包含边框、背景、内边距、外边距样式 (多选模式)—
itemContent多选项内容区域,包含文字的省略样式 (多选模式)—
itemRemove多选项移除按钮,包含字体相关样式 (多选模式)—

语义说明来源: vendor/.dumi/theme/common/SelectSemanticTemplate.tsx

主题变量(Design Token)

类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。

令牌类型原生说明
controlWidthnumber | string选择器宽度
controlItemWidthnumber | string选项宽度
dropdownHeightnumber | string下拉菜单高度
optionSelectedBgstring选项选中时背景色
optionSelectedColorstring选项选中时文本颜色
optionSelectedFontWeightCSSProperties['fontWeight']选项选中时字重
optionPaddingCSSProperties['padding']选项内间距
menuPaddingCSSProperties['padding']选项菜单(单列)内间距

声明来源: vendor/components/cascader/style/index.ts

import type { CSSProperties } from 'react';

import { genCompactItemStyle } from '../../style/compact-item';

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

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

import getColumnsStyle from './columns';

export interface ComponentToken {
  /**
   * @desc 选择器宽度
   * @descEN Width of Cascader
   */
  controlWidth: number | string;
  /**
   * @desc 选项宽度
   * @descEN Width of item
   */
  controlItemWidth: number | string;
  /**
   * @desc 下拉菜单高度
   * @descEN Height of dropdown
   */
  dropdownHeight: number | string;
  /**
   * @desc 选项选中时背景色
   * @descEN Background color of selected item
   */
  optionSelectedBg: string;
  /**
   * @desc 选项选中时文本颜色
   * @descEN Text color when option is selected
   */
  optionSelectedColor: string;
  /**
   * @desc 选项选中时字重
   * @descEN Font weight of selected item
   */
  optionSelectedFontWeight: CSSProperties['fontWeight'];
  /**
   * @desc 选项内间距
   * @descEN Padding of menu item
   */
  optionPadding: CSSProperties['padding'];
  /**
   * @desc 选项菜单(单列)内间距
   * @descEN Padding of menu item (single column)
   */
  menuPadding: CSSProperties['padding'];
}

export type CascaderToken = FullToken<'Cascader'>;

原生通用属性

Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。

参数说明类型默认值
style自定义样式CSSProperties-
className自定义类名string-
rootClassName添加在组件最外层的 classNamestring-
autoFocus自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效booleanfalse

安装并复现示例

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

使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

原生类型参考

源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

  • vendor/components/cascader/Panel.tsx
  • vendor/components/cascader/hooks/useIcons.tsx
  • vendor/components/cascader/index.tsx
  • vendor/components/cascader/style/index.ts

来源与许可

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

保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。