ronds-metadata 1.2.93 → 1.2.95

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,7 +37,7 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
37
37
  /*
38
38
  * @Author:wangxian
39
39
  * @Date: 2021-09-18 14:15:04
40
- * @LastEditTime: 2023-07-04 16:30:59
40
+ * @LastEditTime: 2023-08-14 09:44:23
41
41
  */
42
42
  import React from 'react';
43
43
  import { MetadataFormContext, MetadataRefContext } from '../interface';
@@ -49,7 +49,7 @@ import { tr } from '../../../framework/locale';
49
49
  import useObservable from '../../../framework/rxjs-hooks/useObservable';
50
50
  import { assignReplace, findTopParentIndex } from '../utils';
51
51
  function Index(props) {
52
- var _extraInfo$http9, _extraInfo$http10, _extraInfo$http11, _extraInfo$http12, _extraInfo$http13, _extraInfo$http14;
52
+ var _extraInfo$http11, _extraInfo$http12, _extraInfo$http13, _extraInfo$http14, _extraInfo$http15, _extraInfo$http16;
53
53
  var id = props.id,
54
54
  name = props.name,
55
55
  isRefForm = props.isRefForm,
@@ -274,12 +274,12 @@ function Index(props) {
274
274
  return processSelectOptionsData();
275
275
  }, [initEnumValue, myEnum, id, extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.maxSelect]);
276
276
  var httpOptions = useAsyncMemo( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
277
- var _extraInfo$http3, _extraInfo$http4, _extraInfo$http5, res;
277
+ var _extraInfo$http3, _extraInfo$http4, _extraInfo$http5, res, _extraInfo$http6, _extraInfo$http7, _enum;
278
278
  return _regeneratorRuntime.wrap(function _callee2$(_context2) {
279
279
  while (1) switch (_context2.prev = _context2.next) {
280
280
  case 0:
281
281
  if (!((extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.http) && (extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.http.url))) {
282
- _context2.next = 7;
282
+ _context2.next = 9;
283
283
  break;
284
284
  }
285
285
  _context2.next = 3;
@@ -293,9 +293,22 @@ function Index(props) {
293
293
  setTreeData(_toConsumableArray(res));
294
294
  }
295
295
  return _context2.abrupt("return", res);
296
- case 7:
296
+ case 9:
297
+ if (!initEnumValue[id]) {
298
+ _context2.next = 14;
299
+ break;
300
+ }
301
+ _enum = deepClone(initEnumValue[id]);
302
+ if (extraInfo === null || extraInfo === void 0 ? void 0 : (_extraInfo$http6 = extraInfo.http) === null || _extraInfo$http6 === void 0 ? void 0 : _extraInfo$http6.isCascader) {
303
+ setCascaderOption(_toConsumableArray(_enum));
304
+ }
305
+ if (extraInfo === null || extraInfo === void 0 ? void 0 : (_extraInfo$http7 = extraInfo.http) === null || _extraInfo$http7 === void 0 ? void 0 : _extraInfo$http7.isTreeSelect) {
306
+ setTreeData(_toConsumableArray(_enum));
307
+ }
308
+ return _context2.abrupt("return", _enum);
309
+ case 14:
297
310
  return _context2.abrupt("return", []);
298
- case 8:
311
+ case 15:
299
312
  case "end":
300
313
  return _context2.stop();
301
314
  }
@@ -524,7 +537,7 @@ function Index(props) {
524
537
  }
525
538
  };
526
539
  if (isObj && isRefForm && field) {
527
- var _extraInfo$http6, _extraInfo$http7, _extraInfo$http8;
540
+ var _extraInfo$http8, _extraInfo$http9, _extraInfo$http10;
528
541
  return /*#__PURE__*/React.createElement(_Form.Item, _extends({}, field, {
529
542
  style: {
530
543
  flex: 1,
@@ -552,7 +565,7 @@ function Index(props) {
552
565
  }],
553
566
  help: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.help,
554
567
  tooltip: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.tooltip
555
- }), processSelectType((_extraInfo$http6 = extraInfo.http) === null || _extraInfo$http6 === void 0 ? void 0 : _extraInfo$http6.isCascader, (_extraInfo$http7 = extraInfo.http) === null || _extraInfo$http7 === void 0 ? void 0 : _extraInfo$http7.isTreeSelect, (_extraInfo$http8 = extraInfo.http) === null || _extraInfo$http8 === void 0 ? void 0 : _extraInfo$http8.isPagination));
568
+ }), processSelectType((_extraInfo$http8 = extraInfo.http) === null || _extraInfo$http8 === void 0 ? void 0 : _extraInfo$http8.isCascader, (_extraInfo$http9 = extraInfo.http) === null || _extraInfo$http9 === void 0 ? void 0 : _extraInfo$http9.isTreeSelect, (_extraInfo$http10 = extraInfo.http) === null || _extraInfo$http10 === void 0 ? void 0 : _extraInfo$http10.isPagination));
556
569
  }
