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 support for <ReferenceField emptyText> when the reference is missing #7851

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/ra-ui-materialui/src/field/ReferenceField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { QueryClient } from 'react-query';
import { createTheme, ThemeProvider } from '@mui/material/styles';

import { ReferenceField } from './ReferenceField';
import { Children } from './ReferenceField.stories';
import { Children, MissingReference } from './ReferenceField.stories';
import { TextField } from './TextField';

const theme = createTheme({});
Expand Down Expand Up @@ -243,6 +243,11 @@ describe('<ReferenceField />', () => {
expect(screen.getByText('EMPTY')).not.toBeNull();
});

it('should display the emptyText if there is no reference', async () => {
render(<MissingReference />);
await screen.findByText('no detail');
});

it('should use record from RecordContext', async () => {
const dataProvider = testDataProvider({
getMany: jest.fn().mockResolvedValue({
Expand Down
19 changes: 19 additions & 0 deletions packages/ra-ui-materialui/src/field/ReferenceField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ export const Empty = () => (
</Wrapper>
);

const missingReferenceDataProvider = {
getMany: () =>
Promise.resolve({
data: [],
}),
} as any;

export const MissingReference = () => (
<Wrapper dataProvider={missingReferenceDataProvider}>
<ReferenceField
source="detail_id"
reference="book_details"
emptyText="no detail"
>
<TextField source="ISBN" />
</ReferenceField>
</Wrapper>
);

export const Link = () => (
<Wrapper>
<ReferenceField source="detail_id" reference="book_details" link="show">
Expand Down
12 changes: 9 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export const ReferenceField: FC<ReferenceFieldProps> = props => {
</Typography>
) : null
) : (
<NonEmptyReferenceField {...rest} record={record} id={id} />
<NonEmptyReferenceField
{...rest}
emptyText={emptyText}
record={record}
id={id}
/>
);
};

Expand Down Expand Up @@ -123,7 +128,7 @@ export interface ReferenceFieldProps<RecordType extends RaRecord = any>
* which cannot be called conditionally when get(record, source) is empty.
*/
export const NonEmptyReferenceField: FC<
Omit<ReferenceFieldProps, 'emptyText' | 'source'> & { id: Identifier }
Omit<ReferenceFieldProps, 'source'> & { id: Identifier }
> = ({ children, id, record, reference, link, ...props }) => {
const createPath = useCreatePath();
const resourceLinkPath =
Expand Down Expand Up @@ -162,6 +167,7 @@ export const ReferenceFieldView: FC<ReferenceFieldViewProps> = props => {
const {
children,
className,
emptyText,
error,
isLoading,
referenceRecord,
Expand All @@ -185,7 +191,7 @@ export const ReferenceFieldView: FC<ReferenceFieldViewProps> = props => {
return <LinearProgress />;
}
if (!referenceRecord) {
return null;
return emptyText ? <>{emptyText}</> : null;
}

if (resourceLinkPath) {
Expand Down