UNPKG

2.85 kBMarkdownView Raw
1Sometimes 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.
2
3## API
4
5The 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`.
6
7## How to Use?
8
9The following example implements sticky headers within a fixed viewport through `props`.
10
11```jsx
12/*
13import React from 'react';
14import { Table, Sticky } from 'reactabular';
15// import { Sticky } from 'reactabular-sticky';
16
17import { generateRows } from './helpers';
18*/
19
20const schema = {
21 type: 'object',
22 properties: {
23 id: {
24 type: 'string'
25 },
26 name: {
27 type: 'string'
28 },
29 product: {
30 type: 'string'
31 },
32 company: {
33 type: 'string'
34 },
35 age: {
36 type: 'integer'
37 }
38 },
39 required: ['id', 'name', 'product', 'company', 'age']
40};
41const rows = generateRows(100, schema);
42
43const columns = [
44 {
45 props: {
46 style: { minWidth: 300, width: 300 }
47 },
48 header: {
49 label: 'Name'
50 },
51 cell: {
52 property: 'name'
53 }
54 },
55 {
56 props: {
57 style: { minWidth: 100, width: 100 }
58 },
59 header: {
60 label: 'Age'
61 },
62 cell: {
63 property: 'age'
64 }
65 },
66 {
67 props: {
68 style: { minWidth: 400, width: 400 }
69 },
70 header: {
71 label: 'Company'
72 },
73 cell: {
74 property: 'company'
75 }
76 },
77 {
78 props: {
79 style: { minWidth: 400, width: 400 }
80 },
81 header: {
82 label: 'Product'
83 },
84 cell: {
85 property: 'product'
86 }
87 }
88];
89
90class StickyTable extends React.Component {
91 constructor(props) {
92 super(props);
93
94 this.state = {
95 rows,
96 columns
97 };
98
99 this.tableHeader = null;
100 this.tableBody = null;
101 }
102 componentDidMount() {
103 // We have refs now. Force update to get those to Header/Body.
104 this.forceUpdate();
105 }
106 render() {
107 const { rows, columns } = this.state;
108
109 return (
110 <Table.Provider
111 className="pure-table pure-table-striped"
112 columns={columns}
113 >
114 <Sticky.Header
115 style={{
116 maxWidth: 800
117 }}
118 ref={tableHeader => {
119 this.tableHeader = tableHeader && tableHeader.getRef();
120 }}
121 tableBody={this.tableBody}
122 />
123
124 <Sticky.Body
125 rows={rows}
126 rowKey="id"
127 style={{
128 maxWidth: 800,
129 maxHeight: 400
130 }}
131 ref={tableBody => {
132 this.tableBody = tableBody && tableBody.getRef();
133 }}
134 tableHeader={this.tableHeader}
135 />
136 </Table.Provider>
137 );
138 }
139}
140
141<StickyTable />
142```