@adaptabletools/adaptable-react-aggrid-cjs 19.2.4 → 20.0.0-canary.1

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.
@@ -3,49 +3,78 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const React = tslib_1.__importStar(require("react"));
5
5
  const react_1 = require("react");
6
+ const ag_grid_enterprise_1 = require("ag-grid-enterprise");
6
7
  const agGrid_1 = require("@adaptabletools/adaptable-cjs/agGrid");
7
8
  const setupFrameworkComponents_1 = require("../setupFrameworkComponents");
8
9
  const join_1 = tslib_1.__importDefault(require("../utils/join"));
9
10
  const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max));
10
- const AdaptableReact = (_a) => {
11
- var { adaptableOptions, gridOptions, onAdaptableReady } = _a, props = tslib_1.__rest(_a, ["adaptableOptions", "gridOptions", "onAdaptableReady"]);
11
+ const columnApiModule = ag_grid_enterprise_1.ColumnApiModule;
12
+ const AdaptableReact = ({ adaptableOptions, gridOptions, onAdaptableReady, ...props }) => {
12
13
  const seedId = (0, react_1.useMemo)(() => `${getRandomInt(1000)}-${Date.now()}`, []);
13
- // TODO: not sure this is used anywhere
14
14
  const adaptableContainerId = `adaptable-${seedId}`;
15
15
  const [adaptableJSXElement, setAdaptableJSXElement] = (0, react_1.useState)(null);
16
+ const isMounted = (0, react_1.useRef)(true);
17
+ const adaptableApiRef = (0, react_1.useRef)(null);
16
18
  const containerRef = (0, react_1.useMemo)(() => {
17
- let adaptableApi;
19
+ const cleanup = () => {
20
+ if (adaptableApiRef.current) {
21
+ adaptableApiRef.current.destroy({
22
+ unmount: true,
23
+ destroyAgGrid: false,
24
+ });
25
+ adaptableApiRef.current = null;
26
+ }
27
+ };
18
28
  return (node) => {
19
29
  if (node) {
30
+ isMounted.current = true;
20
31
  agGrid_1._AdaptableAgGrid
21
32
  ._initInternal({
22
33
  modules: props.modules,
23
- adaptableOptions: Object.assign(Object.assign({}, adaptableOptions), { containerOptions: Object.assign(Object.assign({}, adaptableOptions.containerOptions), { adaptableContainer: node }) }),
34
+ adaptableOptions: {
35
+ ...adaptableOptions,
36
+ containerOptions: {
37
+ ...adaptableOptions.containerOptions,
38
+ adaptableContainer: node,
39
+ },
40
+ },
24
41
  gridOptions,
25
42
  variant: 'react',
26
43
  renderAgGridFrameworkComponent: props.renderAgGridFrameworkComponent,
44
+ getAgGridColumnApiModuleReference: () => {
45
+ return columnApiModule;
46
+ },
27
47
  })
28
48
  .then((api) => {
29
- adaptableApi = api;
49
+ // If the component was unmounted while initializing, cleanup immediately
50
+ if (!isMounted.current) {
51
+ api.destroy({
52
+ unmount: true,
53
+ destroyAgGrid: false,
54
+ });
55
+ return;
56
+ }
57
+ adaptableApiRef.current = api;
30
58
  //see #no_additional_react_root
31
59
  setAdaptableJSXElement(api.internalApi.getAdaptableJSXElement());
32
60
  if (onAdaptableReady) {
33
- adaptableApi.eventApi.on('AdaptableReady', onAdaptableReady);
61
+ api.eventApi.on('AdaptableReady', onAdaptableReady);
34
62
  }
35
- (0, setupFrameworkComponents_1.setupFrameworkComponents)(adaptableApi);
63
+ (0, setupFrameworkComponents_1.setupFrameworkComponents)(api);
36
64
  });
37
65
  }
38
66
  else {
39
- if (adaptableApi) {
40
- adaptableApi.destroy({
41
- unmount: true,
42
- destroyAgGrid: false,
43
- });
44
- }
45
- adaptableApi = null;
67
+ isMounted.current = false;
68
+ cleanup();
46
69
  }
47
70
  };
48
71
  }, []);
72
+ // Cleanup on component unmount
73
+ React.useEffect(() => {
74
+ return () => {
75
+ isMounted.current = false;
76
+ };
77
+ }, []);
49
78
  return (React.createElement("div", { style: props.style, ref: containerRef, id: adaptableContainerId, className: (0, join_1.default)(props.className, 'ab__react-wrapper') }, adaptableJSXElement));
50
79
  };
51
80
  exports.default = AdaptableReact;
@@ -0,0 +1,4 @@
1
+ import { AdaptableState, Layout } from "@adaptabletools/adaptable-cjs/types";
2
+ export declare function useAdaptableState(): AdaptableState | null;
3
+ export declare function useAdaptableState<T>(selector: (state: AdaptableState) => T): T | null;
4
+ export declare const useCurrentLayout: () => [Layout | null, (layoutName: string) => void];
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCurrentLayout = exports.useAdaptableState = void 0;
4
+ const AdaptableProvider_1 = require("./AdaptableProvider");
5
+ const react_1 = require("react");
6
+ function useAdaptableState(selector) {
7
+ const { adaptableApi } = (0, AdaptableProvider_1.useAdaptableAgGridContext)();
8
+ const adaptable = adaptableApi?.internalApi.getAdaptableInstance();
9
+ const store = adaptable?.adaptableStore;
10
+ const [_refreshKey, setRefreshKey] = (0, react_1.useState)(0);
11
+ const resultRef = (0, react_1.useRef)(null);
12
+ const getResult = () => {
13
+ if (!store) {
14
+ return null;
15
+ }
16
+ const state = store.getCurrentStorageState();
17
+ if (!state) {
18
+ return null;
19
+ }
20
+ return selector ? selector(state) : state;
21
+ };
22
+ (0, react_1.useEffect)(() => {
23
+ if (!store) {
24
+ return;
25
+ }
26
+ return store.onAny(() => {
27
+ const newResult = getResult();
28
+ if (newResult !== resultRef.current) {
29
+ //only refresh when the result has changed
30
+ setRefreshKey((prev) => prev + 1);
31
+ }
32
+ });
33
+ }, [store]);
34
+ if (!adaptable || !adaptable.isReady) {
35
+ return null;
36
+ }
37
+ const res = getResult();
38
+ resultRef.current = res;
39
+ return res;
40
+ }
41
+ exports.useAdaptableState = useAdaptableState;
42
+ const useCurrentLayout = () => {
43
+ const { adaptableApi } = (0, AdaptableProvider_1.useAdaptableAgGridContext)();
44
+ const layout = useAdaptableState((state) => {
45
+ const currentLayout = state.Layout.CurrentLayout;
46
+ return state.Layout.Layouts.find((layout) => layout.Name === currentLayout);
47
+ });
48
+ const setLayout = (layoutName) => {
49
+ if (!adaptableApi) {
50
+ return;
51
+ }
52
+ adaptableApi.layoutApi.setLayout(layoutName);
53
+ };
54
+ return [layout ?? null, setLayout];
55
+ };
56
+ exports.useCurrentLayout = useCurrentLayout;
package/src/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { AdaptableQL, AdaptableUpgradeHelper } from "@adaptabletools/adaptable-c
2
2
  export { AdaptableOptionsWizardView } from './components/AdaptableOptionsWizardView';
3
3
  export * from "@adaptabletools/adaptable-cjs/types";
4
4
  export { Adaptable } from './components/AdaptableProvider';
5
+ export * from './components/useAdaptableState';
package/src/index.js CHANGED
@@ -11,3 +11,4 @@ Object.defineProperty(exports, "AdaptableOptionsWizardView", { enumerable: true,
11
11
  tslib_1.__exportStar(require("@adaptabletools/adaptable-cjs/types"), exports);
12
12
  var AdaptableProvider_1 = require("./components/AdaptableProvider");
13
13
  Object.defineProperty(exports, "Adaptable", { enumerable: true, get: function () { return AdaptableProvider_1.Adaptable; } });
14
+ tslib_1.__exportStar(require("./components/useAdaptableState"), exports);