Detalhes do pacote

reactabular-table

reactabular35kMIT8.14.0

Table components for Reactabular

react, reactjs, table, tables

readme (leia-me)

Reactabular provides three components: Table.Provider, Table.Header, and Table.Body. Table.Provider sets the data context. It may contain multiple Table.Header and Table.Body elements. You can control data per body while the provider maintains a shared column definition.

Table.Provider

Table.Provider is the core of Reactabular. It sets up a context and maps the column definition to its children components. The following example illustrates the basic idea.

/*
import * as Table from 'reactabular-table';
*/

const rows = [
  {
    id: 100,
    name: 'Adam',
    dad: 'John',
    lovesBeeGees: true
  },
  {
    id: 101,
    name: 'Brian',
    dad: 'George',
    lovesBeeGees: false
  },
];

const columns = [
  {
    property: 'name',
    header: {
      label: 'Name'
    }
  },
  {
    property: 'dad',
    header: {
      label: 'Dad'
    }
  }
];

<Table.Provider
  className="pure-table pure-table-striped"
  columns={columns}
>
  <Table.Header />

  <Table.Body rows={rows} rowKey="id" />
</Table.Provider>

Table.Header

Table.Header renders a table header within a Table.Provider context.

<Table.Provider
  className="pure-table pure-table-striped"
  columns={columns}
>
  <Table.Header />

  <Table.Body rows={rows} rowKey="id"/>

  <Table.Header />
</Table.Provider>

Customizing Table.Header

It is possible to customize a header by passing child components to it. This way you can implement filtering per column for instance.

Here search.Columns injects an additional row for the filter controls. An alternative way to handle it would be to push the problem to the column definition.

<Table.Provider
  className="pure-table pure-table-striped"
  columns={columns}
>
  <Table.Header>
    <search.Columns
      query={{}}
      columns={columns}
      onChange={value => console.log('new value', value)}
    />
  </Table.Header>

  <Table.Body rows={rows} rowKey="id" />
</Table.Provider>

Table.Body

Table.Body renders table rows within a Table.Provider context. It accepts either an array of objects or an array of arrays (see the Excel example). In the former case you should define a rowKey. This allows React to render in a more performant way.

Most often you'll define rowKey as a string. An alternative is to define it using a function like this: rowKey={({ rowData, rowIndex }) => rowData.nested.id}. This is useful if your key is nested or related to some other data. Another way to avoid this problem is to generate the field using reactabular-resolve and then point to that through a string.

Example:

<Table.Provider
  className="pure-table pure-table-striped"
  columns={columns}
>
  <Table.Header />

  <Table.Body rows={rows.filter(r => r.name === 'Adam')} rowKey="id" />

  <Table.Header />

  <Table.Body rows={rows.filter(r => r.name === 'Brian')} rowKey="id" />
</Table.Provider>

Getting Refs

Sometimes you might need to access the underlying DOM nodes for measuring etc. This can be achieved as follows:

class RefTable extends React.Component {
  constructor(props) {
    super(props);

    this.onRow = this.onRow.bind(this);

    this.headerRef = null;
    this.bodyRef = null;
  }
  render() {
    return (
      <Table.Provider columns={columns}>
        <Table.Header
          ref={header => {
            this.headerRef = header && header.getRef();
          }}
        />
        <Table.Body
          ref={body => {
            this.bodyRef = body && body.getRef();
          }}
          rows={rows}
          rowKey="id"
          onRow={this.onRow}
        />
      </Table.Provider>
    );
  }
  onRow(row, { rowIndex, rowKey }) {
    return {
      onClick: () => console.log(this.headerRef, this.bodyRef)
    };
  }
}

<RefTable />

Customizing Table.Header and Table.Body Rows

It is possible to customize body behavior on a row level. onRow prop accepts function (row, { rowIndex, rowKey }) => ({...}) that allows you to set custom attributes per each row.

class CustomTable extends React.Component {
  render() {
    return (
      <Table.Provider
        className="pure-table pure-table-striped"
        columns={columns}
      >
        <Table.Header
          onRow={this.onHeaderRow}
        />

        <Table.Body
          rows={rows}
          rowKey="id"
          onRow={this.onBodyRow}
        />
      </Table.Provider>
    );
  }
  onHeaderRow(row, { rowIndex }) {
    return {
      onClick: () => console.log('clicked header row', row)
    };
  }
  onBodyRow(row, { rowIndex, rowKey }) {
    return {
      onClick: () => console.log('clicked body row', row)
    };
  }
}

