@hw-component/form 0.0.9-beta-v21 → 0.0.9-beta-v22

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 (111) hide show
  1. package/package.json +1 -1
  2. package/src/Layout.tsx +61 -0
  3. package/src/app.tsx +33 -0
  4. package/src/components/Cascader/index.tsx +31 -0
  5. package/src/components/CheckboxGroup/CheckBox/index.tsx +38 -0
  6. package/src/components/CheckboxGroup/hooks.ts +105 -0
  7. package/src/components/CheckboxGroup/index.tsx +85 -0
  8. package/src/components/CheckboxGroup/modal.ts +17 -0
  9. package/src/components/DialogForm/DrawerForm/Footer.tsx +20 -0
  10. package/src/components/DialogForm/DrawerForm/Title.tsx +12 -0
  11. package/src/components/DialogForm/DrawerForm/index.tsx +110 -0
  12. package/src/components/DialogForm/ModalForm.tsx +91 -0
  13. package/src/components/DialogForm/hooks.ts +122 -0
  14. package/src/components/DialogForm/modal.ts +44 -0
  15. package/src/components/Form/Context/FormConfigProvider.tsx +38 -0
  16. package/src/components/Form/Context/index.tsx +14 -0
  17. package/src/components/Form/FormItem/BasicItem.tsx +95 -0
  18. package/src/components/Form/FormItem/Helper.tsx +25 -0
  19. package/src/components/Form/FormItem/RegularFormItem.tsx +13 -0
  20. package/src/components/Form/FormItem/UpFormItem.tsx +21 -0
  21. package/src/components/Form/FormItem/hooks.tsx +54 -0
  22. package/src/components/Form/FormItem/index.tsx +8 -0
  23. package/src/components/Form/HFormConnect.tsx +74 -0
  24. package/src/components/Form/InitSet.tsx +10 -0
  25. package/src/components/Form/Label.tsx +50 -0
  26. package/src/components/Form/config.ts +58 -0
  27. package/src/components/Form/hooks/index.ts +114 -0
  28. package/src/components/Form/hooks/useDefaultRender.tsx +45 -0
  29. package/src/components/Form/hooks/useHForm.ts +182 -0
  30. package/src/components/Form/hooks/useInitConfigData.tsx +194 -0
  31. package/src/components/Form/index.less +54 -0
  32. package/src/components/Form/index.tsx +113 -0
  33. package/src/components/Form/modal.ts +206 -0
  34. package/src/components/Input/ButtonInput.tsx +47 -0
  35. package/src/components/Input/ColorInput/Picker.tsx +18 -0
  36. package/src/components/Input/ColorInput/data.ts +102 -0
  37. package/src/components/Input/ColorInput/index.less +10 -0
  38. package/src/components/Input/ColorInput/index.tsx +58 -0
  39. package/src/components/Input/InputNumber.tsx +5 -0
  40. package/src/components/Input/SelectInput.tsx +75 -0
  41. package/src/components/Input/TrimInput.tsx +26 -0
  42. package/src/components/Input/VerificationCodeInput.tsx +74 -0
  43. package/src/components/Input/defaultConfig.ts +8 -0
  44. package/src/components/Input/index.tsx +26 -0
  45. package/src/components/Input/modal.ts +40 -0
  46. package/src/components/PageHandler/ErrorComponent.tsx +20 -0
  47. package/src/components/PageHandler/LoadingComponent.tsx +9 -0
  48. package/src/components/PageHandler/index.tsx +14 -0
  49. package/src/components/PageHandler/modal.ts +6 -0
  50. package/src/components/RadioGroup/index.tsx +45 -0
  51. package/src/components/Select/components/AllSelect.tsx +54 -0
  52. package/src/components/Select/components/CheckBoxOption.tsx +14 -0
  53. package/src/components/Select/components/DropdownComponent.tsx +36 -0
  54. package/src/components/Select/components/NoFindItem.tsx +7 -0
  55. package/src/components/Select/components/NotFoundContent.tsx +25 -0
  56. package/src/components/Select/defaultConfig.tsx +23 -0
  57. package/src/components/Select/hooks/changeHooks.tsx +159 -0
  58. package/src/components/Select/hooks/norHooks.ts +85 -0
  59. package/src/components/Select/index.less +22 -0
  60. package/src/components/Select/index.tsx +126 -0
  61. package/src/components/Select/modal.ts +36 -0
  62. package/src/components/Select/utils.ts +18 -0
  63. package/src/components/Submit/components.tsx +57 -0
  64. package/src/components/Submit/hooks.ts +31 -0
  65. package/src/components/Submit/index.tsx +33 -0
  66. package/src/components/Switch/index.tsx +77 -0
  67. package/src/components/TDPicker/RangePicker.tsx +109 -0
  68. package/src/components/TDPicker/TimePicker.tsx +29 -0
  69. package/src/components/TDPicker/hooks.ts +150 -0
  70. package/src/components/TDPicker/index.tsx +33 -0
  71. package/src/components/TDPicker/modal.ts +48 -0
  72. package/src/components/TextArea/TrimTextArea.tsx +30 -0
  73. package/src/components/TextArea/index.tsx +8 -0
  74. package/src/components/Upload/Btn.tsx +24 -0
  75. package/src/components/Upload/MediaTypeEle/TypeEle.tsx +26 -0
  76. package/src/components/Upload/MediaTypeEle/index.tsx +34 -0
  77. package/src/components/Upload/Preview/index.tsx +14 -0
  78. package/src/components/Upload/UrlUpload/index.tsx +104 -0
  79. package/src/components/Upload/enums.ts +5 -0
  80. package/src/components/Upload/hooks/change.ts +79 -0
  81. package/src/components/Upload/hooks/customRequest.ts +87 -0
  82. package/src/components/Upload/hooks/propsMaker.ts +20 -0
  83. package/src/components/Upload/index.tsx +119 -0
  84. package/src/components/Upload/modal.ts +33 -0
  85. package/src/components/Upload/util.ts +27 -0
  86. package/src/components/config.ts +47 -0
  87. package/src/components/hooks/index.ts +53 -0
  88. package/src/components/index.tsx +37 -0
  89. package/src/components/modal.ts +20 -0
  90. package/src/components/styles/index.less +3 -0
  91. package/src/components/styles/local.less +1 -0
  92. package/src/components/typings.d.ts +11 -0
  93. package/src/index.less +20 -0
  94. package/src/index.tsx +12 -0
  95. package/src/pages/Checkbox/index.tsx +14 -0
  96. package/src/pages/DatePicker/index.tsx +33 -0
  97. package/src/pages/DrawerForm/index.tsx +145 -0
  98. package/src/pages/Form/index.tsx +172 -0
  99. package/src/pages/Input/index.tsx +30 -0
  100. package/src/pages/ModalForm/index.tsx +191 -0
  101. package/src/pages/Radio/index.tsx +18 -0
  102. package/src/pages/Select/index.tsx +63 -0
  103. package/src/pages/Switch/index.tsx +18 -0
  104. package/src/pages/Upload/index.tsx +14 -0
  105. package/src/routes.tsx +80 -0
  106. package/.husky/pre-commit +0 -4
  107. package/.idea/hw-component-form.iml +0 -12
  108. package/.idea/inspectionProfiles/Project_Default.xml +0 -7
  109. package/.idea/misc.xml +0 -6
  110. package/.idea/modules.xml +0 -8
  111. package/.idea/vcs.xml +0 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hw-component/form",
