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

Fix ReferenceField doesn't accept the emptyText prop #5579

Merged
merged 1 commit into from
Nov 24, 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
16 changes: 16 additions & 0 deletions packages/ra-ui-materialui/src/field/ReferenceField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ describe('<ReferenceField />', () => {
});
});

it('should display the emptyText if the field is empty', () => {
const { getByText } = renderWithRedux(
<ReferenceField
record={{ id: 123 }}
resource="comments"
source="postId"
reference="posts"
basePath="/comments"
emptyText="EMPTY"
>
<TextField source="title" />
</ReferenceField>
);
expect(getByText('EMPTY')).not.toBeNull();
});

it('should use the reference from the store if available', () => {
const { container, getByText } = renderWithRedux(
<MemoryRouter>
Expand Down
80 changes: 49 additions & 31 deletions packages/ra-ui-materialui/src/field/ReferenceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import PropTypes from 'prop-types';
import classnames from 'classnames';
import get from 'lodash/get';
import { makeStyles } from '@material-ui/core/styles';
import { Typography } from '@material-ui/core';
import ErrorIcon from '@material-ui/icons/Error';
import {
useReference,
UseReferenceProps,
getResourceLinkPath,
LinkToType,
ResourceContextProvider,
Record,
} from 'ra-core';

import LinearProgress from '../layout/LinearProgress';
Expand Down Expand Up @@ -63,40 +65,21 @@ import { ClassesOverride } from '../types';
* In previous versions of React-Admin, the prop `linkType` was used. It is now deprecated and replaced with `link`. However
* backward-compatibility is still kept
*/

const ReferenceField: FC<ReferenceFieldProps> = ({
children,
record,
source,
emptyText,
...props
}) => {
if (React.Children.count(children) !== 1) {
throw new Error('<ReferenceField> only accepts a single child');
}
const { basePath, resource } = props;
const resourceLinkPath = getResourceLinkPath({
...props,
resource,
record,
source,
basePath,
});

return (
<ResourceContextProvider value={props.reference}>
<PureReferenceFieldView
{...props}
{...useReference({
reference: props.reference,
id: get(record, source),
})}
resourceLinkPath={resourceLinkPath}
>
{children}
</PureReferenceFieldView>
</ResourceContextProvider>
}) =>
get(record, source) == null ? (
emptyText ? (
<Typography component="span" variant="body2">
{emptyText}
</Typography>
) : null
) : (
<NonEmptyReferenceField {...props} record={record} source={source} />
);
};

ReferenceField.propTypes = {
addLabel: PropTypes.bool,
Expand Down Expand Up @@ -132,9 +115,9 @@ ReferenceField.defaultProps = {
link: 'edit',
};

export interface ReferenceFieldProps
export interface ReferenceFieldProps<RecordType extends Record = Record>
extends PublicFieldProps,
InjectedFieldProps {
InjectedFieldProps<RecordType> {
children: ReactElement;
classes?: ClassesOverride<typeof useStyles>;
reference: string;
Expand All @@ -145,6 +128,41 @@ export interface ReferenceFieldProps
link?: LinkToType;
}

/**
* This intermediate component is made necessary by the useReference hook,
* which cannot be called conditionally when get(record, source) is empty.
*/
export const NonEmptyReferenceField: FC<Omit<
ReferenceFieldProps,
'emptyText'
>> = ({ children, record, source, ...props }) => {
if (React.Children.count(children) !== 1) {
throw new Error('<ReferenceField> only accepts a single child');
}
const { basePath, resource } = props;
const resourceLinkPath = getResourceLinkPath({
...props,
resource,
record,
source,
basePath,
});
return (
<ResourceContextProvider value={props.reference}>
<PureReferenceFieldView
{...props}
{...useReference({
reference: props.reference,
id: get(record, source),
})}
resourceLinkPath={resourceLinkPath}
>
{children}
</PureReferenceFieldView>
</ResourceContextProvider>
);
};

const useStyles = makeStyles(
theme => ({
link: {
Expand Down