3

Some fields I want to only show if they have a value. I would expect to do this like so:

<Show {...props} >
  <SimpleShowLayout>
    { props.record.id ? <TextField source="id" />: null }
  </SimpleShowLayout>
</Show>

But that doesn't work. I can make it somewhat work by making each field a higher order component, but I wanted to do something cleaner. Here's the HOC method I have:

const exists = WrappedComponent => props => props.record[props.source] ?
  <WrappedComponent {...props} />: null;

const ExistsTextField = exists(TextField);

// then in the component:

<Show {...props} >
  <SimpleShowLayout>
    <ExistsTextField source="id" />
  </SimpleShowLayout>
</Show>

This correctly shows the value, but strips the label.

4

2 回答 2

10

我们需要更新我们的文档。同时,您可以在升级指南中找到有关如何实现该目标的信息:https ://github.com/marmelab/react-admin/blob/master/UPGRADE.md#aor-dependent-input-was-removed

这是一个例子:

import { ShowController, ShowView, SimpleShowLayout, TextField } from 'react-admin';

const UserShow = props => (
    <ShowController {...props}>
        {controllerProps => 
            <ShowView {...props} {...controllerProps}>
                <SimpleShowLayout>
                    <TextField source="username" />
                    {controllerProps.record && controllerProps.record.hasEmail && 
                        <TextField source="email" />
                    }
                </SimpleShowLayout>
            </ShowView>
        }
    </ShowController>
);
于 2018-06-27T17:49:27.823 回答
0

也许这种方式会很有用

import { FormDataConsumer } from 'react-admin'

<FormDataConsumer>
    {
      ({ formData, ...rest}) => formData.id &&
        <>
          <ExistsTextField source="id" />
        </>
    }
</FormDataConsumer>
于 2022-02-04T01:29:22.060 回答