Back to Ant Design

Cascader

components/cascader/index.zh-CN.md

6.3.79.2 KB
Original Source

何时使用 {#when-to-use}

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

代码演示 {#examples}

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">基本</code> <code src="./demo/default-value.tsx">默认值</code> <code src="./demo/custom-trigger.tsx">可以自定义显示</code> <code src="./demo/hover.tsx">移入展开</code> <code src="./demo/disabled-option.tsx">禁用选项</code> <code src="./demo/change-on-select.tsx">选择即改变</code> <code src="./demo/multiple.tsx">多选</code> <code src="./demo/showCheckedStrategy.tsx">自定义回填方式</code> <code src="./demo/size.tsx">大小</code> <code src="./demo/custom-render.tsx">自定义已选项</code> <code src="./demo/search.tsx">搜索</code> <code src="./demo/lazy.tsx">动态加载选项</code> <code src="./demo/fields-name.tsx">自定义字段名</code> <code src="./demo/suffix.tsx" version="5.22.0">前后缀</code> <code src="./demo/custom-dropdown.tsx">扩展菜单</code> <code src="./demo/placement.tsx">弹出位置</code> <code src="./demo/variant.tsx" version="5.13.0">形态变体</code> <code src="./demo/status.tsx">自定义状态</code> <code src="./demo/style-class.tsx" version="6.0.0">自定义语义结构的样式和类</code> <code src="./demo/panel.tsx" version=">= 5.10.0">面板使用</code> <code src="./demo/ellipsis-debug.tsx" debug>菜单项省略样式调试</code> <code src="./demo/render-panel.tsx" debug>_InternalPanelDoNotUseOrYouWillBeFired</code> <code src="./demo/component-token.tsx" debug>Component Token</code>

API

通用属性参考:通用属性

jsx
<Cascader options={options} onChange={onChange} />
参数说明类型默认值版本
allowClear支持清除boolean | { clearIcon?: ReactNode }true5.8.0: 支持对象形式
autoClearSearchValue是否在选中项后清空搜索框,只在 multipletrue 时有效booleantrue5.9.0
bordered是否带边框,请使用 variant 替代booleantrue-
changeOnSelect单选时生效(multiple 下始终都可以选择),点选每级菜单选项值都会发生变化。booleanfalse
className自定义类名string-
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
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: ReactNode) => ReactNode-4.4.0
popupRender自定义下拉框内容(menus: ReactNode) => ReactNode-
dropdownStyle下拉菜单的 style 属性,使用 styles.popup.root 替换CSSProperties-
expandIcon自定义次级菜单展开图标ReactNode-4.4.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-
maxTagCount最多显示多少个 tag,响应式模式会对性能产生损耗number | responsive-4.17.0
maxTagPlaceholder隐藏 tag 时显示的内容ReactNode | function(omittedValues)-4.17.0
maxTagTextLength最大显示的 tag 文本长度number-4.17.0
notFoundContent当下拉列表为空时显示的内容ReactNodeNot Found
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 | Objectfalse
size输入框大小large | medium | smallmedium
status设置校验状态'error' | 'warning'-4.19.0
styles用于自定义组件内部各语义化结构的行内 style,支持对象或函数Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
suffixIcon自定义的选择框后缀图标ReactNode-
value指定选中项string[] | number[]-
variant形态变体outlined | borderless | filled | underlinedoutlined5.13.0 | underlined: 5.24.0
onChange选择完成后的回调(value, selectedOptions) => void-
onDropdownVisibleChange显示/隐藏浮层的回调,请使用 onOpenChange 替换(value) => void-4.17.0
onOpenChange显示/隐藏浮层的回调(value) => void-
onPopupVisibleChange显示或隐藏浮层的回调,请使用 onOpenChange 替代(value) => void--
multiple支持多选节点boolean-4.17.0
removeIcon自定义的多选框清除图标ReactNode-
showCheckedStrategy定义选中项回填的方式(仅在 multipletrue 时生效)。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是否在选中项后清空搜索框,只在 multipletrue 时有效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

Option

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

方法 {#methods}

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

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

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

主题变量(Design Token){#design-token}

<ComponentTokenTable component="Cascader"></ComponentTokenTable>