有些字段我只想显示它们是否有值。我希望这样做:
<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)
这正确地显示了值,但剥离了标签。