Package Exports
- react-table-sticky
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-table-sticky) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React Table Sticky
⚠️ Under developpment, still unstable
Sticky components and tools for React Table v7
npm i react-table-sticky
⚠️ react-table@7.x is still in beta. Last verified compatible version is 7.0.0-rc.10
Documentation
- Demo on CodeSandbox
- Features
- Simple example
- API
- Sticky columns for react-table v6
- Browser support
- Contribute
Features
- sticky header
- sticky columns left and/or right
- full customizable
Simple example
Steps:
- create CSS classes
.table.header.body.stickyetc... (check following example) - create HTML elements
<div className="table sticky">,<div className="header">,<div className="body">etc... - add
stickyStylesanduseSticky - then, add
sticky: 'left'orsticky: 'right'to your column
full example:
import React from 'react';
import styled from 'styled-components';
import { useTable, useBlockLayout } from 'react-table';
import { useSticky } from 'react-table-sticky';
const Styles = styled.div`
.table {
border: 1px solid #ddd;
.tr {
:last-child {
.td {
border-bottom: 0;
}
}
}
.th,
.td {
padding: 5px;
border-bottom: 1px solid #ddd;
border-right: 1px solid #ddd;
background-color: #fff;
overflow: hidden;
:last-child {
border-right: 0;
}
}
&.sticky {
overflow: scroll;
.header {
position: sticky;
top: 0;
z-index: 1;
box-shadow: 0px 3px 3px #ccc;
width: fit-content;
}
.body {
position: relative;
z-index: 0;
}
}
}
`;
function TableDemo() {
const columns = [
{
Header: 'First Name',
accessor: 'firstName',
sticky: 'left',
},
{
Header: 'Last Name',
accessor: 'lastName',
sticky: 'left',
},
...
{
Header: 'age',
accessor: 'age',
sticky: 'right',
}
]
const data = [
...
]
const stickyStyles = {
lastLeftTdStyle: {
boxShadow: '2px 0px 3px #ccc',
},
firstRightTdStyle: {
boxShadow: '-2px 0px 3px #ccc',
},
};
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable(
{
columns,
data,
// stickyStyles,
},
useBlockLayout,
useSticky,
);
return (
<Styles>
<div {...getTableProps()} className="table sticky" style={{ width: 1000, height: 500 }}>
<div className="header">
{headerGroups.map((headerGroup) => (
<div {...headerGroup.getHeaderGroupProps()} className="tr">
{headerGroup.headers.map((column) => (
<div {...column.getHeaderProps()} className="th">
{column.render('Header')}
</div>
))}
</div>
))}
</div>
<div {...getTableBodyProps()} className="body">
{rows.map((row) => {
prepareRow(row);
return (
<div {...row.getRowProps()} className="tr">
{row.cells.map((cell) => (
<div {...cell.getCellProps()} className="td">
{cell.render('Cell')}
</div>
))}
</div>
);
})}
</div>
</div>
</Styles>
);
}
Tips: if your table contain at least one header group, place yours sticky columns into a group too (even with an empty Header name)
const columns = [
{
Header: ' ',
sticky: 'left',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
]
},
{
Header: 'Other group',
columns: [
...
]
}
]API
stickyHeaderGroupsstickyRow
Sticky columns for React Table v6
If you search tool for sticky position in React Table v6, take a look at react-table-hoc-fixed-columns
Browser support
Check CSS sticky support https://caniuse.com/#search=sticky
Contribute
git clone https://github.com/guillaumejasmin/react-table-sticky.gitnpm installnpm run demo- Go to http://localhost:8080