@openmrs/esm-react-utils 3.2.1-pre.1020 → 3.2.1-pre.1025

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-react-utils",
3
- "version": "3.2.1-pre.1020",
3
+ "version": "3.2.1-pre.1025",
4
4
  "license": "MPL-2.0",
5
5
  "description": "React utilities for OpenMRS.",
6
6
  "browser": "dist/openmrs-esm-react-utils.js",
@@ -55,11 +55,11 @@
55
55
  "react-i18next": "11.x"
56
56
  },
57
57
  "devDependencies": {
58
- "@openmrs/esm-api": "^3.2.1-pre.1020",
59
- "@openmrs/esm-config": "^3.2.1-pre.1020",
60
- "@openmrs/esm-error-handling": "^3.2.1-pre.1020",
61
- "@openmrs/esm-extensions": "^3.2.1-pre.1020",
62
- "@openmrs/esm-globals": "^3.2.1-pre.1020",
58
+ "@openmrs/esm-api": "^3.2.1-pre.1025",
59
+ "@openmrs/esm-config": "^3.2.1-pre.1025",
60
+ "@openmrs/esm-error-handling": "^3.2.1-pre.1025",
61
+ "@openmrs/esm-extensions": "^3.2.1-pre.1025",
62
+ "@openmrs/esm-globals": "^3.2.1-pre.1025",
63
63
  "dayjs": "^1.10.8",
64
64
  "i18next": "^19.6.0",
65
65
  "react": "^16.13.1",
@@ -68,5 +68,5 @@
68
68
  "rxjs": "^6.5.3",
69
69
  "unistore": "^3.5.2"
70
70
  },
71
- "gitHead": "05aa900c651fe7d95b15fcdc746f7a850c60e5ac"
71
+ "gitHead": "feb09b45245797ce9d01c6cfbebd315ebdfaaf54"
72
72
  }
@@ -18,6 +18,7 @@ function bindActions<T>(store: Store<T>, actions: Actions) {
18
18
  return bound;
19
19
  }
20
20
 
21
+ /** Avoid this; generally prefer to have clients use `useStore(yourStore)` */
21
22
  export function createUseStore<T>(store: Store<T>) {
22
23
  function useStore(): T;
23
24
  function useStore(actions: Actions): T & BoundActions;
package/src/index.ts CHANGED
@@ -14,7 +14,6 @@ export * from "./useConnectivity";
14
14
  export * from "./usePatient";
15
15
  export * from "./useCurrentPatient";
16
16
  export * from "./useExtensionInternalStore";
17
- export * from "./useExtensionSlotConfig";
18
17
  export * from "./useExtensionSlot";
19
18
  export * from "./useExtensionSlotMeta";
20
19
  export * from "./useExtensionStore";
@@ -26,7 +25,6 @@ export * from "./useOnClickOutside";
26
25
  export * from "./UserHasAccess";
27
26
  export * from "./useSessionUser";
28
27
  export * from "./useStore";
29
- export * from "./useStoreState";
30
28
  export * from "./useVisit";
31
29
  export * from "./useVisitTypes";
32
30
  export * from "./usePagination";
@@ -23,7 +23,7 @@ export function useAssignedExtensions(slotName: string) {
23
23
  }
24
24
  update(getExtensionStore().getState());
25
25
  return getExtensionStore().subscribe(update);
26
- }, [slotName]);
26
+ }, [slotName, extensions]);
27
27
 
28
28
  return extensions;
29
29
  }
@@ -4,7 +4,9 @@ import {
4
4
  } from "@openmrs/esm-extensions";
5
5
  import { createUseStore } from "./createUseStore";
6
6
 
7
- /** @internal */
7
+ /** @internal
8
+ * @deprecated Use `useStore(getExtensionInternalStore())`
9
+ */
8
10
  export const useExtensionInternalStore = createUseStore<ExtensionInternalStore>(
9
11
  getExtensionInternalStore()
10
12
  );
