Ant Design学习——Select

本文详细介绍了Ant Design中的Select组件,包括其用途、API、参数、模式、事件处理等,展示了如何创建下拉菜单、多选和标签模式,以及自定义样式和交互。还提到了与React的rc-select库的关系,以及Select组件在不同模式下的行为和配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2021SC@SDUSC

Select选择器

用法:

  • 弹出一个下拉菜单给用户选择操作,用于代替原生的选择器,或者需要一个更优雅的多选器时。
  • 当选项少时(少于 5 项),建议直接将选项平铺,使用 Radio 是更好的选择。
API
<Select>
  <Option value="lucy">lucy</Option>
</Select>

Select

参数说明类型默认值版本
allowClear支持清除booleanfalse
autoClearSearchValue是否在选中项后清空搜索框,只在 modemultipletags 时有效booleantrue
autoFocus默认获取焦点booleanfalse
bordered是否有边框booleantrue
clearIcon自定义的多选框清空图标ReactNode-
defaultActiveFirstOption是否默认高亮第一个选项booleantrue
defaultOpen是否默认展开下拉菜单boolean-
defaultValue指定默认选中的条目string | string[]
number | number[]
LabeledValue | LabeledValue[]
-
disabled是否禁用booleanfalse
dropdownClassName下拉菜单的 className 属性string-
dropdownMatchSelectWidth下拉菜单和选择器同宽。默认将设置 min-width,当值小于选择框宽度时会被忽略。false 时会关闭虚拟滚动boolean | numbertrue
dropdownRender自定义下拉框内容(originNode: ReactNode) => ReactNode-
dropdownStyle下拉菜单的 style 属性CSSProperties-
fieldNames自定义节点 label、key、options 的字段object{ label: label, key: key, options: options }4.17.0
filterOption是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 falseboolean | function(inputValue, option)true
filterSort搜索时对筛选结果项的排序函数, 类似Array.sort里的 compareFunction(optionA: Option, optionB: Option) => number-4.9.0
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。function(triggerNode)() => document.body
labelInValue是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 string 变为 { value: string, label: ReactNode } 的格式booleanfalse
listHeight设置弹窗滚动高度number256
loading加载中状态booleanfalse
maxTagCount最多显示多少个 tag,响应式模式会对性能产生损耗number | responsive-responsive: 4.10
maxTagPlaceholder隐藏 tag 时显示的内容ReactNode | function(omittedValues)-
maxTagTextLength最大显示的 tag 文本长度number-
menuItemSelectedIcon自定义多选时当前选中的条目图标ReactNode-
mode设置 Select 的模式为多选或标签multiple | tags-
notFoundContent当下拉列表为空时显示的内容ReactNodeNot Found
open是否展开下拉菜单boolean-
optionFilterProp搜索时过滤对应的 option 属性,如设置为 children 表示对内嵌内容进行搜索。若通过 options 属性配置选项内容,建议设置 optionFilterProp="label" 来对内容进行搜索。stringvalue
optionLabelProp回填到选择框的 Option 的属性值,默认是 Option 的子元素。比如在子元素需要高亮效果时,此值可以设为 valuestringchildren
options数据化配置选项内容,相比 jsx 定义会获得更好的渲染性能{ label, value }[]-
placeholder选择框默认文本string-
removeIcon自定义的多选框清除图标ReactNode-
searchValue控制搜索文本string-
showArrow是否显示下拉小箭头boolean单选为 true,多选为 false
showSearch使单选模式可搜索booleanfalse
size选择框大小large | middle | smallmiddle
suffixIcon自定义的选择框后缀图标ReactNode-
tagRender自定义 tag 内容 render,仅在 modemultipletags 时生效(props) => ReactNode-
tokenSeparatorstagsmultiple 模式下自动分词的分隔符string[]-
value指定当前选中的条目,多选时为一个数组。(value 数组引用未变化时,Select 不会更新)string | string[]
number | number[]
LabeledValue | LabeledValue[]
-
virtual设置 false 时关闭虚拟滚动booleantrue4.1.0
onBlur失去焦点时回调function-
onChange选中 option,或 input 的 value 变化时,调用此函数function(value, option:Option | Array<Option>)-
onClear清除内容时回调function-4.6.0
onDeselect取消选中时调用,参数为选中项的 value (或 key) 值,仅在 multipletags 模式下生效function(string | number | LabeledValue)-
onDropdownVisibleChange展开下拉菜单的回调function(open)-
onFocus获得焦点时回调function-
onInputKeyDown按键按下时回调function-
onMouseEnter鼠标移入时回调function-
onMouseLeave鼠标移出时回调function-
onPopupScroll下拉列表滚动时的回调function-
onSearch文本框值变化时回调function(value: string)-
onSelect被选中时调用,参数为选中项的 value (或 key) 值function(string | number | LabeledValue, option: Option)-

