【问题标题】:Material UI table components decomposition strange behaviorMaterial UI 表格组件分解的奇怪行为
【发布时间】:2016-08-30 13:13:59
【问题描述】:

我使用来自Material UI react components library 的表格。 我需要向 TableRow 添加一些自定义行为,因此我尝试对其进行分解(并且我计划将其包装在我的组件中以添加一些额外的 ui 和逻辑)。

所以,当我尝试分解 table component - 它破坏了标记。如您所见,没有复选框。

See Demo

我的代码:

import React from 'react';
import {Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn} from 'material-ui/Table';

const MyTableRow = () => {
  return <TableRow>
    <TableRowColumn>1</TableRowColumn>
    <TableRowColumn>John Smith</TableRowColumn>
    <TableRowColumn>Employed</TableRowColumn>
  </TableRow>;
}

const TableExampleSimple = () => (
  <Table>
    <TableHeader>
      <TableRow>
        <TableHeaderColumn>ID</TableHeaderColumn>
        <TableHeaderColumn>Name</TableHeaderColumn>
        <TableHeaderColumn>Status</TableHeaderColumn>
      </TableRow>
    </TableHeader>
    <TableBody>

      <MyTableRow />
      <MyTableRow />
      <MyTableRow />

      <TableRow>
        <TableRowColumn>2</TableRowColumn>
        <TableRowColumn>Randal White</TableRowColumn>
        <TableRowColumn>Unemployed</TableRowColumn>
      </TableRow>
      <TableRow>
        <TableRowColumn>4</TableRowColumn>
        <TableRowColumn>Steve Brown</TableRowColumn>
        <TableRowColumn>Employed</TableRowColumn>
      </TableRow>
    </TableBody>
  </Table>
);

export default TableExampleSimple;

这种行为的原因是什么?

更新

有一个假设:TableBody clones children components。 但是我仍然不知道分解 Table 组件并添加一些自定义行为的最佳实践是什么。谁能解释一下怎么做?

【问题讨论】:

    标签: javascript reactjs markup material-ui


    【解决方案1】:

    将表格组件分解为单独的组件(如果这就是您所说的“分解”)应该可以正常工作。

    我已经编辑了您的示例,以便将每一行封装在一个单独的组件中。

    http://www.webpackbin.com/VyxHMxyo-

    或者您是否有硬性要求阻止您将所有行放在单独的组件中?

    【讨论】:

    • 不幸的是,在您的示例中,我看到了同样的问题:没有复选框。如果您添加本机 TableRow 组件,您将看到问题。我想将 table 分解成我自己的组件,每个组件都会包装原生 Table 组件并添加一些附加行为 - 这就是想法。
    【解决方案2】:

    this question 中解决了类似的问题。

    我如何解决我的问题:

    Demo

    const MyTableRow = (props) => {
      return <TableRow {...props}>
        { props.children[0] }
        <TableRowColumn>1</TableRowColumn>
        <TableRowColumn>John Smith</TableRowColumn>
        <TableRowColumn>Employed</TableRowColumn>
      </TableRow>;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      相关资源
      最近更新 更多