@fairys/taro-tools-react 1.0.17 → 1.0.18

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.
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from 'react';
2
+ export declare const ValtioContext: import("react").Context<any>;
3
+ export declare function ValtioContextProvider<T>({ value, children }: {
4
+ value: T;
5
+ children?: ReactNode;
6
+ }): import("react/jsx-runtime").JSX.Element;
7
+ export declare function useValtioContext<T>(): T;
@@ -0,0 +1,15 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext } from "react";
3
+ const ValtioContext = /*#__PURE__*/ createContext(void 0);
4
+ function ValtioContextProvider({ value, children }) {
5
+ return /*#__PURE__*/ jsx(ValtioContext.Provider, {
6
+ value: value,
7
+ children: children
8
+ });
9
+ }
10
+ function useValtioContext() {
11
+ const context = useContext(ValtioContext);
12
+ if (!context) throw new Error('useValtioContext must be used within a ValtioContextProvider');
13
+ return context;
14
+ }
15
+ export { ValtioContext, ValtioContextProvider, useValtioContext };
@@ -1,13 +1,14 @@
1
1
  import { ProxyInstanceObjectBase } from './instance';
2
2
  export * from './instance';
3
+ export * from './context';
3
4
  /**创建简单的状态管理*/
4
- export declare const useValtioState: <T extends object>(inital?: T) => readonly [import("valtio").Snapshot<T>, ProxyInstanceObjectBase<T>, any];
5
+ export declare const useValtioState: <T extends object>(inital?: T) => [T, ProxyInstanceObjectBase<T>, any];
5
6
  export declare const useValtioInstaceState: <T extends object = any, K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>, M extends {
6
7
  new (...args: any[]): K;
7
8
  } = {
8
9
  new (...args: any[]): K;
9
- }>(Instance: M) => readonly [import("valtio").Snapshot<T>, K, any];
10
+ }>(Instance: M) => [T, K, any];
10
11
  /**
11
12
  * 创建valtio proxy 状态管理
12
13
  */
13
- export declare const useValtioProxyState: <T extends object>(inital?: T) => readonly [import("valtio").Snapshot<T>, T, any];
14
+ export declare const useValtioProxyState: <T extends object>(inital?: T) => [T, T, any];
@@ -1,9 +1,10 @@
1
1
  import { useRef } from "react";
2
2
  import { proxy, useSnapshot } from "valtio";
3
- import { ProxyInstanceObjectBase } from "./instance.js";
3
+ import { useProxyInstanceObjectBase } from "./instance.js";
4
4
  export * from "./instance.js";
5
+ export * from "./context.js";
5
6
  const useValtioState = (inital)=>{
6
- const instance = useRef(new ProxyInstanceObjectBase()._ctor(inital)).current;
7
+ const instance = useProxyInstanceObjectBase(inital);
7
8
  const state = useSnapshot(instance.store);
8
9
  return [
9
10
  state,
@@ -11,8 +12,13 @@ const useValtioState = (inital)=>{
11
12
  state.__defaultValue
12
13
  ];
13
14
  };
15
+ const usNewInstance = (Instance)=>{
16
+ const ref = useRef();
17
+ if (!ref.current) ref.current = new Instance();
18
+ return ref.current;
19
+ };
14
20
  const useValtioInstaceState = (Instance)=>{
15
- const instance = useRef(new Instance()).current;
21
+ const instance = usNewInstance(Instance);
16
22
  const state = useSnapshot(instance.store);
17
23
  return [
18
24
  state,
@@ -21,11 +27,12 @@ const useValtioInstaceState = (Instance)=>{
21
27
  ];
22
28
  };
23
29
  const useValtioProxyState = (inital)=>{
24
- const instance = useRef(proxy(inital || {})).current;
25
- const state = useSnapshot(instance);
30
+ const instance = useRef();
31
+ if (!instance.current) instance.current = proxy(inital || {});
32
+ const state = useSnapshot(instance.current);
26
33
  return [
27
34
  state,
28
- instance,
35
+ instance.current,
29
36
  state.__defaultValue
30
37
  ];
31
38
  };
@@ -15,3 +15,7 @@ export declare class ProxyInstanceObjectBase<T extends Object = any> {
15
15
  /**创建 ref 对象 (ref对象不做监听更新)*/
16
16
  _createRef: <K extends Object = any>(inital?: K) => K;
17
17
  }
18
+ /**
19
+ * 创建单个proxy对象数据基础实例封装
20
+ */
21
+ export declare const useProxyInstanceObjectBase: <T extends object>(inital?: T) => ProxyInstanceObjectBase<T>;
@@ -1,5 +1,5 @@
1
- import { proxy, ref } from "valtio";
2
- import react from "react";
1
+ import { proxy, ref as external_valtio_ref } from "valtio";
2
+ import react, { useRef } from "react";
3
3
  class ProxyInstanceObjectBase {
4
4
  notRefFields = [];
5
5
  store = proxy({});
@@ -13,8 +13,8 @@ class ProxyInstanceObjectBase {
13
13
  const value = values[key];
14
14
  if (Array.isArray(fields) && fields.includes(key)) this.store[key] = values[key];
15
15
  else if (Array.isArray(this.notRefFields) && this.notRefFields.includes(key)) this.store[key] = value;
16
- else if (react.isValidElement(value) || 'function' == typeof value) this.store[key] = ref(values[key]);
17
- else if ('object' == typeof value && null !== value) this.store[key] = ref(values[key]);
16
+ else if (react.isValidElement(value) || 'function' == typeof value) this.store[key] = external_valtio_ref(values[key]);
17
+ else if ('object' == typeof value && null !== value) this.store[key] = external_valtio_ref(values[key]);
18
18
  else this.store[key] = values[key];
19
19
  });
20
20
  return this;
@@ -31,6 +31,11 @@ class ProxyInstanceObjectBase {
31
31
  } else delete this.store[names];
32
32
  return this;
33
33
  };
34
- _createRef = (inital)=>ref(inital || {});
34
+ _createRef = (inital)=>external_valtio_ref(inital || {});
35
35
  }