557
570
  return !field ? /*#__PURE__*/React.createElement(_Form.Item, {
558
571
  key: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.defaultValue,
@@ -582,7 +595,7 @@ function Index(props) {
582
595
  }],
583
596
  help: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.help,
584
597
  tooltip: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.tooltip
585
- }, processSelectType((_extraInfo$http9 = extraInfo.http) === null || _extraInfo$http9 === void 0 ? void 0 : _extraInfo$http9.isCascader, (_extraInfo$http10 = extraInfo.http) === null || _extraInfo$http10 === void 0 ? void 0 : _extraInfo$http10.isTreeSelect, (_extraInfo$http11 = extraInfo.http) === null || _extraInfo$http11 === void 0 ? void 0 : _extraInfo$http11.isPagination)) : /*#__PURE__*/React.createElement(_Form.Item, _extends({}, field, {
598
+ }, processSelectType((_extraInfo$http11 = extraInfo.http) === null || _extraInfo$http11 === void 0 ? void 0 : _extraInfo$http11.isCascader, (_extraInfo$http12 = extraInfo.http) === null || _extraInfo$http12 === void 0 ? void 0 : _extraInfo$http12.isTreeSelect, (_extraInfo$http13 = extraInfo.http) === null || _extraInfo$http13 === void 0 ? void 0 : _extraInfo$http13.isPagination)) : /*#__PURE__*/React.createElement(_Form.Item, _extends({}, field, {
586
599
  style: {
587
600
  flex: 1,
588
601
  paddingRight: '10px'
@@ -593,6 +606,6 @@ function Index(props) {
593
606
  }],
594
607
  help: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.help,
595
608
  tooltip: extraInfo === null || extraInfo === void 0 ? void 0 : extraInfo.tooltip
596
- }), processSelectType((_extraInfo$http12 = extraInfo.http) === null || _extraInfo$http12 === void 0 ? void 0 : _extraInfo$http12.isCascader, (_extraInfo$http13 = extraInfo.http) === null || _extraInfo$http13 === void 0 ? void 0 : _extraInfo$http13.isTreeSelect, (_extraInfo$http14 = extraInfo.http) === null || _extraInfo$http14 === void 0 ? void 0 : _extraInfo$http14.isPagination));
609
+ }), processSelectType((_extraInfo$http14 = extraInfo.http) === null || _extraInfo$http14 === void 0 ? void 0 : _extraInfo$http14.isCascader, (_extraInfo$http15 = extraInfo.http) === null || _extraInfo$http15 === void 0 ? void 0 : _extraInfo$http15.isTreeSelect, (_extraInfo$http16 = extraInfo.http) === null || _extraInfo$http16 === void 0 ? void 0 : _extraInfo$http16.isPagination));
597
610
  }
598
611
  export default /*#__PURE__*/React.memo(Index);
