mcard.jsx 11 KB
Newer Older
wuhao's avatar
wuhao 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
import React, { PureComponent } from "react";
import { PullToRefresh, ListView } from "antd-mobile";
import LoadingFooter from "./LoadingFooter";
import ProCard from "@ant-design/pro-card";
import { postFetch } from "@/utils/doFetch";
import { Row, Col, Tabs, Drawer, Empty } from "antd";
import { SearchOutlined } from "@ant-design/icons";
import InitForm from "../InitForm";

const { TabPane } = Tabs;

class Mcard extends PureComponent {
  constructor(props) {
    super(props);
    const dataSource = new ListView.DataSource({
      rowHasChanged: (row1, row2) => row1 !== row2,
    });

    this.state = {
      visible: false,
      dataSource,
      isLoading: true,
      scrolltop: 0,
      refreshing: false,
      fields: {},
      searchlist: [],
      dataArr: props.dataSource || [],
      isEmpty: false,
      params: {
        pageIndex: 1,
        ...props.extraparams,
      },
    };
  }

  //获取一段分页数据
  getsectiondata(np) {
    let { path, dataSource } = this.props || np;
    if (!path) {
      this.setState({
        dataSource: dataSource
          ? this.state.dataSource.cloneWithRows(dataSource)
          : this.state.dataSource,
        isLoading: false,
        refreshing: false,
        isEmpty: this.state.dataArr.length == 0,
      });
      return;
    } else {
      let postparams = this.state.params;
      if (this.props.pagination == "false" || this.props.pagination === false) {
        delete postparams.pageIndex;
        delete postparams.pageSize;
      }
      postFetch({ url: path, params: postparams }).then((res) => {
        if (!res.data) {
          return;
        }
        let list = res.data?.page?.list || res.data.dataList;
        list = list ? list : [];
        let dataArr = this.state.dataArr.concat(list);
        this.setState({
          dataSource: this.state.dataSource.cloneWithRows(dataArr),
          isLoading: false,
          refreshing: false,
          hasMore: res?.data?.page?.hasnextpage,
          dataArr,
          isEmpty: this.state.params.pageIndex == 1 && list.length == 0,
        });
      });
    }
  }

  getfields() {
    let fields = {},
      { columns } = this.props,
      searchlist = columns.filter(
        (it) => it.search !== false && it.option != "option" && it.key
      );
    searchlist.map((it) => {
      let key = "";
      if (it?.formItemProps?.name) {
        key = it.formItemProps.name;
      } else {
        key = it.key;
      }

      let valuetype = it.valueType ? it.valueType : "input",
        picker = "",
        format = "YYYY-MM-DD";

      if (valuetype == "dateMonth") {
        valuetype = "datepicker";
        picker = "month";
        format = "YYYY-MM";
      }
      fields[key] = {
        value: it.initialValue || this.state.params[key],
        type: valuetype,
        title: it?.title,
        name: [key],
        col: { span: 24 }, //栅格布局 默认 12
        linked: it.linked,
        belinked: it.belinked,
        options: it.options,
        picker: picker,
        format: format,
      };
    });
    this.setState({
      fields,
      searchlist,
    });
  }

  componentDidMount() {
    this.getsectiondata();
    this.getfields();
    this.props.onRef && this.props.onRef(this);
  }

  componentWillReceiveProps(np) {
    np.onRef && np.onRef(this);
    if (
      this.props.path != np.path ||
      JSON.stringify(this.props.extraparams) !=
wuhao's avatar
wuhao committed
127
      JSON.stringify(np.extraparams) ||
wuhao's avatar
wuhao committed
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
      JSON.stringify(this.props.dataSource) != JSON.stringify(np.dataSource)
    ) {
      this.lv.scrollTo(0);
      this.setState(
        {
          hasMore: true,
          dataArr: np.dataSource || [],
          params: {
            pageIndex: 1,
            ...np.extraparams,
          },
        },
        () => {
          this.onRefresh(np);
        }
      );
    }
    if (this.props.vs != np.vs) {
      this.onRefresh(np);
    }

    if (this.props.columns != np.columns) {
      this.getfields();
    }
  }

  onRefresh = (np, searchdata) => {
    let extraparams = np ? np.extraparams : this.props.extraparams;
    searchdata = searchdata || {};
    this.setState(
      {
        refreshing: true,
        isLoading: true,
        hasMore: true,
        dataArr: [],
        params: {
          pageIndex: 1,
          ...extraparams,
          ...searchdata,
        },
      },
      () => {
        this.getsectiondata(np);
      }
    );
  };

  onEndReached = (event) => {
    if (this.state.isLoading || !this.state.hasMore) {
      return;
    }
    let pageIndex = this.state.params.pageIndex + 1;
    this.setState(
      {
        isLoading: true,
        params: {
          pageIndex,
        },
      },
      () => {
        this.getsectiondata();
      }
    );
  };

