@hw-component/table 0.0.6-beta-v9 → 0.0.6-beta-v10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/es/src/components/HTableHeader/index.d.ts +3 -0
  2. package/es/src/components/Table.d.ts +3 -0
  3. package/es/src/components/context.d.ts +23 -0
  4. package/es/src/components/hooks/index.d.ts +1 -0
  5. package/es/src/components/index.d.ts +11 -0
  6. package/es/src/components/modal.d.ts +100 -0
  7. package/lib/src/components/HTableHeader/index.d.ts +3 -0
  8. package/lib/src/components/Table.d.ts +3 -0
  9. package/lib/src/components/context.d.ts +23 -0
  10. package/lib/src/components/hooks/index.d.ts +1 -0
  11. package/lib/src/components/index.d.ts +11 -0
  12. package/lib/src/components/modal.d.ts +100 -0
  13. package/package.json +1 -1
  14. package/src/Layout.tsx +61 -0
  15. package/src/app.tsx +33 -0
  16. package/src/components/GoTop/goTop.png +0 -0
  17. package/src/components/GoTop/index.tsx +37 -0
  18. package/src/components/HTableBody/AlertMsg.tsx +37 -0
  19. package/src/components/HTableBody/RowSelection.tsx +143 -0
  20. package/src/components/HTableBody/hooks.tsx +72 -0
  21. package/src/components/HTableBody/index.tsx +148 -0
  22. package/src/components/HTableFooter/index.tsx +78 -0
  23. package/src/components/HTableHeader/defaultSubComponent.tsx +62 -0
  24. package/src/components/HTableHeader/hooks.tsx +164 -0
  25. package/src/components/HTableHeader/index.tsx +58 -0
  26. package/src/components/HTableHeader/modal.d.ts +15 -0
  27. package/src/components/HTablePagination/index.tsx +91 -0
  28. package/src/components/ModalTable/hooks.ts +60 -0
  29. package/src/components/ModalTable/index.tsx +78 -0
  30. package/src/components/Table.tsx +85 -0
  31. package/src/components/TableConfig.tsx +30 -0
  32. package/src/components/TableCustomize.tsx +71 -0
  33. package/src/components/context.ts +28 -0
  34. package/src/components/hooks/index.ts +14 -0
  35. package/src/components/hooks/useCurrentTable.ts +32 -0
  36. package/src/components/hooks/useDispatch.ts +8 -0
  37. package/src/components/hooks/useHTable.tsx +26 -0
  38. package/src/components/hooks/useReq.ts +65 -0
  39. package/src/components/hooks/useRowObj.ts +27 -0
  40. package/src/components/index.less +46 -0
  41. package/src/components/index.tsx +11 -0
  42. package/src/components/modal.ts +130 -0
  43. package/src/components/styles/local.less +1 -0
  44. package/src/index.less +21 -0
  45. package/src/index.tsx +12 -0
  46. package/src/pages/ModalTable/index.tsx +101 -0
  47. package/src/pages/Table/index.tsx +108 -0
  48. package/src/pages/TableCustomize/index.tsx +79 -0
  49. package/src/routes.tsx +37 -0
  50. package/src/typings.d.ts +46 -0
