@carbonorm/carbonreact 4.0.15 → 4.0.17

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "4.0.15",
4
+ "version": "4.0.17",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
@@ -1,15 +1,16 @@
1
1
  import changed from "hoc/changed";
2
- import { GlobalHistory } from "hoc/GlobalHistory";
2
+ import {GlobalHistory} from "hoc/GlobalHistory";
3
3
  import hexToRgb from "hoc/hexToRgb";
4
- import { Component, Context, createContext, ReactElement, ReactNode } from 'react';
5
- import { ToastContainer } from 'react-toastify';
4
+ import {Component, Context, createContext, ReactElement, ReactNode} from 'react';
5
+ import {ToastContainer} from 'react-toastify';
6
6
  import 'react-toastify/dist/ReactToastify.min.css';
7
7
  import BackendThrowable from 'components/Errors/BackendThrowable';
8
8
  import Nest from 'components/Nest/Nest';
9
- import { initialRestfulObjectsState, iRestfulObjectArrayTypes } from "variables/C6";
10
- import CarbonWebSocket, { iCarbonWebSocketProps } from "./components/WebSocket/CarbonWebSocket";
11
- import updateRestfulObjectArrays, { iUpdateRestfulObjectArrays } from "./hoc/updateRestfulObjectArrays";
12
- import deleteRestfulObjectArrays, { iDeleteRestfulObjectArrays } from "./hoc/deleteRestfulObjectArrays";
9
+ import {initialRestfulObjectsState, iRestfulObjectArrayTypes} from "variables/C6";
10
+ import CarbonWebSocket, {iCarbonWebSocketProps} from "./components/WebSocket/CarbonWebSocket";
11
+ import updateRestfulObjectArrays, {iUpdateRestfulObjectArrays} from "./hoc/updateRestfulObjectArrays";
12
+ import deleteRestfulObjectArrays, {iDeleteRestfulObjectArrays} from "./hoc/deleteRestfulObjectArrays";
13
+ import {BrowserRouter, HashRouter, MemoryRouter, Routes} from "react-router-dom";
13
14
 
14
15
  export type tStatefulApiData<T extends { [key: string]: any } = {}> = T[] | undefined | null;
15
16
 
@@ -44,15 +45,21 @@ export function isJsonString(str: string) {
44
45
  return true;
45
46
  }
46
47
 
48
+ export enum eRouterType {
49
+ BrowserRouter,
50
+ HashRouter,
51
+ MemoryRouter,
52
+ }
53
+
47
54
  abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
48
55
  children?: ReactNode | ReactNode[],
49
56
  instanceId?: string,
50
57
  persistentState?: boolean,
58
+ routerType?: eRouterType,
51
59
  websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false
52
60
  } & P, S> {
53
61
 
54
62
  private static allInstances = new Map<string, CarbonReact<any, any>>();
55
- private static activeInstances = new Map<string, CarbonReact<any, any>>();
56
63
 
57
64
  context: Context<S & iCarbonReactState> = createContext(this.state);
58
65
  protected target: typeof CarbonReact;
@@ -64,8 +71,8 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
64
71
  const identifier = this.generateIdentifier(instanceId);
65
72
 
66
73
  if (undefined !== instanceId) {
67
- if (CarbonReact.activeInstances.has(identifier)) {
68
- return CarbonReact.activeInstances.get(identifier) as T;
74
+ if (CarbonReact.allInstances.has(identifier)) {
75
+ return CarbonReact.allInstances.get(identifier) as T;
69
76
  }
70
77
  throw new Error(`No instance has been instantiated yet for class (${this.name}) with instanceId (${instanceId})`);
71
78
  }
@@ -75,6 +82,7 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
75
82
  }
76
83
 
77
84
  return this._instance as T;
85
+
78
86
  }
79
87
 
80
88
  static get instance() {
@@ -111,13 +119,6 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
111
119
 
112
120
  const identifier = this.generateIdentifier();
113
121
 
114
- if (CarbonReact.activeInstances.has(identifier)) {
115
- throw new Error(`${identifier} instance already exists in the DOM! Each instance should have a unique instanceId.`);
116
- }
117
-
118
- // Register the new instance
119
- CarbonReact.activeInstances.set(identifier, this);
120
-
121
122
  if (props.persistentState && CarbonReact.allInstances.has(identifier)) {
122
123
  // Reuse the state from the existing instance
123
124
  this.state = CarbonReact.allInstances.get(identifier)!.state as S & iCarbonReactState;
@@ -144,11 +145,6 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
144
145
  return this.props.instanceId ? `${className}-${this.props.instanceId}` : className;
145
146
  }
146
147
 
147
- componentWillUnmount() {
148
- const identifier = this.generateIdentifier();
149
- CarbonReact.activeInstances.delete(identifier);
150
- }
151
-
152
148
  shouldComponentUpdate(
153
149
  nextProps: Readonly<P>,
154
150
  nextState: Readonly<S>,
@@ -171,6 +167,19 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
171
167
  }
172
168
  }
173
169
 
170
+ reactRouterContext(children: ReactElement) {
171
+ switch (this.props.routerType) {
172
+ case eRouterType.BrowserRouter:
173
+ return <BrowserRouter>{children}</BrowserRouter>
174
+ case eRouterType.MemoryRouter:
175
+ return <MemoryRouter initialEntries={['/']}>{children}</MemoryRouter>
176
+ case eRouterType.HashRouter:
177
+ return <HashRouter>{children}</HashRouter>
178
+ default:
179
+ throw new Error('Invalid routerType');
180
+ }
181
+ }
182
+
174
183
  render(): ReactElement {
175
184
  console.log('CarbonORM TSX RENDER');
176
185
 
@@ -178,27 +187,28 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
178
187
 
179
188
  console.log('%c color (' + colorHex + ')', 'color: ' + colorHex);
180
189
 
181
- const nest = <Nest position={'fixed'} backgroundColor={''} color={hexToRgb(colorHex)} count={100} />;
190
+ const nest = <Nest position={'fixed'} backgroundColor={''} color={hexToRgb(colorHex)} count={100}/>;
182
191
 
183
192
  if (this.state.backendThrowable.length > 0) {
184
193
  return <>
185
194
  {nest}
186
- <BackendThrowable instance={this} />
195
+ <BackendThrowable instance={this}/>
187
196
  </>;
188
197
  }
189
198
 
199
+ this.context = createContext(this.state)
190
200
  const Context = this.context.Provider;
191
201
 
192
- return <>
193
- <GlobalHistory />
202
+ return this.reactRouterContext(<Routes>
203
+ <GlobalHistory/>
194
204
  {this.props.websocket &&
195
205
  <CarbonWebSocket<P, S> {...(false !== this.props.websocket ? this.props.websocket : {})}
196
- instance={this} />}
206
+ instance={this}/>}
197
207
  <Context value={this.state}>
198
208
  {this.props.children}
199
209
  </Context>
200
- <ToastContainer />
201
- </>;
210
+ <ToastContainer/>
211
+ </Routes>);
202
212
  }
203
213
  }
204
214