Input & DropDown 拼接成 select组件(基于antd和react)

前言:为什么不直接用select,还要舍近求远搞input+dropdown这种缝合怪,是因为antd的select不支持选中项再编辑,效果如图

选中的closed文案变成了placeholder不能再编辑了

封装此组件虽然比较简单,但还是有弊端的,就是失去了select自带的上下键选中下拉项的功能

一、组件代码

import React, { useState, useRef, useEffect } from 'react';  
import { Dropdown, Menu, Input } from 'antd';  
import { DownOutlined } from '@ant-design/icons';  

	  
const CustomSelect = (props) => {  
      //可用props参数将方法抛出去供外部父组件使用
      const {pressEnter} = props;//例如:抛出回车事件

	  const [value, setValue] = useState('');  
	  const [options, setOptions] = useState(['洋芋果果', '测试1号', '测试2号']);  
	  const [open, setOpen] = useState(false);  

	  // 下拉菜单隐藏可见 
      const handleDropdown = (flag) => {  
	    setOpen(flag);  
	  };  
      //选中下拉项
      const handleMenu = (item,key)=>{
        setValue(item.props.label);
        setOpen(false);
      }  


	  return (  
	    <Dropdown  
	      overlay={
            <Menu
               onClick={handleMenu} 
            >
                {options.map((option, index) => (  
                    <Menu.Item key={option}>  
                        {option}  
                     </Menu.Item>  
                ))}
            </Menu>
          }  
	      trigger={['click']}  
	      visible={open}  
	      onVisibleChange={handleDropdown}  
	      getPopupContainer={() => dropdownRef.current}  
	    >  

	      <Input  
	        value={value}  
	        placeholder="请选择"  
	        suffix={<DownOutlined />}  
            onChange={(e)=>{ 
                setValue(e.target.value);
            }}
            onPressEnter={pressEnter}
	      />  
	    </Dropdown>  
	  );  
	};  

	 
	export default CustomSelect;

二、调用组件

import React from 'react';  
import CustomSelect from './CustomSelect'; // 导入自定义下拉组件  

	  
const App = () => {  
   return (  
	    <div>  
	      <CustomSelect pressEnter={(e)=>{
            console.log(e,"回调参数")
          }}/> 
	    </div>  
	  );  
  };  

export default App;

相关推荐

  1. react-select使用

    2024-04-09 19:00:01       11 阅读
  2. 自动补全的 select antd react

    2024-04-09 19:00:01       41 阅读
  3. React HOC》异步引入定制化的Antd Modal

    2024-04-09 19:00:01       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-09 19:00:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-09 19:00:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-09 19:00:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-09 19:00:01       18 阅读

热门阅读

  1. memset()函数及其作用

    2024-04-09 19:00:01       18 阅读
  2. 蓝桥杯-【二分】求阶乘

    2024-04-09 19:00:01       12 阅读
  3. 力扣经典150题第九题:跳跃游戏

    2024-04-09 19:00:01       17 阅读
  4. K8S Deployment HA

    2024-04-09 19:00:01       12 阅读
  5. 查找同一个进程显示在任务栏上的多个窗口

    2024-04-09 19:00:01       14 阅读
  6. Qt 翻译工具:使用 tr() 函数实现多语言支持

    2024-04-09 19:00:01       20 阅读
  7. Vue中Suspense组件详细介绍

    2024-04-09 19:00:01       12 阅读
  8. 特权账号怎么管?企业真的很为难!

    2024-04-09 19:00:01       13 阅读
  9. NIO与BIO

    2024-04-09 19:00:01       10 阅读
  10. 蓝桥杯 算法训练 藏匿的刺客

    2024-04-09 19:00:01       11 阅读