@carbonorm/carbonreact 3.6.1 → 3.6.2
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.
- package/dist/CarbonReact.d.ts +21 -59
- package/dist/index.cjs.js +24 -18
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +25 -19
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/CarbonReact.tsx +33 -25
package/package.json
CHANGED
package/src/CarbonReact.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import {clearCache} from "@carbonorm/carbonnode";
|
|
|
2
2
|
import changed from "hoc/changed";
|
|
3
3
|
import {GlobalHistory} from "hoc/GlobalHistory";
|
|
4
4
|
import hexToRgb from "hoc/hexToRgb";
|
|
5
|
-
import {Component, Context, createContext, ReactElement, ReactNode} from 'react';
|
|
5
|
+
import {Component, Context, createContext, useContext, ReactElement, ReactNode} from 'react';
|
|
6
6
|
import {ToastContainer} from 'react-toastify';
|
|
7
7
|
import 'react-toastify/dist/ReactToastify.min.css';
|
|
8
8
|
import BackendThrowable from 'components/Errors/BackendThrowable';
|
|
@@ -45,19 +45,30 @@ export function isJsonString(str: string) {
|
|
|
45
45
|
|
|
46
46
|
// Create a context
|
|
47
47
|
|
|
48
|
-
const
|
|
48
|
+
const persistentStateMap = new Map<string, iCarbonReactState>();
|
|
49
|
+
|
|
50
|
+
abstract class CarbonReact<P = {}, S = {}> extends Component<{
|
|
49
51
|
children?: ReactNode | ReactNode[],
|
|
50
|
-
|
|
52
|
+
instanceId?: string,
|
|
51
53
|
websocket?: iCarbonWebSocketProps | boolean
|
|
52
54
|
} & P, S & iCarbonReactState> {
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
context: Context<S & iCarbonReactState> = createContext(this.state);
|
|
57
|
+
|
|
58
|
+
// Private static member
|
|
59
|
+
protected static instance: CarbonReact & Component<{
|
|
55
60
|
children?: ReactNode | ReactNode[],
|
|
56
61
|
} & any, any & iCarbonReactState>;
|
|
57
62
|
|
|
58
|
-
|
|
63
|
+
protected static getState() {
|
|
64
|
+
return CarbonReact.instance.state;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected static useContext(){
|
|
68
|
+
return () => useContext(CarbonReact.instance.context);
|
|
69
|
+
}
|
|
59
70
|
|
|
60
|
-
|
|
71
|
+
protected ;
|
|
61
72
|
|
|
62
73
|
static lastLocation = window.location.pathname;
|
|
63
74
|
|
|
@@ -65,27 +76,32 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
|
|
|
65
76
|
// noinspection JSUnusedGlobalSymbols
|
|
66
77
|
static whyDidYouRender = true;
|
|
67
78
|
|
|
68
|
-
constructor(props) {
|
|
79
|
+
protected constructor(props: { children?: ReactNode | ReactNode[]; shouldStatePersist?: boolean | undefined; websocket?: boolean | iCarbonWebSocketProps | undefined; } & P) {
|
|
69
80
|
|
|
70
81
|
super(props);
|
|
71
82
|
|
|
72
83
|
console.log('CarbonORM TSX CONSTRUCTOR');
|
|
73
84
|
|
|
74
|
-
|
|
85
|
+
Object.assign(this, {
|
|
86
|
+
instance: this
|
|
87
|
+
})
|
|
75
88
|
|
|
76
|
-
|
|
89
|
+
if (this.props.instanceId && persistentStateMap.has(this.props.instanceId)) {
|
|
90
|
+
|
|
91
|
+
this.state = persistentStateMap.get(this.props.instanceId) as S & iCarbonReactState;
|
|
77
92
|
|
|
78
93
|
} else {
|
|
79
94
|
|
|
95
|
+
// This should only ever be done here, when the full state is being trashed.
|
|
96
|
+
// todo - does this suck in context of multiple instances?
|
|
97
|
+
clearCache({
|
|
98
|
+
ignoreWarning: true
|
|
99
|
+
});
|
|
100
|
+
|
|
80
101
|
this.state = initialCarbonReactState as unknown as S & iCarbonReactState;
|
|
81
102
|
|
|
82
103
|
}
|
|
83
104
|
|
|
84
|
-
// This should only ever be done here, when the full state is being trashed.
|
|
85
|
-
clearCache({
|
|
86
|
-
ignoreWarning: true
|
|
87
|
-
});
|
|
88
|
-
|
|
89
105
|
/** We can think of our app as having one state; this state.
|
|
90
106
|
* Long-term, I'd like us to store this state to local storage and only load updates on reload...
|
|
91
107
|
* Class based components are far easier to manage state in local storage and pass state down to children.
|
|
@@ -95,23 +111,15 @@ const CarbonReact = class<P = {}, S = {}> extends Component<{
|
|
|
95
111
|
|
|
96
112
|
}
|
|
97
113
|
|
|
98
|
-
|
|
99
|
-
return CarbonReact.instance.state;
|
|
100
|
-
}
|
|
114
|
+
|
|
101
115
|
|
|
102
116
|
shouldComponentUpdate(
|
|
103
117
|
nextProps: Readonly<any>,
|
|
104
118
|
nextState: Readonly<iCarbonReactState>,
|
|
105
119
|
_nextContext: any): boolean {
|
|
106
120
|
|
|
107
|
-
if (this.props.
|
|
108
|
-
|
|
109
|
-
CarbonReact.persistentState = undefined;
|
|
110
|
-
|
|
111
|
-
} else {
|
|
112
|
-
|
|
113
|
-
CarbonReact.persistentState = nextState;
|
|
114
|
-
|
|
121
|
+
if (this.props.instanceId) {
|
|
122
|
+
persistentStateMap.set(this.props.instanceId, nextState);
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
changed(this.constructor.name + ' (C6Api)', 'props', this.props, nextProps);
|