Skip to content

Add RecordField component#10749

Merged
slax57 merged 9 commits intonextfrom
recordfield
May 27, 2025
Merged

Add RecordField component#10749
slax57 merged 9 commits intonextfrom
recordfield

Conversation

@fzaninotto
Copy link
Copy Markdown
Member

@fzaninotto fzaninotto commented May 16, 2025

Problem

<SimpleShowLayout> uses child inspection to decorate its children with labels.

This isn't robust, as you just need to wrap a child component to break this decoration:

export const BookShow = () => (
  <Show>
    <SimpleShowLayout>
      <TextField source="id" />
      <CanAccess resource="posts.title" action="read">
        <TextField source="title" />
      </CanAccess>
    </SimpleShowLayout>
  </Show>
);

Solution

Invert the responsibilities : it's no longer the layout's role to add a label, but the field's.

Provide a new component to render a field and its label, RecordField:

import { Show, RecordField } from 'react-admin';
import { Stack } from '@mui/material';

export const BookShow = () => (
  <Show>
    <Stack>
      <RecordField source="id" />
      <CanAccess resource="posts.title" action="read">
         <RecordField source="title" />
      </CanAccess>
    </Stack>
  </Show>
);

How To Test

Use the fields/RecordField story

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

@fzaninotto fzaninotto added the WIP Work In Progress label May 16, 2025
Copy link
Copy Markdown
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than that LGTM

<RecordField source="title" empty="resources.books.fields.title.missing" />
```

If you use the `render` prop, you can even use a React element as `empty` value.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes as a surprise to me. Why require a render prop to support a React element? To me, you should be able to use a React element in all cases. You can simply render empty (when it's a React element) instead of the provided children/field/render when the value is empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except TextField and other fields don't use the empty, they use emptyText, and it can only be a string. This may change in the future, but until then we're blocked with allowing React Element in empty only when using the render prop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's no big deal for now, so this is non-blocking. But just for the record, my idea was that RecordField would be responsible for rendering the empty element, not TextField, nor any other component passed as children or field.

fzaninotto and others added 2 commits May 26, 2025 19:46
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
@slax57 slax57 added this to the 5.9.0 milestone May 27, 2025
@slax57 slax57 merged commit 33aef90 into next May 27, 2025
16 checks passed
@slax57 slax57 deleted the recordfield branch May 27, 2025 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RFR Ready For Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants