Skip to content

Commit

Permalink
Merge pull request #6036 from marmelab/singlefieldlist-component
Browse files Browse the repository at this point in the history
singlefieldlist-component
  • Loading branch information
djhi authored Mar 15, 2021
2 parents 2e3dfa1 + bdc41c9 commit 76def6e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/ra-ui-materialui/src/list/SingleFieldList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as React from 'react';
import { cloneElement, Children, HtmlHTMLAttributes, FC } from 'react';
import {
cloneElement,
Children,
HtmlHTMLAttributes,
FC,
ComponentType,
} from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import LinearProgress from '@material-ui/core/LinearProgress';
Expand All @@ -10,6 +16,7 @@ import {
useListContext,
useResourceContext,
RecordContextProvider,
ComponentPropType,
} from 'ra-core';

import Link from '../Link';
Expand Down Expand Up @@ -74,19 +81,21 @@ const SingleFieldList: FC<SingleFieldListProps> = props => {
className,
children,
linkType = 'edit',
component = 'div',
...rest
} = props;
const { ids, data, loaded, basePath } = useListContext(props);
const resource = useResourceContext(props);

const classes = useStyles(props);
const Component = component;

if (loaded === false) {
return <LinearProgress />;
}

return (
<div
<Component
className={classnames(classes.root, className)}
{...sanitizeListRestProps(rest)}
>
Expand All @@ -97,7 +106,7 @@ const SingleFieldList: FC<SingleFieldListProps> = props => {

if (resourceLinkPath) {
return (
<RecordContextProvider value={data[id]}>
<RecordContextProvider value={data[id]} key={id}>
<Link
className={classes.link}
key={id}
Expand All @@ -117,7 +126,7 @@ const SingleFieldList: FC<SingleFieldListProps> = props => {
}

return (
<RecordContextProvider value={data[id]}>
<RecordContextProvider value={data[id]} key={id}>
{cloneElement(Children.only(children), {
key: id,
record: data[id],
Expand All @@ -127,7 +136,7 @@ const SingleFieldList: FC<SingleFieldListProps> = props => {
</RecordContextProvider>
);
})}
</div>
</Component>
);
};

Expand All @@ -136,6 +145,7 @@ SingleFieldList.propTypes = {
children: PropTypes.element.isRequired,
classes: PropTypes.object,
className: PropTypes.string,
component: ComponentPropType,
data: PropTypes.object,
ids: PropTypes.array,
// @ts-ignore
Expand All @@ -147,6 +157,7 @@ export interface SingleFieldListProps
extends HtmlHTMLAttributes<HTMLDivElement> {
className?: string;
classes?: ClassesOverride<typeof useStyles>;
component?: ComponentType<any>;
linkType?: string | false;
children: React.ReactElement;
}
Expand Down

0 comments on commit 76def6e

Please sign in to comment.