@carbonorm/carbonreact 4.0.2 → 4.0.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.
- package/dist/CarbonReact.d.ts +7 -7
- package/dist/components/Alert/Alert.d.ts +6 -6
- package/dist/components/Errors/BackendThrowable.d.ts +3 -3
- package/dist/components/WebSocket/CarbonWebSocket.d.ts +5 -5
- package/dist/hoc/deleteRestfulObjectArrays.d.ts +2 -6
- package/dist/hoc/updateRestfulObjectArrays.d.ts +3 -7
- package/dist/index.cjs.js +11 -6
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +11 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/variables/C6.d.ts +842 -0
- package/package.json +1 -1
- package/src/CarbonReact.tsx +20 -12
- package/src/components/Alert/Alert.tsx +8 -10
- package/src/components/Errors/BackendThrowable.tsx +3 -3
- package/src/components/WebSocket/CarbonWebSocket.tsx +6 -6
- package/src/hoc/deleteRestfulObjectArrays.tsx +2 -2
- package/src/hoc/updateRestfulObjectArrays.tsx +3 -3
- package/src/variables/C6.tsx +1 -0
package/package.json
CHANGED
package/src/CarbonReact.tsx
CHANGED
|
@@ -48,22 +48,30 @@ export function isJsonString(str: string) {
|
|
|
48
48
|
return true;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
abstract class CarbonReact<P = {}, S extends
|
|
51
|
+
abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
|
|
52
52
|
children?: ReactNode | ReactNode[],
|
|
53
53
|
instanceId?: string,
|
|
54
|
-
websocket?: Omit<iCarbonWebSocketProps, "instance"> |
|
|
54
|
+
websocket?: Omit<iCarbonWebSocketProps<P,S>, "instance"> | false
|
|
55
55
|
} & P, S> {
|
|
56
56
|
|
|
57
57
|
private static persistentStateMap = new Map<string, { [key: string]: any; }>();
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// Private static member
|
|
62
|
-
// we actually implement this in the constructor todo - test this
|
|
63
|
-
protected static instance: CarbonReact;
|
|
59
|
+
// Context is for functional components to access the state of this class efficiently
|
|
60
|
+
context: Context<S & iCarbonReactState> = createContext(this.state);
|
|
64
61
|
|
|
65
62
|
protected target: typeof CarbonReact;
|
|
66
63
|
|
|
64
|
+
protected static _instance: typeof this;
|
|
65
|
+
|
|
66
|
+
static get instance() {
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static set instance(instance: typeof this) {
|
|
71
|
+
this._instance = instance;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
67
75
|
protected updateRestfulObjectArrays = <ObjectType extends { [key: string]: any; } = {}>
|
|
68
76
|
(rest: Omit<iUpdateRestfulObjectArrays<ObjectType, S, P>, "instance">) => updateRestfulObjectArrays<ObjectType, S, P>({
|
|
69
77
|
instance: this,
|
|
@@ -86,7 +94,7 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = typeof in
|
|
|
86
94
|
protected constructor(props: {
|
|
87
95
|
children?: ReactNode | ReactNode[];
|
|
88
96
|
shouldStatePersist?: boolean | undefined;
|
|
89
|
-
websocket?: boolean | iCarbonWebSocketProps | undefined;
|
|
97
|
+
websocket?: boolean | iCarbonWebSocketProps<P,S> | undefined;
|
|
90
98
|
} & P) {
|
|
91
99
|
|
|
92
100
|
super(props);
|
|
@@ -98,7 +106,7 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = typeof in
|
|
|
98
106
|
// this is the magic that allows each class that's extends this to have a static instance - a singleton pattern
|
|
99
107
|
// new.target is a meta-property introduced in ES6 that references the constructor that was directly invoked with the new keyword.
|
|
100
108
|
Object.assign(new.target, {
|
|
101
|
-
|
|
109
|
+
_instance: this
|
|
102
110
|
})
|
|
103
111
|
|
|
104
112
|
if (this.props.instanceId && CarbonReact.persistentStateMap.has(this.props.instanceId)) {
|
|
@@ -169,7 +177,7 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = typeof in
|
|
|
169
177
|
|
|
170
178
|
return <>
|
|
171
179
|
{nest}
|
|
172
|
-
<BackendThrowable instance={
|
|
180
|
+
<BackendThrowable instance={this}/>
|
|
173
181
|
</>;
|
|
174
182
|
|
|
175
183
|
}
|
|
@@ -179,8 +187,8 @@ abstract class CarbonReact<P = {}, S extends { [key: string]: any; } = typeof in
|
|
|
179
187
|
return <>
|
|
180
188
|
<GlobalHistory/>
|
|
181
189
|
{this.props.websocket &&
|
|
182
|
-
<CarbonWebSocket {...(
|
|
183
|
-
instance={
|
|
190
|
+
<CarbonWebSocket<P,S> {...(false !== this.props.websocket ? this.props.websocket : {})}
|
|
191
|
+
instance={this}/>}
|
|
184
192
|
<Context value={this.state}>
|
|
185
193
|
{this.props.children}
|
|
186
194
|
</Context>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import classNames from "classnames";
|
|
2
|
-
import CarbonReact from "CarbonReact";
|
|
2
|
+
import CarbonReact, {iCarbonReactState} from "CarbonReact";
|
|
3
3
|
import {ReactNode} from "react";
|
|
4
4
|
import Popup from "components/Popup/Popup";
|
|
5
5
|
import getStyles from "hoc/getStyles";
|
|
@@ -18,10 +18,10 @@ export interface iAlertButtonOptions {
|
|
|
18
18
|
color: "default" | "primary" | "secondary" | "inherit" | "danger" | "info" | "success" | "warning" | undefined,
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export interface iAlert {
|
|
21
|
+
export interface iAlert<P,S> {
|
|
22
22
|
title: string,
|
|
23
23
|
text: string,
|
|
24
|
-
instance: CarbonReact,
|
|
24
|
+
instance: CarbonReact<P,S>,
|
|
25
25
|
component?: ReactNode,
|
|
26
26
|
icon?: "warning" | "error" | "success" | "info" | "question" | null,
|
|
27
27
|
buttons?: (iAlertButtonOptions)[] | undefined, //['No thanks!', 'Yes, Delete it'],
|
|
@@ -33,23 +33,21 @@ export interface iAlert {
|
|
|
33
33
|
backendThrowable?: { [key: string]: any },
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export function addAlert(props: iAlert) {
|
|
37
|
-
|
|
36
|
+
export function addAlert<P, S extends Partial<iCarbonReactState>>(props: iAlert<P, S>) {
|
|
38
37
|
props.instance.setState(previousState => ({
|
|
39
38
|
alertsWaiting: previousState.alertsWaiting.length === 0
|
|
40
39
|
? [props]
|
|
41
40
|
: [...previousState.alertsWaiting, props]
|
|
42
|
-
}))
|
|
43
|
-
|
|
41
|
+
}));
|
|
44
42
|
}
|
|
45
43
|
|
|
46
|
-
export default function Alert({
|
|
44
|
+
export default function Alert<P,S extends Partial<iCarbonReactState>>({
|
|
47
45
|
instance
|
|
48
|
-
}: { instance: CarbonReact }) {
|
|
46
|
+
}: { instance: CarbonReact<P,S> }) {
|
|
49
47
|
|
|
50
48
|
const {alertsWaiting, backendThrowable} = instance.state
|
|
51
49
|
|
|
52
|
-
let alert: iAlert | undefined = undefined;
|
|
50
|
+
let alert: iAlert<P,S> | undefined = undefined;
|
|
53
51
|
|
|
54
52
|
const alertWaiting = alertsWaiting.length + backendThrowable.length
|
|
55
53
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import styles from './style.module.scss';
|
|
2
2
|
import OutsideClickHandler from 'react-outside-click-handler';
|
|
3
|
-
import CarbonReact from "../../CarbonReact";
|
|
3
|
+
import CarbonReact, {iCarbonReactState} from "../../CarbonReact";
|
|
4
4
|
import {ReactElement} from "react";
|
|
5
5
|
|
|
6
|
-
export default (props: {
|
|
7
|
-
instance: CarbonReact,
|
|
6
|
+
export default <P,S extends Partial<iCarbonReactState>>(props: {
|
|
7
|
+
instance: CarbonReact<P,S>,
|
|
8
8
|
}): ReactElement => {
|
|
9
9
|
|
|
10
10
|
const {instance} = props;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import CarbonReact, {iCarbonReactState, isJsonString} from "CarbonReact";
|
|
2
2
|
import {addAlert} from "../Alert/Alert";
|
|
3
3
|
import {useEffectOnce} from "../../api/hoc/useEffectOnce";
|
|
4
4
|
import {tC6Tables, tC6RestApi} from "@carbonorm/carbonnode";
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
export interface iCarbonWebSocketProps {
|
|
7
|
+
export interface iCarbonWebSocketProps<P,S> {
|
|
8
8
|
url?: string,
|
|
9
9
|
timeoutSeconds?: number,
|
|
10
10
|
heartbeatSeconds?: number,
|
|
11
|
-
instance: CarbonReact,
|
|
11
|
+
instance: CarbonReact<P,S>,
|
|
12
12
|
TABLES?: tC6Tables,
|
|
13
13
|
WsLiveUpdates?: tC6RestApi,
|
|
14
14
|
}
|
|
@@ -17,7 +17,7 @@ export interface iCarbonWebSocketProps {
|
|
|
17
17
|
* @function connect
|
|
18
18
|
* This function establishes a connection with the websocket and also ensures constant reconnection if connection closes
|
|
19
19
|
**/
|
|
20
|
-
export function initiateWebsocket(props: iCarbonWebSocketProps) {
|
|
20
|
+
export function initiateWebsocket<P,S extends Partial<iCarbonReactState>>(props: iCarbonWebSocketProps<P,S>) {
|
|
21
21
|
|
|
22
22
|
let {
|
|
23
23
|
instance,
|
|
@@ -34,7 +34,7 @@ export function initiateWebsocket(props: iCarbonWebSocketProps) {
|
|
|
34
34
|
if (!("WebSocket" in window)) {
|
|
35
35
|
|
|
36
36
|
// todo - store that this has been shown in the state
|
|
37
|
-
addAlert({
|
|
37
|
+
addAlert<P,S>({
|
|
38
38
|
title: 'Browser does not support websockets, live updates will fail. You may need to refresh the page to see the newest content.',
|
|
39
39
|
text: 'Please use a modern browser.',
|
|
40
40
|
icon: 'warning',
|
|
@@ -286,7 +286,7 @@ export function initiateWebsocket(props: iCarbonWebSocketProps) {
|
|
|
286
286
|
|
|
287
287
|
}
|
|
288
288
|
|
|
289
|
-
export default function (props: iCarbonWebSocketProps) {
|
|
289
|
+
export default function <P,S extends Partial<iCarbonReactState>>(props: iCarbonWebSocketProps<P,S>) {
|
|
290
290
|
|
|
291
291
|
useEffectOnce(() => {
|
|
292
292
|
|
|
@@ -5,7 +5,7 @@ export interface iDeleteRestfulObjectArrays<
|
|
|
5
5
|
ObjectType extends {
|
|
6
6
|
[key: string]: any
|
|
7
7
|
} = {},
|
|
8
|
-
S
|
|
8
|
+
S = CarbonReact['state'],
|
|
9
9
|
P = CarbonReact['props']
|
|
10
10
|
> {
|
|
11
11
|
instance: CarbonReact<P, S>,
|
|
@@ -19,7 +19,7 @@ export default function deleteRestfulObjectArrays<
|
|
|
19
19
|
ObjectType extends {
|
|
20
20
|
[key: string]: any
|
|
21
21
|
} = {},
|
|
22
|
-
S
|
|
22
|
+
S = CarbonReact['state'],
|
|
23
23
|
P = CarbonReact['props']
|
|
24
24
|
>({
|
|
25
25
|
instance,
|
|
@@ -12,7 +12,7 @@ export interface iUpdateRestfulObjectArrays<
|
|
|
12
12
|
ObjectType extends {
|
|
13
13
|
[key: string]: any;
|
|
14
14
|
} = {},
|
|
15
|
-
S extends
|
|
15
|
+
S extends CarbonReact['state'] = CarbonReact['state'],
|
|
16
16
|
P = CarbonReact['props']
|
|
17
17
|
> {
|
|
18
18
|
instance: CarbonReact<P, S>;
|
|
@@ -36,7 +36,7 @@ export default function updateRestfulObjectArrays<
|
|
|
36
36
|
ObjectType extends {
|
|
37
37
|
[key: string]: any;
|
|
38
38
|
} = {},
|
|
39
|
-
S extends
|
|
39
|
+
S extends iCarbonReactState = CarbonReact['state'],
|
|
40
40
|
P = CarbonReact['props']
|
|
41
41
|
>({
|
|
42
42
|
instance,
|
|
@@ -52,7 +52,7 @@ export default function updateRestfulObjectArrays<
|
|
|
52
52
|
type ValidObject = SubsetMatching<S & iCarbonReactState, tStatefulApiData<ObjectType>>;
|
|
53
53
|
|
|
54
54
|
instance.setState((
|
|
55
|
-
previousBootstrapState: Readonly<S
|
|
55
|
+
previousBootstrapState: Readonly<S>,
|
|
56
56
|
props: Readonly<P>
|
|
57
57
|
): Pick<S & iCarbonReactState, keyof S> | null => {
|
|
58
58
|
|
package/src/variables/C6.tsx
CHANGED