36
- export { ProxyInstanceObjectBase };
36
+ const useProxyInstanceObjectBase = (inital)=>{
37
+ const ref = useRef();
38
+ if (!ref.current) ref.current = new ProxyInstanceObjectBase()._ctor(inital);
39
+ return ref.current;
40
+ };
41
+ export { ProxyInstanceObjectBase, useProxyInstanceObjectBase };
@@ -6,7 +6,7 @@ var __webpack_modules__ = {
6
6
  "./global.message.data.instance": function(module) {
7
7
  module.exports = require("./global.message.data.instance.js");
8
8
  },
9
- "./global.setting.data.instance": function(module) {
9
+ "context/global.setting.data.instance": function(module) {
10
10
  module.exports = require("./global.setting.data.instance.js");
11
11
  },
12
12
  "./page.data.instance": function(module) {
@@ -83,7 +83,7 @@ var __webpack_exports__ = {};
83
83
  return _auth_data_instance__WEBPACK_IMPORTED_MODULE_3__[key];
84
84
  }).bind(0, __WEBPACK_IMPORT_KEY__);
85
85
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
86
- var _global_setting_data_instance__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("./global.setting.data.instance");
86
+ var _global_setting_data_instance__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("context/global.setting.data.instance");
87
87
  var __WEBPACK_REEXPORT_OBJECT__ = {};
88
88
  for(var __WEBPACK_IMPORT_KEY__ in _global_setting_data_instance__WEBPACK_IMPORTED_MODULE_4__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
89
89
  return _global_setting_data_instance__WEBPACK_IMPORTED_MODULE_4__[key];
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ var __webpack_modules__ = {
3
3
  "./components": function(module) {
4
4
  module.exports = require("./components/index.js");
5
5
  },
6
- "./context": function(module) {
6
+ "./context?4a23": function(module) {
7
7
  module.exports = require("./context/index.js");
8
8
  },
9
9
  "./utils": function(module) {
@@ -59,7 +59,7 @@ var __webpack_exports__ = {};
59
59
  return _components__WEBPACK_IMPORTED_MODULE_0__[key];
60
60
  }).bind(0, __WEBPACK_IMPORT_KEY__);
61
61
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
62
- var _context__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./context");
62
+ var _context__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./context?4a23");
63
63
  var __WEBPACK_REEXPORT_OBJECT__ = {};
64
64
  for(var __WEBPACK_IMPORT_KEY__ in _context__WEBPACK_IMPORTED_MODULE_1__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
65
65
  return _context__WEBPACK_IMPORTED_MODULE_1__[key];
@@ -0,0 +1,7 @@
1
+ import { ReactNode } from 'react';
2
+ export declare const ValtioContext: import("react").Context<any>;
3
+ export declare function ValtioContextProvider<T>({ value, children }: {
4
+ value: T;
5
+ children?: ReactNode;
6
+ }): import("react/jsx-runtime").JSX.Element;
7
+ export declare function useValtioContext<T>(): T;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ValtioContext: ()=>ValtioContext,
28
+ ValtioContextProvider: ()=>ValtioContextProvider,
29
+ useValtioContext: ()=>useValtioContext
30
+ });
31
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
32
+ const external_react_namespaceObject = require("react");
33
+ const ValtioContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(void 0);
34
+ function ValtioContextProvider({ value, children }) {
35
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ValtioContext.Provider, {
36
+ value: value,
37
+ children: children
38
+ });
39
+ }
40
+ function useValtioContext() {
41
+ const context = (0, external_react_namespaceObject.useContext)(ValtioContext);
42
+ if (!context) throw new Error('useValtioContext must be used within a ValtioContextProvider');
43
+ return context;
44
+ }
45
+ exports.ValtioContext = __webpack_exports__.ValtioContext;
46
+ exports.ValtioContextProvider = __webpack_exports__.ValtioContextProvider;
47
+ exports.useValtioContext = __webpack_exports__.useValtioContext;
48
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
49
+ "ValtioContext",
50
+ "ValtioContextProvider",
51
+ "useValtioContext"
52
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
53
+ Object.defineProperty(exports, '__esModule', {
54
+ value: true
55
+ });
@@ -1,13 +1,14 @@
1
1
  import { ProxyInstanceObjectBase } from './instance';
2
2
  export * from './instance';
3
+ export * from './context';
3
4
  /**创建简单的状态管理*/
4
- export declare const useValtioState: <T extends object>(inital?: T) => readonly [import("valtio").Snapshot<T>, ProxyInstanceObjectBase<T>, any];
5
+ export declare const useValtioState: <T extends object>(inital?: T) => [T, ProxyInstanceObjectBase<T>, any];
5
6
  export declare const useValtioInstaceState: <T extends object = any, K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>, M extends {
6
7
  new (...args: any[]): K;
7
8
  } = {
8
9
  new (...args: any[]): K;
9
- }>(Instance: M) => readonly [import("valtio").Snapshot<T>, K, any];
10
+ }>(Instance: M) => [T, K, any];
10
11
  /**
11
12
  * 创建valtio proxy 状态管理
12
13
  */
13
- export declare const useValtioProxyState: <T extends object>(inital?: T) => readonly [import("valtio").Snapshot<T>, T, any];
14
+ export declare const useValtioProxyState: <T extends object>(inital?: T) => [T, T, any];
@@ -1,5 +1,8 @@
1
1
  "use strict";
2
2
  var __webpack_modules__ = {
3
+ "./context?fe51": function(module) {
4
+ module.exports = require("./context.js");
5
+ },
3
6
  "./instance": function(module) {
4
7
  module.exports = require("./instance.js");
5
8
  },
@@ -71,8 +74,19 @@ var __webpack_exports__ = {};
71
74
  return _instance__WEBPACK_IMPORTED_MODULE_2__[key];
72
75
  }).bind(0, __WEBPACK_IMPORT_KEY__);
73
76
  __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