@@ -0,0 +1,3 @@
1
+ import type { IHeaderProps } from "./modal";
2
+ declare const _default: ({ configData, onFinish, searchSpan, headerStyle, }: IHeaderProps) => JSX.Element;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { HTableProps } from "./modal";
2
+ declare const _default: ({ request, configData, searchSpan, table, hideHeader, headerStyle, tableStyle, action, spaceSize, className, paginationStyle, rowKey, ...props }: HTableProps) => JSX.Element;
3
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import type { Dispatch, SetStateAction } from "react";
2
+ import React from "react";
3
+ import type { ResultModal, RowObj, HTableInstance } from "./modal";
4
+ import type { HTableProps } from "./modal";
5
+ interface HContextModal extends Omit<HTableProps, "request"> {
6
+ tableInstance: HTableInstance;
7
+ data?: ResultModal;
8
+ selectedRowData: RowObj;
9
+ rowOnChange: (keys: React.Key[], rowData: any[]) => void;
10
+ error?: Error;
11
+ loading?: boolean;
12
+ allSelectChange?: (checked?: boolean) => void;
13
+ params?: any;
14
+ onFinish: (value: Record<string, any>) => Promise<any>;
15
+ onPageChange: (value: Record<string, any>) => Promise<any>;
16
+ setSelectedRowData: (row: RowObj) => void;
17
+ rowKey?: string;
18
+ headerOpen?: boolean;
19
+ setHeaderOpen: Dispatch<SetStateAction<boolean | undefined>>;
20
+ }
21
+ export declare const HTableContext: React.Context<HContextModal | null>;
22
+ export declare const useHTableContext: () => HContextModal;
23
+ export {};
@@ -0,0 +1 @@
1
+ export declare const useClassName: (className: string | string[]) => string;
@@ -0,0 +1,11 @@
1
+ import "./index.less";
2
+ export { default as HTable } from "./Table";
3
+ export { default as useHTable } from "./hooks/useHTable";
4
+ export { default as HTableConfig } from "./TableConfig";
5
+ export { default as TableCustomize } from "./TableCustomize";
6
+ export { default as HTableBody } from "./HTableBody";
7
+ export { default as HTableFooter } from "./HTableFooter";
8
+ export { default as HTableHeader } from "./HTableHeader";
9
+ export { default as HTablePagination } from "./HTablePagination";
10
+ export { default as HModalTable } from "./ModalTable";
11
+ export { useHDialogTable } from "./ModalTable/hooks";
@@ -0,0 +1,100 @@
1
+ import type { ProColumns } from "@ant-design/pro-table/lib/typing";
2
+ import type { ProTableProps } from "@ant-design/pro-table";
3
+ import type { HItemProps, HFormInstance } from "@hw-component/form/es/Form/modal";
4
+ import type { ColProps } from "antd";
5
+ import type React from "react";
6
+ import type { ModalProps } from "antd";
7
+ import type { TableProps } from "antd/lib/table";
8
+ import type { AffixProps } from "antd/lib/affix";
9
+ export interface RowObj {
10
+ keys?: React.Key[];
11
+ rowData?: any[];
12
+ selectAll?: boolean;
13
+ }
14
+ export interface ResultModal {
15
+ records: any[];
16
+ current: string;
17
+ size: string;
18
+ total: string;
19
+ }
20
+ export interface ParamsModal extends Record<string, any> {
21
+ size?: number;
22
+ current?: number;
23
+ }
24
+ interface HColumns extends Omit<ProColumns, "render"> {
25
+ render?: (dom: React.ReactNode, data: any, index: number, tableInstance: HTableInstance) => React.ReactNode;
26
+ showSearch?: boolean;
27
+ searchType?: HItemProps["type"];
28
+ searchRender?: HItemProps["render"];
29
+ }
30
+ export type ConfigItemModal = Omit<HItemProps, "render" | "type"> & HColumns;
31
+ export type ConfigDataModal = ConfigItemModal[];
32
+ export type ActionRenderFn = (selectedRowKeys: RowObj, tableInstance: HTableInstance) => React.ReactNode;
33
+ export type FooterBtnRenderFn = (dom: React.ReactNode, total: string, selectedRowKeys: RowObj, setAllCheck: (row: RowObj) => void) => React.ReactNode;
34
+ export type actionFn = (...arg: any[]) => void;
35
+ export interface RowSelectionOuter {
36
+ allPageCheck?: boolean;
37
+ }
38
+ export type HRowSelection = RowSelectionOuter & (TableProps<any>["rowSelection"] & {
39
+ alwaysShowAlert?: boolean;
40
+ });
41
+ export interface HTableProps extends Omit<ProTableProps<any, any>, "request" | "dataSource" | "rowKey" | "rowSelection"> {
42
+ request?: (params: ParamsModal) => Promise<ResultModal>;
43
+ configData: ConfigDataModal;
44
+ searchSpan?: ColProps;
45
+ table?: HTableInstance;
46
+ actionRender?: ActionRenderFn;
47
+ emptyRender?: (tableInstance: HTableInstance) => React.ReactNode;
48
+ errorRender?: (tableInstance: HTableInstance, error: Error) => React.ReactNode;
49
+ hideHeader?: boolean;
50
+ action?: Record<string, actionFn>;
51
+ headerStyle?: React.CSSProperties;
52
+ tableStyle?: React.CSSProperties;
53
+ spaceSize?: number;
54
+ className?: string;
55
+ error?: Error;
56
+ reload?: (params: any) => Promise<any>;
57
+ manual?: boolean;
58
+ dataSource?: ResultModal;
59
+ paginationStyle?: React.CSSProperties;
60
+ rowKey?: string;
61
+ allPageCheck?: boolean;
62
+ rowSelection?: HRowSelection | false;
63
+ affixProps?: AffixProps | false;
64
+ goTop?: boolean;
65
+ }
66
+ export interface TableInstance {
67
+ reload: (params?: ParamsModal) => Promise<any>;
68
+ setSelectedRowData: (keys: React.Key[], data: any) => void;
69
+ dispatch: (key: string, params: any) => void;
70
+ reloadWithParams: (params?: ParamsModal) => Promise<any>;
71
+ getParams: () => any;
72
+ }
73
+ export interface HTableInstance {
74
+ form: HFormInstance;
75
+ table: TableInstance;
76
+ }
77
+ export interface DialogParamsModal {
78
+ configData?: HTableProps["configData"];
79
+ request?: HTableProps["request"];
80
+ params?: Record<string, any>;
81
+ title?: string;
82
+ }
83
+ export interface HOnDiaLogTableInstance {
84
+ show: (params?: DialogParamsModal) => void;
85
+ hide: () => void;
86
+ }
87
+ export interface HDiaLogTableInstance {
88
+ form: HFormInstance;
89
+ table: TableInstance;
90
+ show: (params?: DialogParamsModal) => void;
91
+ hide: () => void;
92
+ }
93
+ export interface ModalTableProps extends ModalProps {
94
+ tableProps?: Omit<HTableProps, "configData" | "request">;
95
+ dialogTable?: HDiaLogTableInstance;
96
+ params?: Record<string, any>;
97
+ configData?: HTableProps["configData"];
98
+ request?: HTableProps["request"];
99
+ }
100
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { IHeaderProps } from "./modal";
2
+ declare const _default: ({ configData, onFinish, searchSpan, headerStyle, }: IHeaderProps) => JSX.Element;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import type { HTableProps } from "./modal";
2
+ declare const _default: ({ request, configData, searchSpan, table, hideHeader, headerStyle, tableStyle, action, spaceSize, className, paginationStyle, rowKey, ...props }: HTableProps) => JSX.Element;
3
+ export default _default;
@@ -0,0 +1,23 @@
1
+ import type { Dispatch, SetStateAction } from "react";
2
+ import React from "react";
3
+ import type { ResultModal, RowObj, HTableInstance } from "./modal";
4
+ import type { HTableProps } from "./modal";
5
+ interface HContextModal extends Omit<HTableProps, "request"> {
6
+ tableInstance: HTableInstance;
7
+ data?: ResultModal;
8
+ selectedRowData: RowObj;
9
+ rowOnChange: (keys: React.Key[], rowData: any[]) => void;
10
+ error?: Error;
11
+ loading?: boolean;
12
+ allSelectChange?: (checked?: boolean) => void;
13
+ params?: any;
14
+ onFinish: (value: Record<string, any>) => Promise<any>;
15
+ onPageChange: (value: Record<string, any>) => Promise<any>;
16
+ setSelectedRowData: (row: RowObj) => void;
17
+ rowKey?: string;
18
+ headerOpen?: boolean;
19
+ setHeaderOpen: Dispatch<SetStateAction<boolean | undefined>>;
20
+ }
21
+ export declare const HTableContext: React.Context<HContextModal | null>;
22
+ export declare const useHTableContext: () => HContextModal;
23
+ export {};
@@ -0,0 +1 @@
1
+ export declare const useClassName: (className: string | string[]) => string;
@@ -0,0 +1,11 @@
1
+ import "./index.less";
2
+ export { default as HTable } from "./Table";
3
+ export { default as useHTable } from "./hooks/useHTable";
4
+ export { default as HTableConfig } from "./TableConfig";
5
+ export { default as TableCustomize } from "./TableCustomize";
6
+ export { default as HTableBody } from "./HTableBody";
7
+ export { default as HTableFooter } from "./HTableFooter";
8
+ export { default as HTableHeader } from "./HTableHeader";
9
+ export { default as HTablePagination } from "./HTablePagination";
10
+ export { default as HModalTable } from "./ModalTable";
11
+ export { useHDialogTable } from "./ModalTable/hooks";
@@ -0,0 +1,100 @@
1
+ import type { ProColumns } from "@ant-design/pro-table/lib/typing";
2
+ import type { ProTableProps } from "@ant-design/pro-table";
3
+ import type { HItemProps, HFormInstance } from "@hw-component/form/es/Form/modal";
4
+ import type { ColProps } from "antd";
5
+ import type React from "react";
6
+ import type { ModalProps } from "antd";
7
+ import type { TableProps } from "antd/lib/table";
8
+ import type { AffixProps } from "antd/lib/affix";
9
+ export interface RowObj {
10
+ keys?: React.Key[];
11
+ rowData?: any[];
12
+ selectAll?: boolean;
13
+ }
14
+ export interface ResultModal {
15
+ records: any[];
16
+ current: string;
17
+ size: string;
18
+ total: string;
19
+ }
20
+ export interface ParamsModal extends Record<string, any> {
21
+ size?: number;
22
+ current?: number;
23
+ }
24
+ interface HColumns extends Omit<ProColumns, "render"> {
25
+ render?: (dom: React.ReactNode, data: any, index: number, tableInstance: HTableInstance) => React.ReactNode;
26
+ showSearch?: boolean;
27
+ searchType?: HItemProps["type"];
28
+ searchRender?: HItemProps["render"];
29
+ }
30
+ export type ConfigItemModal = Omit<HItemProps, "render" | "type"> & HColumns;
31
+ export type ConfigDataModal = ConfigItemModal[];
32
+ export type ActionRenderFn = (selectedRowKeys: RowObj, tableInstance: HTableInstance) => React.ReactNode;
33
+ export type FooterBtnRenderFn = (dom: React.ReactNode, total: string, selectedRowKeys: RowObj, setAllCheck: (row: RowObj) => void) => React.ReactNode;
34
+ export type actionFn = (...arg: any[]) => void;
35
+ export interface RowSelectionOuter {
36
+ allPageCheck?: boolean;
37
+ }
38
+ export type HRowSelection = RowSelectionOuter & (TableProps<any>["rowSelection"] & {
39
+ alwaysShowAlert?: boolean;
40
+ });
41
+ export interface HTableProps extends Omit<ProTableProps<any, any>, "request" | "dataSource" | "rowKey" | "rowSelection"> {
42
+ request?: (params: ParamsModal) => Promise<ResultModal>;
43
+ configData: ConfigDataModal;
44
+ searchSpan?: ColProps;
45
+ table?: HTableInstance;
46
+ actionRender?: ActionRenderFn;
47
+ emptyRender?: (tableInstance: HTableInstance) => React.ReactNode;
48
+ errorRender?: (tableInstance: HTableInstance, error: Error) => React.ReactNode;
49
+ hideHeader?: boolean;
50
+ action?: Record<string, actionFn>;
51
+ headerStyle?: React.CSSProperties;
52
+ tableStyle?: React.CSSProperties;
53
+ spaceSize?: number;
54
+ className?: string;
55
+ error?: Error;
56
+ reload?: (params: any) => Promise<any>;
57
+ manual?: boolean;
58
+ dataSource?: ResultModal;
59
+ paginationStyle?: React.CSSProperties;
60
+ rowKey?: string;
61
+ allPageCheck?: boolean;
62
+ rowSelection?: HRowSelection | false;
63
+ affixProps?: AffixProps | false;
64
+ goTop?: boolean;
65
+ }
66
+ export interface TableInstance {
67
+ reload: (params?: ParamsModal) => Promise<any>;
68
+ setSelectedRowData: (keys: React.Key[], data: any) => void;
69
+ dispatch: (key: string, params: any) => void;
70
+ reloadWithParams: (params?: ParamsModal) => Promise<any>;
71
+ getParams: () => any;
72
+ }
73
+ export interface HTableInstance {
74
+ form: HFormInstance;
75
+ table: TableInstance;
76
+ }
77
+ export interface DialogParamsModal {
78
+ configData?: HTableProps["configData"];
79
+ request?: HTableProps["request"];
80
+ params?: Record<string, any>;
81
+ title?: string;
82
+ }
83
+ export interface HOnDiaLogTableInstance {
84
+ show: (params?: DialogParamsModal) => void;
85
+ hide: () => void;
86
+ }
87
+ export interface HDiaLogTableInstance {
88
+ form: HFormInstance;
89
+ table: TableInstance;
90
+ show: (params?: DialogParamsModal) => void;
91
+ hide: () => void;
92
+ }
93
+ export interface ModalTableProps extends ModalProps {
94
+ tableProps?: Omit<HTableProps, "configData" | "request">;
95
+ dialogTable?: HDiaLogTableInstance;
96
+ params?: Record<string, any>;
97
+ configData?: HTableProps["configData"];
98
+ request?: HTableProps["request"];
99
+ }
100
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hw-component/table",
3
- "version": "0.0.6-beta-v9",
3
+ "version": "0.0.6-beta-v10",
4
4
  "description": "基于antd二次开发table组件",
