@zat-design/sisyphus-react 3.6.5-beta.2 → 3.6.5-beta.3

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.
@@ -1,11 +1,12 @@
1
- import { Form, FormInstance } from 'antd';
1
+ import { FormInstance } from 'antd';
2
2
  import React, { ForwardRefRenderFunction } from 'react';
3
3
  import { FormProviderProps } from 'antd/es/form/context';
4
4
  import { ProFormProps } from './propsType';
5
5
  import { useForm } from './utils/useForm';
6
+ import useWatch from './utils/useWatch';
6
7
  interface IProForm<T = any> extends ForwardRefRenderFunction<FormInstance<T>, ProFormProps<T>> {
7
8
  useForm: typeof useForm;
8
- useWatch: typeof Form.useWatch;
9
+ useWatch: typeof useWatch;
9
10
  [key: string]: any;
10
11
  }
11
12
  declare const ProFormForward: IProForm;
@@ -16,7 +16,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
16
16
  import { DoubleLeftOutlined } from '@ant-design/icons';
17
17
  import classnames from 'classnames';
18
18
  import React, { forwardRef, useImperativeHandle, useMemo } from 'react';
19
- import { isObject, isString, merge } from 'lodash';
19
+ import { isObject, merge } from 'lodash';
20
20
  import { FormFooter, InputRange, ProAddress, ProCascader, ProCertNo, ProCertValidity, ProCombination, ProModalSelect, ProNumberRange, ProRangeBox, ProTimeLimit, transferAddressInfoToRegion, ProUpload, ProTreeModal, ProTree, EnumSelect } from './components';
21
21
  import RenderFields from './components/render/RenderFields';
22
22
  import { useProConfig, useContextForms } from '../ProConfigProvider';