  render() {
    let {
wuhao's avatar
wuhao committed
195 196 197 198 199 200 201 202 203 204
      scrolltop,
      dataSource,
      isLoading,
      refreshing,
      hasMore,
      isEmpty,
      visible,
      fields,
      searchlist,
    } = this.state,
wuhao's avatar
wuhao committed
205 206 207 208 209 210 211 212 213 214 215 216
      {
        columns,
        cardtitle,
        cardavatar,
        pagetitle,
        pageextra,
        onSearchChange,
        tabBarExtraContent,
        onTabChange,
        tabList,
      } = this.props;

wuhao's avatar
wuhao committed
217

wuhao's avatar
wuhao committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    return (
      <div style={{ height: "100%" }}>
        <div className="fixedsearch">
          {searchlist.length > 0 && (
            <div
              className="action"
              onClick={() => {
                this.setState({
                  visible: true,
                });
              }}
            >
              <SearchOutlined style={{ color: "#fff" }} />
            </div>
          )}
wuhao's avatar
wuhao committed
233 234 235 236 237 238 239 240 241
          {
            pageextra ?
              Array.isArray(pageextra) ?
                pageextra.map((it) => {
                  return <div>{it}</div>;
                }) :
                pageextra
              : null
          }
wuhao's avatar
wuhao committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255
        </div>

        <Drawer
          visible={visible}
          title={"输入您的查询条件"}
          onClose={() => {
            this.setState({
              visible: false,
            });
          }}
          closable={true}
        >
          <InitForm
            fields={fields}
wuhao's avatar
wuhao committed
256
            onChange={() => { }}
wuhao's avatar
wuhao committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
            reset={true}
            submitData={(values) => {
              this.onRefresh(this.props, values);
              this.setState({
                visible: false,
              });
              onSearchChange(values);
            }}
          ></InitForm>
        </Drawer>
        <ListView
          ref={(el) => (this.lv = el)}
          dataSource={dataSource}
          renderHeader={() => {
            return pagetitle ? (
              <div className="pagetitle">
                <p style={{ marginBottom: 12 }} className="spacebt">
                  {pagetitle}
                  {!tabList && tabBarExtraContent}
                </p>
                <Tabs
                  style={{ width: "100%" }}
                  onChange={onTabChange}
                  tabBarExtraContent={tabBarExtraContent}
                >
                  {tabList &&
                    tabList.map((it) => {
                      return <TabPane tab={it.tab} key={it.key}></TabPane>;
                    })}
                </Tabs>
              </div>
            ) : null;
          }}
          renderFooter={() => (
            <LoadingFooter
              isLoading={isLoading && hasMore}
              isEmpty={isEmpty}
            ></LoadingFooter>
          )}
          renderRow={(rowData = {}) => {
            let extrarow = columns.filter(
              (it) =>
                it.key != cardtitle &&
                it.key != cardavatar &&
                it.option != "option" &&
                it.key
            );
            return (
              <ProCard
                headerBordered
                style={{ borderRadius: 4, overflow: "hidden" }}
                title={
                  <div
                    style={{
                      display: "flex",
                      justifyContent: "flex-end",
                      alignItems: "center",
                    }}
                  >
                    {cardavatar &&
                      columns
                        .filter((it) => it.key == cardavatar)[0]
                        ?.render(null, rowData)}
                    <span
                      className="oneline"
                      style={{
                        marginLeft: cardavatar ? 12 : 0,
                        color: "#1890ff",
                        maxWidth: 300,
                      }}
                    >
                      {rowData[cardtitle ? cardtitle : columns[0].key]}
                    </span>
                  </div>
                }
                actions={
                  columns.filter((it) => it.valueType == "option").length == 0
                    ? null
                    : columns
wuhao's avatar
wuhao committed
336 337
                      .filter((it) => it.valueType == "option")[0]
                      ?.render(null, rowData, null, { type: "text" })
wuhao's avatar
wuhao committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
                }
              >
                <Row>
                  {extrarow.map((it) => {
                    return (
                      <Col
                        span={24}
                        key={it.key}
                        style={{
                          display: "flex",
                          justifyContent: "space-between",
                          margin: "6px 0",
                        }}
                      >
                        <div style={{ display: "inline-block", flexShrink: 0 }}>
                          {it?.title}
                        </div>
                        <div
                          style={{
                            color: "#999",
                            flex: 1,
                            overflow: "hidden",
                            textAlign: "right",
                            wordBreak: "break-all",
                          }}
                        >
                          {it.render
                            ? it.render(null, rowData)
                            : rowData[it.key]}
                        </div>
                      </Col>
                    );
                  })}
                </Row>
              </ProCard>
            );
          }}
          renderSeparator={(sectionID, rowID) => (
            <div
              key={`${sectionID}-${rowID}`}
              style={{
                backgroundColor: "transparent",
                height: 12,
              }}
            />
          )}
          style={{
            overflow: "auto",
            height: "100%",
            minHeight: "70vh",
          }}
          className={scrolltop > 0 ? "notrans" : "trans"}
          pageSize={10}
          onScroll={(e) => {
            this.setState({
              scrolltop: e.target.scrollTop,
            });
            if (e.target.scrollTop > 400) {
            } else {
            }
          }}
          scrollRenderAheadDistance={800}
          distanceToRefresh={window.devicePixelRatio * 25}
          onEndReached={this.onEndReached}
          onEndReachedThreshold={10}
          pullToRefresh={
            <PullToRefresh refreshing={refreshing} onRefresh={this.onRefresh} />
          }
        />
      </div>
    );
  }
}

export default Mcard;