5
5
  "keywords": [
6
6
  "table"
package/src/Layout.tsx ADDED
@@ -0,0 +1,61 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import { Menu } from "antd";
3
+ import "./index.less";
4
+ import type { RouteModal } from "./routes";
5
+ import routes from "./routes";
6
+ import { Outlet, useNavigate, useLocation } from "react-router-dom";
7
+
8
+ const menuItem = (item: RouteModal) => {
9
+ const { name, path, children } = item;
10
+ if (!path) {
11
+ return null;
12
+ }
13
+ if (children) {
14
+ return (
15
+ <Menu.SubMenu title={name} key={path}>
16
+ {children.map((childItem) => {
17
+ return menuItem(childItem);
18
+ })}
19
+ </Menu.SubMenu>
20
+ );
21
+ }
22
+ return <Menu.Item key={path}>{name}</Menu.Item>;
23
+ };
24
+ export default () => {
25
+ const navigate = useNavigate();
26
+ const { pathname } = useLocation();
27
+ const [keys, setKeys] = useState(["/form/select"]);
28
+ const [openKey, setOpenKey] = useState(["/form"]);
29
+ useEffect(() => {
30
+ setKeys([pathname]);
31
+ const pathOpenKeys = pathname.split("/");
32
+ const openKeys = pathOpenKeys.map((path) => {
33
+ return "/" + path;
34
+ });
35
+ setOpenKey(openKeys);
36
+ }, [pathname]);
37
+ return (
38
+ <div className="layout">
39
+ <Menu
40
+ className="menu"
41
+ selectedKeys={keys}
42
+ openKeys={openKey}
43
+ onOpenChange={(openKeys) => {
44
+ setOpenKey(openKeys);
45
+ }}
46
+ onSelect={(e) => {
47
+ navigate(e.key);
48
+ }}
49
+ mode={"inline"}
50
+ theme="dark"
51
+ >
52
+ {routes.map((item) => {
53
+ return menuItem(item);
54
+ })}
55
+ </Menu>
56
+ <div className="body">
57
+ <Outlet />
58
+ </div>
59
+ </div>
60
+ );
61
+ };
package/src/app.tsx ADDED
@@ -0,0 +1,33 @@
1
+ import React from "react";
2
+ import routes from "./routes";
3
+ import {
4
+ createBrowserRouter,
5
+ Navigate,
6
+ RouterProvider,
7
+ useRouteError,
8
+ } from "react-router-dom";
9
+ import type { RouteObject } from "react-router";
10
+ import Layout from "./Layout";
11
+ const Error = () => {
12
+ const error = useRouteError();
13
+ if (error.status === 404) {
14
+ return <Navigate to="/form/select" replace />;
15
+ }
16
+ return <div>错误</div>;
17
+ };
18
+ const rootRoute = [
19
+ {
20
+ path: "/",
21
+ element: <Layout />,
22
+ errorElement: <Error />,
23
+ children: routes,
24
+ },
25
+ ];
26
+ const router = () => {
27
+ return createBrowserRouter(rootRoute as RouteObject[]);
28
+ };
29
+ export default () => {
30
+ return (
31
+ <RouterProvider router={router()} fallbackElement={<p>Loading...</p>} />
32
+ );
33
+ };
Binary file
@@ -0,0 +1,37 @@
1
+ import type { IPaginationProps } from "../HTablePagination/index";
2
+ import { useClassName } from "../hooks";
3
+ import { useEffect, useState } from "react";
4
+ export default ({ affixProps }: IPaginationProps) => {
5
+ const className = useClassName("hw-top-btn");
6
+ const { target } = affixProps || {};
7
+ const [style, setStyle] = useState({ display: "none" });
8
+ const dom = target?.();
9
+ const cuDom = dom || document.body;
10
+ const goTopFn = () => {
11
+ cuDom.scrollTo({ top: 0 });
12
+ };
13
+ useEffect(() => {
14
+ const scroll = (e) => {
15
+ const top = e.target.scrollTop;
16
+ if (top > 10) {
17
+ setStyle({ display: "block" });
18
+ return;
19
+ }
20
+ setStyle({ display: "none" });
21
+ };
22
+ cuDom.addEventListener("scroll", scroll);
23
+ return () => {
24
+ cuDom.removeEventListener("scroll", scroll);
25
+ };
26
+ }, [dom]);
27
+ useEffect(() => {
28
+ const top = (cuDom as HTMLElement).scrollTop;
29
+ if (top > 10) {
30
+ setStyle({ display: "block" });
31
+ return;
32
+ }
33
+ setStyle({ display: "none" });
34
+ }, []);
35
+
36
+ return <div className={className} style={style} onClick={goTopFn} />;
37
+ };
@@ -0,0 +1,37 @@
1
+ import { Button, Row, Typography } from "antd";
2
+ import React from "react";
3
+ import { useHTableContext } from "../context";
4
+ import type { ActionRenderFn } from "../modal";
5
+ const { Text, Link } = Typography;
6
+ interface IProps {
7
+ actionRender?: ActionRenderFn;
8
+ }
9
+ export default ({ actionRender }: IProps) => {
10
+ const { data, selectedRowData, rowOnChange, tableInstance } =
11
+ useHTableContext();
12
+ const { total = "0" } = data || {};
13
+ const { keys = [], selectAll } = selectedRowData;
14
+ const totalNum = Number.parseInt(total, 10);
15
+ const showTotalNum = Number.isNaN(totalNum) ? 0 : totalNum;
16
+ const num = selectAll ? showTotalNum : keys.length;
17
+
18
+ return (
19
+ <Row justify={"space-between"} align={"middle"}>
20
+ <Text>
21
+ 已选择<Link>{num}</Link>条
22
+ </Text>
23
+ <div>
24
+ {actionRender?.(selectedRowData, tableInstance)}
25
+ <Button
26
+ type={"link"}
27
+ size={"small"}
28
+ onClick={() => {
29
+ rowOnChange([], []);
30
+ }}
31
+ >
32
+ 取消选择
33
+ </Button>
34
+ </div>
35
+ </Row>
36
+ );
37
+ };
@@ -0,0 +1,143 @@
1
+ import { useHTableContext } from "../context";
2
+ import { Checkbox, Dropdown, Menu } from "antd";
3
+ import { useMemo } from "react";
4
+ import type { HRowSelection } from "../modal";
5
+ export const RowSelectionTitle = ({
6
+ allPageCheck = true,
7
+ onChange,
8
+ getCheckboxProps,
9
+ }: HRowSelection) => {
10
+ const {
11
+ data,
12
+ selectedRowData,
13
+ rowOnChange,
14
+ allSelectChange,
15
+ rowKey = "id",
16
+ } = useHTableContext();
17
+ const { records = [] } = data || {};
18
+ const newData = records?.filter((item) => {
19
+ const { disabled = false } = getCheckboxProps?.(item) || {};
20
+ return !disabled;
21
+ });
22
+ const { keys, selectAll } = selectedRowData;
23
+ const allCheck = () => {
24
+ const setKeys = newData.map((item) => {
25
+ return item[rowKey];
26
+ });
27
+ rowOnChange(setKeys, newData);
28
+ onChange?.(setKeys, newData);
29
+ };
30
+ const allCancel = () => {
31
+ rowOnChange([], []);
32
+ onChange?.([], []);
33
+ };
34
+ const checkChange = (e) => {
35
+ const checked = e.target.checked;
36
+ if (checked) {
37
+ allCheck();
38
+ return;
39
+ }
40
+ allCancel();
41
+ };
42
+ const change = ({ key }) => {
43
+ if (key === "check") {
44
+ allCheck();
45
+ return;
46
+ }
47
+ if (key === "cancel") {
48
+ allCancel();
49
+ return;
50
+ }
51
+ if (key === "checkAll") {
52
+ allSelectChange?.(true);
53
+ onChange?.([], []);
54
+ return;
55
+ }
56
+ allSelectChange?.(false);
57
+ onChange?.([], []);
58
+ };
59
+ const len = keys?.length || 0;
60
+ const dataLen = newData?.length || 0;
61
+ const checked = len === dataLen && len !== 0;
62
+ const configItems = useMemo(() => {
63
+ const fsArray = checked
64
+ ? {
65
+ label: "取消此页数据",
66
+ key: "cancel",
67
+ }
68
+ : {
69
+ label: "选择此页数据",
70
+ key: "check",
71
+ };
72
+ const enArray = selectAll
73
+ ? {
74
+ label: "取消全部数据",
75
+ key: "cancelAll",
76
+ }
77
+ : {
78
+ label: "选择全部数据",
79
+ key: "checkAll",
80
+ };
81
+ return [fsArray, enArray];
82
+ }, [checked, selectAll]);
83
+
84
+ const menu = () => {
85
+ const MenuEle = Menu as any;
86
+ return <MenuEle onClick={change} items={configItems} />;
87
+ };
88
+ const disabled = dataLen === 0;
89
+ if (allPageCheck) {
90
+ return (
91
+ <Dropdown overlay={menu} arrow placement={"bottom"} disabled={disabled}>
92
+ <Checkbox
93
+ checked={checked}
94
+ onChange={checkChange}
95
+ disabled={disabled}
96
+ />
97
+ </Dropdown>
98
+ );
99
+ }
100
+ return (
101
+ <Checkbox checked={checked} onChange={checkChange} disabled={disabled} />
102
+ );
103
+ };
104
+
105
+ interface RowSelectionBoxProps {
106
+ data: any;
107
+ onChange?: HRowSelection["onChange"];
108
+ getCheckboxProps?: HRowSelection["getCheckboxProps"];
109
+ }
110
+ export const RowSelectionBox = ({
111
+ data,
112
+ onChange,
113
+ getCheckboxProps,
114
+ }: RowSelectionBoxProps) => {
115
+ const { selectedRowData, rowOnChange, rowKey = "id" } = useHTableContext();
116
+ const { rowData = [], keys = [] } = selectedRowData;
117
+ const key = data[rowKey];
118
+ const check = (e) => {
119
+ const checked = e.target.checked;
120
+ const newKeys = [...keys];
121
+ const newRowData = [...rowData];
122
+ if (checked) {
123
+ newKeys.push(key);
124
+ newRowData.push(data);
125
+ rowOnChange(newKeys, newRowData);
126
+ onChange?.(newKeys, newRowData);
127
+ return;
128
+ }
129
+ const index = newKeys.indexOf(key);
130
+ newKeys.splice(index, 1);
131
+ newRowData.splice(index, 1);
132
+ rowOnChange(newKeys, newRowData);
133
+ onChange?.(newKeys, newRowData);
134
+ };
135
+ const { disabled = false } = getCheckboxProps?.(data) || {};
136
+ return (
137
+ <Checkbox
138
+ checked={keys.indexOf(key) !== -1}
139
+ onChange={check}
140
+ disabled={disabled}
141
+ />
142
+ );
143
+ };