• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

antd select使用 open 对下拉菜单进行自由扩展,新增唯一性判断

武飞扬头像
开发小白菜
帮助1

import { Button, Input, Drawer, Space, Form } from 'antd';
import { useState, useRef, useEffect } from 'react';
import type { InputRef } from 'antd';
import { PlusOutlined } from '@ant-design/icons';

  const inputRef = useRef<InputRef>(null);
  const [items, setItems] = useState([]);
  const [name, setName] = useState('');
  const [name, setName] = useState(["test","poc"]);
  
  const onNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setName(event.target.value);
  };
  
//下拉选择判断唯一性 list是已经存在的,用来判断唯一性的数组 
const addItem = (e: React.MouseEvent<HTMLAnchorElement>) => {
    e.preventDefault();
    if (!name) {
      notification['warning']({
        message: '请输入环境类型',
      });
      return;
    } else {
      if (name && list.includes(name)) {
        notification['warning']({
          message: '环境类型已存在,不可以重复添加',
        });

        return;
      } else {
        setItems([...items, name]);
        setName('');
        setTimeout(() => {
          inputRef.current?.focus();
        }, 0);
      }
    }
  };
 return (
		<Select
            placeholder="请选择环境类型"
            dropdownRender={(menu) => (
              <>
                {menu}
                <Divider style={{ margin: '8px 0' }} />
                <Space style={{ padding: '0 8px 4px' }}>
                  <Input
                    placeholder="请选择环境类型"
                    ref={inputRef}
                    value={name}
                    onChange={onNameChange}
                  />
                  <Button icon={<PlusOutlined />} onClick={addItem}>
                    新增
                  </Button>
                </Space>
              </>
            )}
            options={items.map((item) => ({
              label: item,
              value: item,
              disabled: list.includes(item),
            }))}
          />
        )
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghhig
系列文章
更多 icon
同类精品
更多 icon
继续加载