当前位置: 首页 > news >正文

AntDesign下,Select内嵌Menu标签,做一个多选下拉框,既可以搜索,还可以选择下拉项

话不多说,直接上效果和代码
效果图一:
在这里插入图片描述

效果图二:

在这里插入图片描述

renderAddStyleOption = (item: any) => {
   const { value } = this.props;
   const { currentSelectedOptionIds, currentStyleId } = this.state;
   const styleSettings = value?.styleSettings;

   const newStyleSettings = styleSettings && styleSettings.length ? styleSettings : [];
   if (item) {
       const hasSearchKeyword = find([...item.allOptions, ...item.options], {
           name: item.optionKeyword,
       });
       return (
           <div
               className="specs-settings-popover"
               onBlur={() => {
                   this.setState({
                       currentStyleId: undefined,
                   });
               }}
           >
               <div
                   onMouseDown={(e) => {
                       e.preventDefault();
                       return false;
                   }}
               >
                   <Select
                       style={{ width: '300px' }}
                       placeholder={language.getText('selectPlease')}
                       mode="multiple"
                       autoClearSearchValue
                       showSearch
                       filterOption={false}
                       onChange={(selectedValues: any) => {
                           this.setState({
                               currentSelectedOptionIds: selectedValues,
                           });
                       }}
                       value={currentSelectedOptionIds}
                       onSearch={(keyword: string) => {
                           this.onSearch(item.id, newStyleSettings, keyword);
                       }}
                       onFocus={() => {
                           this.onSearch(item.id, newStyleSettings);
                       }}
                       open={currentStyleId === item.id}
                       onDropdownVisibleChange={() => {
                           this.setState({
                               currentStyleId: item.id,
                           });
                       }}
                       dropdownRender={() => (
                           <Menu
                               className="select-menu"
                               onClick={async (e: any) => {
                                   this.selectedStyleOption(e.key, item.id);
                               }}
                           >
                               {!hasSearchKeyword && item.optionKeyword && (
                                   <Menu.Item
                                       key={item.optionKeyword}
                                       className="add-menu-item"
                                   >
                                       <Icon type="plus" style={{ marginRight: '5px' }} />
                                       {item.optionKeyword}
                                   </Menu.Item>
                               )}
                               {item.options &&
                                   item.options.map((attribute: any) => {
                                       return (
                                           <Menu.Item
                                               key={attribute.id}
                                               className="select-menu-item"
                                           >
                                               {attribute.name}
                                               {currentSelectedOptionIds.includes(
                                                   attribute.id + ''
                                               ) && (
                                                   <Icon
                                                       type="check"
                                                       style={{ marginLeft: '5px' }}
                                                   />
                                               )}
                                           </Menu.Item>
                                       );
                                   })}
                           </Menu>
                       )}
                   >
                       {item.options &&
                           item.options.map((attribute: any) => {
                               return (
                                   <Select.Option
                                       key={attribute.id}
                                       value={String(attribute.id)}
                                   >
                                       {attribute.name}
                                   </Select.Option>
                               );
                           })}
                   </Select>
               </div>
               <Button
                   className="popover-btn"
                   type="default"
                   onClick={() => {
                       this.toggleOptionPopover(item.id);
                   }}
               >
                   {language.getText('common.cancel')}
               </Button>
               <Button
                   className="popover-btn"
                   type="primary"
                   onClick={() => {
                       this.addStyleOption(item.id);
                   }}
               >
                   {language.getText('common.add')}
               </Button>
           </div>
       );
   } else {
       return null;
   }
};

直接说重点,时间问题,不单纯出简单代码为大家说明了

1.关于下拉项事件的加载,可以通过onSearch和onFocus
获取你们的数据源,其中onSearch会返回name也就是select输入框的关键字,可以与后端接口请求获取

2.为什么使用Menu?是发现在Select输入关键字,下拉项出现“新增关键字”选项时,即使不点也会默认选中第一项,而“新增关键字”还未加入数据库,id获取不到,因此使用了Menu来控制到底是不是用户手动点的,
(也有人想要这种简便操作,就是点击“新增关键字”,移除select也仍然加入数据库这种交互的话,那直接用select无需嵌套Menu可做到。我的需求是必须是用户手动点击下拉项才可以添加到数据库,避免一些伪操作数据的产生,大家自行判断~)

3.使用Menu大家应该也会遇到其他的卡点,可以移步到我上次写的一个博客,里面有详细写遇到的问题~
https://blog.csdn.net/weixin_43517190/article/details/147008156?spm=1001.2014.3001.5501

http://www.dtcms.com/a/112966.html

相关文章:

  • 【项目管理】第一部分 信息技术 1/2
  • 十道海量数据处理面试题与十个方法总结
  • 嵌入式Linux驱动—— 1 GPIO配置
  • 浙考!【触发器逻辑方程推导(电位运算)】
  • Ai提示词大师 1.0 | 预制1000+模板满足
  • 《Java实战:素数检测算法优化全解析——从暴力枚举到筛法进阶》
  • GD32H759IMT6 Cortex-M7 OpenHarmony轻量系统移植——4.1版本升级到5.0.3
  • 网络编程—TCP/IP模型(UDP协议与自定义协议)
  • Altshuller矛盾矩阵查询:基于python和streamlit
  • 哈希表(Hashtable)核心知识点详解
  • 【虚拟仪器技术】Labview虚拟仪器技术习题答案(二),设计VI程序
  • 练习题:124
  • Java Web从入门到精通:全面探索与实战(一)
  • 基于YOLO11实例分割与奥比中光相机的快递包裹抓取点检测
  • 基于CATIA产品结构树智能排序的二次开发技术解析——深度定制BOM层级管理系统的Pycatia实践
  • docker搭建minio集群(简化版)
  • MessageQueue --- RabbitMQ WorkQueue
  • Redis-Hash类型
  • 我的购物车设计思考:从个人项目到生产实战思考的蜕变
  • 【Linux网络与网络编程】05.应用层自定义协议序列化和反序列化
  • Systemd构建自动化备份服务与外部存储管理
  • KAPC的前世今生--(下)下RPCRT4!NMP_SyncSendRecv函数分析
  • 去中心化指数(链上ETF)
  • 【小沐学Web3D】three.js 加载三维模型(React Three Fiber)
  • Cribl 通过Generic API新建 Dataset
  • 谈谈策略模式,策略模式的适用场景是什么?
  • 基本机动飞行性能
  • 车辆信息查询API——车辆车五项查询
  • 金融数据分析(Python)个人学习笔记(6):安装相关软件
  • PyTorch参数管理详解:从访问到初始化与共享