77
+ var _context__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("./context?fe51");
78
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
79
+ for(var __WEBPACK_IMPORT_KEY__ in _context__WEBPACK_IMPORTED_MODULE_3__)if ([
80
+ "useValtioProxyState",
81
+ "default",
82
+ "useValtioInstaceState",
83
+ "useValtioState"
84
+ ].indexOf(__WEBPACK_IMPORT_KEY__) < 0) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
85
+ return _context__WEBPACK_IMPORTED_MODULE_3__[key];
86
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
87
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
74
88
  const useValtioState = (inital)=>{
75
- const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)(new _instance__WEBPACK_IMPORTED_MODULE_2__.ProxyInstanceObjectBase()._ctor(inital)).current;
89
+ const instance = (0, _instance__WEBPACK_IMPORTED_MODULE_2__.useProxyInstanceObjectBase)(inital);
76
90
  const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.store);
77
91
  return [
78
92
  state,
@@ -80,8 +94,13 @@ var __webpack_exports__ = {};
80
94
  state.__defaultValue
81
95
  ];
82
96
  };
97
+ const usNewInstance = (Instance)=>{
98
+ const ref = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
99
+ if (!ref.current) ref.current = new Instance();
100
+ return ref.current;
101
+ };
83
102
  const useValtioInstaceState = (Instance)=>{
84
- const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)(new Instance()).current;
103
+ const instance = usNewInstance(Instance);
85
104
  const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.store);
86
105
  return [
87
106
  state,
@@ -90,11 +109,12 @@ var __webpack_exports__ = {};
90
109
  ];
91
110
  };
92
111
  const useValtioProxyState = (inital)=>{
93
- const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)((0, valtio__WEBPACK_IMPORTED_MODULE_1__.proxy)(inital || {})).current;
94
- const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance);
112
+ const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
113
+ if (!instance.current) instance.current = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.proxy)(inital || {});
114
+ const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.current);
95
115
  return [
96
116
  state,
97
- instance,
117
+ instance.current,
98
118
  state.__defaultValue
99
119
  ];
100
120
  };
@@ -15,3 +15,7 @@ export declare class ProxyInstanceObjectBase<T extends Object = any> {
15
15
  /**创建 ref 对象 (ref对象不做监听更新)*/
16
16
  _createRef: <K extends Object = any>(inital?: K) => K;
17
17
  }
18
+ /**
19
+ * 创建单个proxy对象数据基础实例封装
20
+ */
21
+ export declare const useProxyInstanceObjectBase: <T extends object>(inital?: T) => ProxyInstanceObjectBase<T>;
@@ -33,7 +33,8 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
- ProxyInstanceObjectBase: ()=>ProxyInstanceObjectBase
36
+ ProxyInstanceObjectBase: ()=>ProxyInstanceObjectBase,
37
+ useProxyInstanceObjectBase: ()=>useProxyInstanceObjectBase
37
38
  });
38
39
  const external_valtio_namespaceObject = require("valtio");
39
40
  const external_react_namespaceObject = require("react");
@@ -71,9 +72,16 @@ class ProxyInstanceObjectBase {
71
72
  };
72
73
  _createRef = (inital)=>(0, external_valtio_namespaceObject.ref)(inital || {});
73
74
  }
75
+ const useProxyInstanceObjectBase = (inital)=>{
76
+ const ref = (0, external_react_namespaceObject.useRef)();
77
+ if (!ref.current) ref.current = new ProxyInstanceObjectBase()._ctor(inital);
78
+ return ref.current;
79
+ };
74
80
  exports.ProxyInstanceObjectBase = __webpack_exports__.ProxyInstanceObjectBase;
81
+ exports.useProxyInstanceObjectBase = __webpack_exports__.useProxyInstanceObjectBase;
75
82
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
76
- "ProxyInstanceObjectBase"
83
+ "ProxyInstanceObjectBase",
84
+ "useProxyInstanceObjectBase"
77
85
  ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
