NewTreeSelect.jsx 5.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* eslint-disable no-restricted-syntax */
import React, { useState, useEffect, forwardRef, useImperativeHandle } from 'react';
import { GetWebMenuInfo, getWebModuleTree } from '@/services/webConfig/api';
import { message, Input, Tree, Empty, Tooltip, notification } from 'antd';
import lodash from 'lodash';
import {
  FolderFilled,
  FileOutlined,
  InfoCircleOutlined,
  HomeOutlined,
  DesktopOutlined,
} from '@ant-design/icons';
import styles from './TreeSelect.less';
const TreeSelect = (props, ref) => {
  const { value, onChange, menuChange, code, initCurrentMenu, userMode, curWeb } = props;
  const [menuWebList, setMenuWebList] = useState({});
  const [webList, setWebList] = useState([]);
  const [visible, setVisible] = useState(false);
  const [expandedKeys, setExpandedKeys] = useState([]);
  const { TreeNode } = TreeSelect;
21
  const [keepKey, setKeepKey] = useState([]);
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  useEffect(() => {
    getMenu();
  }, []);

  const getMenu = () => {
    getWebModuleTree('super')
      .then(res => {
        const { data } = res;
        if (res.code === 0) {
          let list = [];
          let arr = data.filter(item => item.id === 'Web4SingleStation');
          let datalist = arr[0].children;
          datalist.map((i, j) => {
            let aa = [];
            i.children.map(k => {
              if (k.text === '菜单管理') {
                aa = k.children;
              }
            });
            let a = {};
            a.value = `praent${j}`;
            a.text = i.text;
            a.children = aa;
            a.menuID = `praent${j}`;
            a.key = `praent${j}`;
            a.item = 'father';
            list.push(a);
          });
          let str = list.find(i => i.text === curWeb.text);
          setMenuWebList(str);
          setWebList(str.children);
          let aa = getKey(str.children);
54 55
          setKeepKey(aa);
          setExpandedKeys([...aa]);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        } else {
          notification.error({
            message: '提示',
            duration: 3,
            description: res.msg,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };

  const filterList = val => {
    if (!val) {
      val = '';
    }
    if (val !== '') {
74
      let newTree = menuWebList.children.map(item => mapTree(item, val));
75 76 77 78 79 80 81 82 83 84
      let filter = filterTree(newTree);
      let newData = filterTreeParent(filter);
      let arr = [];
      newData.map(i => {
        if (i.children.length !== 0 || i.show !== '') {
          arr.push(i);
        }
      });
      setWebList(arr);
      let aa = getKey(arr);
85
      setExpandedKeys([...aa]);
86 87
    } else {
      setWebList(menuWebList.children);
88
      setExpandedKeys([...keepKey]);
89 90 91 92 93 94
    }
  };

  const getKey = (users, allKeys = []) => {
    users.forEach(v => {
      let name = v.menuID || v.key;
95 96
      if (v.children.length > 0) {
        name && allKeys.push(name);
97
      }
98
      if (v.children.length > 0) {
99 100 101 102
        return getKey(v.children, allKeys);
      }
    });

103
    return allKeys;
104 105
  };

106
  const mapTree = (val, word) => {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    const obj = { ...val };
    const hasChild = obj.children.length > 0;
    let status = '';
    if (!condition(obj, word) && word !== '' && obj.children.length === 0) {
      status = false;
    }
    if (condition(obj, word) && word !== '' && obj.children.length === 0) {
      status = true;
    }
    return {
      title: obj.text || obj.title,
      key: obj.menuID || obj.key,
      icon: obj.menuType === 'Web4MenuGroup' ? <FolderFilled /> : <FileOutlined />,
      menuType: obj.menuType,
      disabled: hasChild || obj.pageUrl === null,
      pageUrl: obj.pageUrl,
      product: obj.product,
      show: status,
125
      children: hasChild ? obj.children.map(i => mapTree(i, word)) : [],
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    };
  };

  const filterTree = data => {
    let result = [];
    for (let item of data) {
      if (item.show !== false) {
        let node = { ...item };
        if (item.children) {
          node.children = filterTree(item.children);
        }
        result.push(node);
      }
    }
    return result;
  };

  const filterTreeParent = data => {
    let result = [];
    for (let item of data) {
      if (item.show !== '' || item.children.length > 0) {
        let node = { ...item };
        if (item.children) {
          node.children = filterTreeParent(item.children);
        }
        result.push(node);
      }
    }
    return result;
  };

  // 过滤条件
  const condition = (node, val) => {
    let name = node.title || node.text;
    return name?.includes(val) || node.pageUrl?.includes(val);
  };

  const changeValue = e => {
    // 过滤数组
    onChange(e.target.value);
    filterList(e.target.value);
    setVisible(true);
  };

  const onSelect = (selectedKeysValue, info) => {
    onChange(info.node.pageUrl);
    setVisible(false);
  };

  const onExpand = expandedKeysValue => {
    setExpandedKeys(expandedKeysValue);
  };

  return (
    <div className={styles.treeSelect}>
      <Input
        allowClear
        placeholder="请选填写菜单功能路径"
        value={value}
        onFocus={() => filterList(value)}
        onClick={() => setVisible(!visible)}
        onChange={changeValue}
        onBlur={() => setVisible(false)}
      />
      <div
        className={styles.dropBox}
        onMouseDown={event => {
          event.preventDefault();
        }}
        style={{ display: visible ? 'block' : 'none' }}
      >
        <Tree
          onExpand={onExpand}
          selectedKeys={null}
          showIcon
          expandedKeys={expandedKeys}
          autoExpandParent
          onSelect={onSelect}
204
          treeData={webList && webList.map(val => mapTree(val))}
205 206 207 208 209 210 211 212 213
          style={{ width: '100%' }}
        />
        {Object.keys(webList).length === 0 ? <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} /> : null}
      </div>
    </div>
  );
};

export default forwardRef(TreeSelect);