<CustomTable />

It's a good idea to define a possible row handler separately to avoid binding per each render. If you write the handler inline, it will bind each time render() is called and reduce performance slightly.

Customizing Table Footer

It is possible to inject a custom footer like this:

<Table.Provider
  className="pure-table pure-table-striped"
  columns={columns}
>
  <Table.Header />

  <Table.Body rows={rows} rowKey="id" />

  <tfoot>
    <tr>
      <td>Show custom rows here</td>
      <td>Show custom rows here</td>
    </tr>
  </tfoot>
</Table.Provider>

See Also

changelog (log de mudanças)

8.14.0 / 2017-05-17

  • Fix - Make components rename work properly in production as well. #348

8.13.0 / 2017-04-19

  • Refactor - Rename components field to renderers. The old prop name will be dropped in the next major version although it works for now.

8.12.0 / 2017-10-05

  • Chore - Support React 16. #324

8.8.0 / 2017-04-03

  • Refactor - Refactor mergePropPair logic. Now Babel should generate easier code.

8.7.2 / 2017-04-03

  • Bug fix - If there are no transforms, skip processing them entirely.

8.6.0 / 2017-01-16

  • Bug fix - Bump lodash peer dependency to v4 minimum. This was expected already, but the version was wrong.

8.5.0 / 2016-12-20

  • Feature - Drop deep-diff dependency. Now it checks through lodash instead. Simpler this way.
  • Bug fix - Allow rowKey to be zero. #262

8.4.2 / 2016-12-15

  • Bug fix - Fix false negative for rowKey check. Now objects with getters should work as expected. #261

8.1.0 / 2016-11-27

  • Feature - Allow overflow to be overridden through style. #246

8.0.0 / 2016-11-27

  • Feature - Pass whole column through header/body for extra parameters.
  • Feature - Support onRow at Table.Header.
  • Feature - Allow Table.Header to accept headerRows (an array of column definitions) to override default columns. See below.
  • Feature - Move utils.resolveRowKey, utils.evaluateFormatters, utils.evaluateTransforms, utils.mergeProps, utils.columnsAreEqual to reactabular-table.
  • Bug fix - Skip functions at BodyRow shouldComponentUpdate.
  • Breaking - Generalize format: <fn> as formatters: [<fn>]. The formatters are applied recursively from left to right: [f1, f2, f3] => f1(f2(f3(value, extra))). This allows composition.
  • Breaking - Extract nested column logic. Now you will have to resolve nested columns before passing them to the table. The advantage of doing this is that now all logic (search/sorting/etc.) works with nested tables. Basic idea:
import * as resolve from 'table-resolver';

...

const NestedColumnsTable = () => {
  const resolvedColumns = resolve.columnChildren({ columns });
  const resolvedRows = resolve.resolve({
    columns: resolvedColumns,
    method: resolve.nested
  })(rows);

  return (
    <Table.Provider columns={resolvedColumns}>
      <Table.Header
        headerRows={resolve.headerRows({ columns })}
      />

      <Table.Body
        rows={resolvedRows}
        rowKey="id"
      />
    </Table.Provider>
  );
};

...

6.0.0 / 2016-10-14

  • Feature - Allow table body and body row shouldComponentUpdate to be overridden.

3.0.6 / 2016-09-12

  • Feature - Allow BodyRow shouldComponentUpdate to be overridden by setting components.body.row.shouldComponentUpdate = true.

3.0.0 / 2016-09-01

  • Breaking - onRow accepts row, { rowIndex, rowKey } instead of row, rowIndex.

2.0.5 / 2016-08-26

  • Feature - Allow Body rowKey to be defined as a function (({ rowData, rowIndex }) => {... return a rowKey ...}). #193

2.0.0 / 2016-08-16

  • Feature - Improve performance by pushing onRow check lower in the component hierarchy.

1.2.3 / 2016-08-08

  • Feature - Make rowKey propType check compatible with React 15.3. It should give you better output during development now.

1.2.0 / 2016-08-05

  • Bug fix - Pass unresolved values to Table.Body transforms instead of resolved ones.

1.1.1 / 2016-08-04

  • Feature - Drop lodash/omit dependency.

1.1.0 / 2016-08-03

  • Feature - Added getRef for getting references to underlying DOM elements.

1.0.0 / 2016-07-25

  • Initial release.