Skip to content
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

Add generic type in datagrid #10607

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ const defaultBulkActionButtons = <BulkDeleteButton />;
* );
* }
*/
export const Datagrid: React.ForwardRefExoticComponent<
Omit<DatagridProps, 'ref'> & React.RefAttributes<HTMLTableElement>
> = React.forwardRef<HTMLTableElement, DatagridProps>((props, ref) => {

const fixedForwardRef = <T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode
): ((props: P & React.RefAttributes<T>) => React.ReactNode) =>
React.forwardRef(render) as any;
Comment on lines +121 to +124
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to genericForwardRef (just like our genericMemo) and extract it in its own file directly under src? I have a feeling we're going to use it elsewhere


const WDatagrid = <RecordType extends RaRecord = any>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDatagrid? I suggest DatagridComponent

props: DatagridProps<RecordType>,
ref
) => {
const resourceFromContext = useResourceContext(props);
const { canAccess: canDelete } = useCanAccess({
resource: resourceFromContext,
Expand Down Expand Up @@ -299,7 +306,9 @@ export const Datagrid: React.ForwardRefExoticComponent<
</OptionalResourceContextProvider>
</DatagridContextProvider>
);
});
};

export const Datagrid = fixedForwardRef(WDatagrid);

const createOrCloneElement = (element, props, children) =>
isValidElement(element)
Expand Down Expand Up @@ -524,7 +533,7 @@ export interface DatagridProps<RecordType extends RaRecord = any>
* </List>
* );
*/
rowClick?: string | RowClickFunction | false;
rowClick?: string | RowClickFunction<RecordType> | false;

/**
* A function that returns the sx prop to apply to a row.
Expand Down Expand Up @@ -599,6 +608,6 @@ const sanitizeRestProps = props =>
)
.reduce((acc, key) => ({ ...acc, [key]: props[key] }), {});

Datagrid.displayName = 'Datagrid';
WDatagrid.displayName = 'Datagrid';

const DefaultEmpty = <ListNoResults />;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { ReactElement, FC, memo } from 'react';

Check warning on line 2 in packages/ra-ui-materialui/src/list/datagrid/DatagridLoading.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'ReactElement' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 2 in packages/ra-ui-materialui/src/list/datagrid/DatagridLoading.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'FC' is defined but never used. Allowed unused vars must match /^_/u
import {
Table,
TableCell,
Expand All @@ -11,10 +11,11 @@
} from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import clsx from 'clsx';
import { useTimeout, Identifier, RaRecord } from 'ra-core';

Check warning on line 14 in packages/ra-ui-materialui/src/list/datagrid/DatagridLoading.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'Identifier' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in packages/ra-ui-materialui/src/list/datagrid/DatagridLoading.tsx

View workflow job for this annotation

GitHub Actions / typecheck

'RaRecord' is defined but never used. Allowed unused vars must match /^_/u

import { DatagridClasses } from './useDatagridStyles';
import { Placeholder } from '../Placeholder';
import { DatagridProps } from './Datagrid';

const times = (nbChildren, fn) =>
Array.from({ length: nbChildren }, (_, key) => fn(key));
Expand Down Expand Up @@ -117,13 +118,7 @@

export interface DatagridLoadingProps {
className?: string;
expand?:
| ReactElement
| FC<{
id: Identifier;
record: RaRecord;
resource: string;
}>;
expand?: DatagridProps['expand'];
hasBulkActions?: boolean;
nbChildren: number;
nbFakeLines?: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/list/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Identifier, RaRecord } from 'ra-core';

export type RowClickFunction = <RecordType extends RaRecord = RaRecord>(
export type RowClickFunction<RecordType extends RaRecord = RaRecord> = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this in SimpleListItem from SimpleList too. Can you make it accept a RecordType as well?

id: Identifier,
resource: string,
record: RecordType
Expand Down
Loading