• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SkylerHu / antd-restful / #84

22 Sep 2025 04:22AM UTC coverage: 82.996% (+0.3%) from 82.661%
#84

push

web-flow
Merge aa08f6cca into 186ad040d

1329 of 1688 branches covered (78.73%)

Branch coverage included in aggregate %.

111 of 117 new or added lines in 11 files covered. (94.87%)

1 existing line in 1 file now uncovered.

1751 of 2023 relevant lines covered (86.55%)

66.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/components/RouteBaseTable.jsx
1
import React, { forwardRef, useCallback, useEffect, useRef, useState } from "react";
2
import PropTypes from "prop-types";
3
import { dequal as deepEqual } from "dequal";
4
import { guessQueryTypes, parseQueryTypes } from "src/common/parser";
5
import { isEmpty, isFunction } from "src/common/typeTools";
6
import RestTable from "src/components/RestTable";
7
import globalConfig from "src/config";
8
import { useDeepCompareMemoize } from "src/hooks";
9

10
// 因为兼容不了react-router v5和v6 版本,所以传递 location 进来,然后父类组件实现路由的变更
11
const RouteBaseTable = forwardRef(({ location, onSearchChange, restProps }, ref) => {
1✔
12
  const {
13
    parseOptions,
14
    parseTypes,
15
    onFiltersChange,
16
    columns,
17
    filterFormProps,
18
  } = restProps;
99✔
19
  const searchRef = useRef(location.search);
99✔
20

21
  const memParseOptions = useDeepCompareMemoize(parseOptions);
99✔
22
  // 后续可以废弃
23
  const memParseTypes = useDeepCompareMemoize(parseTypes);
99✔
24
  // 猜测的类型
25
  const [guessTypes, setGuessTypes] = useState(null);
99✔
26

27
  const [params, setParams] = useState();
99✔
28

29
  useEffect(() => {
99✔
30
    setGuessTypes((oldV) => {
27✔
31
      const newV = { ...guessQueryTypes(columns), ...guessQueryTypes(filterFormProps?.fields) };
27✔
32
      if (deepEqual(newV, oldV)) {
27!
NEW
33
        return oldV;
×
34
      }
35
      return newV;
27✔
36
    });
37
  }, [columns, filterFormProps?.fields]);
38

39
  useEffect(() => {
99✔
40
    if (guessTypes === null) {
57✔
41
      // 猜测类型未完成,不进行初始化
42
      return;
27✔
43
    }
44
    // 合并猜测的类型和配置的类型
45
    const options = {
30✔
46
      ...memParseOptions,
47
      types: {
48
        ...guessTypes,
49
        ...memParseOptions?.types,
50
      },
51
    };
52
    let query = globalConfig.queryParse(location.search, options);
30✔
53
    if (memParseTypes) {
30✔
54
      // query-string > 9 支持直接 parasOptions 配置字段类型,但这个低版本node又不能使用
55
      // 主要是为了解决低版本 query参数中 超大int溢出 和 普通 int存在的场景,需要额外指定参数类型
56
      query = parseQueryTypes(query, memParseTypes);
3✔
57
    }
58
    setParams((oldV) => {
30✔
59
      const newV = { ...query };
30✔
60
      if (deepEqual(newV, oldV)) {
30!
61
        return oldV;
×
62
      }
63
      return newV;
30✔
64
    });
65
  }, [location.search, memParseOptions, memParseTypes, guessTypes]);
66

67
  const onChange = useCallback(
99✔
68
    (values) => {
69
      const filters = { ...values };
30✔
70
      let changedSearch = globalConfig.queryStringify(filters, memParseOptions);
30✔
71
      if (isEmpty(changedSearch)) {
30✔
72
        changedSearch = "";
3✔
73
      } else {
74
        changedSearch = `?${changedSearch}`;
27✔
75
      }
76
      if (searchRef.current !== changedSearch) {
30✔
77
        searchRef.current = changedSearch;
12✔
78
        setParams(filters);
12✔
79
        if (isFunction(onSearchChange)) {
12!
80
          onSearchChange(changedSearch);
12✔
81
        }
82
        if (isFunction(onFiltersChange)) {
12!
83
          onFiltersChange(values);
12✔
84
        }
85
      }
86
    },
87
    [onFiltersChange, onSearchChange, memParseOptions]
88
  );
89

90
  if (params === undefined || params === null || guessTypes === null) {
99✔
91
    // 等待params根据search初始化完成
92
    return null;
54✔
93
  }
94

95
  return <RestTable ref={ref} {...restProps} routeParams={params} onFiltersChange={onChange} />;
45✔
96
});
97

98
RouteBaseTable.propTypes = {
1✔
99
  location: PropTypes.object,
100
  onSearchChange: PropTypes.func,
101
  restProps: PropTypes.object,
102
};
103

104
RouteBaseTable.displayName = "RouteBaseTable";
1✔
105

106
export default RouteBaseTable;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc