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

Forward ref in data grid element #5202

Merged
merged 1 commit into from
Aug 31, 2020
Merged
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
5 changes: 3 additions & 2 deletions packages/ra-ui-materialui/src/list/Datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { ClassesOverride } from '../types';
* </Datagrid>
* </ReferenceManyField>
*/
const Datagrid: FC<DatagridProps> = props => {
const Datagrid: FC<DatagridProps> = React.forwardRef((props, ref) => {
const classes = useDatagridStyles(props);
const {
optimized = false,
Expand Down Expand Up @@ -168,6 +168,7 @@ const Datagrid: FC<DatagridProps> = props => {
*/
return (
<Table
ref={ref}
className={classnames(classes.table, className)}
size={size}
{...sanitizeListRestProps(rest)}
Expand Down Expand Up @@ -244,7 +245,7 @@ const Datagrid: FC<DatagridProps> = props => {
)}
</Table>
);
};
});

Datagrid.propTypes = {
basePath: PropTypes.string,
Expand Down
106 changes: 58 additions & 48 deletions packages/ra-ui-materialui/src/list/DatagridBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,64 @@ import { Identifier, Record, RecordMap } from 'ra-core';
import DatagridRow, { PureDatagridRow } from './DatagridRow';
import useDatagridStyles from './useDatagridStyles';

const DatagridBody: FC<DatagridBodyRow> = ({
basePath,
children,
classes,
className,
data,
expand,
hasBulkActions,
hover,
ids,
onToggleItem,
resource,
row,
rowClick,
rowStyle,
selectedIds,
isRowSelectable,
...rest
}) => (
<TableBody className={classnames('datagrid-body', className)} {...rest}>
{ids.map((id, rowIndex) =>
cloneElement(
row,
{
basePath,
classes,
className: classnames(classes.row, {
[classes.rowEven]: rowIndex % 2 === 0,
[classes.rowOdd]: rowIndex % 2 !== 0,
[classes.clickableRow]: rowClick,
}),
expand,
hasBulkActions,
hover,
id,
key: id,
onToggleItem,
record: data[id],
resource,
rowClick,
selectable: !isRowSelectable || isRowSelectable(data[id]),
selected: selectedIds.includes(id),
style: rowStyle ? rowStyle(data[id], rowIndex) : null,
},
children
)
)}
</TableBody>
const DatagridBody: FC<DatagridBodyRow> = React.forwardRef(
(
{
basePath,
children,
classes,
className,
data,
expand,
hasBulkActions,
hover,
ids,
onToggleItem,
resource,
row,
rowClick,
rowStyle,
selectedIds,
isRowSelectable,
...rest
},
ref
) => (
<TableBody
ref={ref}
className={classnames('datagrid-body', className)}
{...rest}
>
{ids.map((id, rowIndex) =>
cloneElement(
row,
{
basePath,
classes,
className: classnames(classes.row, {
[classes.rowEven]: rowIndex % 2 === 0,
[classes.rowOdd]: rowIndex % 2 !== 0,
[classes.clickableRow]: rowClick,
}),
expand,
hasBulkActions,
hover,
id,
key: id,
onToggleItem,
record: data[id],
resource,
rowClick,
selectable:
!isRowSelectable || isRowSelectable(data[id]),
selected: selectedIds.includes(id),
style: rowStyle ? rowStyle(data[id], rowIndex) : null,
},
children
)
)}
</TableBody>
)
);

DatagridBody.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import TableCell from '@material-ui/core/TableCell';
import TableCell, { TableCellProps } from '@material-ui/core/TableCell';
import classnames from 'classnames';

export type DatagridCellProps = {
field: React.ReactElement;
record: Record<string, any>;
basePath: string;
resource: string;
} & TableCellProps;

const sanitizeRestProps = ({
cellClassName,
className,
Expand All @@ -13,17 +20,14 @@ const sanitizeRestProps = ({
basePath,
resource,
...rest
}) => rest;
}: Record<string, any>) => rest;

const DatagridCell = ({
className,
field,
record,
basePath,
resource,
...rest
}) => (
const DatagridCell: React.FC<DatagridCellProps> = React.forwardRef<
HTMLTableCellElement,
DatagridCellProps
>(({ className, field, record, basePath, resource, ...rest }, ref) => (
<TableCell
ref={ref}
className={classnames(className, field.props.cellClassName)}
align={field.props.textAlign}
{...sanitizeRestProps(rest)}
Expand All @@ -34,7 +38,7 @@ const DatagridCell = ({
resource,
})}
</TableCell>
);
));

DatagridCell.propTypes = {
className: PropTypes.string,
Expand Down
Loading