@linzjs/step-ag-grid 21.2.0 → 21.3.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/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": "21.2.0",
5
+ "version": "21.3.0",
6
6
  "keywords": [
7
7
  "aggrid",
8
8
  "ag-grid",
@@ -39,6 +39,7 @@ export interface GridBaseRow {
39
39
 
40
40
  export interface GridProps {
41
41
  readOnly?: boolean; // set all editables to false when read only, make all styles black, otherwise style is gray for not editable
42
+ defaultPostSort?: boolean; // Retain sort order after edit, Defaults to true.
42
43
  selectable?: boolean;
43
44
  theme?: string; // should have prefix ag-theme-
44
45
  ["data-testid"]?: string;
@@ -114,6 +115,7 @@ export interface GridProps {
114
115
  */
115
116
  export const Grid = ({
116
117
  "data-testid": dataTestId,
118
+ defaultPostSort = true,
117
119
  rowSelection = "multiple",
118
120
  suppressColumnVirtualization = true,
119
121
  theme = "ag-theme-step-default",
@@ -742,7 +744,7 @@ export const Grid = ({
742
744
  onModelUpdated={onModelUpdated}
743
745
  onGridReady={onGridReady}
744
746
  onSortChanged={ensureSelectedRowIsVisible}
745
- postSortRows={params.onRowDragEnd ? undefined : postSortRows}
747
+ postSortRows={params.onRowDragEnd || !defaultPostSort ? undefined : postSortRows}
746
748
  onSelectionChanged={synchroniseExternalStateToGridSelection}
747
749
  onColumnMoved={params.onColumnMoved}
748
750
  alwaysShowVerticalScroll={params.alwaysShowVerticalScroll}
@@ -639,7 +639,24 @@ export const GridContextProvider = <TData extends GridBaseRow>(props: PropsWithC
639
639
  const onFilterChanged = useMemo(
640
640
  () =>
641
641
  debounce(() => {
642
- gridApi && gridApi.onFilterChanged();
642
+ // This is terrible, but there's no other way for me to check whether a filter has changed the grid
643
+ const getDisplayedRowsHash = () => {
644
+ const arr: any[] = [];
645
+ gridApi?.forEachNodeAfterFilter((rowNode) => {
646
+ arr.push(rowNode.id);
647
+ });
648
+ return arr.join("|");
649
+ };
650
+
651
+ if (gridApi) {
652
+ const hasFocusedCell = gridApi.getFocusedCell();
653
+ const preHash = hasFocusedCell && getDisplayedRowsHash();
654
+ gridApi.onFilterChanged();
655
+ const postHash = hasFocusedCell && getDisplayedRowsHash();
656
+ // Ag-grid has a bug where if a focused cell comes into view after a filter the filter loses focus
657
+ // So the focus is cleared to prevent this
658
+ preHash !== postHash && gridApi.clearFocusedCell();
659
+ }
643
660
  }, 200),
644
661
  [gridApi],
645
662
  );
@@ -255,7 +255,14 @@ const GridReadOnlyTemplate: StoryFn<typeof Grid> = (props: GridProps) => {
255
255
  );
256
256
  };
257
257
 
258
- const GridFilterLessThan = (props: { field: keyof ITestRow; text: string }): ReactElement => {
258
+ type KeysOfType<TObject, TValue> = {
259
+ [K in keyof TObject]: TObject[K] extends TValue ? K : never;
260
+ }[keyof TObject];
261
+
262
+ const GridFilterLessThan = (props: {
263
+ field: KeysOfType<ITestRow, number | null | undefined>;
264
+ text: string;
265
+ }): ReactElement => {
259
266
  const [value, setValue] = useState<number>();
260
267
 
261
268
  const filter = useCallback(