-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] An input element in a custom header filter loses focus after a first letter is typed #12326
Comments
Hey @vovarudomanenko, Thanks for reporting the issue, the reason why it's not working is the header filter edit state needs to be imperatively triggered, you can do that by adding an function CustomFilter(props: GridHeaderFilterCellProps) {
+ const apiRef = useGridApiContext();
return <input
placeholder="Search..."
+ onFocus={() => apiRef.current.startHeaderFilterEditMode(props.colDef.field)}
/>;
} It works on two clicks because the first click is intercepted by the cell itself, you can expose the diff --git a/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx b/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx
index e2d8d8de8..f11a9c1a7 100644
--- a/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx
+++ b/packages/x-data-grid-pro/src/components/headerFiltering/GridHeaderFilterCell.tsx
@@ -30,6 +30,10 @@ import { DataGridProProcessedProps } from '../../models/dataGridProProps';
import { GridHeaderFilterMenuContainer } from './GridHeaderFilterMenuContainer';
import { GridHeaderFilterClearButton } from './GridHeaderFilterClearButton';
+export interface RenderHeaderFilterProps extends GridHeaderFilterCellProps {
+ inputRef: React.RefObject<unknown>;
+}
+
export interface GridHeaderFilterCellProps extends Pick<GridStateColDef, 'headerClassName'> {
colIndex: number;
height: number;
@@ -133,7 +137,7 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe
let headerFilterComponent: React.ReactNode;
if (colDef.renderHeaderFilter) {
- headerFilterComponent = colDef.renderHeaderFilter(props);
+ headerFilterComponent = colDef.renderHeaderFilter({...props, inputRef});
}
React.useLayoutEffect(() => {
diff --git a/packages/x-data-grid-pro/src/typeOverloads/modules.ts b/packages/x-data-grid-pro/src/typeOverloads/modules.ts
index a2fcde4f2..04d0d4ff0 100644
--- a/packages/x-data-grid-pro/src/typeOverloads/modules.ts
+++ b/packages/x-data-grid-pro/src/typeOverloads/modules.ts
@@ -4,18 +4,19 @@ import type {
GridRowOrderChangeParams,
GridFetchRowsParams,
} from '../models';
-import type { GridHeaderFilterCellProps } from '../components/headerFiltering/GridHeaderFilterCell';
+import type { RenderHeaderFilterProps } from '../components/headerFiltering/GridHeaderFilterCell';
import type { GridColumnPinningInternalCache } from '../hooks/features/columnPinning/gridColumnPinningInterface';
import type { GridCanBeReorderedPreProcessingContext } from '../hooks/features/columnReorder/columnReorderInterfaces';
import { GridRowPinningInternalCache } from '../hooks/features/rowPinning/gridRowPinningInterface';
+
export interface GridColDefPro {
/**
* Allows to render a component in the column header filter cell.
- * @param {GridHeaderFilterCellProps} params Object containing parameters for the renderer.
+ * @param {RenderHeaderFilterProps} params Object containing parameters for the renderer and `inputRef`.
* @returns {React.ReactNode} The element to be rendered.
*/
- renderHeaderFilter?: (params: GridHeaderFilterCellProps) => React.ReactNode;
+ renderHeaderFilter?: (params: RenderHeaderFilterProps) => React.ReactNode;
}
export interface GridControlledStateEventLookupPro { Feel free to update the PR you have with this diff. |
Thank you for the response @MBilalShafi. |
How did we do @vovarudomanenko? |
@MBilalShafi I don't think the issue has been resolved as currently I'm facing the same issue when trying to render a Textfield in the renderHeaderFilter. When I tried the solution that you have provided the input only focus on the second click.
|
@Shudhanshu-Appnox you forgot to pass down inputRef to the underlying input element i const { colDef, inputRef } = props;
//...
<TextField
// ...
InputProps={{
// ...
ref: inputRef
}}
/> |
@vovarudomanenko You have extracted the input ref from the props but its not present in the passed props. |
@Shudhanshu-Appnox It is not present because your props have old type ![]() |
Steps to reproduce
Link to live example: https://codesandbox.io/p/sandbox/optimistic-boyd-xjx87z?file=%2Fsrc%2FDemo.tsx%3A16%2C37
Steps:
Current behavior
When I try typing the first letter in an input element, then the input immediately loses focus, preventing me from continuing to type
Expected behavior
An input element doesn't immediately lose focus after pressing a first letter
Context
I'm trying to use custom header filter instead of the default one.
Your environment
npx @mui/envinfo
Search keywords: header filter, renderHeaderFilter, DataGrid, data grid
Order ID: 74777
The text was updated successfully, but these errors were encountered: