Skip to content

Add RecordField component #10749

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

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open

Add RecordField component #10749

wants to merge 6 commits into from

Conversation

fzaninotto
Copy link
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
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

/>
```

The `source`, `field`, `children`, and `render` props are mutually exclusive.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not true. You can have source and field for instance.

Did you mean the following?

Suggested change
The `source`, `field`, `children`, and `render` props are mutually exclusive.
The `field`, `children`, and `render` props are mutually exclusive.

<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
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.

Comment on lines +138 to +147
[`& .${RecordFieldClasses.label}`]: {
fontSize: '0.75em',
marginBottom: '0.2em',
color: (theme.vars || theme).palette.text.secondary,
},
[`&.${RecordFieldClasses.inline} .${RecordFieldClasses.label}`]: {
fontSize: '0.875rem',
display: 'block',
minWidth: 150,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

FMI, why are you using em units with the default variant, but rem units for the inline variant?

Comment on lines +154 to +156
interface ComponentNameToClassKey {
RaRecordField: 'root' | 'content' | 'button' | 'icon';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
interface ComponentNameToClassKey {
RaRecordField: 'root' | 'content' | 'button' | 'icon';
}
interface ComponentNameToClassKey {
RaRecordField: 'root' | 'label' | 'value' | 'inline';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Development

Successfully merging this pull request may close these issues.

3 participants