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 a story of a custom <DatagridRow> #9847

Merged
merged 1 commit into from
May 14, 2024
Merged
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
68 changes: 65 additions & 3 deletions packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import fakeRestDataProvider from 'ra-data-fakerest';
import defaultMessages from 'ra-language-english';
import polyglotI18nProvider from 'ra-i18n-polyglot';

import { Box, styled } from '@mui/material';
import { Box, Checkbox, TableCell, TableRow, styled } from '@mui/material';
import { createTheme, ThemeProvider } from '@mui/material/styles';

import { TextField } from '../../field';
import { FieldProps, TextField } from '../../field';
import { BulkDeleteButton, BulkExportButton } from '../../button';
import { Datagrid } from './Datagrid';
import { Datagrid, DatagridProps } from './Datagrid';
import { SimpleShowLayout } from '../../detail';
import { AdminUI } from '../../AdminUI';
import { AdminContext } from '../../AdminContext';
import { List } from '../List';
import { EditGuesser } from '../../detail';
import { DatagridRowProps } from './DatagridRow';
import DatagridBody, { DatagridBodyProps } from './DatagridBody';

export default { title: 'ra-ui-materialui/list/Datagrid' };

Expand Down Expand Up @@ -491,3 +493,63 @@ export const FullApp = () => (
</AdminUI>
</AdminContext>
);

const MyDatagridRow = ({
onToggleItem,
children,
selected,
selectable,
}: DatagridRowProps) => {
const record = useRecordContext();
return record ? (
<TableRow
sx={{
'&:nth-of-type(odd)': {
backgroundColor: theme.palette.action.hover,
},
'&:last-child td, &:last-child th': {
border: 0,
},
}}
>
<TableCell padding="none">
{selectable && (
<Checkbox
checked={selected}
onClick={event => {
if (onToggleItem) {
onToggleItem(record.id, event);
}
}}
/>
)}
</TableCell>
{React.Children.map(children, field =>
React.isValidElement<FieldProps>(field) &&
field.props.source ? (
<TableCell key={`${record.id}-${field.props.source}`}>
{field}
</TableCell>
) : null
)}
</TableRow>
) : null;
};

const MyDatagridBody = (props: DatagridBodyProps) => (
<DatagridBody {...props} row={<MyDatagridRow />} />
);
const MyDatagrid = (props: DatagridProps) => (
<Datagrid {...props} body={<MyDatagridBody />} />
);

export const CustomDatagridRow = () => (
<Wrapper>
<MyDatagrid>
<TextField source="id" />
<TextField source="title" />
<TextField source="author" />
<TextField source="year" />
</MyDatagrid>
</Wrapper>
);
Loading