【问题标题】:Create a custom Show Layout component创建自定义显示布局组件
【发布时间】:2020-09-23 18:39:49
【问题描述】:

我正在尝试创建一个自定义布局组件,以便更好地设计我的Show 页面。

我不确定为什么有些东西有效而有些无效。我的计划是使用 Material-UI <Grid> 组件。我知道Grid 组件没有通过道具,那么我将扩展到我自己的:

export default function OrderShow(props) {
    return (
        <Show {...props}>
            <CustomGrid>
                <TextField source="shortId" label="Short ID" />
            </CustomGrid>
        </Show>
    );
}

我的CustomGrid 组件还只是一个草稿,正在克隆它的子组件以传递道具:

function CustomGrid(props) {
    return React.Children.map(props.children, (child) => React.cloneElement(child, props));
}

当我使用它时,TextField 接收到source,并正确呈现它。然而label 不见了。我不确定为什么会这样。

另一个问题是,当我尝试将CustomGridReferenceField 一起使用时,它根本不起作用:

<Show {...props}>
  <CustomGrid>
    <ReferenceField source="user" reference="users" label="Name" link="show">
      <FunctionField render={(record) => `${record.firstName} ${record.lastName}`} />
    </ReferenceField>
  </CustomGrid>
</Show>

可以看到 props 一直传递到ReferenceField,但在到达FunctionField 之前就丢失了:

有没有更好的方法来自定义SimpleShowLayout 并继续使用TextFieldreact-admin 中的其他组件?

【问题讨论】:

标签: javascript reactjs material-ui react-admin


【解决方案1】:

是的,有办法。简单地说,react-adminSimpleShowLayout 比将道具传递给子字段要复杂一些。这就是为什么你必须递归迭代直到你到达一个字段并执行 react-admin 所做的事情。

最后,我决定在我正在构建的新包中分享一些私人开发的组件。我添加了适用于BoxGrid material-ui 的组件的布局。你可以看这里:ra-compact-ui

【讨论】:

  • 不错!我使用 useShowController 钩子解决了我的问题并传递了组件所需的道具
猜你喜欢
  • 2018-01-23
  • 2012-10-28
  • 1970-01-01
  • 2016-10-12
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多