@@ -0,0 +1,9 @@
1
+ import Provider from './utils/Provider';
2
+ import Context from './utils/Context';
3
+ type ReturnTypes<T extends any> = T extends (...args: any) => infer R ? R : any;
4
+ type Model<T extends keyof U, U> = {
5
+ [key in keyof U]: ReturnTypes<U[T]>;
6
+ };
7
+ type UseModelFn = <T extends keyof U, U>(namespace: T) => Model<T, U>[T];
8
+ declare const useModel: UseModelFn;
9
+ export { useModel, Provider, Context, Model };
@@ -0,0 +1,50 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ /*
3
+ * @Author: wangxian
4
+ * @Date: 2023-08-01 19:30:25
5
+ * @LastEditTime: 2023-08-01 19:30:35
6
+ */
7
+ import { useEffect, useContext, useRef, useState } from 'react';
8
+ import Provider from './utils/Provider';
9
+ import Context from './utils/Context';
10
+ var useModel = function useModel(namespace) {
11
+ var dispatcher = useContext(Context);
12
+ // 判断组件是否挂载
13
+ var isMount = useRef(false);
14
+ var _useState = useState(dispatcher.data[namespace]),
15
+ _useState2 = _slicedToArray(_useState, 2),
16
+ state = _useState2[0],
17
+ setState = _useState2[1];
18
+ useEffect(function () {
19
+ isMount.current = true;
20
+ return function () {
21
+ isMount.current = false;
22
+ };
23
+ }, []);
24
+ useEffect(function () {
25
+ var handler = function handler(value) {
26
+ if (!isMount.current) {
27
+ // 如果函数执行中,组件被卸载,则强制更新全局 data
28
+ setTimeout(function () {
29
+ dispatcher.data[namespace] = value;
30
+ dispatcher.update(namespace);
31
+ });
32
+ } else {
33
+ setState(value);
34
+ }
35
+ };
36
+ try {
37
+ dispatcher.callBacks[namespace].add(handler);
38
+ dispatcher.update(namespace);
39
+ } catch (e) {
40
+ dispatcher.callBacks[namespace] = new Set();
41
+ dispatcher.callBacks[namespace].add(handler);
42
+ dispatcher.update(namespace);
43
+ }
44
+ return function () {
45
+ dispatcher.callBacks[namespace].delete(handler);
46
+ };
47
+ }, [namespace]);
48
+ return state;
49
+ };
50
+ export { useModel, Provider, Context };
@@ -0,0 +1,3 @@
1
+ /// <reference types="react" />
2
+ declare const _default: import("react").Context<{}>;
3
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import { createContext } from 'react';
2
+ export default /*#__PURE__*/createContext({});
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ type ExecutorProps = {
3
+ hook: () => void;
4
+ namespace: string;
5
+ onUpdate: (value: any) => void;
6
+ };
7
+ declare const Executor: React.FC<ExecutorProps>;
8
+ export default Executor;
@@ -0,0 +1,33 @@
1
+ import React, { useEffect, useMemo, useRef } from 'react';
2
+ var Executor = function Executor(_ref) {
3
+ var hook = _ref.hook,
4
+ namespace = _ref.namespace,
5
+ onUpdate = _ref.onUpdate;
6
+ var updateRef = useRef(onUpdate);
7
+ updateRef.current = onUpdate;
8
+ var initRef = useRef(false);
9
+ var data;
10
+ try {
11
+ if (typeof hook === 'function') {
12
+ data = hook();
13
+ } else {
14
+ data = null;
15
+ }
16
+ } catch (e) {
17
+ throw new TypeError(e);
18
+ }
19
+ // 初始化时执行一次
20
+ useMemo(function () {
21
+ updateRef.current(data);
22
+ initRef.current = false;
23
+ }, []);
24
+ useEffect(function () {
25
+ if (initRef.current) {
26
+ updateRef.current(data);
27
+ } else {
28
+ initRef.current = true;
29
+ }
30
+ });
31
+ return /*#__PURE__*/React.createElement(React.Fragment, null);
32
+ };
33
+ export default Executor;
@@ -0,0 +1,5 @@
1
+ import React from 'react';
2
+ declare const Provider: React.FC<{
3
+ models: any;
4
+ }>;
5
+ export default Provider;
@@ -0,0 +1,30 @@
1
+ /*
2
+ * @Author: wangxian
3
+ * @Date: 2023-08-01 19:31:08
4
+ * @LastEditTime: 2023-08-01 19:32:27
5
+ */
6
+ import React from 'react';
7
+ import Dispatcher from './dispatcher';
8
+ import Executor from './Executor';
9
+ import Context from './Context';
10
+ var dispatcher = new Dispatcher();
11
+ var Provider = function Provider(_ref) {
12
+ var children = _ref.children,
13
+ _ref$models = _ref.models,
14
+ models = _ref$models === void 0 ? {} : _ref$models;
15
+ var Exe = Executor;
16
+ return /*#__PURE__*/React.createElement(Context.Provider, {
17
+ value: dispatcher
18
+ }, Object.keys(models).map(function (item) {
19
+ return /*#__PURE__*/React.createElement(Exe, {
20
+ key: item,
21
+ namespace: item,
22
+ hook: models[item],
23
+ onUpdate: function onUpdate(value) {
24
+ dispatcher.data[item] = value;
25
+ dispatcher.update(item);
26
+ }
27
+ });
28
+ }), children);
29
+ };
30
+ export default Provider;
@@ -0,0 +1,5 @@
1
+ export default class Dispatcher {
2
+ data: any;
3
+ callBacks: any;
4
+ update: (namespace: string) => void;
5
+ }
@@ -0,0 +1,24 @@
1
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ /*
4
+ * @Author: wangxian
5
+ * @Date: 2023-08-01 19:31:09
6
+ * @LastEditTime: 2023-08-01 19:32:07
7
+ */
8
+ var Dispatcher = /*#__PURE__*/_createClass(function Dispatcher() {
9
+ var _this = this;
10
+ _classCallCheck(this, Dispatcher);
11
+ this.data = {};
12
+ this.callBacks = {};
13
+ this.update = function (namespace) {
14
+ (_this.callBacks[namespace] || []).forEach(function (callback) {
15
+ try {
16
+ var data = _this.data[namespace];
17
+ callback(data);
18
+ } catch (e) {
19
+ callback(undefined);
20
+ }
21
+ });
22
+ };
23
+ });
24
+ export { Dispatcher as default };
package/es/index.d.ts CHANGED
@@ -19,6 +19,7 @@ export * from './framework/metadata/index';
19
19
  export { default as http, addInterceptor } from './framework/http/index';
