mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-23 10:20:27 +00:00
114 lines
2.5 KiB
JavaScript
114 lines
2.5 KiB
JavaScript
import React from 'react'
|
|
|
|
import CssBaseline from '@material-ui/core/CssBaseline'
|
|
import MaUTable from '@material-ui/core/Table'
|
|
import TableBody from '@material-ui/core/TableBody'
|
|
import TableCell from '@material-ui/core/TableCell'
|
|
import TableHead from '@material-ui/core/TableHead'
|
|
import TableRow from '@material-ui/core/TableRow'
|
|
|
|
import { useTable } from 'react-table'
|
|
|
|
import makeData from './makeData'
|
|
|
|
function Table({ columns, data }) {
|
|
// Use the state and functions returned from useTable to build your UI
|
|
const {
|
|
getTableProps,
|
|
getTableHeaderProps,
|
|
headerGroups,
|
|
rows,
|
|
prepareRow,
|
|
} = useTable({
|
|
columns,
|
|
data,
|
|
})
|
|
|
|
// Render the UI for your table
|
|
return (
|
|
<MaUTable {...getTableProps()}>
|
|
<TableHead>
|
|
{headerGroups.map(headerGroup => (
|
|
<TableRow {...headerGroup.getHeaderGroupProps()}>
|
|
{headerGroup.headers.map(column => (
|
|
<TableCell {...column.getHeaderProps()}>
|
|
{column.render('Header')}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))}
|
|
</TableHead>
|
|
<TableBody>
|
|
{rows.map(
|
|
(row, i) => {
|
|
prepareRow(row);
|
|
return (
|
|
<TableRow {...row.getRowProps()}>
|
|
{row.cells.map(cell => {
|
|
return (
|
|
<TableCell {...cell.getCellProps()}>
|
|
{cell.render('Cell')}
|
|
</TableCell>
|
|
)
|
|
})}
|
|
</TableRow>
|
|
)}
|
|
)}
|
|
</TableBody>
|
|
</MaUTable>
|
|
)
|
|
}
|
|
|
|
function App() {
|
|
const columns = React.useMemo(
|
|
() => [
|
|
{
|
|
Header: 'Name',
|
|
columns: [
|
|
{
|
|
Header: 'First Name',
|
|
accessor: 'firstName',
|
|
},
|
|
{
|
|
Header: 'Last Name',
|
|
accessor: 'lastName',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
Header: 'Info',
|
|
columns: [
|
|
{
|
|
Header: 'Age',
|
|
accessor: 'age',
|
|
},
|
|
{
|
|
Header: 'Visits',
|
|
accessor: 'visits',
|
|
},
|
|
{
|
|
Header: 'Status',
|
|
accessor: 'status',
|
|
},
|
|
{
|
|
Header: 'Profile Progress',
|
|
accessor: 'progress',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
[]
|
|
)
|
|
|
|
const data = React.useMemo(() => makeData(20), [])
|
|
|
|
return (
|
|
<div>
|
|
<CssBaseline />
|
|
<Table columns={columns} data={data} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default App
|