@jsoc/react-grid 0.0.1 → 0.0.2
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/index.mjs +1 -1
- package/dist/plugins/ag.mjs +1 -1
- package/dist/plugins/ant.mjs +1 -1
- package/dist/plugins/mantine.mjs +1 -1
- package/dist/plugins/mui.mjs +1 -1
- package/dist/plugins/prime.mjs +1 -1
- package/dist/plugins/tanstack.mjs +1 -1
- package/dist/{shared-usOIqnt_.mjs → shared-C27_qTEa.mjs} +15 -14
- package/dist/shared-C27_qTEa.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/shared-usOIqnt_.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as SubGridToggle, c as useStoreContext, i as ujsonObjectCellRenderer, l as StoreContext, n as dateCellRenderer, o as StoreContextProvider, r as stringDateCellRenderer, s as GridClose, t as booleanCellRenderer, u as useCoreStore } from "./shared-
|
|
1
|
+
import { a as SubGridToggle, c as useStoreContext, i as ujsonObjectCellRenderer, l as StoreContext, n as dateCellRenderer, o as StoreContextProvider, r as stringDateCellRenderer, s as GridClose, t as booleanCellRenderer, u as useCoreStore } from "./shared-C27_qTEa.mjs";
|
|
2
2
|
export { GridClose, StoreContext, StoreContextProvider, SubGridToggle, booleanCellRenderer, dateCellRenderer, stringDateCellRenderer, ujsonObjectCellRenderer, useCoreStore, useStoreContext };
|
package/dist/plugins/ag.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, r as stringDateCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, r as stringDateCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { prettyJSON, toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
//#region src/plugins/ag/COLUMN_GENERATORS.tsx
|
package/dist/plugins/ant.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, r as stringDateCellRenderer, t as booleanCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, r as stringDateCellRenderer, t as booleanCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { compareBooleans, compareNumbers, compareStringDates, compareStrings, toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
import { Fragment, jsx } from "react/jsx-runtime";
|
package/dist/plugins/mantine.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, n as dateCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, n as dateCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { booleanToString, stringDateToDate, toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
//#region src/plugins/mantine/COLUMN_GENERATORS.tsx
|
package/dist/plugins/mui.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { prettyJSON, stringDateToDate, toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
//#region src/plugins/mui/COLUMN_GENERATORS.tsx
|
package/dist/plugins/prime.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
//#region src/plugins/prime/COLUMN_GENERATORS.tsx
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as ujsonObjectCellRenderer, n as dateCellRenderer, u as useCoreStore } from "../shared-
|
|
1
|
+
import { i as ujsonObjectCellRenderer, n as dateCellRenderer, u as useCoreStore } from "../shared-C27_qTEa.mjs";
|
|
2
2
|
import { COLUMN_DATA_TYPES, generateColumns } from "@jsoc/grid-core";
|
|
3
3
|
import { booleanToString, stringDateToDate, toReadableString, ujsonValueToString } from "@jsoc/utils";
|
|
4
4
|
//#region src/plugins/tanstack/COLUMN_GENERATORS.tsx
|
|
@@ -4,11 +4,21 @@ import { createContext, useContext, useEffect, useMemo, useRef, useState } from
|
|
|
4
4
|
import { jsx } from "react/jsx-runtime";
|
|
5
5
|
//#region src/hooks/useCoreStore.ts
|
|
6
6
|
function useCoreStore(gridOptions, configGenerator, configGeneratorOptions) {
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const [gridStore, setGridStore] = useState(() =>
|
|
7
|
+
const gridOptionsRef = useRef(gridOptions);
|
|
8
|
+
const generatorOptionsRef = useRef(configGeneratorOptions);
|
|
9
|
+
const [gridStore, setGridStore] = useState(() => newGridStore(gridOptions, {
|
|
10
|
+
configGenerator,
|
|
11
|
+
configGeneratorOptions
|
|
12
|
+
}));
|
|
10
13
|
useEffect(() => {
|
|
11
|
-
if (!
|
|
14
|
+
if (!shallowEqual(gridOptions, gridOptionsRef.current) || !shallowEqual(configGeneratorOptions, generatorOptionsRef.current)) {
|
|
15
|
+
gridOptionsRef.current = gridOptions;
|
|
16
|
+
generatorOptionsRef.current = configGeneratorOptions;
|
|
17
|
+
setGridStore(newGridStore(gridOptions, {
|
|
18
|
+
configGenerator,
|
|
19
|
+
configGeneratorOptions
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
12
22
|
}, [
|
|
13
23
|
gridOptions,
|
|
14
24
|
configGenerator,
|
|
@@ -19,15 +29,6 @@ function useCoreStore(gridOptions, configGenerator, configGeneratorOptions) {
|
|
|
19
29
|
setGridStore
|
|
20
30
|
}), [gridStore, setGridStore]);
|
|
21
31
|
}
|
|
22
|
-
function createStore(lastGridOptionsRef, lastGeneratorOptionsRef, gridOptions, configGenerator, configGeneratorOptions) {
|
|
23
|
-
const gridStore = newGridStore(gridOptions, {
|
|
24
|
-
configGenerator,
|
|
25
|
-
configGeneratorOptions
|
|
26
|
-
});
|
|
27
|
-
lastGridOptionsRef.current = gridOptions;
|
|
28
|
-
lastGeneratorOptionsRef.current = configGeneratorOptions;
|
|
29
|
-
return gridStore;
|
|
30
|
-
}
|
|
31
32
|
//#endregion
|
|
32
33
|
//#region src/contexts/StoreContext.tsx
|
|
33
34
|
const StoreContext = createContext(void 0);
|
|
@@ -150,4 +151,4 @@ const ujsonObjectCellRenderer = (params, value, row) => {
|
|
|
150
151
|
//#endregion
|
|
151
152
|
export { SubGridToggle as a, useStoreContext as c, ujsonObjectCellRenderer as i, StoreContext as l, dateCellRenderer as n, StoreContextProvider as o, stringDateCellRenderer as r, GridClose as s, booleanCellRenderer as t, useCoreStore as u };
|
|
152
153
|
|
|
153
|
-
//# sourceMappingURL=shared-
|
|
154
|
+
//# sourceMappingURL=shared-C27_qTEa.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared-C27_qTEa.mjs","names":[],"sources":["../src/hooks/useCoreStore.ts","../src/contexts/StoreContext.tsx","../src/errors/ReactGridError.ts","../src/hooks/useStoreContext.ts","../src/components/GridClose.tsx","../src/components/StoreContextProvider.tsx","../src/components/SubGridToggle.tsx","../src/utils/cellRenderers.tsx"],"sourcesContent":["import type { StoreContextValue } from \"#contexts/StoreContext.tsx\";\n\nimport {\n type GridOptions,\n newGridStore,\n type PluginConfig,\n type PluginConfigGenerator,\n type PluginConfigGeneratorOptions,\n} from \"@jsoc/grid-core\";\nimport { shallowEqual } from \"@jsoc/utils\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\n\nexport function useCoreStore<C extends PluginConfig>(\n gridOptions: GridOptions,\n configGenerator: PluginConfigGenerator<C>,\n configGeneratorOptions?: PluginConfigGeneratorOptions<C>,\n): StoreContextValue<C> {\n const gridOptionsRef = useRef(gridOptions);\n const generatorOptionsRef = useRef(configGeneratorOptions);\n\n const [gridStore, setGridStore] = useState(() =>\n newGridStore(gridOptions, {\n configGenerator,\n configGeneratorOptions,\n }),\n );\n\n useEffect(() => {\n const shouldUpdate =\n !shallowEqual(gridOptions, gridOptionsRef.current) ||\n !shallowEqual(configGeneratorOptions, generatorOptionsRef.current);\n\n if (shouldUpdate) {\n gridOptionsRef.current = gridOptions;\n generatorOptionsRef.current = configGeneratorOptions;\n setGridStore(\n newGridStore(gridOptions, {\n configGenerator,\n configGeneratorOptions,\n }),\n );\n }\n }, [gridOptions, configGenerator, configGeneratorOptions]);\n\n return useMemo(\n () => ({\n gridStore,\n setGridStore,\n }),\n [gridStore, setGridStore],\n );\n}\n","import type { GridStore, PluginConfig } from \"@jsoc/grid-core\";\nimport { createContext, type Dispatch, type SetStateAction } from \"react\";\n\nexport type StoreContextValue<C extends PluginConfig = PluginConfig> = {\n gridStore: GridStore<C>;\n setGridStore: Dispatch<SetStateAction<GridStore<C>>>;\n};\n\nexport const StoreContext = createContext<\n StoreContextValue<PluginConfig> | undefined\n>(undefined);\n","export class ReactGridError extends Error {\n constructor(message: string, cause?: unknown) {\n super(message, { cause });\n this.name = \"ReactGridError\";\n }\n}\n","import {\n StoreContext,\n type StoreContextValue,\n} from \"#contexts/StoreContext.tsx\";\nimport { ReactGridError } from \"#errors/ReactGridError.ts\";\n\nimport type { PluginConfig } from \"@jsoc/grid-core\";\nimport { useContext } from \"react\";\n\nexport function useStoreContext<\n C extends PluginConfig = PluginConfig,\n>(): StoreContextValue<C> {\n const ctx = useContext(StoreContext);\n\n if (!ctx) {\n throw new ReactGridError(`Missing provider for useStoreContext`);\n }\n\n return ctx as unknown as StoreContextValue<C>;\n}\n","import { useStoreContext } from \"#hooks/index.ts\";\n\nexport type GridCloseRendererParams = {\n close: () => void;\n};\n\nexport type GridCloseProps = {\n /**\n * Either a custom renderer for the close action component or a string to display as text of default close action component.\n */\n children?: string | ((params: GridCloseRendererParams) => React.ReactNode);\n};\n\nexport function GridClose({ children }: GridCloseProps) {\n const { gridStore, setGridStore } = useStoreContext();\n const isRootSchema = gridStore.getSchemas().length === 1;\n\n if (isRootSchema) {\n return null;\n }\n\n const close = () => {\n const newGridStore = gridStore.clone();\n newGridStore.removeSchema();\n setGridStore(newGridStore);\n };\n\n const params = { close };\n\n if (typeof children === \"function\") {\n return children(params);\n }\n\n return DefaultGridCloseRenderer(children || \"Close\", params);\n}\n\nfunction DefaultGridCloseRenderer(\n children: string,\n params: GridCloseRendererParams,\n) {\n return (\n <button\n onClick={params.close}\n style={{\n padding: \"4px 12px\",\n border: \"1px solid #ddd\",\n borderRadius: \"4px\",\n background: \"white\",\n color: \"black\",\n cursor: \"pointer\",\n fontSize: \"14px\",\n marginBottom: \"8px\",\n }}\n >\n {children}\n </button>\n );\n}\n","import {\n StoreContext,\n type StoreContextValue,\n} from \"#contexts/StoreContext.tsx\";\n\nimport type { PluginConfig } from \"@jsoc/grid-core\";\nimport type { ReactNode } from \"react\";\n\nexport type StoreContextProviderProps<C extends PluginConfig = PluginConfig> = {\n value: StoreContextValue<C>;\n children: ReactNode;\n};\n\nexport function StoreContextProvider<C extends PluginConfig>({\n value,\n children,\n}: StoreContextProviderProps<C>) {\n return (\n <StoreContext.Provider\n value={value as unknown as StoreContextValue<PluginConfig>}\n >\n {children}\n </StoreContext.Provider>\n );\n}\n","import { useStoreContext } from \"#hooks/useStoreContext.ts\";\n\nimport {\n createSubGridId,\n type GridCellLocation,\n type GridDataReadonly,\n type GridId,\n type GridName,\n} from \"@jsoc/grid-core\";\nimport { toReadableString } from \"@jsoc/utils\";\n\nexport type SubGridToggleRendererParams = {\n isPresent: boolean;\n name: GridName;\n toggle: () => void;\n};\n\nexport type SubGridToggleProps = {\n /**\n * Custom renderer for the toggle action.\n * @param isPresent - Flag that indicates whether the subgrid is already present in store or not\n * @default DefaultSubGridToggleRenderer\n */\n children?: (params: SubGridToggleRendererParams) => React.ReactNode;\n /**\n * data of the sub grid, is actually the `ColumnValue` of the `GridRow[rowId][ColumnKey]`\n * using which the rows and columns will be generated.\n */\n subGridData: GridDataReadonly;\n /**\n * `GridId` of the grid that will render this component\n */\n parentGridId: GridId;\n /**\n * Location of the `GridCell` that will render this component\n */\n parentGridCellLocation: GridCellLocation;\n};\n\nexport function SubGridToggle({\n subGridData,\n parentGridId,\n parentGridCellLocation,\n children = DefaultSubGridToggleRenderer,\n}: SubGridToggleProps) {\n const { gridStore, setGridStore } = useStoreContext();\n\n const subGridId = createSubGridId(parentGridId, parentGridCellLocation);\n const subGridName = parentGridCellLocation.columnKey;\n const index = gridStore.findIndex(subGridId);\n const isPresent = index > -1;\n\n return children({\n isPresent,\n name: subGridName,\n toggle,\n });\n\n function toggle() {\n const storeClone = gridStore.clone();\n\n if (isPresent) {\n storeClone.removeSchema(index);\n } else {\n storeClone.addSchema({\n id: subGridId,\n name: subGridName,\n data: subGridData,\n });\n }\n\n setGridStore(storeClone);\n }\n}\n\nfunction DefaultSubGridToggleRenderer(params: SubGridToggleRendererParams) {\n const { name, isPresent, toggle } = params;\n const buttonText = isPresent ? \"Close \" : \"View \" + toReadableString(name);\n return (\n <button\n onClick={toggle}\n style={{\n background: \"none\",\n border: \"none\",\n color: \"#007bff\",\n textDecoration: \"underline\",\n cursor: \"pointer\",\n padding: 0,\n }}\n >\n {buttonText}\n </button>\n );\n}\n","import { SubGridToggle } from \"#components/index.ts\";\n\nimport type {\n ColumnGeneratorParams,\n GridDataReadonly,\n GridRow,\n GridRowId,\n} from \"@jsoc/grid-core\";\nimport { booleanToString, stringDateToDate } from \"@jsoc/utils\";\n\nexport const booleanCellRenderer = (value: boolean) => {\n return booleanToString(value);\n};\n\nexport const dateCellRenderer = (value: Date) => {\n return value.toLocaleString();\n};\n\nexport const stringDateCellRenderer = (value: string) => {\n return dateCellRenderer(stringDateToDate(value));\n};\n\nexport const ujsonObjectCellRenderer = (\n params: ColumnGeneratorParams,\n value: GridDataReadonly | null | undefined,\n row: GridRow | null | undefined,\n) => {\n if (!value || !row) {\n return;\n }\n\n const { columnKey, gridSchema } = params;\n const { primaryColumnKey } = gridSchema.meta;\n\n return (\n <SubGridToggle\n subGridData={value}\n parentGridId={gridSchema.options.id}\n parentGridCellLocation={{\n rowId: row[primaryColumnKey] as GridRowId,\n columnKey,\n }}\n />\n );\n};\n"],"mappings":";;;;;AAYA,SAAgB,aACd,aACA,iBACA,wBACsB;CACtB,MAAM,iBAAiB,OAAO,YAAY;CAC1C,MAAM,sBAAsB,OAAO,uBAAuB;CAE1D,MAAM,CAAC,WAAW,gBAAgB,eAChC,aAAa,aAAa;EACxB;EACA;EACD,CAAC,CACH;AAED,iBAAgB;AAKd,MAHE,CAAC,aAAa,aAAa,eAAe,QAAQ,IAClD,CAAC,aAAa,wBAAwB,oBAAoB,QAAQ,EAElD;AAChB,kBAAe,UAAU;AACzB,uBAAoB,UAAU;AAC9B,gBACE,aAAa,aAAa;IACxB;IACA;IACD,CAAC,CACH;;IAEF;EAAC;EAAa;EAAiB;EAAuB,CAAC;AAE1D,QAAO,eACE;EACL;EACA;EACD,GACD,CAAC,WAAW,aAAa,CAC1B;;;;AC1CH,MAAa,eAAe,cAE1B,KAAA,EAAU;;;ACVZ,IAAa,iBAAb,cAAoC,MAAM;CACxC,YAAY,SAAiB,OAAiB;AAC5C,QAAM,SAAS,EAAE,OAAO,CAAC;AACzB,OAAK,OAAO;;;;;ACMhB,SAAgB,kBAEU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WAAW,WAAW;AAEpC,QACE,oBAAC,UAAD;EACE,SAAS;EACT,OAAO;GACL,YAAY;GACZ,QAAQ;GACR,OAAO;GACP,gBAAgB;GAChB,QAAQ;GACR,SAAS;GACV;YAXc,YAAY,WAAW,UAAU,iBAAiB,KAAK;EAc/D,CAAA;;;;ACjFb,MAAa,uBAAuB,UAAmB;AACrD,QAAO,gBAAgB,MAAM;;AAG/B,MAAa,oBAAoB,UAAgB;AAC/C,QAAO,MAAM,gBAAgB;;AAG/B,MAAa,0BAA0B,UAAkB;AACvD,QAAO,iBAAiB,iBAAiB,MAAM,CAAC;;AAGlD,MAAa,2BACX,QACA,OACA,QACG;AACH,KAAI,CAAC,SAAS,CAAC,IACb;CAGF,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,EAAE,qBAAqB,WAAW;AAExC,QACE,oBAAC,eAAD;EACE,aAAa;EACb,cAAc,WAAW,QAAQ;EACjC,wBAAwB;GACtB,OAAO,IAAI;GACX;GACD;EACD,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsoc/react-grid",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Dynamic columns/rows generator for popular React grid components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datagrid",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"dist"
|
|
59
59
|
],
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@jsoc/
|
|
62
|
-
"@jsoc/
|
|
61
|
+
"@jsoc/grid-core": "0.0.1",
|
|
62
|
+
"@jsoc/utils": "0.0.1"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@mui/x-data-grid": "^8.16.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"shared-usOIqnt_.mjs","names":[],"sources":["../src/hooks/useCoreStore.ts","../src/contexts/StoreContext.tsx","../src/errors/ReactGridError.ts","../src/hooks/useStoreContext.ts","../src/components/GridClose.tsx","../src/components/StoreContextProvider.tsx","../src/components/SubGridToggle.tsx","../src/utils/cellRenderers.tsx"],"sourcesContent":["import type { StoreContextValue } from \"#contexts/StoreContext.tsx\";\n\nimport {\n type GridOptions,\n type GridStore,\n newGridStore,\n type PluginConfig,\n type PluginConfigGenerator,\n type PluginConfigGeneratorOptions,\n} from \"@jsoc/grid-core\";\nimport { shallowEqual } from \"@jsoc/utils\";\nimport { type RefObject, useEffect, useMemo, useRef, useState } from \"react\";\n\nexport function useCoreStore<C extends PluginConfig>(\n gridOptions: GridOptions,\n configGenerator: PluginConfigGenerator<C>,\n configGeneratorOptions?: PluginConfigGeneratorOptions<C>,\n): StoreContextValue<C> {\n const lastGridOptionsRef = useRef<GridOptions | null>(null);\n const lastGeneratorOptionsRef = useRef<\n PluginConfigGeneratorOptions<C> | null | undefined\n >(null);\n\n const [gridStore, setGridStore] = useState(() =>\n createStore<C>(\n lastGridOptionsRef,\n lastGeneratorOptionsRef,\n gridOptions,\n configGenerator,\n configGeneratorOptions,\n ),\n );\n\n // update grid store when options fields shallowly change.\n useEffect(() => {\n const shouldUpdate =\n !lastGridOptionsRef.current ||\n !lastGeneratorOptionsRef.current ||\n !shallowEqual(gridOptions, lastGridOptionsRef.current) ||\n !shallowEqual(configGeneratorOptions, lastGeneratorOptionsRef.current);\n\n if (shouldUpdate) {\n const store = createStore<C>(\n lastGridOptionsRef,\n lastGeneratorOptionsRef,\n gridOptions,\n configGenerator,\n configGeneratorOptions,\n );\n\n setGridStore(store);\n }\n }, [gridOptions, configGenerator, configGeneratorOptions]);\n\n // prepare the store context value\n const storeContextValue = useMemo(\n () => ({\n gridStore,\n setGridStore,\n }),\n [gridStore, setGridStore],\n );\n\n return storeContextValue;\n}\n\nfunction createStore<C extends PluginConfig>(\n lastGridOptionsRef: RefObject<GridOptions | null>,\n lastGeneratorOptionsRef: RefObject<\n PluginConfigGeneratorOptions<C> | null | undefined\n >,\n gridOptions: GridOptions,\n configGenerator: PluginConfigGenerator<C>,\n configGeneratorOptions?: PluginConfigGeneratorOptions<C>,\n): GridStore<C> {\n const pluginOptions = {\n configGenerator,\n configGeneratorOptions,\n };\n\n const gridStore = newGridStore(gridOptions, pluginOptions);\n\n lastGridOptionsRef.current = gridOptions;\n lastGeneratorOptionsRef.current = configGeneratorOptions;\n\n return gridStore;\n}\n","import type { GridStore, PluginConfig } from \"@jsoc/grid-core\";\nimport { createContext, type Dispatch, type SetStateAction } from \"react\";\n\nexport type StoreContextValue<C extends PluginConfig = PluginConfig> = {\n gridStore: GridStore<C>;\n setGridStore: Dispatch<SetStateAction<GridStore<C>>>;\n};\n\nexport const StoreContext = createContext<\n StoreContextValue<PluginConfig> | undefined\n>(undefined);\n","export class ReactGridError extends Error {\n constructor(message: string, cause?: unknown) {\n super(message, { cause });\n this.name = \"ReactGridError\";\n }\n}\n","import {\n StoreContext,\n type StoreContextValue,\n} from \"#contexts/StoreContext.tsx\";\nimport { ReactGridError } from \"#errors/ReactGridError.ts\";\n\nimport type { PluginConfig } from \"@jsoc/grid-core\";\nimport { useContext } from \"react\";\n\nexport function useStoreContext<\n C extends PluginConfig = PluginConfig,\n>(): StoreContextValue<C> {\n const ctx = useContext(StoreContext);\n\n if (!ctx) {\n throw new ReactGridError(`Missing provider for useStoreContext`);\n }\n\n return ctx as unknown as StoreContextValue<C>;\n}\n","import { useStoreContext } from \"#hooks/index.ts\";\n\nexport type GridCloseRendererParams = {\n close: () => void;\n};\n\nexport type GridCloseProps = {\n /**\n * Either a custom renderer for the close action component or a string to display as text of default close action component.\n */\n children?: string | ((params: GridCloseRendererParams) => React.ReactNode);\n};\n\nexport function GridClose({ children }: GridCloseProps) {\n const { gridStore, setGridStore } = useStoreContext();\n const isRootSchema = gridStore.getSchemas().length === 1;\n\n if (isRootSchema) {\n return null;\n }\n\n const close = () => {\n const newGridStore = gridStore.clone();\n newGridStore.removeSchema();\n setGridStore(newGridStore);\n };\n\n const params = { close };\n\n if (typeof children === \"function\") {\n return children(params);\n }\n\n return DefaultGridCloseRenderer(children || \"Close\", params);\n}\n\nfunction DefaultGridCloseRenderer(\n children: string,\n params: GridCloseRendererParams,\n) {\n return (\n <button\n onClick={params.close}\n style={{\n padding: \"4px 12px\",\n border: \"1px solid #ddd\",\n borderRadius: \"4px\",\n background: \"white\",\n color: \"black\",\n cursor: \"pointer\",\n fontSize: \"14px\",\n marginBottom: \"8px\",\n }}\n >\n {children}\n </button>\n );\n}\n","import {\n StoreContext,\n type StoreContextValue,\n} from \"#contexts/StoreContext.tsx\";\n\nimport type { PluginConfig } from \"@jsoc/grid-core\";\nimport type { ReactNode } from \"react\";\n\nexport type StoreContextProviderProps<C extends PluginConfig = PluginConfig> = {\n value: StoreContextValue<C>;\n children: ReactNode;\n};\n\nexport function StoreContextProvider<C extends PluginConfig>({\n value,\n children,\n}: StoreContextProviderProps<C>) {\n return (\n <StoreContext.Provider\n value={value as unknown as StoreContextValue<PluginConfig>}\n >\n {children}\n </StoreContext.Provider>\n );\n}\n","import { useStoreContext } from \"#hooks/useStoreContext.ts\";\n\nimport {\n createSubGridId,\n type GridCellLocation,\n type GridDataReadonly,\n type GridId,\n type GridName,\n} from \"@jsoc/grid-core\";\nimport { toReadableString } from \"@jsoc/utils\";\n\nexport type SubGridToggleRendererParams = {\n isPresent: boolean;\n name: GridName;\n toggle: () => void;\n};\n\nexport type SubGridToggleProps = {\n /**\n * Custom renderer for the toggle action.\n * @param isPresent - Flag that indicates whether the subgrid is already present in store or not\n * @default DefaultSubGridToggleRenderer\n */\n children?: (params: SubGridToggleRendererParams) => React.ReactNode;\n /**\n * data of the sub grid, is actually the `ColumnValue` of the `GridRow[rowId][ColumnKey]`\n * using which the rows and columns will be generated.\n */\n subGridData: GridDataReadonly;\n /**\n * `GridId` of the grid that will render this component\n */\n parentGridId: GridId;\n /**\n * Location of the `GridCell` that will render this component\n */\n parentGridCellLocation: GridCellLocation;\n};\n\nexport function SubGridToggle({\n subGridData,\n parentGridId,\n parentGridCellLocation,\n children = DefaultSubGridToggleRenderer,\n}: SubGridToggleProps) {\n const { gridStore, setGridStore } = useStoreContext();\n\n const subGridId = createSubGridId(parentGridId, parentGridCellLocation);\n const subGridName = parentGridCellLocation.columnKey;\n const index = gridStore.findIndex(subGridId);\n const isPresent = index > -1;\n\n return children({\n isPresent,\n name: subGridName,\n toggle,\n });\n\n function toggle() {\n const storeClone = gridStore.clone();\n\n if (isPresent) {\n storeClone.removeSchema(index);\n } else {\n storeClone.addSchema({\n id: subGridId,\n name: subGridName,\n data: subGridData,\n });\n }\n\n setGridStore(storeClone);\n }\n}\n\nfunction DefaultSubGridToggleRenderer(params: SubGridToggleRendererParams) {\n const { name, isPresent, toggle } = params;\n const buttonText = isPresent ? \"Close \" : \"View \" + toReadableString(name);\n return (\n <button\n onClick={toggle}\n style={{\n background: \"none\",\n border: \"none\",\n color: \"#007bff\",\n textDecoration: \"underline\",\n cursor: \"pointer\",\n padding: 0,\n }}\n >\n {buttonText}\n </button>\n );\n}\n","import { SubGridToggle } from \"#components/index.ts\";\n\nimport type {\n ColumnGeneratorParams,\n GridDataReadonly,\n GridRow,\n GridRowId,\n} from \"@jsoc/grid-core\";\nimport { booleanToString, stringDateToDate } from \"@jsoc/utils\";\n\nexport const booleanCellRenderer = (value: boolean) => {\n return booleanToString(value);\n};\n\nexport const dateCellRenderer = (value: Date) => {\n return value.toLocaleString();\n};\n\nexport const stringDateCellRenderer = (value: string) => {\n return dateCellRenderer(stringDateToDate(value));\n};\n\nexport const ujsonObjectCellRenderer = (\n params: ColumnGeneratorParams,\n value: GridDataReadonly | null | undefined,\n row: GridRow | null | undefined,\n) => {\n if (!value || !row) {\n return;\n }\n\n const { columnKey, gridSchema } = params;\n const { primaryColumnKey } = gridSchema.meta;\n\n return (\n <SubGridToggle\n subGridData={value}\n parentGridId={gridSchema.options.id}\n parentGridCellLocation={{\n rowId: row[primaryColumnKey] as GridRowId,\n columnKey,\n }}\n />\n );\n};\n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