index.jsx 4.29 KB
Newer Older
krysent's avatar
krysent committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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
import React, { useEffect, useReducer } from "react";
import { Tree, Input, Card, Tooltip } from "antd";
import styles from "./index.less";
import { doFetch } from "@/utils/doFetch";
import Fieldmanage from "./Fieldmanage";
import { useRequest } from "umi";
const { Search } = Input;
const initState = {
  searchValue: {},
  expandedKeys: [],
  autoExpandParent: false,
  checkedNode: {},
};
function reducer(state, action) {
  if (action.type == "changeSearch") {
    return {
      ...state,
      autoExpandParent: true,
      expandedKeys: action.expandedKeys,
      searchValue: action.searchValue,
    };
  } else if (action.type == "expand") {
    return {
      ...state,
      autoExpandParent: false,
      expandedKeys: action.expandedKeys,
    };
  } else if (action.type == "checked") {
    return {
      ...state,
      checkedNode: action.checkedNode,
    };
  }
}
const getParentKey = (key, system) => {
  let parentKey;
  for (let i = 0; i < system.length; i++) {
    const node = system[i];
    if (node.children) {
      if (node.children.some((item) => item.id === key)) {
        parentKey = node.id;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
const Specialproperties = (props) => {
  const [state, dispatch] = useReducer(reducer, initState);
  const { searchValue, expandedKeys, autoExpandParent, checkedNode } = state;
  let { data, loading } = useRequest(
    () => {
      return doFetch({
        url: "/ngic-base-business/paForm/queryList",
        params: searchValue,
      });
    },
    {
      formatResult: (res) => res?.data?.dataList,
      refreshDeps: [searchValue],
    }
  );
  const loop = (datas) => {
    return datas?.map((item) => {
      if (item.children) {
        return {
          title: item.formName,
          key: item.id,
          children: loop(item.children),
        };
      }
      return {
        title: item.formName,
        key: item.id,
      };
    });
  };
  const onExpand = (expandedKeys) => {
    dispatch({ type: "expand", expandedKeys });
  };
  const searchTree = (val, e) => {
    let value;
    if (val) {
      value = val;
    } else {
      value = e.target.value;
    }
    const dataLists = getAllList();
    const expandedKeys = dataLists
      .map((item) => {
        if (item.title.indexOf(value) > -1) {
          return getParentKey(item.key, data);
        }
        return null;
      })
      .filter((item, i, self) => item && self.indexOf(item) === i);
    dispatch({
      type: "changeSearch",
      expandedKeys,
      searchValue: { formName: value },
    });
  };



  
  const selectTree = (selectedKeys, selectedNodes) => {
    const dataLists = getAllList();
    let checkedNode = dataLists.filter((it) => it.key == selectedKeys[0])[0];
    dispatch({ type: "checked", checkedNode });
  };
  function getAllList() {
    const dataLists = [];
    const generateLists = (datas) => {
      for (let i = 0; i < datas.length; i++) {
        const node = datas[i];
        const key = node.id;
        dataLists.push({ key, title: node.formName, type: node.type });
        if (node.children) {
          generateLists(node.children, node.id);
        }
      }
    };
    generateLists(data);
    return dataLists;
  }
  return (
    <div
      style={{
        width: "100%",
        height: "100%",
        backgroundColor: "#fff",
        minHeight: 300,
      }}
    >
      <div style={{ width: "100%", border: "1px solid rgba(0, 0, 0, 0.06)" }}>
        <div style={{ padding: "16px 20px", fontSize: 16 }}>
          {props.route.name}
        </div>
      </div>
      <div style={{ display: "flex" }}>
        <div className={styles.leftTree} style={{ width: 200 }}>
          <Search
            style={{ marginBottom: 15 }}
            placeholder="请输入"
            onSearch={searchTree}
            allowClear
            
          />
          <Tree
            onExpand={onExpand}
            expandedKeys={expandedKeys}
            autoExpandParent={autoExpandParent}
            treeData={loop(data)}
            onSelect={selectTree}
          />
        </div>
        <div className={`${styles.rightTable} specialpropertiesCard`}>
          <Fieldmanage checkedNode={checkedNode} />
        </div>
      </div>
    </div>
  );
};

export default Specialproperties;