iglooform 3.2.3 → 3.2.4

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.
@@ -114,7 +114,7 @@ var Element = function Element(_ref) {
114
114
  registerDependencies(dependentField, dispatch);
115
115
  }, []);
116
116
  invariant(type !== 'Pages' && type !== 'Page', "".concat(type, " should not be inner form item"));
117
- var _useComponent = useComponent(config),
117
+ var _useComponent = useComponent(config, parentName),
118
118
  Component = _useComponent.Component,
119
119
  extraConfig = _useComponent.formItemProps,
120
120
  withoutForm = _useComponent.withoutForm;
@@ -1,5 +1,6 @@
1
+ import { NamePath } from 'antd/es/form/interface';
1
2
  import { FormItemConfig, FormItemExtraConfig } from '../types';
2
- type UseComponentHook = (config: FormItemConfig) => {
3
+ type UseComponentHook = (config: FormItemConfig, parentName: NamePath) => {
3
4
  Component: any;
4
5
  formItemProps: FormItemExtraConfig;
5
6
  withoutForm: boolean;
@@ -1,20 +1,31 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ var _excluded = ["onChange"];
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
7
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
8
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
9
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
1
10
  import { useMemo, useContext } from 'react';
2
11
  import Button from "../button";
3
- import Radio, { RadioGroup, RadioGroupWithOther } from "../radio";
4
- import Input, { PhoneNumber, Amount, Email, Password, InputNumber, TextArea, InputDate, CreditCard, Otp, ExpiryDate, InputId } from "../input";
5
- import DatePicker, { IglooBuddhistDatePicker as BuddhistDatePicker, IglooRangePicker as RangePicker, IglooWeekPicker as WeekPicker, IglooMonthPicker as MonthPicker } from "../date-picker";
12
+ import Checkbox, { CheckboxGroup } from "../checkbox";
6
13
  import Confirmation from "../confirmation";
14
+ import DatePicker, { IglooBuddhistDatePicker as BuddhistDatePicker, IglooRangePicker as RangePicker, IglooWeekPicker as WeekPicker, IglooMonthPicker as MonthPicker } from "../date-picker";
15
+ import Declaration from "../declaration";
16
+ import formContext from "../form-context";
17
+ import Input, { PhoneNumber, Amount, Email, Password, InputNumber, TextArea, InputDate, CreditCard, Otp, ExpiryDate, InputId } from "../input";
18
+ import OCR from "../ocr";
19
+ import Radio, { RadioGroup, RadioGroupWithOther } from "../radio";
20
+ import SearchBox from "../search-box";
7
21
  import { AttachedSelect as Select, TimeSelect } from "../select";
8
- import Checkbox, { CheckboxGroup } from "../checkbox";
22
+ import Typography from "../typography";
9
23
  import Upload from "../upload";
10
- import Declaration from "../declaration";
11
- import Divider from "./divider";
12
24
  import UploadPhoto from "../upload-photo";
25
+ import Divider from "./divider";
13
26
  import RenderElement from "./render";
14
- import Typography from "../typography";
15
- import SearchBox from "../search-box";
16
- import formContext from "../form-context";
17
- import OCR from "../ocr";
27
+ import useWrapperOnChangeForAutomaticCalc from "../hooks/useWrapperOnChangeForAutomaticCalc";
28
+ import { jsx as _jsx } from "react/jsx-runtime";
18
29
  var elementMap = {
19
30
  Input: Input,
20
31
  PhoneNumber: PhoneNumber,
@@ -53,12 +64,13 @@ var elementMap = {
53
64
  var Unknown = function Unknown() {
54
65
  return 'Unknown Form Item';
55
66
  };
56
- var useComponent = function useComponent(config) {
67
+ var useComponent = function useComponent(config, parentName) {
57
68
  var _useContext = useContext(formContext),
58
69
  _useContext$customize = _useContext.customizeComponents,
59
70
  customizeComponents = _useContext$customize === void 0 ? {} : _useContext$customize;
60
71
  var type = config.type,
61
- render = config.render;
72
+ render = config.render,
73
+ enableAutomaticCalc = config.enableAutomaticCalc;
62
74
  if (!type && !render) {
63
75
  return {
64
76
  Component: Unknown,
@@ -69,8 +81,16 @@ var useComponent = function useComponent(config) {
69
81
  return useMemo(function () {
70
82
  var Component = type ? elementMap[type] || customizeComponents[type] || Unknown : RenderElement;
71
83
  var formItemProps = typeof Component.formItemPropsHandler === 'function' ? Component.formItemPropsHandler(config) : {};
84
+ var WrapperComponent = function WrapperComponent(_ref) {
85
+ var onChange = _ref.onChange,
86
+ props = _objectWithoutProperties(_ref, _excluded);
87
+ var handleOnChange = useWrapperOnChangeForAutomaticCalc(config, parentName, onChange);
88
+ return /*#__PURE__*/_jsx(Component, _objectSpread(_objectSpread({}, props), {}, {
89
+ onChange: handleOnChange
90
+ }));
91
+ };
72
92
  return {
73
- Component: Component,
93
+ Component: enableAutomaticCalc ? WrapperComponent : Component,
74
94
  formItemProps: formItemProps,
75
95
  withoutForm: type === 'Divider' || Boolean(config.withoutForm) || config.name === undefined
76
96
  };
@@ -0,0 +1,5 @@
1
+ import { FormItemConfig } from '../types';
2
+ import { NamePath } from 'antd/es/form/interface';
3
+ type UseWrapperOnChangeForAutomaticCalcHook = (config: FormItemConfig, parentName: NamePath, onChange: any) => (value: any, automaticCalc?: boolean) => void;
4
+ declare const useWrapperOnChangeForAutomaticCalc: UseWrapperOnChangeForAutomaticCalcHook;
5
+ export default useWrapperOnChangeForAutomaticCalc;
@@ -0,0 +1,26 @@
1
+ import { useUpdateEffect } from '@umijs/hooks';
2
+ import { useContext } from 'react';
3
+ import { isEqual } from 'lodash';
4
+ import { calcAutomaticCalculate } from "../utils/form-utils";
5
+ import formContext from "../form-context";
6
+ var useWrapperOnChangeForAutomaticCalc = function useWrapperOnChangeForAutomaticCalc(config, parentName, onChange) {
7
+ var _useContext = useContext(formContext),
8
+ form = _useContext.form;
9
+ var name = config.name,
10
+ enableAutomaticCalc = config.enableAutomaticCalc,
11
+ automaticCalcCode = config.automaticCalcCode;
12
+ var handleOnChange = function handleOnChange(value) {
13
+ var automaticCalc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
14
+ if (automaticCalc && isEqual(form === null || form === void 0 ? void 0 : form.getFieldValue(name), value)) {
15
+ return;
16
+ }
17
+ onChange(value);
18
+ };
19
+ useUpdateEffect(function () {
20
+ if (form && enableAutomaticCalc && automaticCalcCode) {
21
+ calcAutomaticCalculate(name, parentName, automaticCalcCode, form, handleOnChange);
22
+ }
23
+ });
24
+ return handleOnChange;
25
+ };
26
+ export default useWrapperOnChangeForAutomaticCalc;
@@ -74,6 +74,6 @@ export declare const calcSelectValue: (selectValue?: FormItemSelectValue[], form
74
74
  disabled: boolean | undefined;
75
75
  };
76
76
  export declare const calcShouldRender: (name: NamePath, parentName: NamePath, dependencies?: NamePath[], asserts?: FormItemAssert[], shouldRender?: ((form?: FormInstance) => boolean) | undefined, shouldRenderCode?: string, form?: FormInstance) => any;
77
- export declare const calcAutomaticCalculate: (name: NamePath, parentName: NamePath, automaticCalcCode?: string, form?: FormInstance) => undefined;
77
+ export declare const calcAutomaticCalculate: (name: NamePath, parentName: NamePath, automaticCalcCode?: string, form?: FormInstance, onChange?: ((value: any, automaticCalc?: boolean) => any) | undefined) => undefined;
78
78
  export declare const getRuleValidation: (url: string, rule: string | string[], values?: any, lang?: string) => Promise<any>;
79
79
  export declare const calcAddableSectionAssertField: (name: any[], field: FormItemAssert['field'], index: number, elements: FormItemConfig[] | React.ReactElement[]) => any;
@@ -226,7 +226,7 @@ export var calcFormItemProps = function calcFormItemProps(config, extraProps, fo
226
226
  Object.keys(_objectSpread(_objectSpread({}, extraRest), rest)).forEach(function (key) {
227
227
  elementProps[key.replace('element-', '')] = rest[key];
228
228
  });
229
- elementProps.disabled = calcDisabled(disabled, form, disabledCode, name, parentName) || copiedValue.disabled || selectedValue.disabled;
229
+ elementProps.disabled = enableAutomaticCalc || calcDisabled(disabled, form, disabledCode, name, parentName) || copiedValue.disabled || selectedValue.disabled;
230
230
  if (dateLimitationType) {
231
231
  var rangeStart;
232
232
  var rangeEnd;
@@ -322,9 +322,6 @@ export var calcFormItemProps = function calcFormItemProps(config, extraProps, fo
322
322
  };
323
323
  })));
324
324
  }
325
- if (enableAutomaticCalc) {
326
- calcAutomaticCalculate(name, parentName, automaticCalcCode, form);
327
- }
328
325
  return {
329
326
  colProps: {
330
327
  span: extraFullRow && fullRow === undefined || fullRow || extraHalfRow && halfRow === undefined || halfRow ? 24 : span || extraSpan || 12,
@@ -387,9 +384,9 @@ export var calcDisabled = function calcDisabled(disabled, form, disabledCode, na
387
384
  var fn = eval(disabledCode);
388
385
  if (typeof fn === 'function') {
389
386
  try {
390
- var value = form.getFieldValue(name);
387
+ var _value = form.getFieldValue(name);
391
388
  var values = form.getFieldsValue(true);
392
- return fn(value, values, {
389
+ return fn(_value, values, {
393
390
  moment: moment,
394
391
  get: get
395
392
  }, {
@@ -454,12 +451,12 @@ export var calcSelectValue = function calcSelectValue(selectValue, form) {
454
451
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
455
452
  var select = _step2.value;
456
453
  var assert = select.assert,
457
- value = select.value,
454
+ _value2 = select.value,
458
455
  disabled = select.disabled;
459
456
  if (testAssert(form, assert)) {
460
457
  return {
461
458
  selected: true,
462
- value: value,
459
+ value: _value2,
463
460
  disabled: disabled
464
461
  };
465
462
  }
@@ -501,9 +498,9 @@ export var calcShouldRender = function calcShouldRender(name, parentName) {
501
498
  var fn = eval(shouldRenderCode);
502
499
  if (typeof fn === 'function') {
503
500
  try {
504
- var value = form.getFieldValue(name);
501
+ var _value3 = form.getFieldValue(name);
505
502
  var values = form.getFieldsValue(true);
506
- return fn(value, values, {
503
+ return fn(_value3, values, {
507
504
  moment: moment,
508
505
  get: get
509
506
  }, {
@@ -530,16 +527,16 @@ export var calcShouldRender = function calcShouldRender(name, parentName) {
530
527
  }
531
528
  return true;
532
529
  };
533
- export var calcAutomaticCalculate = function calcAutomaticCalculate(name, parentName, automaticCalcCode, form) {
530
+ export var calcAutomaticCalculate = function calcAutomaticCalculate(name, parentName, automaticCalcCode, form, onChange) {
534
531
  if (!form) return undefined;
535
532
  if (automaticCalcCode) {
536
533
  var calcValue = undefined;
537
534
  var fn = eval(automaticCalcCode);
538
535
  if (typeof fn === 'function') {
539
536
  try {
540
- var value = form.getFieldValue(name);
537
+ var _value4 = form.getFieldValue(name);
541
538
  var values = form.getFieldsValue(true);
542
- calcValue = fn(value, values, {
539
+ calcValue = fn(_value4, values, {
543
540
  moment: moment,
544
541
  get: get
545
542
  }, {
@@ -548,10 +545,7 @@ export var calcAutomaticCalculate = function calcAutomaticCalculate(name, parent
548
545
  });
549
546
  } catch (_unused4) {}
550
547
  }
551
- form.setFields([{
552
- name: name,
553
- value: calcValue
554
- }]);
548
+ onChange === null || onChange === void 0 ? void 0 : onChange(calcValue, true);
555
549
  }
556
550
  };
557
551
  export var getRuleValidation = /*#__PURE__*/function () {
@@ -120,7 +120,7 @@ var Element = function Element(_ref) {
120
120
  registerDependencies(dependentField, dispatch);
121
121
  }, []);
122
122
  (0, _invariant.default)(type !== 'Pages' && type !== 'Page', "".concat(type, " should not be inner form item"));
123
- var _useComponent = (0, _elements.default)(config),
123
+ var _useComponent = (0, _elements.default)(config, parentName),
124
124
  Component = _useComponent.Component,
125
125
  extraConfig = _useComponent.formItemProps,
126
126
  withoutForm = _useComponent.withoutForm;
@@ -1,5 +1,6 @@
1
+ import { NamePath } from 'antd/es/form/interface';
1
2
  import { FormItemConfig, FormItemExtraConfig } from '../types';
2
- type UseComponentHook = (config: FormItemConfig) => {
3
+ type UseComponentHook = (config: FormItemConfig, parentName: NamePath) => {
3
4
  Component: any;
4
5
  formItemProps: FormItemExtraConfig;
5
6
  withoutForm: boolean;
@@ -7,24 +7,34 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.default = void 0;
8
8
  var _react = require("react");
9
9
  var _button = _interopRequireDefault(require("../button"));
10
- var _radio = _interopRequireWildcard(require("../radio"));
11
- var _input = _interopRequireWildcard(require("../input"));
12
- var _datePicker = _interopRequireWildcard(require("../date-picker"));
10
+ var _checkbox = _interopRequireWildcard(require("../checkbox"));
13
11
  var _confirmation = _interopRequireDefault(require("../confirmation"));
12
+ var _datePicker = _interopRequireWildcard(require("../date-picker"));
13
+ var _declaration = _interopRequireDefault(require("../declaration"));
14
+ var _formContext = _interopRequireDefault(require("../form-context"));
15
+ var _input = _interopRequireWildcard(require("../input"));
16
+ var _ocr = _interopRequireDefault(require("../ocr"));
17
+ var _radio = _interopRequireWildcard(require("../radio"));
18
+ var _searchBox = _interopRequireDefault(require("../search-box"));
14
19
  var _select = require("../select");
15
- var _checkbox = _interopRequireWildcard(require("../checkbox"));
20
+ var _typography = _interopRequireDefault(require("../typography"));
16
21
  var _upload = _interopRequireDefault(require("../upload"));
17
- var _declaration = _interopRequireDefault(require("../declaration"));
18
- var _divider = _interopRequireDefault(require("./divider"));
19
22
  var _uploadPhoto = _interopRequireDefault(require("../upload-photo"));
23
+ var _divider = _interopRequireDefault(require("./divider"));
20
24
  var _render = _interopRequireDefault(require("./render"));
21
- var _typography = _interopRequireDefault(require("../typography"));
22
- var _searchBox = _interopRequireDefault(require("../search-box"));
23
- var _formContext = _interopRequireDefault(require("../form-context"));
24
- var _ocr = _interopRequireDefault(require("../ocr"));
25
+ var _useWrapperOnChangeForAutomaticCalc = _interopRequireDefault(require("../hooks/useWrapperOnChangeForAutomaticCalc"));
26
+ var _jsxRuntime = require("react/jsx-runtime");
27
+ var _excluded = ["onChange"];
25
28
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
26
29
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
27
30
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
32
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
33
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
34
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
35
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
36
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
37
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
28
38
  var elementMap = {
29
39
  Input: _input.default,
30
40
  PhoneNumber: _input.PhoneNumber,
@@ -63,12 +73,13 @@ var elementMap = {
63
73
  var Unknown = function Unknown() {
64
74
  return 'Unknown Form Item';
65
75
  };
66
- var useComponent = function useComponent(config) {
76
+ var useComponent = function useComponent(config, parentName) {
67
77
  var _useContext = (0, _react.useContext)(_formContext.default),
68
78
  _useContext$customize = _useContext.customizeComponents,
69
79
  customizeComponents = _useContext$customize === void 0 ? {} : _useContext$customize;
70
80
  var type = config.type,
71
- render = config.render;
81
+ render = config.render,
82
+ enableAutomaticCalc = config.enableAutomaticCalc;
72
83
  if (!type && !render) {
73
84
  return {
74
85
  Component: Unknown,
@@ -79,8 +90,16 @@ var useComponent = function useComponent(config) {
79
90
  return (0, _react.useMemo)(function () {
80
91
  var Component = type ? elementMap[type] || customizeComponents[type] || Unknown : _render.default;
81
92
  var formItemProps = typeof Component.formItemPropsHandler === 'function' ? Component.formItemPropsHandler(config) : {};
93
+ var WrapperComponent = function WrapperComponent(_ref) {
94
+ var onChange = _ref.onChange,
95
+ props = _objectWithoutProperties(_ref, _excluded);
96
+ var handleOnChange = (0, _useWrapperOnChangeForAutomaticCalc.default)(config, parentName, onChange);
97
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _objectSpread(_objectSpread({}, props), {}, {
98
+ onChange: handleOnChange
99
+ }));
100
+ };
82
101
  return {
83
- Component: Component,
102
+ Component: enableAutomaticCalc ? WrapperComponent : Component,
84
103
  formItemProps: formItemProps,
85
104
  withoutForm: type === 'Divider' || Boolean(config.withoutForm) || config.name === undefined
86
105
  };
@@ -0,0 +1,5 @@
1
+ import { FormItemConfig } from '../types';
2
+ import { NamePath } from 'antd/es/form/interface';
3
+ type UseWrapperOnChangeForAutomaticCalcHook = (config: FormItemConfig, parentName: NamePath, onChange: any) => (value: any, automaticCalc?: boolean) => void;
4
+ declare const useWrapperOnChangeForAutomaticCalc: UseWrapperOnChangeForAutomaticCalcHook;
5
+ export default useWrapperOnChangeForAutomaticCalc;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _hooks = require("@umijs/hooks");
8
+ var _react = require("react");
9
+ var _lodash = require("lodash");
10
+ var _formUtils = require("../utils/form-utils");
11
+ var _formContext = _interopRequireDefault(require("../form-context"));
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ var useWrapperOnChangeForAutomaticCalc = function useWrapperOnChangeForAutomaticCalc(config, parentName, onChange) {
14
+ var _useContext = (0, _react.useContext)(_formContext.default),
15
+ form = _useContext.form;
16
+ var name = config.name,
17
+ enableAutomaticCalc = config.enableAutomaticCalc,
18
+ automaticCalcCode = config.automaticCalcCode;
19
+ var handleOnChange = function handleOnChange(value) {
20
+ var automaticCalc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
21
+ if (automaticCalc && (0, _lodash.isEqual)(form === null || form === void 0 ? void 0 : form.getFieldValue(name), value)) {
22
+ return;
23
+ }
24
+ onChange(value);
25
+ };
26
+ (0, _hooks.useUpdateEffect)(function () {
27
+ if (form && enableAutomaticCalc && automaticCalcCode) {
28
+ (0, _formUtils.calcAutomaticCalculate)(name, parentName, automaticCalcCode, form, handleOnChange);
29
+ }
30
+ });
31
+ return handleOnChange;
32
+ };
33
+ var _default = useWrapperOnChangeForAutomaticCalc;
34
+ exports.default = _default;
@@ -74,6 +74,6 @@ export declare const calcSelectValue: (selectValue?: FormItemSelectValue[], form
74
74
  disabled: boolean | undefined;
75
75
  };
76
76
  export declare const calcShouldRender: (name: NamePath, parentName: NamePath, dependencies?: NamePath[], asserts?: FormItemAssert[], shouldRender?: ((form?: FormInstance) => boolean) | undefined, shouldRenderCode?: string, form?: FormInstance) => any;
77
- export declare const calcAutomaticCalculate: (name: NamePath, parentName: NamePath, automaticCalcCode?: string, form?: FormInstance) => undefined;
77
+ export declare const calcAutomaticCalculate: (name: NamePath, parentName: NamePath, automaticCalcCode?: string, form?: FormInstance, onChange?: ((value: any, automaticCalc?: boolean) => any) | undefined) => undefined;
78
78
  export declare const getRuleValidation: (url: string, rule: string | string[], values?: any, lang?: string) => Promise<any>;
79
79
  export declare const calcAddableSectionAssertField: (name: any[], field: FormItemAssert['field'], index: number, elements: FormItemConfig[] | React.ReactElement[]) => any;
@@ -234,7 +234,7 @@ var calcFormItemProps = function calcFormItemProps(config, extraProps, form, par
234
234
  Object.keys(_objectSpread(_objectSpread({}, extraRest), rest)).forEach(function (key) {
235
235
  elementProps[key.replace('element-', '')] = rest[key];
236
236
  });
237
- elementProps.disabled = calcDisabled(disabled, form, disabledCode, name, parentName) || copiedValue.disabled || selectedValue.disabled;
237
+ elementProps.disabled = enableAutomaticCalc || calcDisabled(disabled, form, disabledCode, name, parentName) || copiedValue.disabled || selectedValue.disabled;
238
238
  if (dateLimitationType) {
239
239
  var rangeStart;
240
240
  var rangeEnd;
@@ -330,9 +330,6 @@ var calcFormItemProps = function calcFormItemProps(config, extraProps, form, par
330
330
  };
331
331
  })));
332
332
  }
333
- if (enableAutomaticCalc) {
334
- calcAutomaticCalculate(name, parentName, automaticCalcCode, form);
335
- }
336
333
  return {
337
334
  colProps: {
338
335
  span: extraFullRow && fullRow === undefined || fullRow || extraHalfRow && halfRow === undefined || halfRow ? 24 : span || extraSpan || 12,
@@ -396,9 +393,9 @@ var calcDisabled = function calcDisabled(disabled, form, disabledCode, name, par
396
393
  var fn = eval(disabledCode);
397
394
  if (typeof fn === 'function') {
398
395
  try {
399
- var value = form.getFieldValue(name);
396
+ var _value = form.getFieldValue(name);
400
397
  var values = form.getFieldsValue(true);
401
- return fn(value, values, {
398
+ return fn(_value, values, {
402
399
  moment: _dayjs.default,
403
400
  get: _lodash.get
404
401
  }, {
@@ -465,12 +462,12 @@ var calcSelectValue = function calcSelectValue(selectValue, form) {
465
462
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
466
463
  var select = _step2.value;
467
464
  var assert = select.assert,
468
- value = select.value,
465
+ _value2 = select.value,
469
466
  disabled = select.disabled;
470
467
  if (testAssert(form, assert)) {
471
468
  return {
472
469
  selected: true,
473
- value: value,
470
+ value: _value2,
474
471
  disabled: disabled
475
472
  };
476
473
  }
@@ -513,9 +510,9 @@ var calcShouldRender = function calcShouldRender(name, parentName) {
513
510
  var fn = eval(shouldRenderCode);
514
511
  if (typeof fn === 'function') {
515
512
  try {
516
- var value = form.getFieldValue(name);
513
+ var _value3 = form.getFieldValue(name);
517
514
  var values = form.getFieldsValue(true);
518
- return fn(value, values, {
515
+ return fn(_value3, values, {
519
516
  moment: _dayjs.default,
520
517
  get: _lodash.get
521
518
  }, {
@@ -543,16 +540,16 @@ var calcShouldRender = function calcShouldRender(name, parentName) {
543
540
  return true;
544
541
  };
545
542
  exports.calcShouldRender = calcShouldRender;
546
- var calcAutomaticCalculate = function calcAutomaticCalculate(name, parentName, automaticCalcCode, form) {
543
+ var calcAutomaticCalculate = function calcAutomaticCalculate(name, parentName, automaticCalcCode, form, onChange) {
547
544
  if (!form) return undefined;
548
545
  if (automaticCalcCode) {
549
546
  var calcValue = undefined;
550
547
  var fn = eval(automaticCalcCode);
551
548
  if (typeof fn === 'function') {
552
549
  try {
553
- var value = form.getFieldValue(name);
550
+ var _value4 = form.getFieldValue(name);
554
551
  var values = form.getFieldsValue(true);
555
- calcValue = fn(value, values, {
552
+ calcValue = fn(_value4, values, {
556
553
  moment: _dayjs.default,
557
554
  get: _lodash.get
558
555
  }, {
@@ -561,10 +558,7 @@ var calcAutomaticCalculate = function calcAutomaticCalculate(name, parentName, a
561
558
  });
562
559
  } catch (_unused4) {}
563
560
  }
564
- form.setFields([{
565
- name: name,
566
- value: calcValue
567
- }]);
561
+ onChange === null || onChange === void 0 ? void 0 : onChange(calcValue, true);
568
562
  }
569
563
  };
570
564
  exports.calcAutomaticCalculate = calcAutomaticCalculate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iglooform",
3
- "version": "3.2.3",
3
+ "version": "3.2.4",
4
4
  "scripts": {
5
5
  "start": "dumi dev",
6
6
  "build-dev": "dumi build",
package/es/types.d.ts DELETED
@@ -1,187 +0,0 @@
1
- /// <reference path="../typings.d.ts" />
2
- import { BlinkIdSingleSideRecognizerResult } from '@microblink/blinkid-in-browser-sdk';
3
- import { FormItemProps, FormInstance } from 'antd/es/form';
4
- import { ReactNode } from 'react';
5
- import { NamePath } from 'rc-field-form/lib/interface';
6
- import { CheckboxOptionType } from 'antd/es/checkbox';
7
- import { SelectProps } from 'antd/es/select';
8
- export { Rule } from 'rc-field-form/lib/interface';
9
- export interface OCRHooks {
10
- microBlink?: {
11
- initResult: {
12
- error?: string;
13
- failed: boolean;
14
- };
15
- recognize: ((file: File) => Promise<BlinkIdSingleSideRecognizerResult | null>) | null;
16
- loading?: boolean;
17
- };
18
- }
19
- export interface OCRConfig {
20
- microBlink?: {
21
- licenseKey: string;
22
- };
23
- }
24
- export type FormItemName = NamePath;
25
- export interface FormBasicConfig {
26
- config: FormItemConfig;
27
- className?: string;
28
- style?: Object;
29
- locales?: {
30
- cancelText?: string;
31
- submitText?: string;
32
- previewText?: string;
33
- nextText?: string;
34
- continueText?: string;
35
- validateMessages?: Object;
36
- };
37
- initialValues?: any;
38
- form?: FormInstance;
39
- onCancel?(): void;
40
- onSubmit?(values: Object, allValues?: Object): any;
41
- requiredMark?: boolean;
42
- showSubmitButton?: boolean;
43
- getScrollContainer?: () => HTMLElement | null;
44
- onFirstModified?: () => any;
45
- uploadApi?: string;
46
- selectDatasourceApi?: string;
47
- validationRule?: string;
48
- getRuleValidationApi?: string;
49
- stepDirection?: 'vertical' | 'horizontal';
50
- customizeComponents?: {
51
- [name: string]: FC;
52
- };
53
- ocr?: OCRConfig;
54
- validationCodeExtraParams?: Record<string, any>;
55
- }
56
- export type FormItemAssert = {
57
- field: NamePath;
58
- value?: any;
59
- operation?: 'eq' | 'ne' | 'in' | 'ni' | 'lt' | 'le' | 'gt' | 'ge' | 'co' | 'nc' | 'filled' | 'unfilled' | 'co-some' | 'co-every' | 'some-in' | 'every-in';
60
- };
61
- export type FormItemCopyValue = {
62
- assert: FormItemAssert;
63
- copyFrom: NamePath;
64
- disabled?: boolean;
65
- };
66
- export type FormItemSelectValue = {
67
- assert: FormItemAssert;
68
- value: any;
69
- disabled?: boolean;
70
- };
71
- export interface FormItemExtraConfig extends FormItemProps {
72
- elements?: FormItemConfig[];
73
- required?: boolean;
74
- requiredAsserts?: FormItemAssert[];
75
- extraLabel?: any;
76
- xl?: number;
77
- md?: number;
78
- xs?: number;
79
- span?: number;
80
- areaCode?: number | string | (number | string)[];
81
- phoneNumber?: number | string;
82
- mergeRules?: boolean;
83
- dependencies?: NamePath[];
84
- antdDependencies?: NamePath[];
85
- asserts?: FormItemAssert[];
86
- shouldRender?: (form?: FormInstance) => boolean;
87
- ignore?: boolean;
88
- copyValue?: FormItemCopyValue;
89
- selectValue?: FormItemSelectValue[];
90
- disabled?: boolean | FormItemAssert | FormItemAssert[];
91
- halfRow?: boolean;
92
- fullRow?: boolean;
93
- limit?: number;
94
- hideWhenPreview?: boolean;
95
- hideWhenEdit?: boolean;
96
- requiredMessage?: string | ReactNode;
97
- showOptional?: boolean;
98
- previewFormater?(value: any, form?: FormInstance): any;
99
- handleNext?(values: any): any;
100
- render?(preview: boolean, form?: FormInstance, value?: any, onChange?: (params: any) => any, setShowStepButton?: IglooComponentProps['setShowStepButton']): any;
101
- locales?: {
102
- [key: string]: string;
103
- };
104
- options?: (CheckboxOptionType & {
105
- extraInfo?: {
106
- content: any;
107
- shownTrigger: 'unchecked' | 'checked' | 'all';
108
- };
109
- })[] | SelectProps['options'];
110
- sendOtp?: (value: any) => any;
111
- dependField?: FormItemName;
112
- countDownSeconds?: number;
113
- getButtonDisabledState?: (form: FormInstance) => boolean;
114
- getPreviousDisabledState?: (form: FormInstance) => boolean;
115
- handleUpload?(file: File): PromiseLike<string>;
116
- withoutForm?: boolean;
117
- disableEditButton?: boolean;
118
- hidePreviewDivider?: boolean;
119
- valueFormater?: (value: any) => any;
120
- currentStep?: number;
121
- subscribedFields?: FormItemName[];
122
- description?: string | React.ReactNode;
123
- dateLimitationType?: 'relative' | 'absolute';
124
- absoluteRangeStart?: number;
125
- absoluteRangeEnd?: number;
126
- relativeRangeStart?: {
127
- type: 'day' | 'month' | 'year';
128
- quantity: number;
129
- pattern?: string;
130
- dependField?: NamePath;
131
- };
132
- relativeRangeEnd?: {
133
- type: 'day' | 'month' | 'year';
134
- quantity: number;
135
- pattern?: string;
136
- dependField?: NamePath;
137
- };
138
- validationCode?: {
139
- code: string;
140
- errorMessage: string;
141
- warningOnly?: boolean;
142
- }[];
143
- length?: number;
144
- minLength?: number;
145
- maxLength?: number;
146
- idType?: 'KTP' | 'NIK';
147
- validationRule?: string;
148
- shouldRenderCode?: string;
149
- disabledCode?: string;
150
- automaticCalcCode?: string;
151
- enableAutomaticCalc?: boolean;
152
- [key: string]: any;
153
- }
154
- export interface FormItemConfig extends FormItemExtraConfig {
155
- type?: string;
156
- name?: NamePath;
157
- label?: string | React.ReactNode;
158
- previewLabel?: string | React.ReactNode;
159
- labelProps?: any;
160
- contentProps?: any;
161
- ocr?: {
162
- vender: string;
163
- outputMap: {
164
- field: string[];
165
- source: string[];
166
- }[];
167
- };
168
- }
169
- export type FormItemPropsHandler = (config: FormItemConfig) => FormItemConfig;
170
- export interface FC<P = {}> extends React.FunctionComponent<P> {
171
- formItemPropsHandler?: FormItemPropsHandler;
172
- isPreviewSupport?: boolean;
173
- }
174
- export interface Form extends React.ForwardRefExoticComponent<FormBasicConfig & React.RefAttributes<any>> {
175
- useForm: () => FormInstance;
176
- }
177
- export interface IglooComponentProps {
178
- validateField?: (nameList?: NamePath[]) => Promise<any>;
179
- containerRef?: React.RefObject<any>;
180
- setFieldError?(errorMsg?: string): void;
181
- setFieldValue?(value?: any): void;
182
- setShowStepButton?: (showButton: boolean) => void;
183
- getFormInstance?: () => FormInstance;
184
- locales?: {
185
- [key: string]: string;
186
- };
187
- }
package/lib/types.d.ts DELETED
@@ -1,187 +0,0 @@
1
- /// <reference path="../typings.d.ts" />
2
- import { BlinkIdSingleSideRecognizerResult } from '@microblink/blinkid-in-browser-sdk';
3
- import { FormItemProps, FormInstance } from 'antd/es/form';
4
- import { ReactNode } from 'react';
5
- import { NamePath } from 'rc-field-form/lib/interface';
6
- import { CheckboxOptionType } from 'antd/es/checkbox';
7
- import { SelectProps } from 'antd/es/select';
8
- export { Rule } from 'rc-field-form/lib/interface';
9
- export interface OCRHooks {
10
- microBlink?: {
11
- initResult: {
12
- error?: string;
13
- failed: boolean;
14
- };
15
- recognize: ((file: File) => Promise<BlinkIdSingleSideRecognizerResult | null>) | null;
16
- loading?: boolean;
17
- };
18
- }
19
- export interface OCRConfig {
20
- microBlink?: {
21
- licenseKey: string;
22
- };
23
- }
24
- export type FormItemName = NamePath;
25
- export interface FormBasicConfig {
26
- config: FormItemConfig;
27
- className?: string;
28
- style?: Object;
29
- locales?: {
30
- cancelText?: string;
31
- submitText?: string;
32
- previewText?: string;
33
- nextText?: string;
34
- continueText?: string;
35
- validateMessages?: Object;
36
- };
37
- initialValues?: any;
38
- form?: FormInstance;
39
- onCancel?(): void;
40
- onSubmit?(values: Object, allValues?: Object): any;
41
- requiredMark?: boolean;
42
- showSubmitButton?: boolean;
43
- getScrollContainer?: () => HTMLElement | null;
44
- onFirstModified?: () => any;
45
- uploadApi?: string;
46
- selectDatasourceApi?: string;
47
- validationRule?: string;
48
- getRuleValidationApi?: string;
49
- stepDirection?: 'vertical' | 'horizontal';
50
- customizeComponents?: {
51
- [name: string]: FC;
52
- };
53
- ocr?: OCRConfig;
54
- validationCodeExtraParams?: Record<string, any>;
55
- }
56
- export type FormItemAssert = {
57
- field: NamePath;
58
- value?: any;
59
- operation?: 'eq' | 'ne' | 'in' | 'ni' | 'lt' | 'le' | 'gt' | 'ge' | 'co' | 'nc' | 'filled' | 'unfilled' | 'co-some' | 'co-every' | 'some-in' | 'every-in';
60
- };
61
- export type FormItemCopyValue = {
62
- assert: FormItemAssert;
63
- copyFrom: NamePath;
64
- disabled?: boolean;
65
- };
66
- export type FormItemSelectValue = {
67
- assert: FormItemAssert;
68
- value: any;
69
- disabled?: boolean;
70
- };
71
- export interface FormItemExtraConfig extends FormItemProps {
72
- elements?: FormItemConfig[];
73
- required?: boolean;
74
- requiredAsserts?: FormItemAssert[];
75
- extraLabel?: any;
76
- xl?: number;
77
- md?: number;
78
- xs?: number;
79
- span?: number;
80
- areaCode?: number | string | (number | string)[];
81
- phoneNumber?: number | string;
82
- mergeRules?: boolean;
83
- dependencies?: NamePath[];
84
- antdDependencies?: NamePath[];
85
- asserts?: FormItemAssert[];
86
- shouldRender?: (form?: FormInstance) => boolean;
87
- ignore?: boolean;
88
- copyValue?: FormItemCopyValue;
89
- selectValue?: FormItemSelectValue[];
90
- disabled?: boolean | FormItemAssert | FormItemAssert[];
91
- halfRow?: boolean;
92
- fullRow?: boolean;
93
- limit?: number;
94
- hideWhenPreview?: boolean;
95
- hideWhenEdit?: boolean;
96
- requiredMessage?: string | ReactNode;
97
- showOptional?: boolean;
98
- previewFormater?(value: any, form?: FormInstance): any;
99
- handleNext?(values: any): any;
100
- render?(preview: boolean, form?: FormInstance, value?: any, onChange?: (params: any) => any, setShowStepButton?: IglooComponentProps['setShowStepButton']): any;
101
- locales?: {
102
- [key: string]: string;
103
- };
104
- options?: (CheckboxOptionType & {
105
- extraInfo?: {
106
- content: any;
107
- shownTrigger: 'unchecked' | 'checked' | 'all';
108
- };
109
- })[] | SelectProps['options'];
110
- sendOtp?: (value: any) => any;
111
- dependField?: FormItemName;
112
- countDownSeconds?: number;
113
- getButtonDisabledState?: (form: FormInstance) => boolean;
114
- getPreviousDisabledState?: (form: FormInstance) => boolean;
115
- handleUpload?(file: File): PromiseLike<string>;
116
- withoutForm?: boolean;
117
- disableEditButton?: boolean;
118
- hidePreviewDivider?: boolean;
119
- valueFormater?: (value: any) => any;
120
- currentStep?: number;
121
- subscribedFields?: FormItemName[];
122
- description?: string | React.ReactNode;
123
- dateLimitationType?: 'relative' | 'absolute';
124
- absoluteRangeStart?: number;
125
- absoluteRangeEnd?: number;
126
- relativeRangeStart?: {
127
- type: 'day' | 'month' | 'year';
128
- quantity: number;
129
- pattern?: string;
130
- dependField?: NamePath;
131
- };
132
- relativeRangeEnd?: {
133
- type: 'day' | 'month' | 'year';
134
- quantity: number;
135
- pattern?: string;
136
- dependField?: NamePath;
137
- };
138
- validationCode?: {
139
- code: string;
140
- errorMessage: string;
141
- warningOnly?: boolean;
142
- }[];
143
- length?: number;
144
- minLength?: number;
145
- maxLength?: number;
146
- idType?: 'KTP' | 'NIK';
147
- validationRule?: string;
148
- shouldRenderCode?: string;
149
- disabledCode?: string;
150
- automaticCalcCode?: string;
151
- enableAutomaticCalc?: boolean;
152
- [key: string]: any;
153
- }
154
- export interface FormItemConfig extends FormItemExtraConfig {
155
- type?: string;
156
- name?: NamePath;
157
- label?: string | React.ReactNode;
158
- previewLabel?: string | React.ReactNode;
159
- labelProps?: any;
160
- contentProps?: any;
161
- ocr?: {
162
- vender: string;
163
- outputMap: {
164
- field: string[];
165
- source: string[];
166
- }[];
167
- };
168
- }
169
- export type FormItemPropsHandler = (config: FormItemConfig) => FormItemConfig;
170
- export interface FC<P = {}> extends React.FunctionComponent<P> {
171
- formItemPropsHandler?: FormItemPropsHandler;
172
- isPreviewSupport?: boolean;
173
- }
174
- export interface Form extends React.ForwardRefExoticComponent<FormBasicConfig & React.RefAttributes<any>> {
175
- useForm: () => FormInstance;
176
- }
177
- export interface IglooComponentProps {
178
- validateField?: (nameList?: NamePath[]) => Promise<any>;
179
- containerRef?: React.RefObject<any>;
180
- setFieldError?(errorMsg?: string): void;
181
- setFieldValue?(value?: any): void;
182
- setShowStepButton?: (showButton: boolean) => void;
183
- getFormInstance?: () => FormInstance;
184
- locales?: {
185
- [key: string]: string;
186
- };
187
- }