@linzjs/step-ag-grid 15.0.2 → 15.1.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.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@linzjs/step-ag-grid",
3
3
  "repository": "github:linz/step-ag-grid.git",
4
4
  "license": "MIT",
5
- "version": "15.0.2",
5
+ "version": "15.1.1",
6
6
  "keywords": [
7
7
  "aggrid",
8
8
  "ag-grid",
@@ -14,6 +14,7 @@ import { fnOrVar, isNotEmpty } from "../utils/util";
14
14
  import { GridNoRowsOverlay } from "./GridNoRowsOverlay";
15
15
  import { usePostSortRowsHook } from "./PostSortRowsHook";
16
16
  import { GridHeaderSelect } from "./gridHeader";
17
+ import { GridContextMenuComponent, useGridContextMenu } from "./gridHook";
17
18
 
18
19
  export interface GridBaseRow {
19
20
  id: string | number;
@@ -67,6 +68,16 @@ export interface GridProps {
67
68
  * Once the last cell to edit closes this callback is called.
68
69
  */
69
70
  onCellEditingComplete?: () => void;
71
+
72
+ /**
73
+ * Context menu definition if required.
74
+ */
75
+ contextMenu?: GridContextMenuComponent<any>;
76
+
77
+ /**
78
+ * Whether to select row on context menu.
79
+ */
80
+ contextMenuSelectRow?: boolean;
70
81
  }
71
82
 
72
83
  /**
@@ -79,6 +90,7 @@ export const Grid = ({
79
90
  theme = "ag-theme-alpine",
80
91
  sizeColumns = "auto",
81
92
  selectColumnPinned = null,
93
+ contextMenuSelectRow = false,
82
94
  ...params
83
95
  }: GridProps): JSX.Element => {
84
96
  const {
@@ -569,6 +581,9 @@ export const Grid = ({
569
581
  }
570
582
  }, []);
571
583
 
584
+ const gridContextMenu = useGridContextMenu({ contextMenu: params.contextMenu, contextMenuSelectRow });
585
+
586
+ // This is setting a ref in the GridContext so won't be triggering an update loop
572
587
  setOnCellEditingComplete(params.onCellEditingComplete);
573
588
 
574
589
  return (
@@ -581,6 +596,7 @@ export const Grid = ({
581
596
  gridReady && params.rowData && "Grid-ready",
582
597
  )}
583
598
  >
599
+ {gridContextMenu.component}
584
600
  <div style={{ flex: 1 }} ref={gridDivRef}>
585
601
  <AgGridReact
586
602
  rowHeight={params.rowHeight}
@@ -621,6 +637,8 @@ export const Grid = ({
621
637
  isExternalFilterPresent={isExternalFilterPresent}
622
638
  doesExternalFilterPass={doesExternalFilterPass}
623
639
  maintainColumnOrder={true}
640
+ preventDefaultOnContextMenu={true}
641
+ onCellContextMenu={gridContextMenu.cellContextMenu}
624
642
  />
625
643
  </div>
626
644
  </div>
@@ -0,0 +1 @@
1
+ export * from "./useGridContextMenu";
@@ -0,0 +1,92 @@
1
+ import { ColDef } from "ag-grid-community";
2
+ import { CellContextMenuEvent } from "ag-grid-community/dist/lib/events";
3
+ import { ReactElement, useCallback, useContext, useRef, useState } from "react";
4
+
5
+ import { GridContext } from "../../contexts/GridContext";
6
+ import { ControlledMenu } from "../../react-menu3";
7
+ import { GridBaseRow } from "../Grid";
8
+
9
+ export interface GridContextMenuComponentProps<RowType extends GridBaseRow> {
10
+ selectedRows: RowType[];
11
+ clickedRow: RowType;
12
+ colDef: ColDef;
13
+ close: () => void;
14
+ }
15
+
16
+ export type GridContextMenuComponent<RowType extends GridBaseRow> = (
17
+ props: GridContextMenuComponentProps<RowType>,
18
+ ) => ReactElement | null;
19
+
20
+ export const useGridContextMenu = <RowType extends GridBaseRow>({
21
+ contextMenuSelectRow,
22
+ contextMenu: ContextMenu,
23
+ }: {
24
+ contextMenuSelectRow: boolean;
25
+ contextMenu?: GridContextMenuComponent<RowType>;
26
+ }) => {
27
+ const { redrawRows, prePopupOps, postPopupOps, getSelectedRows } = useContext(GridContext);
28
+
29
+ const [isOpen, setOpen] = useState(false);
30
+ const [anchorPoint, setAnchorPoint] = useState({ x: 0, y: 0 });
31
+
32
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
33
+ const clickedColDefRef = useRef<ColDef>(null!);
34
+ const selectedRowsRef = useRef<any[]>([]);
35
+ const clickedRowRef = useRef<any>(null);
36
+
37
+ const openMenu = useCallback(
38
+ (e: PointerEvent | null | undefined) => {
39
+ if (!e) return;
40
+ prePopupOps();
41
+ setAnchorPoint({ x: e.clientX, y: e.clientY });
42
+ setOpen(true);
43
+ },
44
+ [prePopupOps],
45
+ );
46
+
47
+ const closeMenu = useCallback(() => {
48
+ setOpen(false);
49
+ redrawRows();
50
+ postPopupOps();
51
+ }, [postPopupOps, redrawRows]);
52
+
53
+ const cellContextMenu = useCallback(
54
+ (event: CellContextMenuEvent) => {
55
+ if (contextMenuSelectRow && !event.node.isSelected()) {
56
+ event.node.setSelected(true, true);
57
+ }
58
+
59
+ clickedColDefRef.current = event.colDef;
60
+ selectedRowsRef.current = getSelectedRows();
61
+ clickedRowRef.current = event.data;
62
+
63
+ // This is actually a pointer event
64
+ openMenu(event.event as PointerEvent);
65
+ },
66
+ [contextMenuSelectRow, getSelectedRows, openMenu],
67
+ );
68
+
69
+ return {
70
+ openMenu,
71
+ cellContextMenu,
72
+ component: ContextMenu ? (
73
+ <>
74
+ <ControlledMenu
75
+ anchorPoint={anchorPoint}
76
+ state={isOpen ? "open" : "closed"}
77
+ direction="right"
78
+ onClose={closeMenu}
79
+ >
80
+ {isOpen && (
81
+ <ContextMenu
82
+ selectedRows={selectedRowsRef.current}
83
+ clickedRow={clickedRowRef.current}
84
+ colDef={clickedColDefRef.current}
85
+ close={closeMenu}
86
+ />
87
+ )}
88
+ </ControlledMenu>
89
+ </>
90
+ ) : null,
91
+ };
92
+ };
@@ -7,6 +7,7 @@ export * from "./GridCellMultiSelectClassRules";
7
7
  export * from "./gridFilter";
8
8
  export * from "./gridForm";
9
9
  export * from "./gridHeader";
10
+ export * from "./gridHook";
10
11
  export * from "./GridIcon";
11
12
  export * from "./GridLoadableCell";
12
13
  export * from "./GridNoRowsOverlay";
@@ -23,6 +23,7 @@ export interface GridContextType<RowType extends GridBaseRow> {
23
23
  getColumnIds: (filter?: keyof ColDef | ((r: ColDef) => boolean | undefined | null | number | string)) => string[];
24
24
  setApis: (gridApi: GridApi | undefined, columnApi: ColumnApi | undefined, dataTestId?: string) => void;
25
25
  prePopupOps: () => void;
26
+ postPopupOps: () => void;
26
27
  setQuickFilter: (quickFilter: string) => void;
27
28
  editingCells: () => boolean;
28
29
  getSelectedRows: <T extends GridBaseRow>() => T[];
@@ -85,6 +86,9 @@ export const GridContext = createContext<GridContextType<any>>({
85
86
  prePopupOps: () => {
86
87
  console.error("no context provider for prePopupOps");
87
88
  },
89
+ postPopupOps: () => {
90
+ console.error("no context provider for prePopupOps");
91
+ },
88
92
  externallySelectedItemsAreInSync: false,
89
93
  setApis: () => {
90
94
  console.error("no context provider for setApis");
@@ -132,6 +132,16 @@ export const GridContextProvider = <RowType extends GridBaseRow>(props: GridCont
132
132
  prePopupFocusedCell.current = gridApi?.getFocusedCell() ?? undefined;
133
133
  }, [gridApi]);
134
134
 
135
+ /**
136
+ * After a popup refocus the cell.
137
+ */
138
+ const postPopupOps = useCallback(() => {
139
+ if (!gridApi) return;
140
+ if (prePopupFocusedCell.current) {
141
+ gridApi?.setFocusedCell(prePopupFocusedCell.current.rowIndex, prePopupFocusedCell.current.column);
142
+ }
143
+ }, [gridApi]);
144
+
135
145
  /**
136
146
  * Get all row id's in grid.
137
147
  */
@@ -663,6 +673,7 @@ export const GridContextProvider = <RowType extends GridBaseRow>(props: GridCont
663
673
  setInvisibleColumnIds,
664
674
  gridReady,
665
675
  prePopupOps,
676
+ postPopupOps,
666
677
  setApis,
667
678
  setQuickFilter,
668
679
  selectRowsById,
@@ -0,0 +1,136 @@
1
+ import { ComponentMeta, ComponentStory } from "@storybook/react/dist/ts3.9/client/preview/types-6-3";
2
+ import { ReactElement, useCallback, useContext, useMemo, useState } from "react";
3
+
4
+ import "@linzjs/lui/dist/fonts";
5
+ import "@linzjs/lui/dist/scss/base.scss";
6
+
7
+ import {
8
+ ActionButton,
9
+ ColDefT,
10
+ Grid,
11
+ GridCell,
12
+ GridContext,
13
+ GridContextMenuComponentProps,
14
+ GridContextProvider,
15
+ GridProps,
16
+ GridUpdatingContextProvider,
17
+ MenuItem,
18
+ wait,
19
+ } from "../..";
20
+ import "../../styles/GridTheme.scss";
21
+ import "../../styles/index.scss";
22
+ import { IFormTestRow } from "./FormTest";
23
+
24
+ export default {
25
+ title: "Components / Grids",
26
+ component: Grid,
27
+ args: {
28
+ quickFilterValue: "",
29
+ selectable: true,
30
+ },
31
+ decorators: [
32
+ (Story) => (
33
+ <div style={{ width: 1024, height: 400 }}>
34
+ <GridUpdatingContextProvider>
35
+ <GridContextProvider>
36
+ <Story />
37
+ </GridContextProvider>
38
+ </GridUpdatingContextProvider>
39
+ </div>
40
+ ),
41
+ ],
42
+ } as ComponentMeta<typeof Grid>;
43
+
44
+ const ContextMenu = ({ clickedRow, colDef, close }: GridContextMenuComponentProps<IFormTestRow>): ReactElement => {
45
+ const onClick = useCallback(() => {
46
+ switch (colDef.field) {
47
+ case "name":
48
+ clickedRow.name = "";
49
+ break;
50
+ case "distance":
51
+ clickedRow.distance = null;
52
+ break;
53
+ }
54
+ close();
55
+ }, [close, colDef.field, clickedRow]);
56
+
57
+ return (
58
+ <>
59
+ <button onClick={onClick}>Button - Clear cell</button>
60
+ <MenuItem onClick={onClick}>Menu Item - Clear cell</MenuItem>
61
+ </>
62
+ );
63
+ };
64
+
65
+ const GridPopoutContextMenuTemplate: ComponentStory<typeof Grid> = (props: GridProps) => {
66
+ const { selectRowsWithFlashDiff } = useContext(GridContext);
67
+ const [externalSelectedItems, setExternalSelectedItems] = useState<any[]>([]);
68
+ const [rowData, setRowData] = useState([
69
+ { id: 1000, name: "IS IS DP12345", nameType: "IS", numba: "IX", plan: "DP 12345", distance: 10 },
70
+ { id: 1001, name: "PEG V SD523", nameType: "PEG", numba: "V", plan: "SD 523", distance: null },
71
+ ] as IFormTestRow[]);
72
+
73
+ const columnDefs: ColDefT<IFormTestRow>[] = useMemo(
74
+ () => [
75
+ GridCell({
76
+ field: "id",
77
+ headerName: "Id",
78
+ }),
79
+ GridCell({
80
+ field: "name",
81
+ headerName: "Name",
82
+ }),
83
+ GridCell({
84
+ field: "distance",
85
+ headerName: "Number input",
86
+ valueFormatter: (params) => {
87
+ const v = params.data.distance;
88
+ return v != null ? `${v}m` : "–";
89
+ },
90
+ }),
91
+ ],
92
+ [],
93
+ );
94
+
95
+ const addRowAction = useCallback(async () => {
96
+ await wait(1000);
97
+
98
+ const lastRow = rowData[rowData.length - 1];
99
+ await selectRowsWithFlashDiff(async () => {
100
+ setRowData([
101
+ ...rowData,
102
+ {
103
+ id: (lastRow?.id ?? 0) + 1,
104
+ name: "?",
105
+ nameType: "?",
106
+ numba: "?",
107
+ plan: "",
108
+ distance: null,
109
+ },
110
+ ]);
111
+ });
112
+ }, [rowData, selectRowsWithFlashDiff]);
113
+
114
+ return (
115
+ <>
116
+ <Grid
117
+ {...props}
118
+ externalSelectedItems={externalSelectedItems}
119
+ setExternalSelectedItems={setExternalSelectedItems}
120
+ columnDefs={columnDefs}
121
+ rowData={rowData}
122
+ domLayout={"autoHeight"}
123
+ defaultColDef={{ minWidth: 70 }}
124
+ sizeColumns={"auto"}
125
+ onCellEditingComplete={() => {
126
+ /* eslint-disable-next-line no-console */
127
+ console.log("Cell editing complete");
128
+ }}
129
+ contextMenu={ContextMenu}
130
+ />
131
+ <ActionButton icon={"ic_add"} name={"Add new row"} inProgressName={"Adding..."} onClick={addRowAction} />
132
+ </>
133
+ );
134
+ };
135
+
136
+ export const EditContextMenu = GridPopoutContextMenuTemplate.bind({});
@@ -131,14 +131,14 @@ export const getQuick = (filter: IQueryQuick, container?: HTMLElement): HTMLElem
131
131
  };
132
132
 
133
133
  /**
134
- * Find by filter. Waits up to 4 seconds to find filter.
134
+ * Find by filter. Waits up to 5 seconds to find filter.
135
135
  *
136
136
  * @param filter Filter
137
137
  * @param container Optional container to look in
138
138
  * @return HTMLElement. Throws exception if not found.
139
139
  */
140
140
  export const findQuick = async <T extends HTMLElement>(filter: IQueryQuick, container?: HTMLElement): Promise<T> => {
141
- const endTime = Date.now() + 4000;
141
+ const endTime = Date.now() + 5000;
142
142
  while (Date.now() < endTime) {
143
143
  const el = queryQuick<T>(filter, container);
144
144
  if (el) return el;
@@ -1,20 +1,24 @@
1
- import { act, waitFor, within } from "@testing-library/react";
1
+ import { waitFor, within } from "@testing-library/react";
2
2
  import userEvent from "@testing-library/user-event";
3
3
  import { isEqual } from "lodash-es";
4
4
 
5
5
  import { IQueryQuick, findQuick, getAllQuick, getMatcher, getQuick, queryQuick } from "./testQuick";
6
6
 
7
+ let user = userEvent;
8
+ /**
9
+ * allow external userEvent to be used
10
+ * @param customisedUserEvent
11
+ */
12
+ export const setUpUserEvent = (customisedUserEvent: any) => {
13
+ user = customisedUserEvent;
14
+ };
15
+
7
16
  export const countRows = async (within?: HTMLElement): Promise<number> => {
8
17
  return getAllQuick({ tagName: `div[row-id]:not(:empty)` }, within).length;
9
18
  };
10
19
 
11
20
  export const findRow = async (rowId: number | string, within?: HTMLElement): Promise<HTMLDivElement> => {
12
- //if this is not wrapped in an act console errors are logged during testing
13
- let row!: HTMLDivElement;
14
- await act(async () => {
15
- row = await findQuick<HTMLDivElement>({ tagName: `div[row-id='${rowId}']:not(:empty)` }, within);
16
- });
17
- return row;
21
+ return await findQuick<HTMLDivElement>({ tagName: `div[row-id='${rowId}']:not(:empty)` }, within);
18
22
  };
19
23
 
20
24
  export const queryRow = async (rowId: number | string, within?: HTMLElement): Promise<HTMLDivElement | null> => {
@@ -26,19 +30,17 @@ const _selectRow = async (
26
30
  rowId: string | number,
27
31
  within?: HTMLElement,
28
32
  ): Promise<void> => {
29
- await act(async () => {
30
- const row = await findRow(rowId, within);
31
- const isSelected = row.className.includes("ag-row-selected");
32
- if (select === "toggle" || (select === "select" && !isSelected) || (select === "deselect" && isSelected)) {
33
- const cell = await findCell(rowId, "selection", within);
34
- await userEvent.click(cell);
35
- await waitFor(async () => {
36
- const row = await findRow(rowId, within);
37
- const nowSelected = row.className.includes("ag-row-selected");
38
- if (nowSelected == isSelected) throw `Row ${rowId} won't select`;
39
- });
40
- }
41
- });
33
+ const row = await findRow(rowId, within);
34
+ const isSelected = row.className.includes("ag-row-selected");
35
+ if (select === "toggle" || (select === "select" && !isSelected) || (select === "deselect" && isSelected)) {
36
+ const cell = await findCell(rowId, "selection", within);
37
+ await user.click(cell);
38
+ await waitFor(async () => {
39
+ const row = await findRow(rowId, within);
40
+ const nowSelected = row.className.includes("ag-row-selected");
41
+ if (nowSelected === isSelected) throw `Row ${rowId} won't select`;
42
+ });
43
+ }
42
44
  };
43
45
 
44
46
  export const selectRow = async (rowId: string | number, within?: HTMLElement): Promise<void> =>
@@ -48,13 +50,8 @@ export const deselectRow = async (rowId: string | number, within?: HTMLElement):
48
50
  _selectRow("deselect", rowId, within);
49
51
 
50
52
  export const findCell = async (rowId: number | string, colId: string, within?: HTMLElement): Promise<HTMLElement> => {
51
- //if this is not wrapped in an act console errors are logged during testing
52
- let cell!: HTMLElement;
53
- await act(async () => {
54
- const row = await findRow(rowId, within);
55
- cell = await findQuick({ tagName: `[col-id='${colId}']` }, row);
56
- });
57
- return cell;
53
+ const row = await findRow(rowId, within);
54
+ return await findQuick({ tagName: `[col-id='${colId}']` }, row);
58
55
  };
59
56
 
60
57
  export const findCellContains = async (
@@ -73,17 +70,13 @@ export const findCellContains = async (
73
70
  };
74
71
 
75
72
  export const selectCell = async (rowId: string | number, colId: string, within?: HTMLElement): Promise<void> => {
76
- await act(async () => {
77
- const cell = await findCell(rowId, colId, within);
78
- await userEvent.click(cell);
79
- });
73
+ const cell = await findCell(rowId, colId, within);
74
+ await user.click(cell);
80
75
  };
81
76
 
82
77
  export const editCell = async (rowId: number | string, colId: string, within?: HTMLElement): Promise<void> => {
83
- await act(async () => {
84
- const cell = await findCell(rowId, colId, within);
85
- await userEvent.dblClick(cell);
86
- });
78
+ const cell = await findCell(rowId, colId, within);
79
+ await user.dblClick(cell);
87
80
  await waitFor(findOpenPopover);
88
81
  };
89
82
 
@@ -127,10 +120,8 @@ export const validateMenuOptions = async (
127
120
  };
128
121
 
129
122
  export const clickMenuOption = async (menuOptionText: string | RegExp): Promise<void> => {
130
- await act(async () => {
131
- const menuOption = await findMenuOption(menuOptionText);
132
- await userEvent.click(menuOption);
133
- });
123
+ const menuOption = await findMenuOption(menuOptionText);
124
+ await user.click(menuOption);
134
125
  };
135
126
 
136
127
  export const openAndClickMenuOption = async (
@@ -172,26 +163,25 @@ export const findMultiSelectOption = async (value: string): Promise<HTMLElement>
172
163
 
173
164
  export const clickMultiSelectOption = async (value: string): Promise<void> => {
174
165
  const menuItem = await findMultiSelectOption(value);
175
- menuItem.parentElement && (await userEvent.click(menuItem.parentElement));
176
- };
177
-
178
- const typeInput = async (value: string, filter: IQueryQuick): Promise<void> =>
179
- act(async () => {
180
- const openMenu = await findOpenPopover();
181
- const input = await findQuick(filter, openMenu);
182
- await userEvent.clear(input);
183
- //'typing' an empty string will cause a console error, and it's also unnecessary after the previous clear call
184
- if (value.length > 0) {
185
- await userEvent.type(input, value);
186
- }
187
- });
166
+ menuItem.parentElement && (await user.click(menuItem.parentElement));
167
+ };
168
+
169
+ const typeInput = async (value: string, filter: IQueryQuick): Promise<void> => {
170
+ const openMenu = await findOpenPopover();
171
+ const input = await findQuick(filter, openMenu);
172
+ await user.clear(input);
173
+ //'typing' an empty string will cause a console error, and it's also unnecessary after the previous clear call
174
+ if (value.length > 0) {
175
+ await user.type(input, value);
176
+ }
177
+ };
188
178
 
189
179
  export const typeOnlyInput = async (value: string): Promise<void> =>
190
180
  typeInput(value, { child: { tagName: "input[type='text'], textarea" } });
191
181
 
192
182
  export const typeInputByLabel = async (value: string, labelText: string): Promise<void> => {
193
- const labels = getAllQuick({ child: { tagName: "label" } }).filter((l) => l.textContent == labelText);
194
- if (labels.length == 0) {
183
+ const labels = getAllQuick({ child: { tagName: "label" } }).filter((l) => l.textContent === labelText);
184
+ if (labels.length === 0) {
195
185
  throw Error(`Label not found for text: ${labelText}`);
196
186
  }
197
187
  if (labels.length > 1) {
@@ -219,8 +209,6 @@ export const findActionButton = (text: string, container?: HTMLElement): Promise
219
209
  findQuick({ tagName: "button", child: { classes: ".ActionButton-minimalAreaDisplay", text: text } }, container);
220
210
 
221
211
  export const clickActionButton = async (text: string, container?: HTMLElement): Promise<void> => {
222
- await act(async () => {
223
- const button = await findActionButton(text, container);
224
- await userEvent.click(button);
225
- });
212
+ const button = await findActionButton(text, container);
213
+ await user.click(button);
226
214
  };