@tanstack/react-table 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/react-table.development.js +3406 -0
  2. package/dist/react-table.development.js.map +1 -0
  3. package/dist/react-table.production.min.js +2 -0
  4. package/dist/react-table.production.min.js.map +1 -0
  5. package/lib/index.js +65 -0
  6. package/package.json +122 -0
  7. package/src/.DS_Store +0 -0
  8. package/src/aggregationTypes.ts +115 -0
  9. package/src/core.tsx +1194 -0
  10. package/src/createTable.tsx +181 -0
  11. package/src/features/Expanding.ts +388 -0
  12. package/src/features/Filters.ts +707 -0
  13. package/src/features/Grouping.ts +451 -0
  14. package/src/features/Headers.ts +907 -0
  15. package/src/features/Ordering.ts +134 -0
  16. package/src/features/Pinning.ts +213 -0
  17. package/src/features/Sorting.ts +487 -0
  18. package/src/features/Visibility.ts +281 -0
  19. package/src/features/notest/useAbsoluteLayout.test.js +152 -0
  20. package/src/features/notest/useBlockLayout.test.js +158 -0
  21. package/src/features/notest/useColumnOrder.test.js +186 -0
  22. package/src/features/notest/useExpanded.test.js +125 -0
  23. package/src/features/notest/useFilters.test.js +393 -0
  24. package/src/features/notest/useFiltersAndRowSelect.test.js +256 -0
  25. package/src/features/notest/useFlexLayout.test.js +152 -0
  26. package/src/features/notest/useGroupBy.test.js +259 -0
  27. package/src/features/notest/usePagination.test.js +231 -0
  28. package/src/features/notest/useResizeColumns.test.js +229 -0
  29. package/src/features/notest/useRowSelect.test.js +250 -0
  30. package/src/features/notest/useRowState.test.js +178 -0
  31. package/src/features/tests/Visibility.test.tsx +225 -0
  32. package/src/features/tests/__snapshots__/Visibility.test.tsx.snap +390 -0
  33. package/src/features/tests/withSorting.notest.tsx +341 -0
  34. package/src/features/withColumnResizing.ts +281 -0
  35. package/src/features/withPagination.ts +208 -0
  36. package/src/features/withRowSelection.ts +467 -0
  37. package/src/filterTypes.ts +251 -0
  38. package/src/index.tsx +7 -0
  39. package/src/sortTypes.ts +159 -0
  40. package/src/test-utils/makeTestData.ts +41 -0
  41. package/src/tests/__snapshots__/core.test.tsx.snap +148 -0
  42. package/src/tests/core.test.tsx +241 -0
  43. package/src/types.ts +285 -0
  44. package/src/utils/columnFilterRowsFn.ts +162 -0
  45. package/src/utils/expandRowsFn.ts +53 -0
  46. package/src/utils/globalFilterRowsFn.ts +129 -0
  47. package/src/utils/groupRowsFn.ts +196 -0
  48. package/src/utils/sortRowsFn.ts +115 -0
  49. package/src/utils.tsx +243 -0