78
86
  Object.defineProperty(exports, '__esModule', {
79
87
  value: true
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "SunLxy <1011771396@qq.com>",
4
4
  "description": "框架组件库",
5
5
  "homepage": "https://github.com/autumn-fairy-tales/fairys-taro-react",
6
- "version": "1.0.17",
6
+ "version": "1.0.18",
7
7
  "main": "esm/index.js",
8
8
  "types": "esm/index.d.ts",
9
9
  "module": "esm/index.js",
@@ -0,0 +1,15 @@
1
+ import { createContext, ReactNode, useContext } from 'react';
2
+
3
+ export const ValtioContext = createContext(undefined);
4
+
5
+ export function ValtioContextProvider<T>({ value, children }: { value: T; children?: ReactNode }) {
6
+ return <ValtioContext.Provider value={value}>{children}</ValtioContext.Provider>;
7
+ }
8
+
9
+ export function useValtioContext<T>() {
10
+ const context = useContext<T>(ValtioContext);
11
+ if (!context) {
12
+ throw new Error('useValtioContext must be used within a ValtioContextProvider');
13
+ }
14
+ return context;
15
+ }
@@ -1,13 +1,28 @@
1
1
  import { useRef } from 'react';
2
2
  import { useSnapshot, proxy } from 'valtio';
3
- import { ProxyInstanceObjectBase } from './instance';
3
+ import { ProxyInstanceObjectBase, useProxyInstanceObjectBase } from './instance';
4
4
  export * from './instance';
5
+ export * from './context';
5
6
 
6
7
  /**创建简单的状态管理*/
7
8
  export const useValtioState = <T extends object>(inital?: T) => {
8
- const instance = useRef(new ProxyInstanceObjectBase<T>()._ctor(inital)).current;
9
+ const instance = useProxyInstanceObjectBase(inital);
9
10
  const state = useSnapshot(instance.store);
10
- return [state, instance, (state as any).__defaultValue] as const;
11
+ return [state, instance, (state as any).__defaultValue] as [T, ProxyInstanceObjectBase<T>, any];
12
+ };
13
+
14
+ const usNewInstance = <
15
+ T extends object = any,
16
+ K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>,
17
+ M extends { new (...args: any[]): K } = { new (...args: any[]): K },
18
+ >(
19
+ Instance: M,
20
+ ) => {
21
+ const ref = useRef<K>();
22
+ if (!ref.current) {
23
+ ref.current = new Instance();
24
+ }
25
+ return ref.current;
11
26
  };
12
27
 
13
28
  export const useValtioInstaceState = <
@@ -17,16 +32,19 @@ export const useValtioInstaceState = <
17
32
  >(
18
33
  Instance: M,
19
34
  ) => {
20
- const instance = useRef(new Instance()).current;
35
+ const instance = usNewInstance<T, K, M>(Instance);
21
36
  const state = useSnapshot(instance.store);
22
- return [state, instance, (state as any).__defaultValue] as const;
37
+ return [state, instance, (state as any).__defaultValue] as [T, K, any];
23
38
  };
24
39
 
25
40
  /**
26
41
  * 创建valtio proxy 状态管理
27
42
  */
28
43
  export const useValtioProxyState = <T extends object>(inital?: T) => {
29
- const instance = useRef(proxy(inital || ({} as T))).current;
30
- const state = useSnapshot(instance);
31
- return [state, instance, (state as any).__defaultValue] as const;
44
+ const instance = useRef<T>();
45
+ if (!instance.current) {
46
+ instance.current = proxy(inital || ({} as T));
47
+ }
48
+ const state = useSnapshot(instance.current);
49
+ return [state, instance.current, (state as any).__defaultValue] as [T, T, any];
32
50
  };
@@ -1,5 +1,5 @@
1
1
  import { proxy, ref } from 'valtio';
2
- import React from 'react';
2
+ import React, { useRef } from 'react';
3
3
 
4
4
  /**
5
5
  * 单个proxy对象数据基础实例封装
@@ -59,3 +59,14 @@ export class ProxyInstanceObjectBase<T extends Object = any> {
59
59
  return ref<K>(inital || ({} as K)) as K;
60
60
  };
61
61
  }
62
+
63
+ /**
64
+ * 创建单个proxy对象数据基础实例封装
65
+ */
66
+ export const useProxyInstanceObjectBase = <T extends object>(inital?: T) => {
67
+ const ref = useRef<ProxyInstanceObjectBase<T>>();
68
+ if (!ref.current) {
69
+ ref.current = new ProxyInstanceObjectBase<T>()._ctor(inital);
70
+ }
71
+ return ref.current;
72
+ };