@openmrs/esm-react-utils 4.3.2-pre.680 → 4.4.0
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/.turbo/turbo-build.log +5 -5
- package/__mocks__/openmrs-esm-state.mock.ts +12 -14
- package/dist/openmrs-esm-react-utils.js +1 -1
- package/dist/openmrs-esm-react-utils.js.map +1 -1
- package/package.json +8 -9
- package/src/ExtensionSlot.tsx +1 -1
- package/src/createUseStore.ts +19 -13
- package/src/useConfig.ts +8 -8
- package/src/useStore.ts +24 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-react-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "React utilities for OpenMRS.",
|
|
6
6
|
"browser": "dist/openmrs-esm-react-utils.js",
|
|
@@ -55,18 +55,17 @@
|
|
|
55
55
|
"react-i18next": "11.x"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@openmrs/esm-api": "^4.
|
|
59
|
-
"@openmrs/esm-config": "^4.
|
|
60
|
-
"@openmrs/esm-error-handling": "^4.
|
|
61
|
-
"@openmrs/esm-extensions": "^4.
|
|
62
|
-
"@openmrs/esm-globals": "^4.
|
|
58
|
+
"@openmrs/esm-api": "^4.4.0",
|
|
59
|
+
"@openmrs/esm-config": "^4.4.0",
|
|
60
|
+
"@openmrs/esm-error-handling": "^4.4.0",
|
|
61
|
+
"@openmrs/esm-extensions": "^4.4.0",
|
|
62
|
+
"@openmrs/esm-globals": "^4.4.0",
|
|
63
63
|
"dayjs": "^1.10.8",
|
|
64
64
|
"i18next": "^19.6.0",
|
|
65
65
|
"react": "^18.1.0",
|
|
66
66
|
"react-dom": "^18.1.0",
|
|
67
67
|
"react-i18next": "^11.16.9",
|
|
68
|
-
"rxjs": "^6.5.3"
|
|
69
|
-
"unistore": "^3.5.2"
|
|
68
|
+
"rxjs": "^6.5.3"
|
|
70
69
|
},
|
|
71
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "fdaefeded76bee4ec6503ca6590737e89ebe4b26"
|
|
72
71
|
}
|
package/src/ExtensionSlot.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useRef, useMemo } from "react";
|
|
2
|
-
import { ConnectedExtension } from "@openmrs/esm-extensions";
|
|
2
|
+
import type { ConnectedExtension } from "@openmrs/esm-extensions";
|
|
3
3
|
import { ComponentContext } from "./ComponentContext";
|
|
4
4
|
import { Extension } from "./Extension";
|
|
5
5
|
import { useExtensionSlot } from "./useExtensionSlot";
|
package/src/createUseStore.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/** @module @category Store */
|
|
2
2
|
import { useEffect, useMemo, useState } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import type { StoreApi } from "zustand";
|
|
4
4
|
|
|
5
5
|
export type Actions = Function | { [key: string]: Function };
|
|
6
|
-
export type BoundActions = { [key: string]:
|
|
6
|
+
export type BoundActions = { [key: string]: (...args: any[]) => void };
|
|
7
7
|
|
|
8
|
-
function bindActions<T>(store:
|
|
8
|
+
function bindActions<T>(store: StoreApi<T>, actions: Actions): BoundActions {
|
|
9
9
|
if (typeof actions == "function") {
|
|
10
10
|
actions = actions(store);
|
|
11
11
|
}
|
|
@@ -13,28 +13,34 @@ function bindActions<T>(store: Store<T>, actions: Actions) {
|
|
|
13
13
|
const bound = {};
|
|
14
14
|
|
|
15
15
|
for (let i in actions) {
|
|
16
|
-
bound[i] =
|
|
16
|
+
bound[i] = () => {
|
|
17
|
+
const args = arguments;
|
|
18
|
+
store.setState((state) => {
|
|
19
|
+
let _args = [state];
|
|
20
|
+
for (let i = 0; i < args.length; i++) {
|
|
21
|
+
_args.push(args[i]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return actions[i](_args);
|
|
25
|
+
});
|
|
26
|
+
};
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
return bound;
|
|
20
30
|
}
|
|
21
31
|
|
|
22
32
|
/** Avoid this; generally prefer to have clients use `useStore(yourStore)` */
|
|
23
|
-
export function createUseStore<T>(store:
|
|
33
|
+
export function createUseStore<T>(store: StoreApi<T>) {
|
|
24
34
|
function useStore(): T;
|
|
25
35
|
function useStore(actions: Actions): T & BoundActions;
|
|
26
36
|
function useStore(actions?: Actions): T & BoundActions;
|
|
27
37
|
function useStore(actions?: Actions) {
|
|
28
38
|
const [state, set] = useState(store.getState());
|
|
29
39
|
useEffect(() => store.subscribe((state) => set(state)), []);
|
|
30
|
-
let boundActions: BoundActions =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
() => bindActions(store, actions),
|
|
35
|
-
[store, actions]
|
|
36
|
-
);
|
|
37
|
-
}
|
|
40
|
+
let boundActions: BoundActions = useMemo(
|
|
41
|
+
() => (actions ? bindActions(store, actions) : {}),
|
|
42
|
+
[actions]
|
|
43
|
+
);
|
|
38
44
|
|
|
39
45
|
return { ...state, ...boundActions };
|
|
40
46
|
}
|
package/src/useConfig.ts
CHANGED
|
@@ -8,15 +8,15 @@ import {
|
|
|
8
8
|
ExtensionsConfigStore,
|
|
9
9
|
getExtensionConfigFromStore,
|
|
10
10
|
} from "@openmrs/esm-config";
|
|
11
|
-
import {
|
|
12
|
-
import { Store } from "unistore";
|
|
11
|
+
import type { StoreApi } from "zustand";
|
|
13
12
|
import isEqual from "lodash-es/isEqual";
|
|
13
|
+
import { ComponentContext, ExtensionData } from "./ComponentContext";
|
|
14
14
|
|
|
15
15
|
const promises: Record<string, Promise<ConfigObject>> = {};
|
|
16
16
|
const errorMessage = `No ComponentContext has been provided. This should come from "openmrsComponentDecorator".
|
|
17
17
|
Usually this is already applied when using "getAsyncLifecycle" or "getSyncLifecycle".`;
|
|
18
18
|
|
|
19
|
-
function readInitialConfig(store:
|
|
19
|
+
function readInitialConfig(store: StoreApi<ConfigStore>) {
|
|
20
20
|
return () => {
|
|
21
21
|
const state = store.getState();
|
|
22
22
|
|
|
@@ -29,7 +29,7 @@ function readInitialConfig(store: Store<ConfigStore>) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
function readInitialExtensionConfig(
|
|
32
|
-
store:
|
|
32
|
+
store: StoreApi<ExtensionsConfigStore>,
|
|
33
33
|
extension: ExtensionData | undefined
|
|
34
34
|
) {
|
|
35
35
|
if (extension) {
|
|
@@ -48,7 +48,7 @@ function readInitialExtensionConfig(
|
|
|
48
48
|
return null;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
function createConfigPromise(store:
|
|
51
|
+
function createConfigPromise(store: StoreApi<ConfigStore>) {
|
|
52
52
|
return new Promise<ConfigObject>((resolve) => {
|
|
53
53
|
const unsubscribe = store.subscribe((state) => {
|
|
54
54
|
if (state.loaded && state.config) {
|
|
@@ -60,7 +60,7 @@ function createConfigPromise(store: Store<ConfigStore>) {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
function createExtensionConfigPromise(
|
|
63
|
-
store:
|
|
63
|
+
store: StoreApi<ExtensionsConfigStore>,
|
|
64
64
|
extension: ExtensionData
|
|
65
65
|
) {
|
|
66
66
|
return new Promise<ConfigObject>((resolve) => {
|
|
@@ -78,7 +78,7 @@ function createExtensionConfigPromise(
|
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
function useConfigStore(store:
|
|
81
|
+
function useConfigStore(store: StoreApi<ConfigStore>) {
|
|
82
82
|
const [state, setState] = useState(readInitialConfig(store));
|
|
83
83
|
|
|
84
84
|
useEffect(() => {
|
|
@@ -93,7 +93,7 @@ function useConfigStore(store: Store<ConfigStore>) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
function useExtensionConfigStore(
|
|
96
|
-
store:
|
|
96
|
+
store: StoreApi<ExtensionsConfigStore>,
|
|
97
97
|
extension: ExtensionData | undefined
|
|
98
98
|
) {
|
|
99
99
|
const [config, setConfig] = useState<ConfigObject | null>(
|
package/src/useStore.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/** @module @category Store */
|
|
2
2
|
import { subscribeTo } from "@openmrs/esm-state";
|
|
3
3
|
import { useEffect, useMemo, useState } from "react";
|
|
4
|
-
import {
|
|
5
|
-
import { Actions, BoundActions } from "./createUseStore";
|
|
4
|
+
import type { StoreApi } from "zustand";
|
|
5
|
+
import type { Actions, BoundActions } from "./createUseStore";
|
|
6
6
|
|
|
7
|
-
function bindActions<T>(store:
|
|
7
|
+
function bindActions<T>(store: StoreApi<T>, actions: Actions): BoundActions {
|
|
8
8
|
if (typeof actions == "function") {
|
|
9
9
|
actions = actions(store);
|
|
10
10
|
}
|
|
@@ -12,7 +12,17 @@ function bindActions<T>(store: Store<T>, actions: Actions) {
|
|
|
12
12
|
const bound = {};
|
|
13
13
|
|
|
14
14
|
for (let i in actions) {
|
|
15
|
-
bound[i] =
|
|
15
|
+
bound[i] = () => {
|
|
16
|
+
const args = arguments;
|
|
17
|
+
store.setState((state) => {
|
|
18
|
+
let _args = [state];
|
|
19
|
+
for (let i = 0; i < args.length; i++) {
|
|
20
|
+
_args.push(args[i]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return actions[i](_args);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
16
26
|
}
|
|
17
27
|
|
|
18
28
|
return bound;
|
|
@@ -20,37 +30,36 @@ function bindActions<T>(store: Store<T>, actions: Actions) {
|
|
|
20
30
|
|
|
21
31
|
const defaultSelectFunction = (x) => x;
|
|
22
32
|
|
|
23
|
-
function useStore<T, U>(store:
|
|
24
|
-
function useStore<T, U>(store:
|
|
33
|
+
function useStore<T, U>(store: StoreApi<T>): T;
|
|
34
|
+
function useStore<T, U>(store: StoreApi<T>, select: (state: T) => U): U;
|
|
25
35
|
function useStore<T, U>(
|
|
26
|
-
store:
|
|
36
|
+
store: StoreApi<T>,
|
|
27
37
|
select: undefined,
|
|
28
38
|
actions: Actions
|
|
29
39
|
): T & BoundActions;
|
|
30
40
|
function useStore<T, U>(
|
|
31
|
-
store:
|
|
41
|
+
store: StoreApi<T>,
|
|
32
42
|
select: (state: T) => U,
|
|
33
43
|
actions: Actions
|
|
34
44
|
): U & BoundActions;
|
|
35
45
|
function useStore<T, U>(
|
|
36
|
-
store:
|
|
46
|
+
store: StoreApi<T>,
|
|
37
47
|
select: (state: T) => U = defaultSelectFunction,
|
|
38
48
|
actions?: Actions
|
|
39
49
|
) {
|
|
40
50
|
const [state, setState] = useState<U>(() => select(store.getState()));
|
|
41
51
|
useEffect(() => subscribeTo(store, select, setState), [store, select]);
|
|
42
52
|
|
|
43
|
-
let boundActions: BoundActions =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
53
|
+
let boundActions: BoundActions = useMemo(
|
|
54
|
+
() => (actions ? bindActions(store, actions) : {}),
|
|
55
|
+
[store, actions]
|
|
56
|
+
);
|
|
48
57
|
|
|
49
58
|
return { ...state, ...boundActions };
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
function useStoreWithActions<T>(
|
|
53
|
-
store:
|
|
62
|
+
store: StoreApi<T>,
|
|
54
63
|
actions: Actions
|
|
55
64
|
): T & BoundActions {
|
|
56
65
|
return useStore(store, defaultSelectFunction, actions);
|