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

SimpleList: Add support for custom url #6594

Merged
merged 2 commits into from
Sep 27, 2021
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
86 changes: 86 additions & 0 deletions packages/ra-ui-materialui/src/list/SimpleList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,90 @@ describe('<SimpleList />', () => {
).not.toBeNull();
});
});

it.each([
[
'edit',
'edit',
['http://localhost/posts/1', 'http://localhost/posts/2'],
],
[
'show',
'show',
['http://localhost/posts/1/show', 'http://localhost/posts/2/show'],
],
[
'custom',
(record, id) => `/posts/${id}/custom`,
[
'http://localhost/posts/1/custom',
'http://localhost/posts/2/custom',
],
],
])(
'should render %s links for each item',
async (_, link, expectedUrls) => {
const { getByText } = renderWithRouter(
<ListContext.Provider
value={{
loaded: true,
loading: false,
ids: [1, 2],
data: {
1: { id: 1, title: 'foo' },
2: { id: 2, title: 'bar' },
},
total: 2,
resource: 'posts',
basePath: '/posts',
}}
>
<SimpleList
linkType={link}
primaryText={record => record.id.toString()}
secondaryText={<TextField source="title" />}
/>
</ListContext.Provider>
);

await waitFor(() => {
expect(getByText('1').closest('a').href).toEqual(
expectedUrls[0]
);
expect(getByText('2').closest('a').href).toEqual(
expectedUrls[1]
);
});
}
);

it('should not render links if linkType is false', async () => {
const { getByText } = renderWithRouter(
<ListContext.Provider
value={{
loaded: true,
loading: false,
ids: [1, 2],
data: {
1: { id: 1, title: 'foo' },
2: { id: 2, title: 'bar' },
},
total: 2,
resource: 'posts',
basePath: '/posts',
}}
>
<SimpleList
linkType={false}
primaryText={record => record.id.toString()}
secondaryText={<TextField source="title" />}
/>
</ListContext.Provider>
);

await waitFor(() => {
expect(getByText('1').closest('a')).toBeNull();
expect(getByText('2').closest('a')).toBeNull();
});
});
});
178 changes: 100 additions & 78 deletions packages/ra-ui-materialui/src/list/SimpleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ListItem,
ListItemAvatar,
ListItemIcon,
ListItemProps,
ListItemSecondaryAction,
ListItemText,
} from '@material-ui/core';
Expand Down Expand Up @@ -129,76 +130,69 @@ const SimpleList = <RecordType extends Record = Record>(
basePath={basePath}
id={id}
record={data[id]}
style={
rowStyle
? rowStyle(data[id], rowIndex)
: undefined
}
>
<ListItem
button={!!linkType as any}
style={
rowStyle
? rowStyle(data[id], rowIndex)
: undefined
}
>
{leftIcon && (
<ListItemIcon>
{leftIcon(data[id], id)}
</ListItemIcon>
)}
{leftAvatar && (
<ListItemAvatar>
{renderAvatar(id, leftAvatar)}
</ListItemAvatar>
)}
<ListItemText
primary={
<div>
{isValidElement(primaryText)
? primaryText
: primaryText(data[id], id)}
{leftIcon && (
<ListItemIcon>
{leftIcon(data[id], id)}
</ListItemIcon>
)}
{leftAvatar && (
<ListItemAvatar>
{renderAvatar(id, leftAvatar)}
</ListItemAvatar>
)}
<ListItemText
primary={
<div>
{isValidElement(primaryText)
? primaryText
: primaryText(data[id], id)}

{!!tertiaryText &&
(isValidElement(
tertiaryText
) ? (
tertiaryText
) : (
<span
className={
classes.tertiary
}
>
{tertiaryText(
data[id],
id
)}
</span>
))}
</div>
}
secondary={
!!secondaryText &&
(isValidElement(secondaryText)
? secondaryText
: secondaryText(data[id], id))
}
/>
{(rightAvatar || rightIcon) && (
<ListItemSecondaryAction>
{rightAvatar && (
<Avatar>
{renderAvatar(
id,
rightAvatar
)}
</Avatar>
)}
{rightIcon && (
<ListItemIcon>
{rightIcon(data[id], id)}
</ListItemIcon>
)}
</ListItemSecondaryAction>
)}
</ListItem>
{!!tertiaryText &&
(isValidElement(
tertiaryText
) ? (
tertiaryText
) : (
<span
className={
classes.tertiary
}
>
{tertiaryText(
data[id],
id
)}
</span>
))}
</div>
}
secondary={
!!secondaryText &&
(isValidElement(secondaryText)
? secondaryText
: secondaryText(data[id], id))
}
/>
{(rightAvatar || rightIcon) && (
<ListItemSecondaryAction>
{rightAvatar && (
<Avatar>
{renderAvatar(id, rightAvatar)}
</Avatar>
)}
{rightIcon && (
<ListItemIcon>
{rightIcon(data[id], id)}
</ListItemIcon>
)}
</ListItemSecondaryAction>
)}
</LinkOrNot>
</li>
</RecordContextProvider>
Expand Down Expand Up @@ -255,40 +249,68 @@ export interface SimpleListProps<RecordType extends Record = Record>

const useLinkOrNotStyles = makeStyles(
{
link: {
textDecoration: 'none',
color: 'inherit',
},
link: {},
},
{ name: 'RaLinkOrNot' }
);

const LinkOrNot = (props: LinkOrNotProps) => {
const LinkOrNot = (
props: LinkOrNotProps & Omit<ListItemProps, 'button' | 'component' | 'id'>
) => {
const {
classes: classesOverride,
linkType,
basePath,
id,
children,
record,
...rest
} = props;
const classes = useLinkOrNotStyles({ classes: classesOverride });
const link =
typeof linkType === 'function' ? linkType(record, id) : linkType;

return link === 'edit' || link === true ? (
<Link to={linkToRecord(basePath, id)} className={classes.link}>
<ListItem
button
// @ts-ignore
component={Link}
to={linkToRecord(basePath, id)}
className={classes.link}
{...rest}
>
{children}
</Link>
</ListItem>
) : link === 'show' ? (
<Link
<ListItem
button
// @ts-ignore
component={Link}
to={`${linkToRecord(basePath, id)}/show`}
className={classes.link}
{...rest}
>
{children}
</ListItem>
) : link !== false ? (
<ListItem
button
// @ts-ignore
component={Link}
to={link}
className={classes.link}
{...rest}
>
{children}
</Link>
</ListItem>
) : (
<span>{children}</span>
<ListItem
// @ts-ignore
component="div"
{...rest}
>
{children}
</ListItem>
);
};

Expand Down