import React from 'react'
import styled from 'styled-components'
import { useTable, usePagination } from 'react-table'
import makeData from './makeData'
const Styles = styled.div`
/* This is required to make the table full-width */
display: block;
max-width: 100%;
/* This will make the table scrollable when it gets too small */
.tableWrap {
display: block;
max-width: 100%;
overflow-x: scroll;
overflow-y: hidden;
border-bottom: 1px solid black;
}
table {
/* Make sure the inner table is always as wide as needed */
width: 100%;
border-spacing: 0;
tr {
:last-child {
td {
border-bottom: 0;
}
}
}
th,
td {
margin: 0;
padding: 0.5rem;
border-bottom: 1px solid black;
border-right: 1px solid black;
/* The secret sauce */
/* Each cell should grow equally */
width: 1%;
/* But "collapsed" cells should be as small as possible */
&.collapse {
width: 0.0000000001%;
}
:last-child {
border-right: 0;
}
}
}
.pagination {
padding: 0.5rem;
}
`
function Table({ columns, data }) {
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page, // Instead of using 'rows', we'll use page,
// which has only the rows for the active page
// The rest of these things are super handy, too ;)
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
state: { pageIndex, pageSize },
} = useTable(
{
columns,
data,
initialState: { pageIndex: 2 },
},
usePagination
)
// Render the UI for your table
return (
{headerGroups.map(headerGroup => (
{/*
Pagination can be built however you'd like.
This is just a very basic UI implementation:
*/}
{headerGroup.headers.map(column => (
))}
{page.map((row, i) => {
prepareRow(row)
return (
{column.render('Header')}
))}
{row.cells.map(cell => {
return (
)
})}
{cell.render('Cell')}
)
})}