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 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 docs/ReferenceField.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,28 @@ With this configuration, `<ReferenceField>` wraps the user's name in a link to t
| `source` | Required | `string` | - | Name of the property to display |
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'posts' |
| `children` | Required | `ReactNode` | - | One or more Field elements used to render the referenced record |
| `emptyText` | Optional | `string` | '' | Defines a text to be shown when the field has no value or when the reference is missing |
| `label` | Optional | `string | Function` | `resources.[resource].fields.[source]` | Label to use for the field when rendered in layout components |
| `link` | Optional | `string | Function` | `edit` | Target of the link wrapping the rendered child. Set to `false` to disable the link. |
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |

`<ReferenceField>` also accepts the [common field props](./Fields.md#common-field-props).

## `empytText`

`<ReferenceField>` can display a custom message when the referenced record is missing, thanks to the `emptyText` prop.

```jsx
<ReferenceField source="user_id" reference="users" emptyText="Missing user">
<TextField source="name" />
</ReferenceField>
```

`<ReferenceField>` renders the `emptyText`:

- when the referenced record is missing (no record in the `users` table with the right `user_id`), or
- when the field is empty (no `user_id` in the record).

## `label`

By default, `<SimpleShowLayout>`, `<Datagrid>` and other layout components infer the label of a field based on its `source`. For a `<ReferenceField>`, this may not be what you expect:
Expand Down
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