react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Show / SimpleShowLayout Does not Render Fields When in a Grid

Open jayfar opened this issue 8 years ago • 9 comments

What you were expecting: When using material UI Grid in an Edit or Create page, the field contents displayed perfect. I expected the Grid to work the same way on a Show page.

What happened instead: When using the same Grid on a Show page, the fields did not display.

Steps to reproduce: Modify the demo/example https://marmelab.com/react-admin/Show.html#the-simpleshowlayout-component to include a Material UI Grid that wraps the fields.

Related code:

import Grid from '@material-ui/core/Grid';

export const PostShowx = (props) => (
    <Show {...props}>
        <SimpleShowLayout>
			<Grid container >
				<Grid item xs={6}>
					<TextField source="title" />
				</Grid>
				<Grid item xs={6}>
					<TextField source="name" />
				</Grid>
			</Grid>
            <RichTextField source="body" />
        </SimpleShowLayout>
    </Show>
);

Even a simple html table seems to not render the TextFields:

export const PostShow = (props) => (
    <Show {...props}>
        <SimpleShowLayout>
			<table>
				<tr>
					<td>
						<TextField source="title" />
					</td>
					<td>			
					       <TextField source="name" />
					</td>
				</tr>
			</table>
            <RichTextField source="body" />
        </SimpleShowLayout>
    </Show>
);

Note: It works just fine on an Edit page:

export const PostEdit = (props) => (
    <Edit title={<PostTitle />} {...props}>
        <SimpleForm>
            <DisabledInput source="id" />
			<Grid container >
				<Grid item xs={6}>
					<ReferenceInput label="User" source="userId" reference="users">
						<SelectInput optionText="name" />
					</ReferenceInput>
				</Grid>
				<Grid item xs={6}>
					<TextInput source="title" />
				</Grid>
			</Grid>
            <LongTextInput source="body" />
        </SimpleForm>
    </Edit>
);

Environment

  • React-admin version: 2.2.0
  • React version: 16.4.1
  • Browser: Tried on both Firefox and Chrome

jayfar avatar Aug 20 '18 14:08 jayfar

It won't work because the SimpleShowLayout expects its children to be fields and the SimpleForm expect its children to be inputs. You'll have to implement your own layout and form components to have a grid

djhi avatar Aug 20 '18 15:08 djhi

This could probably be fixed using the context API. The layout component might provide a context setting the record (and other required props such as basePath and resource) and we could provide a withRecord HOC consuming this context and passing the required props to the fields components.

This would allow for an easier customization path. I event think this should be the default but for backward compatibility sake, we should not make it mandatory in 2.X version. @fzaninotto, thoughts ?

djhi avatar Aug 22 '18 10:08 djhi

In a form, we inject not only the record, but also the input, the basePath, and the resource. In a list, we inject not only the record, but also the basePath and the resource. So the context should provide more than just the record.

I'm not fond of the idea, because it's even more magic than the props injection that we do. Besides, it makes custom components harder to write, as they need to grab the record from a ContextProvider instead of props.

I think @jayfar's need can be achieved in userland using a custom layout. <SimpleShowLayout> only works if you pass fields as children.

fzaninotto avatar Aug 24 '18 13:08 fzaninotto

Besides, it makes custom components harder to write, as they need to grab the record from a ContextProvider instead of props.

We can ease that by providing an HOC ;)

djhi avatar Aug 24 '18 14:08 djhi

Has anyone created such a HOC, that I could see a example of. We are having this issue and some direction would be appreciated.

jessemlay avatar Nov 08 '18 19:11 jessemlay

I'm experiencing the same problem. Would be really cool to have it supported out of the box because in case of many attributes the list will be very long so UX is not so great.

rafaltrojanowski avatar Nov 09 '18 05:11 rafaltrojanowski

Hence the Simple in its name and why we also provide the TabbedShowLayout and TabbedForm components. For other cases, you'll have to implement your own layout/form

djhi avatar Nov 09 '18 07:11 djhi

Faced the same issue trying to use Grid. Simple workaround : use className :tada:

example : <DateField source="profile.birthdate" className="ra-field-50 " /> <TextField source="profile.firstname" className="ra-field-clear ra-field-50" /> <TextField source="profile.lastname" className="ra-field-50" />

.ra-field.ra-field-50{
    width: 50%;
    padding-right: 10px;
    float: left;
  }
.ra-field.ra-field-clear{
    clear:both;
  }

for input fields use formClassName <TextInput source="profile.lastname" formClassName="ra-field-50" />

implement it how you like, add media queries and so on...

heyimbrucelee avatar Feb 01 '19 10:02 heyimbrucelee

i've solved this issue just pass the code as a const and use the record for call the data

const Forma = ({ record }) => (
  <span>
    <Grid container>
      <Grid xs={12} sm={2}>
        postazione: <br/> {record.postazione}
        </Grid>
        <Grid xs={12} sm={2}>
        riga: <br/> {record.riga}
        </Grid>
        <Grid xs={12} sm={2}>
        commessa: <br/> {record.commessa}
      </Grid>
    </Grid>
  </span>
);

and

<SimpleShowLayout>

      <Forma />
    </SimpleShowLayout>

ronco95 avatar Aug 26 '19 13:08 ronco95