@@ -0,0 +1,159 @@
1
+ import { Row } from './types'
2
+
3
+ const reSplitAlphaNumeric = /([0-9]+)/gm
4
+
5
+ export const sortTypes = {
6
+ alphanumeric,
7
+ alphanumericCaseSensitive,
8
+ text,
9
+ textCaseSensitive,
10
+ datetime,
11
+ basic,
12
+ }
13
+
14
+ export type BuiltInSortType = keyof typeof sortTypes
15
+
16
+ function alphanumeric<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>(
17
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
18
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
19
+ columnId: string
20
+ ) {
21
+ return compareAlphanumeric(
22
+ toString(rowA.values[columnId]).toLowerCase(),
23
+ toString(rowB.values[columnId]).toLowerCase()
24
+ )
25
+ }
26
+
27
+ function alphanumericCaseSensitive<
28
+ TData,
29
+ TValue,
30
+ TFilterFns,
31
+ TSortingFns,
32
+ TAggregationFns
33
+ >(
34
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
35
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
36
+ columnId: string
37
+ ) {
38
+ return compareAlphanumeric(
39
+ toString(rowA.values[columnId]),
40
+ toString(rowB.values[columnId])
41
+ )
42
+ }
43
+
44
+ // Mixed sorting is slow, but very inclusive of many edge cases.
45
+ // It handles numbers, mixed alphanumeric combinations, and even
46
+ // null, undefined, and Infinity
47
+ function compareAlphanumeric(aStr: string, bStr: string) {
48
+ // Split on number groups, but keep the delimiter
49
+ // Then remove falsey split values
50
+ const a = aStr.split(reSplitAlphaNumeric).filter(Boolean)
51
+ const b = bStr.split(reSplitAlphaNumeric).filter(Boolean)
52
+
53
+ // While
54
+ while (a.length && b.length) {
55
+ const aa = a.shift()!
56
+ const bb = b.shift()!
57
+
58
+ const an = parseInt(aa, 10)
59
+ const bn = parseInt(bb, 10)
60
+
61
+ const combo = [an, bn].sort()
62
+
63
+ // Both are string
64
+ if (isNaN(combo[0]!)) {
65
+ if (aa > bb) {
66
+ return 1
67
+ }
68
+ if (bb > aa) {
69
+ return -1
70
+ }
71
+ continue
72
+ }
73
+
74
+ // One is a string, one is a number
75
+ if (isNaN(combo[1]!)) {
76
+ return isNaN(an) ? -1 : 1
77
+ }
78
+
79
+ // Both are numbers
80
+ if (an > bn) {
81
+ return 1
82
+ }
83
+ if (bn > an) {
84
+ return -1
85
+ }
86
+ }
87
+
88
+ return a.length - b.length
89
+ }
90
+
91
+ // The text filter is more basic (less numeric support)
92
+ // but is much faster
93
+ function text<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>(
94
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
95
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
96
+ columnId: string
97
+ ) {
98
+ return compareBasic(
99
+ toString(rowA.values[columnId]).toLowerCase(),
100
+ toString(rowB.values[columnId]).toLowerCase()
101
+ )
102
+ }
103
+
104
+ // The text filter is more basic (less numeric support)
105
+ // but is much faster
106
+ function textCaseSensitive<
107
+ TData,
108
+ TValue,
109
+ TFilterFns,
110
+ TSortingFns,
111
+ TAggregationFns
112
+ >(
113
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
114
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
115
+ columnId: string
116
+ ) {
117
+ return compareBasic(
118
+ toString(rowA.values[columnId]),
119
+ toString(rowB.values[columnId])
120
+ )
121
+ }
122
+
123
+ function datetime<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>(
124
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
125
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
126
+ columnId: string
127
+ ) {
128
+ return compareBasic(
129
+ (rowA.values[columnId] as Date).getTime(),
130
+ (rowB.values[columnId] as Date).getTime()
131
+ )
132
+ }
133
+
134
+ function basic<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>(
135
+ rowA: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
136
+ rowB: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
137
+ columnId: string
138
+ ) {
139
+ return compareBasic(rowA.values[columnId], rowB.values[columnId])
140
+ }
141
+
142
+ // Utils
143
+
144
+ function compareBasic(a: any, b: any) {
145
+ return a === b ? 0 : a > b ? 1 : -1
146
+ }
147
+
148
+ function toString(a: any) {
149
+ if (typeof a === 'number') {
150
+ if (isNaN(a) || a === Infinity || a === -Infinity) {
151
+ return ''
152
+ }
153
+ return String(a)
154
+ }
155
+ if (typeof a === 'string') {
156
+ return a
157
+ }
158
+ return ''
159
+ }
@@ -0,0 +1,41 @@
1
+ const range = len => {
2
+ const arr = []
3
+ for (let i = 0; i < len; i++) {
4
+ arr.push(i)
5
+ }
6
+ return arr
7
+ }
8
+
9
+ const newPerson = (depth, index, total) => {
10
+ const age = (depth + 1) * (index + 1)
11
+ const visits = age * 10
12
+ const progress = (index + 1) / total
13
+
14
+ return {
15
+ firstName: `${depth} ${index} firstName`,
16
+ lastName: `${depth} ${index} lastName`,
17
+ age,
18
+ visits,
19
+ progress,
20
+ status:
21
+ progress > 0.66
22
+ ? 'relationship'
23
+ : progress > 0.33
24
+ ? 'complicated'
25
+ : 'single',
26
+ }
27
+ }
28
+
29
+ export default function makeTestData(...lens) {
30
+ const makeDataLevel = (depth = 0) => {
31
+ const len = lens[depth]
32
+ return range(len).map(d => {
33
+ return {
34
+ ...newPerson(depth, d, len),
35
+ subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined,
36
+ }
37
+ })
38
+ }
39
+
40
+ return makeDataLevel()
41
+ }
@@ -0,0 +1,148 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`core renders a table with markup 1`] = `
4
+ [
5
+ [
6
+ [
7
+ "Name",
8
+ "2",
9
+ ],
10
+ [
11
+ "Info",
12
+ "4",
13
+ ],
14
+ ],
15
+ [
16
+ [
17
+ "",
18
+ "1",
19
+ ],
20
+ [
21
+ "",
22
+ "1",
23
+ ],
24
+ [
25
+ "",
26
+ "1",
27
+ ],
28
+ [
29
+ "More Info",
30
+ "3",
31
+ ],
32
+ ],
33
+ [
34
+ [
35
+ "firstName",
36
+ "1",
37
+ ],
38
+ [
39
+ "<span>Last Name</span>",
40
+ "1",
41
+ ],
42
+ [
43
+ "Age",
44
+ "1",
45
+ ],
46
+ [
47
+ "<span>Visits</span>",
48
+ "1",
49
+ ],
50
+ [
51
+ "Status",
52
+ "1",
53
+ ],
54
+ [
55
+ "Profile Progress",
56
+ "1",
57
+ ],
58
+ ],
59
+ ]
60
+ `;
61
+
62
+ exports[`core renders a table with markup 2`] = `
63
+ [
64
+ [
65
+ "tanner",
66
+ "linsley",
67
+ "29",
68
+ "100",
69
+ "In Relationship",
70
+ "50",
71
+ ],
72
+ [
73
+ "derek",
74
+ "perkins",
75
+ "40",
76
+ "40",
77
+ "Single",
78
+ "80",
79
+ ],
80
+ [
81
+ "joe",
82
+ "bergevin",
83
+ "45",
84
+ "20",
85
+ "Complicated",
86
+ "10",
87
+ ],
88
+ ]
89
+ `;
90
+
91
+ exports[`core renders a table with markup 3`] = `
92
+ [
93
+ [
94
+ [
95
+ "firstName",
96
+ "1",
97
+ ],
98
+ [
99
+ "lastName",
100
+ "1",
101
+ ],
102
+ [
103
+ "age",
104
+ "1",
105
+ ],
106
+ [
107
+ "visits",
108
+ "1",
109
+ ],
110
+ [
111
+ "status",
112
+ "1",
113
+ ],
114
+ [
115
+ "progress",
116
+ "1",
117
+ ],
118
+ ],
119
+ [
120
+ [
121
+ "",
122
+ "1",
123
+ ],
124
+ [
125
+ "",
126
+ "1",
127
+ ],
128
+ [
129
+ "",
130
+ "1",
131
+ ],
132
+ [
133
+ "More Info",
134
+ "3",
135
+ ],
136
+ ],
137
+ [
138
+ [
139
+ "Name",
140
+ "2",
141
+ ],
142
+ [
143
+ "Info",
144
+ "4",
145
+ ],
146
+ ],
147
+ ]
148
+ `;
@@ -0,0 +1,241 @@
1
+ import * as React from 'react'
2
+
3
+ // import { renderHook } from '@testing-library/react-hooks'
4
+ import * as RTL from '@testing-library/react'
5
+ import { createTable } from 'react-table'
6
+ import { act, renderHook } from '@testing-library/react-hooks'
7
+
8
+ type Row = {
9
+ firstName: string
10
+ lastName: string
11
+ age: number
12
+ visits: number
13
+ status: string
14
+ progress: number
15
+ }
16
+
17
+ const table = createTable().RowType<Row>()
18
+
19
+ const defaultData: Row[] = [
20
+ {
21
+ firstName: 'tanner',
22
+ lastName: 'linsley',
23
+ age: 29,
24
+ visits: 100,
25
+ status: 'In Relationship',
26
+ progress: 50,
27
+ },
28
+ {
29
+ firstName: 'derek',
30
+ lastName: 'perkins',
31
+ age: 40,
32
+ visits: 40,
33
+ status: 'Single',
34
+ progress: 80,
35
+ },
36
+ {
37
+ firstName: 'joe',
38
+ lastName: 'bergevin',
39
+ age: 45,
40
+ visits: 20,
41
+ status: 'Complicated',
42
+ progress: 10,
43
+ },
44
+ ]
45
+
46
+ const defaultColumns = table.createColumns([
47
+ table.createGroup({
48
+ header: 'Name',
49
+ footer: props => props.column.id,
50
+ columns: [
51
+ table.createColumn('firstName', {
52
+ cell: info => info.value,
53
+ footer: props => props.column.id,
54
+ }),
55
+ table.createColumn(row => row.lastName, {
56
+ id: 'lastName',
57
+ cell: info => info.value,
58
+ header: <span>Last Name</span>,
59
+ footer: props => props.column.id,
60
+ }),
61
+ ],
62
+ }),
63
+ table.createGroup({
64
+ header: 'Info',
65
+ footer: props => props.column.id,
66
+ columns: [
67
+ table.createColumn('age', {
68
+ header: () => 'Age',
69
+ footer: props => props.column.id,
70
+ }),
71
+ table.createGroup({
72
+ header: 'More Info',
73
+ columns: [
74
+ table.createColumn('visits', {
75
+ header: () => <span>Visits</span>,
76
+ footer: props => props.column.id,
77
+ }),
78
+ table.createColumn('status', {
79
+ header: 'Status',
80
+ footer: props => props.column.id,
81
+ }),
82
+ table.createColumn('progress', {
83
+ header: 'Profile Progress',
84
+ footer: props => props.column.id,
85
+ }),
86
+ ],
87
+ }),
88
+ ],
89
+ }),
90
+ ])
91
+
92
+ describe('core', () => {
93
+ it('renders a table with markup', () => {
94
+ const Table = () => {
95
+ const [data] = React.useState<Row[]>(() => [...defaultData])
96
+ const [columns] = React.useState<typeof defaultColumns>(() => [
97
+ ...defaultColumns,
98
+ ])
99
+ const [columnVisibility, setColumnVisibility] = React.useState({})
100
+
101
+ const rerender = React.useReducer(() => ({}), {})[1]
102
+
103
+ const instance = table.useTable({
104
+ data,
105
+ columns,
106
+ onColumnVisibilityChange: setColumnVisibility,
107
+ state: {
108
+ columnVisibility,
109
+ },
110
+ // debug: true,
111
+ })
112
+
113
+ return (
114
+ <div className="p-2">
115
+ <table {...instance.getTableProps()}>
116
+ <thead>
117
+ {instance.getHeaderGroups().map(headerGroup => (
118
+ <tr {...headerGroup.getHeaderGroupProps()}>
119
+ {headerGroup.headers.map(header => (
120
+ <th {...header.getHeaderProps()}>
121
+ {header.isPlaceholder ? null : header.renderHeader()}
122
+ </th>
123
+ ))}
124
+ </tr>
125
+ ))}
126
+ </thead>
127
+ <tbody {...instance.getTableBodyProps()}>
128
+ {instance.getRows().map(row => (
129
+ <tr {...row.getRowProps()}>
130
+ {row.getVisibleCells().map(cell => (
131
+ <td {...cell.getCellProps()}>{cell.renderCell()}</td>
132
+ ))}
133
+ </tr>
134
+ ))}
135
+ </tbody>
136
+ <tfoot>
137
+ {instance.getFooterGroups().map(footerGroup => (
138
+ <tr {...footerGroup.getFooterGroupProps()}>
139
+ {footerGroup.headers.map(header => (
140
+ <th {...header.getFooterProps()}>
141
+ {header.isPlaceholder ? null : header.renderFooter()}
142
+ </th>
143
+ ))}
144
+ </tr>
145
+ ))}
146
+ </tfoot>
147
+ </table>
148
+ <div className="h-4" />
149
+ <button onClick={() => rerender()} className="border p-2">
150
+ Rerender
151
+ </button>
152
+ </div>
153
+ )
154
+ }
155
+
156
+ // let api: any;
157
+
158
+ const rendered = RTL.render(<Table />)
159
+
160
+ // RTL.screen.logTestingPlaygroundURL();
161
+
162
+ RTL.screen.getByRole('table')
163
+ expect(RTL.screen.getAllByRole('rowgroup').length).toEqual(3)
164
+ expect(RTL.screen.getAllByRole('row').length).toEqual(9)
165
+ expect(RTL.screen.getAllByRole('columnheader').length).toEqual(12)
166
+ expect(RTL.screen.getAllByRole('columnfooter').length).toEqual(12)
167
+ expect(RTL.screen.getAllByRole('gridcell').length).toEqual(18)
168
+
169
+ expect(
170
+ Array.from(rendered.container.querySelectorAll('thead > tr')).map(d =>
171
+ Array.from(d.querySelectorAll('th')).map(d => [
172
+ d.innerHTML,
173
+ d.getAttribute('colspan'),
174
+ ])
175
+ )
176
+ ).toMatchSnapshot()
177
+
178
+ expect(
179
+ Array.from(rendered.container.querySelectorAll('tbody > tr')).map(d =>
180
+ Array.from(d.querySelectorAll('td')).map(d => d.innerHTML)
181
+ )
182
+ ).toMatchSnapshot()
183
+
184
+ expect(
185
+ Array.from(rendered.container.querySelectorAll('tfoot > tr')).map(d =>
186
+ Array.from(d.querySelectorAll('th')).map(d => [
187
+ d.innerHTML,
188
+ d.getAttribute('colspan'),
189
+ ])
190
+ )
191
+ ).toMatchSnapshot()
192
+ })
193
+
194
+ it('has a stable api', () => {
195
+ const { result } = renderHook(() => {
196
+ const rerender = React.useReducer(() => ({}), {})[1]
197
+
198
+ const instance = table.useTable({
199
+ data: defaultData,
200
+ columns: defaultColumns,
201
+ })
202
+
203
+ return {
204
+ instance,
205
+ rerender,
206
+ }
207
+ })
208
+
209
+ const prev = result.current
210
+
211
+ act(() => {
212
+ result.current.rerender()
213
+ })
214
+
215
+ const next = result.current
216
+
217
+ expect(prev).toStrictEqual(next)
218
+ })
219
+
220
+ it('can return the rowModel', () => {
221
+ const { result } = renderHook(() => {
222
+ const rerender = React.useReducer(() => ({}), {})[1]
223
+
224
+ const instance = table.useTable({
225
+ data: defaultData,
226
+ columns: defaultColumns,
227
+ })
228
+
229
+ return {
230
+ instance,
231
+ rerender,
232
+ }
233
+ })
234
+
235
+ const rowModel = result.current.instance.getRowModel()
236
+
237
+ expect(rowModel.rows.length).toEqual(3)
238
+ expect(rowModel.flatRows.length).toEqual(3)
239
+ expect(rowModel.rowsById['2']?.original).toEqual(defaultData[2])
240
+ })
241
+ })