Option props

参数说明类型默认值版本
classNameOption 器类名string-
disabled是否禁用booleanfalse
title选中该 Option 后,Select 的 titlestring-
value默认根据此属性值进行筛选string | number-

OptGroup props

参数说明类型默认值版本
keyKeystring-
label组名string | React.Element-

部分源码

import * as React from 'react';
import omit from 'rc-util/lib/omit';
import classNames from 'classnames';
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps } from 'rc-select';
import { OptionProps } from 'rc-select/lib/Option';
import { ConfigContext } from '../config-provider';
import getIcons from './utils/iconUtil';
import SizeContext, { SizeType } from '../config-provider/SizeContext';
import { getTransitionName } from '../_util/motion';

import部分可以看到rc-select,可以见得ant-design中的select是基于react的select实现的。

type RawValue = string | number;

export { OptionProps };

export type OptionType = typeof Option;

export interface LabeledValue {
  key?: string;
  value: RawValue;
  label: React.ReactNode;
}

export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;

RawValue原始值采用字符串或数字的联合类型
接口LabeledValue则是简单的key value外加一个label的格式
SelectValue能够选用RawValue或是LabeledValue

export interface InternalSelectProps<VT> extends Omit<RcSelectProps<VT>, 'mode'> {
  suffixIcon?: React.ReactNode;
  size?: SizeType;
  mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
  bordered?: boolean;
}

export interface SelectProps<VT>
  extends Omit<
    InternalSelectProps<VT>,
    'inputIcon' | 'mode' | 'getInputElement' | 'getRawInputElement' | 'backfill'
  > {
  mode?: 'multiple' | 'tags';
}

export interface RefSelectProps {
  focus: () => void;
  blur: () => void;
}

COMBOBOX_MODE下拉框模式
这里给出了SelectProps的接口