20
20
  export { default as useMemoSubject } from './framework/rxjs-hooks/useMemoSubject';
21
21
  export { default as useObservable } from './framework/rxjs-hooks/useObservable';
22
+ export { useModel, Provider, Context } from './framework/hooks/useModel';
22
23
  export { default as useSyncScroll, useSyncScrollByRefs } from './framework/hooks/use-sync-scroll';
23
24
  export { registerMetadataAPI } from './framework/metadata/MetadataService';
24
25
  export { FormRobot } from './framework/fg/index';
package/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * @Author:wangxian
3
3
  * @Date: 2021-09-18 14:15:04
4
- * @LastEditTime: 2023-04-27 19:08:54
4
+ * @LastEditTime: 2023-08-02 08:41:39
5
5
  */
6
6
  import './theme.less';
7
7
  export { default as MetadataEdit } from './comps/MetadataEdit';
@@ -24,6 +24,7 @@ export * from './framework/metadata/index';
24
24
  export { default as http, addInterceptor } from './framework/http/index';
25
25
  export { default as useMemoSubject } from './framework/rxjs-hooks/useMemoSubject';
26
26
  export { default as useObservable } from './framework/rxjs-hooks/useObservable';
27
+ export { useModel, Provider, Context } from './framework/hooks/useModel';
27
28
  export { default as useSyncScroll, useSyncScrollByRefs } from './framework/hooks/use-sync-scroll';
28
29
  export { registerMetadataAPI } from './framework/metadata/MetadataService';
29
30
  export { FormRobot } from './framework/fg/index';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "ronds-metadata",
4
- "version": "1.2.93",
4
+ "version": "1.2.95",
5
5
  "scripts": {
6
6
  "start": "dumi dev",
7
7
  "docs:build": "dumi build",