index.js 5.03 KB
Newer Older
涂茜's avatar
涂茜 committed
1 2 3
import React, { useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
涂茜's avatar
涂茜 committed
4
import { Spin, Input, Tree, Divider, message, Pagination, ConfigProvider } from 'antd';
涂茜's avatar
涂茜 committed
5 6 7 8 9 10 11 12 13 14 15 16
import { SearchOutlined } from '@ant-design/icons';
import Empty from '@wisdom-components/empty';
import './index.less';

const DeviceTree = (props) => {
  const {
    prefix,
    placeholder,
    deviceTreeService,
    serviceParams,
    onTreeCheck,
    onTreeSelect,
涂茜's avatar
涂茜 committed
17
    pagination,
涂茜's avatar
涂茜 committed
18 19 20 21 22 23 24
  } = props;

  const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('ec-device-tree');

  const [treeData, setTreeData] = useState([]);
  const [params, setParams] = useState({});
涂茜's avatar
涂茜 committed
25
  const [totalCount, setTotalCount] = useState(0);
涂茜's avatar
涂茜 committed
26
  const [loading, setLoading] = useState(false);
涂茜's avatar
涂茜 committed
27 28 29
  const [expandedKeys, setExpandedKeys] = useState([]);
  const [checkedKeys, setCheckedKeys] = useState([]);
  const [selectedKeys, setSelectedKeys] = useState([]);
李纪文's avatar
李纪文 committed
30

涂茜's avatar
涂茜 committed
31
  useEffect(() => {
涂茜's avatar
涂茜 committed
32
    setParams(serviceParams);
涂茜's avatar
涂茜 committed
33 34
  }, []);

涂茜's avatar
涂茜 committed
35 36 37 38
  useEffect(() => {
    setParams(serviceParams);
  }, [serviceParams]);

涂茜's avatar
涂茜 committed
39
  const handleData = (data) => {
李纪文's avatar
李纪文 committed
40
    data.map((item) => {
涂茜's avatar
涂茜 committed
41 42
      item.title = item.deviceName;
      item.key = item.stationID;
涂茜's avatar
涂茜 committed
43 44 45 46 47 48
      item.children = handleData(item.children);
    });
    return data;
  };

  const fetchData = (param = {}) => {
涂茜's avatar
涂茜 committed
49
    setLoading(true);
涂茜's avatar
涂茜 committed
50 51 52 53
    deviceTreeService(param).then((response) => {
      if (response.code === 0) {
        const data = response.data
          ? response.data.list && response.data.list.length > 0
涂茜's avatar
涂茜 committed
54
            ? response.data.list[0].deviceList
涂茜's avatar
涂茜 committed
55 56
            : []
          : [];
涂茜's avatar
涂茜 committed
57
        const newData = handleData(data);
涂茜's avatar
涂茜 committed
58
        const keys = newData.length > 0 ? [newData[0].key] : [];
涂茜's avatar
涂茜 committed
59
        setLoading(false);
涂茜's avatar
涂茜 committed
60
        setTreeData(newData);
涂茜's avatar
涂茜 committed
61
        setTotalCount(response.data.totalCount);
涂茜's avatar
涂茜 committed
62 63
        onTreeCheck(newData.length > 0 ? [newData[0]] : []);
        onTreeSelect(newData.length > 0 ? [newData[0]] : []);
涂茜's avatar
涂茜 committed
64 65 66
        setCheckedKeys(keys);
        setExpandedKeys(keys);
        setSelectedKeys(keys);
涂茜's avatar
涂茜 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      } else {
        message.error(response.msg);
      }
    });
  };

  useEffect(() => {
    if (JSON.stringify(params) !== '{}') {
      fetchData(params);
    }
  }, [params]);

  const onSearch = (e) => {
    if (e.type === 'keydown' || e.target.value === '') {
      const param = { ...params, queryInfo: e.target.value };
      setParams(param);
    }
  };

  // 选中复选框
涂茜's avatar
涂茜 committed
87 88
  const onCheck = (checkedKeysValue) => {
    const { checked } = checkedKeysValue;
涂茜's avatar
涂茜 committed
89 90
    const checkedTree = [];
    treeData.forEach((item) => {
涂茜's avatar
涂茜 committed
91
      if (checked.includes(item.key)) {
涂茜's avatar
涂茜 committed
92 93 94 95
        checkedTree.push(item);
      }
      if (item.children.length > 0) {
        item.children.forEach((child) => {
涂茜's avatar
涂茜 committed
96
          if (checked.includes(child.key)) {
涂茜's avatar
涂茜 committed
97 98 99 100 101
            checkedTree.push(child);
          }
        });
      }
    });
涂茜's avatar
涂茜 committed
102
    setCheckedKeys(checked);
涂茜's avatar
涂茜 committed
103 104 105 106
    onTreeCheck(checkedTree);
  };

  const onSelect = (selectedKeysValue, info) => {
涂茜's avatar
涂茜 committed
107
    setSelectedKeys(selectedKeysValue);
涂茜's avatar
涂茜 committed
108 109 110
    onTreeSelect(info.selectedNodes);
  };

涂茜's avatar
涂茜 committed
111 112 113 114
  const onExpand = (expandedKeysValue) => {
    setExpandedKeys(expandedKeysValue);
  };

涂茜's avatar
涂茜 committed
115 116 117 118
  const onPaginationChange = (page) => {
    setParams({ ...params, pageIndex: page });
  };

涂茜's avatar
涂茜 committed
119 120 121 122 123 124 125 126 127
  return (
    <div className={classNames(prefixCls)}>
      <Input
        prefix={prefix}
        placeholder={placeholder}
        bordered={false}
        onChange={onSearch}
        onPressEnter={onSearch}
      />
涂茜's avatar
涂茜 committed
128
      <Divider className={classNames(`${prefixCls}-divider`)} />
涂茜's avatar
涂茜 committed
129
      <div className={classNames(`${prefixCls}-content`)}>
涂茜's avatar
涂茜 committed
130 131 132
        <Spin spinning={loading}>
          {!!treeData.length && (
            <Tree
涂茜's avatar
涂茜 committed
133 134 135
              checkedKeys={checkedKeys}
              expandedKeys={expandedKeys}
              selectedKeys={selectedKeys}
涂茜's avatar
涂茜 committed
136
              treeData={treeData}
涂茜's avatar
涂茜 committed
137
              onExpand={onExpand}
涂茜's avatar
涂茜 committed
138 139 140 141 142 143 144 145
              onCheck={onCheck}
              onSelect={onSelect}
              checkStrictly
              {...props}
            />
          )}
          {!treeData.length && !loading && <Empty />}
        </Spin>
涂茜's avatar
涂茜 committed
146
      </div>
涂茜's avatar
涂茜 committed
147 148 149 150 151 152 153
      {pagination && (
        <div className={classNames(`${prefixCls}-pagination`)}>
          <Pagination
            simple
            hideOnSinglePage
            current={params.pageIndex || 1}
            pageSize={params.pageSize || 20}
涂茜's avatar
涂茜 committed
154
            total={totalCount}
涂茜's avatar
涂茜 committed
155 156 157 158
            onChange={onPaginationChange}
          />
        </div>
      )}
涂茜's avatar
涂茜 committed
159 160 161 162 163
      {!pagination && (
        <Divider plain className={classNames(`${prefixCls}-total`)}>
           {totalCount} 记录
        </Divider>
      )}
涂茜's avatar
涂茜 committed
164 165 166 167 168 169 170
    </div>
  );
};

DeviceTree.defaultProps = {
  prefix: <SearchOutlined />,
  placeholder: '搜索设备名称',
涂茜's avatar
涂茜 committed
171
  pagination: true,
涂茜's avatar
涂茜 committed
172 173 174 175 176 177 178 179
  serviceParams: {},
  onTreeCheck: () => {},
  onTreeSelect: () => {},
};

DeviceTree.propTypes = {
  prefix: PropTypes.node,
  placeholder: PropTypes.string,
涂茜's avatar
涂茜 committed
180
  pagination: PropTypes.bool,
涂茜's avatar
涂茜 committed
181 182 183
  serviceParams: PropTypes.object,
  onTreeCheck: PropTypes.func,
  onTreeSelect: PropTypes.func,
涂茜's avatar
涂茜 committed
184
  deviceTreeService: PropTypes.any,
涂茜's avatar
涂茜 committed
185 186 187
};

export default DeviceTree;