package/src/useStore.ts CHANGED
@@ -1,11 +1,58 @@
1
+ import { subscribeTo } from "@openmrs/esm-state";
2
+ import { useEffect, useMemo, useState } from "react";
1
3
  import { Store } from "unistore";
2
- import { Actions, BoundActions, createUseStore } from "./createUseStore";
4
+ import { Actions, BoundActions } from "./createUseStore";
3
5
 
4
- export function useStore<T>(store: Store<T>): T;
5
- export function useStore<T>(
6
+ function bindActions<T>(store: Store<T>, actions: Actions) {
7
+ if (typeof actions == "function") {
8
+ actions = actions(store);
9
+ }
10
+
11
+ const bound = {};
12
+
13
+ for (let i in actions) {
14
+ bound[i] = store.action(actions[i]);
15
+ }
16
+
17
+ return bound;
18
+ }
19
+
20
+ const defaultSelectFunction = (x) => x;
21
+
22
+ function useStore<T, U>(store: Store<T>): T;
23
+ function useStore<T, U>(store: Store<T>, select: (state: T) => U): U;
24
+ function useStore<T, U>(
6
25
  store: Store<T>,
26
+ select: undefined,
7
27
  actions: Actions
8
28
  ): T & BoundActions;
9
- export function useStore<T>(store: Store<T>, actions?: Actions) {
10
- return createUseStore<T>(store)(actions);
29
+ function useStore<T, U>(
30
+ store: Store<T>,
31
+ select: (state: T) => U,
32
+ actions: Actions
33
+ ): U & BoundActions;
34
+ function useStore<T, U>(
35
+ store: Store<T>,
36
+ select: (state: T) => U = defaultSelectFunction,
37
+ actions?: Actions
38
+ ) {
39
+ const [state, setState] = useState<U>(() => select(store.getState()));
40
+ useEffect(() => subscribeTo(store, select, setState), [store, select]);
41
+
42
+ let boundActions: BoundActions = {};
43
+
44
+ if (actions) {
45
+ boundActions = useMemo(() => bindActions(store, actions), [store, actions]);
46
+ }
47
+
48
+ return { ...state, ...boundActions };
49
+ }
50
+
51
+ function useStoreWithActions<T>(
52
+ store: Store<T>,
53
+ actions: Actions
54
+ ): T & BoundActions {
55
+ return useStore(store, defaultSelectFunction, actions);
11
56
  }
57
+
58
+ export { useStore, useStoreWithActions };
@@ -1,24 +0,0 @@
1
- import { useCallback } from "react";
2
- import {
3
- ExtensionSlotConfigObject,
4
- ExtensionSlotConfigStore,
5
- getExtensionSlotConfigStore,
6
- } from "@openmrs/esm-config";
7
- import { useStoreState } from "./useStoreState";
8
-
9
- const defaultConfig: ExtensionSlotConfigObject = {
10
- add: [],
11
- order: [],
12
- remove: [],
13
- };
14
-
15
- /** @internal */
16
- export function useExtensionSlotConfig(slotName: string) {
17
- const store = getExtensionSlotConfigStore(slotName);
18
- const select = useCallback(
19
- (s: ExtensionSlotConfigStore) => s.config,
20
- [slotName]
21
- );
22
- const config = useStoreState(store, select);
23
- return config || defaultConfig;
24
- }
@@ -1,11 +0,0 @@
1
- import { subscribeTo } from "@openmrs/esm-state";
2
- import { useEffect, useState } from "react";
3
- import { Store } from "unistore";
4
-
5
- export function useStoreState<T, U>(store: Store<T>, select: (state: T) => U) {
6
- const [state, setState] = useState<U>(() => select(store.getState()));
7
-
8
- useEffect(() => subscribeTo(store, select, setState), [store, select]);
9
-
10
- return state;
11
- }