@@ -24,6 +24,7 @@ import { getLayout, splitNameStr, useControlled, initialValuesToNames, filterInt
24
24
  import { useForm } from './utils/useForm';
25
25
  import { useFieldProps } from './utils/useFieldProps';
26
26
  import locale from '../locale';
27
+ import useWatch from './utils/useWatch';
27
28
  var ProForm = function ProForm(props, ref) {
28
29
  var _forms$formKey, _localStorage, _ref;
29
30
  var _props$mode = props.mode,
@@ -273,22 +274,7 @@ var ProForm = function ProForm(props, ref) {
273
274
  // @ts-ignore
274
275
  var ProFormForward = /*#__PURE__*/forwardRef(ProForm);
275
276
  ProFormForward.useForm = useForm;
276
- ProFormForward.useWatch = function (watchValue, form) {
277
- if (isString(watchValue)) {
278
- var watchValues = [watchValue];
279
- watchValues.forEach(function (item) {
280
- _Form.useWatch(item, form);
281
- });
282
- return form.getFieldsValue(watchValues);
283
- }
284
- if (watchValue.length) {
285
- watchValue.forEach(function (item) {
286
- _Form.useWatch(item, form);
287
- });
288
- return form.getFieldsValue(watchValue);
289
- }
290
- return _Form.useWatch([], form);
291
- };
277
+ ProFormForward.useWatch = useWatch;
292
278
  export var ProFormProvider = function ProFormProvider(props) {
293
279
  var onFormChange = props.onFormChange,
294
280
  onFormFinish = props.onFormFinish,
@@ -0,0 +1,7 @@
1
+ import { InternalNamePath, FormInstance, NamePath } from 'rc-field-form/es/interface';
2
+ export declare function toArray<T>(value?: T | T[] | null): T[];
3
+ export declare function getNamePath(path: NamePath | null): InternalNamePath;
4
+ export declare function stringify(value: any): string | number;
5
+ declare function useWatch(dependencies: string, form?: FormInstance, wait?: number): any;
6
+ declare function useWatch(dependencies: NamePath[], form?: FormInstance, wait?: number): any[];
7
+ export default useWatch;
@@ -0,0 +1,80 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import { debounce, get } from 'lodash';
3
+ import { HOOK_MARK } from 'rc-field-form/es/FieldContext';
4
+ import warning from 'rc-util/lib/warning';
5
+ import { useState, useEffect, useRef, useMemo } from 'react';
6
+ export function toArray(value) {
7
+ if (value === undefined || value === null) {
8
+ return [];
9
+ }
10
+ return Array.isArray(value) ? value : [value];
11
+ }
12
+ export function getNamePath(path) {
13
+ return toArray(path);
14
+ }
15
+ export function stringify(value) {
16
+ try {
17
+ return JSON.stringify(value);
18
+ } catch (err) {
19
+ return Math.random();
20
+ }
21
+ }
22
+ // 创建之后namepath就不可变
23
+ var useWatchWarning = process.env.NODE_ENV !== 'production' ? function (namePath) {
24
+ var fullyStr = JSON.stringify(namePath);
25
+ var nameStrRef = useRef(fullyStr);
26
+ warning(nameStrRef.current === fullyStr, '`useWatch` is not support dynamic `namePath`. Please provide static instead.');
27
+ } : function () {};
28
+ function useWatch(dependencies, form, wait) {
29
+ var _useState = useState(typeof dependencies === 'string' ? undefined : []),
30
+ _useState2 = _slicedToArray(_useState, 2),
31
+ value = _useState2[0],
32
+ setValue = _useState2[1];
33
+ var valueStr = useMemo(function () {
34
+ return stringify(value);
35
+ }, [value]);
36
+ var valueStrRef = useRef(valueStr);
37
+ valueStrRef.current = valueStr;
38
+ var formInstance = form;
39
+ var isValidForm = formInstance && formInstance._init;
40
+ var _dependencies = typeof dependencies === 'string' ? [dependencies] : dependencies;
41
+ var namePaths = _dependencies === null || _dependencies === void 0 ? void 0 : _dependencies.map(function (item) {
42
+ return getNamePath(item);
43
+ });
44
+ var namePathsRef = useRef(namePaths);
45
+ namePathsRef.current = namePaths;
46
+ useWatchWarning(namePaths);
47
+ useEffect(function () {
48
+ if (!isValidForm) return;
49
+ var getFieldsValue = formInstance.getFieldsValue,
50
+ getInternalHooks = formInstance.getInternalHooks;
51
+ var _getInternalHooks = getInternalHooks(HOOK_MARK),
52
+ registerWatch = _getInternalHooks.registerWatch;
53
+ var callback = function callback(store) {
54
+ var newValue = namePathsRef.current.map(function (namePath) {
55
+ return get(store, namePath);
56
+ });
57
+ var nextValueStr = stringify(newValue);
58
+ // Compare stringify in case it's nest object
59
+ if (valueStrRef.current !== nextValueStr) {
60
+ valueStrRef.current = nextValueStr;
61
+ setValue(newValue);
62
+ }
63
+ };
64
+ // 增加防抖
65
+ if (wait) {
66
+ callback = debounce(callback, wait);
67
+ }
68
+ var cancelRegister = registerWatch(callback);
69
+ var initialValue = namePathsRef.current.map(function (namePath) {
70
+ return get(getFieldsValue(), namePath);
71
+ });
72
+ setValue(initialValue);
73
+ return cancelRegister;
74
+ },
75
+ // We do not need re-register since namePath content is the same
76
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
+ [isValidForm]);
78
+ return value;
79
+ }
80
+ export default useWatch;
@@ -1,11 +1,12 @@
1
- import { Form, FormInstance } from 'antd';
1
+ import { FormInstance } from 'antd';
2
2
  import React, { ForwardRefRenderFunction } from 'react';
3
3
  import { FormProviderProps } from 'antd/es/form/context';
4
4
  import { ProFormProps } from './propsType';
5
5
  import { useForm } from './utils/useForm';
6
+ import useWatch from './utils/useWatch';
6
7
  interface IProForm<T = any> extends ForwardRefRenderFunction<FormInstance<T>, ProFormProps<T>> {
7
8
  useForm: typeof useForm;
8
- useWatch: typeof Form.useWatch;
9
+ useWatch: typeof useWatch;
9
10
  [key: string]: any;
10
11
  }
11
12
  declare const ProFormForward: IProForm;
@@ -24,6 +24,7 @@ var _index = require("./utils/index");
24
24
  var _useForm3 = require("./utils/useForm");
25
25
  var _useFieldProps = require("./utils/useFieldProps");
26
26
  var _locale = _interopRequireDefault(require("../locale"));
27
+ var _useWatch = _interopRequireDefault(require("./utils/useWatch"));
27
28
  var _excluded = ["mode", "span", "disabled", "isView", "columns", "footer", "onOk", "okText", "onCancel", "confirmLoading", "cancelText", "form", "children", "rowProps", "className", "expand", "expandOpen", "expandOpenChange", "viewEmpty", "labelAlign", "labelWidth", "onValuesChange", "onFinish", "originalValues", "submitOnEnter", "clearNotShow", "initialValues", "requiredOnView", "formId", "required", "originalDiffTip", "formKey", "globalControl", "scrollToError", "optimize"];
28
29
  var ProForm = function ProForm(props, ref) {
29
30
  var _forms$formKey, _localStorage, _ref;
@@ -274,22 +275,7 @@ var ProForm = function ProForm(props, ref) {
274
275
  // @ts-ignore
275
276
  var ProFormForward = /*#__PURE__*/(0, _react.forwardRef)(ProForm);
276
277
  ProFormForward.useForm = _useForm3.useForm;
277
- ProFormForward.useWatch = function (watchValue, form) {
278
- if ((0, _lodash.isString)(watchValue)) {
279
- var watchValues = [watchValue];
280
- watchValues.forEach(function (item) {
281
- _antd.Form.useWatch(item, form);
282
- });
283
- return form.getFieldsValue(watchValues);
284
- }
285
- if (watchValue.length) {
286
- watchValue.forEach(function (item) {
287
- _antd.Form.useWatch(item, form);
288
- });
289
- return form.getFieldsValue(watchValue);
290
- }
291
- return _antd.Form.useWatch([], form);
292
- };
278
+ ProFormForward.useWatch = _useWatch.default;
293
279
  var ProFormProvider = exports.ProFormProvider = function ProFormProvider(props) {
294
280
  var onFormChange = props.onFormChange,
295
281
  onFormFinish = props.onFormFinish,
@@ -0,0 +1,7 @@
1
+ import { InternalNamePath, FormInstance, NamePath } from 'rc-field-form/es/interface';
2
+ export declare function toArray<T>(value?: T | T[] | null): T[];
3
+ export declare function getNamePath(path: NamePath | null): InternalNamePath;
4
+ export declare function stringify(value: any): string | number;
5
+ declare function useWatch(dependencies: string, form?: FormInstance, wait?: number): any;
6
+ declare function useWatch(dependencies: NamePath[], form?: FormInstance, wait?: number): any[];
7
+ export default useWatch;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ exports.getNamePath = getNamePath;
9
+ exports.stringify = stringify;
10
+ exports.toArray = toArray;
11
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
12
+ var _lodash = require("lodash");
13
+ var _FieldContext = require("rc-field-form/es/FieldContext");
14
+ var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
15
+ var _react = require("react");
16
+ function toArray(value) {
17
+ if (value === undefined || value === null) {
18
+ return [];
19
+ }
20
+ return Array.isArray(value) ? value : [value];
21
+ }
22
+ function getNamePath(path) {
23
+ return toArray(path);
24
+ }
25
+ function stringify(value) {
26
+ try {
27
+ return JSON.stringify(value);
28
+ } catch (err) {
29
+ return Math.random();
30
+ }
31
+ }
32
+ // 创建之后namepath就不可变
33
+ var useWatchWarning = process.env.NODE_ENV !== 'production' ? function (namePath) {
34
+ var fullyStr = JSON.stringify(namePath);
35
+ var nameStrRef = (0, _react.useRef)(fullyStr);
36
+ (0, _warning.default)(nameStrRef.current === fullyStr, '`useWatch` is not support dynamic `namePath`. Please provide static instead.');
37
+ } : function () {};
38
+ function useWatch(dependencies, form, wait) {
39
+ var _useState = (0, _react.useState)(typeof dependencies === 'string' ? undefined : []),
40
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
41
+ value = _useState2[0],
42
+ setValue = _useState2[1];
43
+ var valueStr = (0, _react.useMemo)(function () {
44
+ return stringify(value);
45
+ }, [value]);
46
+ var valueStrRef = (0, _react.useRef)(valueStr);
47
+ valueStrRef.current = valueStr;
48
+ var formInstance = form;
49
+ var isValidForm = formInstance && formInstance._init;
50
+ var _dependencies = typeof dependencies === 'string' ? [dependencies] : dependencies;
51
+ var namePaths = _dependencies === null || _dependencies === void 0 ? void 0 : _dependencies.map(function (item) {
52
+ return getNamePath(item);
53
+ });
54
+ var namePathsRef = (0, _react.useRef)(namePaths);
55
+ namePathsRef.current = namePaths;
56
+ useWatchWarning(namePaths);
57
+ (0, _react.useEffect)(function () {
58
+ if (!isValidForm) return;
59
+ var getFieldsValue = formInstance.getFieldsValue,
60
+ getInternalHooks = formInstance.getInternalHooks;
61
+ var _getInternalHooks = getInternalHooks(_FieldContext.HOOK_MARK),
62
+ registerWatch = _getInternalHooks.registerWatch;
63
+ var callback = function callback(store) {
64
+ var newValue = namePathsRef.current.map(function (namePath) {
65
+ return (0, _lodash.get)(store, namePath);
66
+ });
67
+ var nextValueStr = stringify(newValue);
68
+ // Compare stringify in case it's nest object
69
+ if (valueStrRef.current !== nextValueStr) {
70
+ valueStrRef.current = nextValueStr;
71
+ setValue(newValue);
72
+ }
73
+ };
74
+ // 增加防抖
75
+ if (wait) {
76
+ callback = (0, _lodash.debounce)(callback, wait);
77
+ }
78
+ var cancelRegister = registerWatch(callback);
79
+ var initialValue = namePathsRef.current.map(function (namePath) {
80
+ return (0, _lodash.get)(getFieldsValue(), namePath);
81
+ });
82
+ setValue(initialValue);
83
+ return cancelRegister;
84
+ },
85
+ // We do not need re-register since namePath content is the same
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
87
+ [isValidForm]);
88
+ return value;
89
+ }
90
+ var _default = exports.default = useWatch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zat-design/sisyphus-react",
3
- "version": "3.6.5-beta.2",
3
+ "version": "3.6.5-beta.3",
4
4
  "license": "Apache-2.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",