Ant Design3.0-Select中扩展菜单

主要是业务代码量比较大,如果把antd从3.0改到4.0存在一定的风险,所以只能自己解决Select 中 扩展菜单存在的缺陷,虽然antd的开发人员已经意识到组件存在缺陷,但是他们提供的那个代码也无法运行,哎~

antd 3.0 版本中扩展菜单无法控制扩展组件,无法改成 Input,而且点一下,浮窗还会消失,所以,重新封装一个组件比较好

antd3.0中的扩展菜单是这种样式
在这里插入图片描述
要把他改成这样
在这里插入图片描述
进行了一些封装

import React, { Component } from 'react';
import { Divider, Select, Button, Input } from '@hyperchain/ql-oa-antd';

const Option = Select.Option;

class DropdownSelect extends Component {
  constructor(props) {
    super(props);

    this.state = {
      // propsValue: props.value,
      dropdownSelectProps: {
        isOpen: false,
        inputValue: '',
        isOutRegion: false
      }
    };
  }


  monitorClick = () => {
    const { dropdownSelectProps } = this.state;
    if (dropdownSelectProps.isOutRegion) {
      dropdownSelectProps.isOpen = false;
      dropdownSelectProps.isOutRegion = false;
      dropdownSelectProps.inputValue = '';
      document.removeEventListener('click', this.monitorClick);
      this.setState({
        dropdownSelectProps
      });
    }
  }


  addDropdownSelectItem = (dSelectOption, item) => {
    const { dropdownSelectProps } = this.state;
    const id = `${item.field}-input`;
    const inputValue = document.getElementById(id).value;
    if (!inputValue) {
      return;
    }

    const newItem = {
      label: inputValue,
      value: inputValue
    };
    const newOptionData = [...dSelectOption, newItem];
    item.optionData = newOptionData;
    dropdownSelectProps.inputValue = '';
    this.setState({
      dropdownSelectProps
    });
  }

  onStartMoniorClick = (flag) => {
    const { dropdownSelectProps } = this.state;
    if (flag) {
      document.addEventListener('click', this.monitorClick);
    }
    dropdownSelectProps.isOutRegion = flag;
  }

  dropInput = (v) => {
    const { dropdownSelectProps } = this.state;
    dropdownSelectProps.inputValue = v.target.value;
    this.setState({
      dropdownSelectProps
    });
  }

  onDropdownVisibleChange = dropdownSelectOpen => {
    const { dropdownSelectProps } = this.state;
    if (dropdownSelectProps.isOpen && !dropdownSelectProps.isOutRegion) {
      return;
    }
    dropdownSelectProps.isOpen = dropdownSelectOpen;
    this.setState({ dropdownSelectProps });
  };


  handleChange = (e) => {
    const { onChange } = this.props;
    onChange(e);
  };

render() {
  const { item = {}, value } = this.props;
  const { dropdownSelectProps } = this.state;
  const { showSearch = false, disabled = false, labelInValue = false, placeholder = `请选择${item.title || ''}` } = item;

  const dSelectOption = item.optionData || [];

  return (
    <Select
      defaultValue={value}
      id={item.field}
      placeholder={placeholder}
      style={{ width: '100%' }}
      disabled={disabled}
      mode="multiple"
      showSearch={showSearch}
      labelInValue={labelInValue}
      notFoundContent={null}
      onChange={this.handleChange}
      onSearch={this.handleSearch}
      onDropdownVisibleChange={this.onDropdownVisibleChange}
      open={dropdownSelectProps.isOpen}
      dropdownRender={menu => (
        <div
          onMouseLeave={() => this.onStartMoniorClick(true)}
          onMouseEnter={() => this.onStartMoniorClick(false)}
        >
          {menu}
          <Divider style={{ margin: '4px 0' }} />
          <div
            style={{ padding: '4px 8px', cursor: 'pointer', display: 'flex' }}
          >
            <Input
              id={`${item.field}-input`}
              value={dropdownSelectProps.inputValue}
              onChange={this.dropInput}
            />
            <Button
              icon="plus"
              onClick={() => this.addDropdownSelectItem(dSelectOption, item)}
              style={{ marginLeft: 8 }}
            >新增
            </Button>
          </div>
        </div>
      )}
    >
      {dSelectOption.map(el => (
        <Option key={el.value} value={el.value}>
          {el.label}
        </Option>
            ))}
    </Select>
    );
  }
}

export default DropdownSelect;

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值