Détail du package

reactabular-sticky

reactabular6.2kMIT8.14.0

Sticky header and body for Reactabular

react, reactjs, table, tables

readme

Sometimes you might want to display data within a fixed container. That's where reactabular-sticky comes in. It includes logic keeping a table header and a table body in sync. Unfortunately you still need to dig DOM references yourself to achieve this given it relies on measuring.

API

The API is exactly the same as for reactabular-table apart from naming. Here you need to use Sticky.Header and Sticky.Body over Table.Header and Table.Body.

How to Use?

The following example implements sticky headers within a fixed viewport through props.

/*
import React from 'react';
import { Table } from 'reactabular-table';
// import * as Sticky from 'reactabular-sticky';

import { generateRows } from './helpers';
*/

const schema = {
  type: 'object',
  properties: {
    id: {
      type: 'string'
    },
    name: {
      type: 'string'
    },
    product: {
      type: 'string'
    },
    company: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  },
  required: ['id', 'name', 'product', 'company', 'age']
};
const rows = generateRows(100, schema);

const columns = [
  {
    property: 'name',
    props: {
      style: { minWidth: 300 }
    },
    header: {
      label: 'Name'
    }
  },
  {
    property: 'age',
    props: {
      style: { minWidth: 100 }
    },
    header: {
      label: 'Age'
    }
  },
  {
    property: 'company',
    props: {
      style: { minWidth: 400 }
    },
    header: {
      label: 'Company'
    }
  },
  {
    property: 'product',
    props: {
      style: { minWidth: 400 }
    },
    header: {
      label: 'Product'
    }
  }
];

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

    this.state = {
      rows,
      columns
    };

    this.tableHeader = null;
    this.tableBody = null;
  }
  componentDidMount() {
    // We have refs now. Force update to get those to Header/Body.
    this.forceUpdate();
  }
  render() {
    const { rows, columns } = this.state;

    return (
      <Table.Provider
        className="pure-table pure-table-striped"
        columns={columns}
      >
        <Sticky.Header
          style={{
            maxWidth: 800
          }}
          ref={tableHeader => {
            this.tableHeader = tableHeader && tableHeader.getRef();
          }}
          tableBody={this.tableBody}
        />

        <Sticky.Body
          rows={rows}
          rowKey="id"
          style={{
            maxWidth: 800,
            maxHeight: 400
          }}
          ref={tableBody => {
            this.tableBody = tableBody && tableBody.getRef();
          }}
          tableHeader={this.tableHeader}
        />
      </Table.Provider>
    );
  }
}

<StickyTable />

changelog

8.12.0 / 2017-10-05

  • Chore - Support React 16. #324

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.2.0 / 2016-11-29

  • Feature - Allow Sticky.Header overflow to be overridden through style.

8.1.0 / 2016-11-27

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

3.0.0 / 2016-09-01

  • Bug fix - Make sure scrollOffset gets a value no matter what. This avoid a React warning.

2.0.5 / 2016-08-26

  • Feature - Allow onScroll handler to be defined for Body. Previously it overrode that.

2.0.4 / 2016-08-24

  • Bug fix - Calculate extra padding to table body so that header and body widths match even if a scrollbar is visible.

1.1.2 / 2016-08-04

  • Bug fix - Fix reactabular-table import. Missing * as.

1.1.1 / 2016-08-04

  • Bug fix - Fix reactabular-table import. It points to the correct package now.

1.1.0 / 2016-08-03

  • Feature - Added getRef for getting references to underlying DOM elements.
  • Feature - Moved reactabular-table as a peer dependency as I realized it's better to let the user decide which version of the table to use.

1.0.3 / 2016-07-27

  • Feature - Make tableHeader prop check looser.

0.1.0 / 2016-07-26

  • Initial release.