const InternalSelect = <VT extends SelectValue = SelectValue>(
  {
    prefixCls: customizePrefixCls,
    bordered = true,
    className,
    getPopupContainer,
    dropdownClassName,
    listHeight = 256,
    listItemHeight = 24,
    size: customizeSize,
    notFoundContent,
    ...props
  }: SelectProps<VT>,
  ref: React.Ref<RefSelectProps>,
) => {
  const {
    getPopupContainer: getContextPopupContainer,
    getPrefixCls,
    renderEmpty,
    direction,
    virtual,
    dropdownMatchSelectWidth,
  } = React.useContext(ConfigContext);
  const size = React.useContext(SizeContext);

  const prefixCls = getPrefixCls('select', customizePrefixCls);
  const rootPrefixCls = getPrefixCls();

  const mode = React.useMemo(() => {
    const { mode: m } = props as InternalSelectProps<VT>;

    if ((m as any) === 'combobox') {
      return undefined;
    }

    if (m === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
      return 'combobox';
    }

    return m;
  }, [props.mode]);

  const isMultiple = mode === 'multiple' || mode === 'tags';

  // ===================== Empty =====================
  let mergedNotFound: React.ReactNode;
  if (notFoundContent !== undefined) {
    mergedNotFound = notFoundContent;
  } else if (mode === 'combobox') {
    mergedNotFound = null;
  } else {
    mergedNotFound = renderEmpty('Select');
  }

  // ===================== Icons =====================
  const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
    ...props,
    multiple: isMultiple,
    prefixCls,
  });

  const selectProps = omit(props as typeof props & { itemIcon: any }, ['suffixIcon', 'itemIcon']);

  const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
    [`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
  });

  const mergedSize = customizeSize || size;
  const mergedClassName = classNames(
    {
      [`${prefixCls}-lg`]: mergedSize === 'large',
      [`${prefixCls}-sm`]: mergedSize === 'small',
      [`${prefixCls}-rtl`]: direction === 'rtl',
      [`${prefixCls}-borderless`]: !bordered,
    },
    className,
  );

  return (
    <RcSelect<VT>
      ref={ref as any}
      virtual={virtual}
      dropdownMatchSelectWidth={dropdownMatchSelectWidth}
      {...selectProps}
      transitionName={getTransitionName(rootPrefixCls, 'slide-up', props.transitionName)}
      listHeight={listHeight}
      listItemHeight={listItemHeight}
      mode={mode}
      prefixCls={prefixCls}
      direction={direction}
      inputIcon={suffixIcon}
      menuItemSelectedIcon={itemIcon}
      removeIcon={removeIcon}
      clearIcon={clearIcon}
      notFoundContent={mergedNotFound}
      className={mergedClassName}
      getPopupContainer={getPopupContainer || getContextPopupContainer}
      dropdownClassName={rcSelectRtlDropDownClassName}
    />
  );
};

内部Select,最后用于Select组件的实现,相当于一层封装,同时设计了组件的样式

### Ant Design Vue 版本兼容性及集成指南 #### 安装与配置 为了在项目中使用 Ant Design Vue,需先安装对应的包并引入样式文件。通过 npm 或 yarn 可以轻松完成安装: ```bash npm install ant-design-vue --save ``` 或 ```bash yarn add ant-design-vue ``` 随后,在项目的入口文件(通常是 `main.js`)中加入如下代码来加载全局样式[^1]。 ```javascript import { createApp } from 'vue'; import App from './App.vue'; // 引入 Ant Design 的 CSS 文件 import 'ant-design-vue/dist/antd.css'; const app = createApp(App); app.mount('#app'); ``` 对于希望按需加载组件的情况,则推荐采用[babel-plugin-import](https://github.com/ant-design/babel-plugin-import)插件优化性能,仅导入实际使用的模块及其对应样式[^3]。 #### 兼容性说明 Ant Design Vue 支持 Vue 2.x 和 Vue 3.x 不同版本的应用程序开发环境。具体来说: - 对于基于 **Vue 2** 构建的应用, 应该选用 `ant-design-vue@2.x` 系列版本; - 如果目标平台是 **Vue 3**, 则建议升级到最新的 `ant-design-vue@next`(即3.x系列)[^4]。 需要注意的是,由于框架内部实现机制的变化,从 Vue 2 迁移到 Vue 3 时可能会遇到一些API差异带来的挑战;因此官方文档提供了详细的迁移指导帮助开发者顺利完成过渡过程[^2]。 #### 组件注册方式 有两种主要的方法可以在应用里注册 Ant Design Vue 提供的各种UI控件——整体注册和局部注册。前者适用于大多数场景,默认情况下会一次性把所有可用资源都挂载至 vue 实例原型链上;后者则允许更细粒度地控制哪些功能被激活,有助于减小打包体积以及提升页面渲染速度[^5]。 ```javascript // 整体注册 import Antd from 'ant-design-vue'; import App from './App.vue'; createApp(App).use(Antd).mount('#app'); // 局部注册 (示例) import { Button, Select } from 'ant-design-vue'; import App from './App.vue'; createApp(App).component(Button.name, Button).component(Select.name, Select).mount('#app'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值