3
- "version": "0.0.9-beta-v21",
3
+ "version": "0.0.9-beta-v22",
4
4
  "description": "基于antd二次开发",
5
5
  "repository": {
6
6
  "type": "git",
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
+ };
@@ -0,0 +1,31 @@
1
+ import type { CascaderProps } from "antd";
2
+ import { Cascader } from "antd";
3
+ import { useRequest } from "ahooks";
4
+ import { useMatchConfigProps } from "../hooks";
5
+ import type { PromiseFnResult } from "../modal";
6
+ export type HCascaderProps = {
7
+ request?: PromiseFnResult<any>;
8
+ } & CascaderProps<any>;
9
+
10
+ export default ({
11
+ request,
12
+ options = [],
13
+ fieldNames: propsFieldNames,
14
+ ...props
15
+ }: HCascaderProps) => {
16
+ const { fieldNames } = useMatchConfigProps({ fieldNames: propsFieldNames });
17
+ const { loading, data } = useRequest((params = {}) => {
18
+ if (!request) {
19
+ return Promise.resolve(options);
20
+ }
21
+ return request(params);
22
+ });
23
+ return (
24
+ <Cascader
25
+ {...props}
26
+ loading={loading}
27
+ options={data}
28
+ fieldNames={fieldNames}
29
+ />
30
+ );
31
+ };
@@ -0,0 +1,38 @@
1
+ import { Checkbox } from "antd";
2
+ import React, { useMemo } from "react";
3
+ import type { CheckboxChangeEvent } from "antd/es/checkbox";
4
+ import type { HCheckboxProps } from "@/components/CheckboxGroup/modal";
5
+ import { useMatchConfigProps } from "../../hooks";
6
+
7
+ const Index: React.FC<HCheckboxProps> = ({
8
+ value,
9
+ onChange,
10
+ children,
11
+ valueMap: propsValueCheckMap,
12
+ }) => {
13
+ const { valueCheckMap = {} } = useMatchConfigProps({
14
+ valueCheckMap: propsValueCheckMap,
15
+ }); //匹配全局
16
+ const checkedVal = useMemo(() => {
17
+ const { checked } = valueCheckMap;
18
+ if (value === checked) {
19
+ return true;
20
+ }
21
+ return false;
22
+ }, [value]);
23
+ const change = (e: CheckboxChangeEvent) => {
24
+ const { checked, noChecked } = valueCheckMap;
25
+ const val = e.target.checked;
26
+ let subVal = checked;
27
+ if (!val) {
28
+ subVal = noChecked;
29
+ }
30
+ onChange?.(subVal);
31
+ };
32
+ return (
33
+ <Checkbox checked={checkedVal} onChange={change}>
34
+ {children}
35
+ </Checkbox>
36
+ );
37
+ };
38
+ export default Index;
@@ -0,0 +1,105 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+ import type { HCheckboxProps } from "@/components/CheckboxGroup/modal";
3
+
4
+ const changValProvider = ({
5
+ value,
6
+ options,
7
+ valueMap = {},
8
+ }: HCheckboxProps) => {
9
+ const newVal = { ...value };
10
+ const { checked, noChecked } = valueMap;
11
+ options?.forEach((item) => {
12
+ const { value: itemVal } = item;
13
+ if (newVal[itemVal] !== checked) {
14
+ newVal[itemVal] = noChecked;
15
+ }
16
+ });
17
+ return newVal;
18
+ };
19
+
20
+ export const useValueProvider = ({
21
+ value,
22
+ valueMap = {},
23
+ keyInValue = true,
24
+ onChange,
25
+ options,
26
+ }: HCheckboxProps) => {
27
+ const [val, setVal] = useState<Record<any, any>>({});
28
+ const oldValueCheckMap = useMemo(() => {
29
+ return {
30
+ valueMap,
31
+ };
32
+ }, []); //存储老的map
33
+ useEffect(() => {
34
+ setVal((oldVal) => {
35
+ return changValProvider({
36
+ value: oldVal,
37
+ options,
38
+ valueMap: oldValueCheckMap.valueMap,
39
+ });
40
+ });
41
+ }, [options]); //初始化数据
42
+ useEffect(() => {
43
+ const { checked: oldChecked } = oldValueCheckMap.valueMap;
44
+ const { checked, noChecked } = valueMap;
45
+ setVal((oldVal) => {
46
+ const newVal = { ...oldVal };
47
+ const keys = Object.keys(newVal);
48
+ keys.forEach((key) => {
49
+ if (newVal[key] === oldChecked) {
50
+ newVal[key] = checked;
51
+ return;
52
+ }
53
+ newVal[key] = noChecked;
54
+ });
55
+ oldValueCheckMap.valueMap = valueMap;
56
+ return newVal;
57
+ });
58
+ }, [valueMap]);
59
+
60
+ useEffect(() => {
61
+ const { checked, noChecked } = oldValueCheckMap.valueMap;
62
+ if (!value) {
63
+ return;
64
+ }
65
+ if (!Array.isArray(value)) {
66
+ setVal(value);
67
+ return;
68
+ }
69
+ setVal((oldVal) => {
70
+ const newVal = { ...oldVal };
71
+ Object.keys(newVal).forEach((key) => {
72
+ newVal[key] = noChecked;
73
+ });
74
+ value.forEach((itemVal) => {
75
+ newVal[itemVal] = checked;
76
+ });
77
+ return newVal;
78
+ });
79
+ }, [value]);
80
+
81
+ const change = (key: any, changeVal: any) => {
82
+ const { checked, noChecked } = valueMap;
83
+ const newVal = { ...val };
84
+ newVal[key] = noChecked;
85
+ if (changeVal === checked) {
86
+ newVal[key] = checked;
87
+ }
88
+ if (!onChange) {
89
+ setVal(newVal);
90
+ return;
91
+ }
92
+ if (keyInValue) {
93
+ onChange(newVal);
94
+ return;
95
+ }
96
+ const subVal = Object.keys(newVal).filter((valKey) => {
97
+ return newVal[valKey] === checked;
98
+ });
99
+ onChange(subVal);
100
+ };
101
+ return {
102
+ val,
103
+ change,
104
+ };
105
+ };
@@ -0,0 +1,85 @@
1
+ import { Space } from "antd";
2
+ import type { HCheckboxProps } from "@/components/CheckboxGroup/modal";
3
+ import { useValueProvider } from "./hooks";
4
+ import Checkbox from "./CheckBox";
5
+ import { useMatchConfigProps, useChangeOptions } from "../hooks";
6
+ import HFormConnect from "../Form/HFormConnect";
7
+ import React from "react";
8
+
9
+ const Index = ({
10
+ options,
11
+ fieldNames,
12
+ onChange,
13
+ valueMap,
14
+ value,
15
+ disabled,
16
+ keyInValue = true,
17
+ addFormat,
18
+ direction,
19
+ }: HCheckboxProps) => {
20
+ const { valueCheckMap: resultValueCheckMap, fieldNames: resultFieldNames } =
21
+ useMatchConfigProps({ fieldNames, valueCheckMap: valueMap }); //匹配全局
22
+ const checkOptions = useChangeOptions({
23
+ options,
24
+ fieldNames: resultFieldNames,
25
+ });
26
+ const { val, change } = useValueProvider({
27
+ options: checkOptions,
28
+ onChange,
29
+ valueMap: resultValueCheckMap,
30
+ keyInValue,
31
+ value,
32
+ });
33
+ addFormat?.({
34
+ float: {
35
+ inputValue: (item, initValue) => {
36
+ const { name = "" } = item;
37
+ const keys = checkOptions?.map((optItem) => {
38
+ return optItem.value;
39
+ });
40
+ const resultObj = {};
41
+ keys?.forEach((key) => {
42
+ resultObj[key] = initValue[key];
43
+ });
44
+ return {
45
+ [name]: resultObj,
46
+ };
47
+ },
48
+ outputValue: (item, outputValue) => {
49
+ const { name = "" } = item;
50
+ const { [name]: itemVal = {} } = outputValue;
51
+ if (Array.isArray(itemVal)) {
52
+ return {
53
+ [name]: itemVal,
54
+ };
55
+ }
56
+ return {
57
+ ...itemVal,
58
+ };
59
+ },
60
+ },
61
+ });
62
+ return (
63
+ <Space direction={direction}>
64
+ {checkOptions?.map((item) => {
65
+ const { label, value: itemVal } = item;
66
+ const checked = val[itemVal];
67
+ return (
68
+ <Checkbox
69
+ value={checked}
70
+ key={itemVal}
71
+ onChange={(changeVal) => {
72
+ change(itemVal, changeVal);
73
+ }}
74
+ valueMap={valueMap}
75
+ disabled={disabled}
76
+ >
77
+ {label}
78
+ </Checkbox>
79
+ );
80
+ })}
81
+ </Space>
82
+ );
83
+ };
84
+
85
+ export default HFormConnect(Index);
@@ -0,0 +1,17 @@
1
+ import type { CheckboxProps } from "antd/lib/checkbox/Checkbox";
2
+ import type { SelectProps } from "antd";
3
+ import type { addFormatItemModal } from "../Form/modal";
4
+ import type { ValueCheckMapModal } from "../modal";
5
+
6
+ export interface HCheckboxProps
7
+ extends Omit<CheckboxProps, "value" | "onChange"> {
8
+ options?: Record<string, any>[];
9
+ fieldNames?: SelectProps["fieldNames"];
10
+ valueMap?: ValueCheckMapModal;
11
+ keyInValue?: boolean;
12
+ value?: Record<string, any> | any[];
13
+ onChange?: (value: Record<string, any> | any[]) => void;
14
+ addFormat?: (config: Record<string, addFormatItemModal>) => void;
15
+ subParams?: (formatValue: Record<string, any>, sourceValue: any[]) => any;
16
+ direction?: "horizontal" | "vertical";
17
+ }
@@ -0,0 +1,20 @@
1
+ import { Row, Space, Button } from "antd";
2
+
3
+ interface IProps {
4
+ onOk?: VoidFunction;
5
+ onCancel?: VoidFunction;
6
+ confirmLoading?: boolean;
7
+ }
8
+
9
+ export default ({ onCancel, onOk, confirmLoading }: IProps) => {
10
+ return (
11
+ <Row justify={"end"}>
12
+ <Space size={"middle"}>
13
+ <Button onClick={onCancel}>关闭</Button>
14
+ <Button type={"primary"} onClick={onOk} loading={confirmLoading}>
15
+ 确定
16
+ </Button>
17
+ </Space>
18
+ </Row>
19
+ );
20
+ };
@@ -0,0 +1,12 @@
1
+ import type { ModalProps } from "antd";
2
+ import { Row } from "antd";
3
+ import { CloseOutlined } from "@ant-design/icons";
4
+
5
+ export default ({ title, closable, onCancel }: ModalProps) => {
6
+ return (
7
+ <Row justify={"space-between"}>
8
+ <div>{title}</div>
9
+ {closable && <CloseOutlined onClick={onCancel} />}
10
+ </Row>
11
+ );
12
+ };
@@ -0,0 +1,110 @@
1
+ import { Drawer } from "antd";
2
+ import type { DialogFormProps } from "../modal";
3
+ import { useCurrentForm, useModifyProps, useSub } from "../hooks";
4
+ import HForm from "../../Form";
5
+ import Title from "./Title";
6
+ import Footer from "./Footer";
7
+ import FormConfigProvider, {
8
+ useFormConfigContext,
9
+ } from "../../Form/Context/FormConfigProvider";
10
+ export default ({
11
+ visible,
12
+ title,
13
+ onCancel,
14
+ configData,
15
+ infoRequest,
16
+ request,
17
+ afterClose,
18
+ dialogForm,
19
+ closable = true,
20
+ initialValues,
21
+ labelWidth,
22
+ onOk,
23
+ onFinish,
24
+ size,
25
+ form,
26
+ footer,
27
+ params,
28
+ onValuesChange,
29
+ autoClear = true,
30
+ contentRender,
31
+ ...props
32
+ }: DialogFormProps) => {
33
+ const currentForm = useCurrentForm(dialogForm);
34
+ const providerConfig = useFormConfigContext();
35
+ const {
36
+ modalVisible,
37
+ modalFormData,
38
+ setModalVisible,
39
+ initValue,
40
+ onAfterClose,
41
+ formParams,
42
+ title: modalTitle,
43
+ } = useModifyProps({
44
+ configData,
45
+ visible,
46
+ initialValues,
47
+ dialogForm: currentForm,
48
+ params,
49
+ title,
50
+ onCancel,
51
+ autoClear,
52
+ });
53
+ const cancel = () => {
54
+ onAfterClose();
55
+ if (onCancel) {
56
+ return onCancel?.();
57
+ }
58
+ setModalVisible(false);
59
+ };
60
+ const { loading, run } = useSub({ request, onFinish });
61
+ const footerComponent = () => {
62
+ if (footer === null) {
63
+ return null;
64
+ }
65
+ return (
66
+ footer || (
67
+ <Footer
68
+ onCancel={cancel}
69
+ onOk={currentForm.submit}
70
+ confirmLoading={loading}
71
+ />
72
+ )
73
+ );
74
+ };
75
+ const node = (
76
+ <HForm
77
+ configData={modalFormData}
78
+ initialValues={initValue}
79
+ onValuesChange={onValuesChange}
80
+ {...props}
81
+ form={currentForm}
82
+ params={formParams}
83
+ onFinish={async (values, subParams) => {
84
+ const result = await run(values, subParams);
85
+ const close = onOk?.(result, subParams);
86
+ if (close === false) {
87
+ return;
88
+ }
89
+ cancel();
90
+ }}
91
+ infoRequest={infoRequest}
92
+ labelWidth={labelWidth}
93
+ />
94
+ );
95
+ return (
96
+ <Drawer
97
+ visible={modalVisible}
98
+ title={<Title title={modalTitle} closable={closable} onCancel={cancel} />}
99
+ {...props}
100
+ onClose={cancel}
101
+ closable={false}
102
+ destroyOnClose={true}
103
+ footer={footerComponent()}
104
+ >
105
+ <FormConfigProvider {...providerConfig}>
106
+ {contentRender ? contentRender(node, currentForm) : node}
107
+ </FormConfigProvider>
108
+ </Drawer>
109
+ );
110
+ };
@@ -0,0 +1,91 @@
1
+ import { Modal } from "antd";
2
+ import type { DialogFormProps } from "./modal";
3
+ import HForm from "../Form";
4
+ import FormConfigProvider, {
5
+ useFormConfigContext,
6
+ } from "../Form/Context/FormConfigProvider";
7
+ import { useCurrentForm, useModifyProps, useSub } from "./hooks";
8
+ export default ({
9
+ visible,
10
+ title,
11
+ onCancel,
12
+ configData,
13
+ infoRequest,
14
+ request,
15
+ afterClose,
16
+ dialogForm,
17
+ initialValues,
18
+ onFinish,
19
+ params,
20
+ onOk,
21
+ onValuesChange,
22
+ autoClear = true,
23
+ contentRender,
24
+ ...props
25
+ }: DialogFormProps) => {
26
+ const currentForm = useCurrentForm(dialogForm);
27
+ const providerConfig = useFormConfigContext();
28
+ const {
29
+ modalVisible,
30
+ modalFormData,
31
+ setModalVisible,
32
+ initValue,
33
+ formParams,
34
+ title: modalTitle,
35
+ } = useModifyProps({
36
+ configData,
37
+ visible,
38
+ initialValues,
39
+ dialogForm: currentForm,
40
+ params,
41
+ title,
42
+ onCancel,
43
+ });
44
+ const cancel = () => {
45
+ if (onCancel) {
46
+ return onCancel?.();
47
+ }
48
+ setModalVisible(false);
49
+ };
50
+ const { loading, run } = useSub({ request, onFinish });
51
+ const node = (
52
+ <HForm
53
+ configData={modalFormData}
54
+ initialValues={initValue}
55
+ onValuesChange={onValuesChange}
56
+ onFinish={async (values, outParams) => {
57
+ const result = await run(values, outParams);
58
+ const close = onOk?.(result, outParams);
59
+ if (close === false) {
60
+ return;
61
+ }
62
+ cancel();
63
+ }}
64
+ {...props}
65
+ params={formParams}
66
+ form={currentForm}
67
+ infoRequest={infoRequest}
68
+ />
69
+ );
70
+ return (
71
+ <Modal
72
+ title={modalTitle}
73
+ visible={modalVisible}
74
+ onCancel={cancel}
75
+ confirmLoading={loading}
76
+ afterClose={() => {
77
+ if (autoClear) {
78
+ currentForm.clear();
79
+ }
80
+ afterClose?.();
81
+ }}
82
+ {...props}
83
+ onOk={currentForm.submit}
84
+ destroyOnClose={true}
85
+ >
86
+ <FormConfigProvider {...providerConfig}>
87
+ {contentRender ? contentRender?.(node, currentForm) : node}
88
+ </FormConfigProvider>
89
+ </Modal>
90
+ );
91
+ };