你如何有条件地在 react-admin 的“显示”组件中显示字段?

Wil*_*ley 3 conditional react-admin

有些字段我只想显示它们是否有值。我希望这样做:

<Show {...props} >
  <SimpleShowLayout>
    { props.record.id ? <TextField source="id" />: null }
  </SimpleShowLayout>
</Show>
Run Code Online (Sandbox Code Playgroud)

但这不起作用。我可以通过使每个字段成为高阶组件来使其工作,但我想做一些更清洁的事情。这是我拥有的 HOC 方法:

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>
Run Code Online (Sandbox Code Playgroud)

这正确地显示了值,但剥离了标签。

Gil*_*cia 8

我们需要更新我们的文档。同时,您可以在升级指南中找到有关如何实现的信息: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>
);
Run Code Online (Sandbox Code Playgroud)