forked from TanStack/table
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b70309
commit 0375a7e
Showing
8 changed files
with
331 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import '@testing-library/react/cleanup-after-each' | ||
import '@testing-library/jest-dom/extend-expect' | ||
// NOTE: jest-dom adds handy assertions to Jest and is recommended, but not required | ||
|
||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import useTable from './useTable' | ||
|
||
function Table({ columns, data }) { | ||
// Use the state and functions returned from useTable to build your UI | ||
const { getTableProps, headerGroups, rows, prepareRow } = useTable({ | ||
columns, | ||
data, | ||
}) | ||
|
||
// Render the UI for your table | ||
return ( | ||
<table {...getTableProps()}> | ||
<thead> | ||
{headerGroups.map(headerGroup => ( | ||
<tr {...headerGroup.getHeaderGroupProps()}> | ||
{headerGroup.headers.map(column => ( | ||
<th {...column.getHeaderProps()}>{column.render('Header')}</th> | ||
))} | ||
</tr> | ||
))} | ||
</thead> | ||
<tbody> | ||
{rows.map( | ||
(row, i) => | ||
prepareRow(row) || ( | ||
<tr {...row.getRowProps()}> | ||
{row.cells.map(cell => { | ||
return <td {...cell.getCellProps()}>{cell.render('Cell')}</td> | ||
})} | ||
</tr> | ||
) | ||
)} | ||
</tbody> | ||
</table> | ||
) | ||
} | ||
|
||
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( | ||
() => [ | ||
{ | ||
firstName: 'tanner', | ||
lastName: 'linsley', | ||
age: 29, | ||
visits: 100, | ||
status: 'In Relationship', | ||
progress: 50, | ||
}, | ||
], | ||
[] | ||
) | ||
|
||
return <Table columns={columns} data={data} /> | ||
} | ||
|
||
test('renders a basic table', () => { | ||
const { getByText } = render(<App />) | ||
|
||
expect(getByText('tanner')).toBeInTheDocument() | ||
expect(getByText('linsley')).toBeInTheDocument() | ||
expect(getByText('29')).toBeInTheDocument() | ||
expect(getByText('100')).toBeInTheDocument() | ||
expect(getByText('In Relationship')).toBeInTheDocument() | ||
expect(getByText('50')).toBeInTheDocument() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import '@testing-library/react/cleanup-after-each' | ||
import '@testing-library/jest-dom/extend-expect' | ||
// NOTE: jest-dom adds handy assertions to Jest and is recommended, but not required | ||
|
||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import useTable from '../../hooks/useTable' | ||
import useSortBy from '../useSortBy' | ||
|
||
const data = React.useMemo( | ||
() => [ | ||
{ | ||
firstName: 'tanner', | ||
lastName: 'linsley', | ||
age: 29, | ||
visits: 100, | ||
status: 'In Relationship', | ||
progress: 50, | ||
}, | ||
{ | ||
firstName: 'derek', | ||
lastName: 'perkins', | ||
age: 40, | ||
visits: 40, | ||
status: 'Single', | ||
progress: 80, | ||
}, | ||
{ | ||
firstName: 'joe', | ||
lastName: 'bergevin', | ||
age: 45, | ||
visits: 20, | ||
status: 'Complicated', | ||
progress: 10, | ||
}, | ||
], | ||
[] | ||
) | ||
|
||
const defaultColumn = { | ||
Cell: ({ value, column: { id } }) => `${id}: ${value}`, | ||
} | ||
|
||
function Table({ columns, data }) { | ||
const { getTableProps, headerGroups, rows, prepareRow } = useTable( | ||
{ | ||
columns, | ||
data, | ||
defaultColumn, | ||
}, | ||
useSortBy | ||
) | ||
|
||
return ( | ||
<table {...getTableProps()}> | ||
<thead> | ||
{headerGroups.map(headerGroup => ( | ||
<tr {...headerGroup.getHeaderGroupProps()}> | ||
{headerGroup.headers.map(column => ( | ||
// Add the sorting props to control sorting. For this example | ||
// we can add them into the header props | ||
<th {...column.getHeaderProps(column.getSortByToggleProps())}> | ||
{column.render('Header')} | ||
{/* Add a sort direction indicator */} | ||
<span> | ||
{column.sorted ? (column.sortedDesc ? ' 🔽' : ' 🔼') : ''} | ||
</span> | ||
</th> | ||
))} | ||
</tr> | ||
))} | ||
</thead> | ||
<tbody> | ||
{rows.map( | ||
(row, i) => | ||
prepareRow(row) || ( | ||
<tr {...row.getRowProps()}> | ||
{row.cells.map(cell => { | ||
return <td {...cell.getCellProps()}>{cell.render('Cell')}</td> | ||
})} | ||
</tr> | ||
) | ||
)} | ||
</tbody> | ||
</table> | ||
) | ||
} | ||
|
||
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', | ||
}, | ||
], | ||
}, | ||
], | ||
[] | ||
) | ||
|
||
return <Table columns={columns} data={data} /> | ||
} | ||
|
||
test('renders a sortable table', () => { | ||
const { getAllByText } = render(<App />) | ||
|
||
const firstNames = getAllByText('firstName') | ||
|
||
console.log(firstNames) | ||
|
||
// expect(getByText('tanner')).toBeInTheDocument() | ||
// expect(getByText('linsley')).toBeInTheDocument() | ||
// expect(getByText('29')).toBeInTheDocument() | ||
// expect(getByText('100')).toBeInTheDocument() | ||
// expect(getByText('In Relationship')).toBeInTheDocument() | ||
// expect(getByText('50')).toBeInTheDocument() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.