Skip to content

Commit

Permalink
fix: custom filter loses focus after filter (#468)
Browse files Browse the repository at this point in the history
* fix filter loses focus after filter
  • Loading branch information
matttdawson committed Jul 2, 2024
1 parent 1ebf76b commit a331b24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/contexts/GridContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,24 @@ export const GridContextProvider = <TData extends GridBaseRow>(props: PropsWithC
const onFilterChanged = useMemo(
() =>
debounce(() => {
gridApi && gridApi.onFilterChanged();
// This is terrible, but there's no other way for me to check whether a filter has changed the grid
const getDisplayedRowsHash = () => {
const arr: any[] = [];
gridApi?.forEachNodeAfterFilter((rowNode) => {
arr.push(rowNode.id);
});
return arr.join("|");
};

if (gridApi) {
const hasFocusedCell = gridApi.getFocusedCell();
const preHash = hasFocusedCell && getDisplayedRowsHash();
gridApi.onFilterChanged();
const postHash = hasFocusedCell && getDisplayedRowsHash();
// Ag-grid has a bug where if a focused cell comes into view after a filter the filter loses focus
// So the focus is cleared to prevent this
preHash !== postHash && gridApi.clearFocusedCell();
}
}, 200),
[gridApi],
);
Expand Down
9 changes: 8 additions & 1 deletion src/stories/grid/GridReadOnly.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ const GridReadOnlyTemplate: StoryFn<typeof Grid> = (props: GridProps) => {
);
};

const GridFilterLessThan = (props: { field: keyof ITestRow; text: string }): ReactElement => {
type KeysOfType<TObject, TValue> = {
[K in keyof TObject]: TObject[K] extends TValue ? K : never;
}[keyof TObject];

const GridFilterLessThan = (props: {
field: KeysOfType<ITestRow, number | null | undefined>;
text: string;
}): ReactElement => {
const [value, setValue] = useState<number>();

const filter = useCallback(
Expand Down

0 comments on commit a331b24

Please sign in to comment.