Skip to content

Commit 107973f

Browse files
committed
chore: refactor table and header core more
1 parent 465ad59 commit 107973f

File tree

40 files changed

+343
-282
lines changed

40 files changed

+343
-282
lines changed

docs/guide/custom-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const DensityFeature: TableFeature<any> = { //Use the TableFeature type!!
188188

189189
// define the new feature's default options
190190
_getDefaultOptions: <TData extends RowData>(
191-
table: Table<TData>
191+
table: Partial<Table<TData>>
192192
): DensityOptions => {
193193
return {
194194
enableDensity: true,

examples/react/custom-features/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const DensityFeature: TableFeature<any> = {
7979

8080
// define the new feature's default options
8181
_getDefaultOptions: <TData extends RowData>(
82-
table: Table<TData>
82+
table: Partial<Table<TData>>
8383
): DensityOptions => {
8484
return {
8585
enableDensity: true,

examples/react/row-selection/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { HTMLAttributes, HTMLProps } from 'react'
1+
import React, { HTMLProps } from 'react'
22
import ReactDOM from 'react-dom/client'
33

44
import './index.css'
File renamed without changes.

packages/table-core/src/core/columns/Columns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getMemoOptions, memo } from '../../utils'
22
import { Table, Column, RowData, TableFeature, CellData } from '../../types'
3-
import { _createColumn } from './CreateColumn'
3+
import { _createColumn } from './_createColumn'
44
import {
55
column_getFlatColumns,
66
column_getLeafColumns,

packages/table-core/src/core/columns/Columns.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RowData,
88
Table,
99
} from '../../types'
10-
import { _createColumn } from './CreateColumn'
10+
import { _createColumn } from './_createColumn'
1111

1212
export function column_getFlatColumns<
1313
TData extends RowData,
File renamed without changes.

packages/table-core/src/core/headers/Headers.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RowData, Table, TableFeature } from '../../types'
1+
import { Header, RowData, Table, TableFeature } from '../../types'
22
import { getMemoOptions, memo } from '../../utils'
3-
import { _createHeader } from './CreateHeader'
4-
import { debugHeaders } from './Headers.types'
3+
import { _createHeader } from './_createHeader'
4+
import { Header_Core } from './Headers.types'
55
import {
66
table_getFlatHeaders,
77
table_getFooterGroups,
@@ -20,19 +20,19 @@ export const Headers: TableFeature = {
2020
],
2121
(allColumns, leafColumns, left, right) =>
2222
table_getHeaderGroups(table, allColumns, leafColumns, left, right),
23-
getMemoOptions(table.options, debugHeaders, 'getHeaderGroups')
23+
getMemoOptions(table.options, 'debugHeaders', 'getHeaderGroups')
2424
)
2525

2626
table.getFooterGroups = memo(
2727
() => [table.getHeaderGroups()],
2828
headerGroups => table_getFooterGroups(headerGroups),
29-
getMemoOptions(table.options, debugHeaders, 'getFooterGroups')
29+
getMemoOptions(table.options, 'debugHeaders', 'getFooterGroups')
3030
)
3131

3232
table.getFlatHeaders = memo(
3333
() => [table.getHeaderGroups()],
3434
headerGroups => table_getFlatHeaders(headerGroups),
35-
getMemoOptions(table.options, debugHeaders, 'getFlatHeaders')
35+
getMemoOptions(table.options, 'debugHeaders', 'getFlatHeaders')
3636
)
3737

3838
table.getLeafHeaders = memo(
@@ -42,7 +42,33 @@ export const Headers: TableFeature = {
4242
table.getRightHeaderGroups(),
4343
],
4444
(left, center, right) => table_getLeafHeaders(left, center, right),
45-
getMemoOptions(table.options, debugHeaders, 'getLeafHeaders')
45+
getMemoOptions(table.options, 'debugHeaders', 'getLeafHeaders')
4646
)
4747
},
48+
49+
_createHeader: <TData extends RowData>(
50+
header: Header<TData, unknown>,
51+
table: Table<TData>
52+
): void => {
53+
header.getLeafHeaders = (): Header<TData, unknown>[] => {
54+
const leafHeaders: Header<TData, unknown>[] = []
55+
56+
const recurseHeader = (h: Header_Core<TData, any>) => {
57+
if (h.subHeaders && h.subHeaders.length) {
58+
h.subHeaders.map(recurseHeader)
59+
}
60+
leafHeaders.push(h as Header<TData, unknown>)
61+
}
62+
63+
recurseHeader(header)
64+
65+
return leafHeaders
66+
}
67+
68+
header.getContext = () => ({
69+
column: header.column,
70+
header,
71+
table,
72+
})
73+
},
4874
}

packages/table-core/src/core/headers/Headers.types.ts

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
import { Column, Header, HeaderGroup, RowData, Table } from '../../types'
22

3-
export const debugHeaders = 'debugHeaders'
3+
export interface TableOptions_Headers {
4+
/**
5+
* Set this option to `true` to output header debugging information to the console.
6+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/table#debugheaders)
7+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/tables)
8+
*/
9+
debugHeaders?: boolean
10+
}
411

5-
export interface HeaderGroup_Core<TData extends RowData> {
6-
depth: number
7-
headers: Header<TData, unknown>[]
8-
id: string
12+
export interface Table_Headers<TData extends RowData> {
13+
/**
14+
* Returns all header groups for the table.
15+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getheadergroups)
16+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
17+
*/
18+
getHeaderGroups: () => HeaderGroup<TData>[]
19+
/**
20+
* Returns the footer groups for the table.
21+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getfootergroups)
22+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
23+
*/
24+
getFooterGroups: () => HeaderGroup<TData>[]
25+
/**
26+
* Returns headers for all columns in the table, including parent headers.
27+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getflatheaders)
28+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
29+
*/
30+
getFlatHeaders: () => Header<TData, unknown>[]
31+
/**
32+
* Returns headers for all leaf columns in the table, (not including parent headers).
33+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getleafheaders)
34+
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
35+
*/
36+
getLeafHeaders: () => Header<TData, unknown>[]
937
}
1038

1139
export interface HeaderContext<TData, TValue> {
@@ -23,7 +51,7 @@ export interface HeaderContext<TData, TValue> {
2351
table: Table<TData>
2452
}
2553

26-
export interface Header_Core<TData extends RowData, TValue> {
54+
export interface Header_CoreProperties<TData extends RowData, TValue> {
2755
/**
2856
* The col-span for the header.
2957
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#colspan)
@@ -42,18 +70,6 @@ export interface Header_Core<TData extends RowData, TValue> {
4270
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
4371
*/
4472
depth: number
45-
/**
46-
* Returns the rendering context (or props) for column-based components like headers, footers and filters.
47-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#getcontext)
48-
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
49-
*/
50-
getContext: () => HeaderContext<TData, TValue>
51-
/**
52-
* Returns the leaf headers hierarchically nested under this header.
53-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#getleafheaders)
54-
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
55-
*/
56-
getLeafHeaders: () => Header<TData, unknown>[]
5773
/**
5874
* The header's associated header group object.
5975
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#headergroup)
@@ -98,29 +114,24 @@ export interface Header_Core<TData extends RowData, TValue> {
98114
subHeaders: Header<TData, TValue>[]
99115
}
100116

101-
export interface Table_Headers<TData extends RowData> {
102-
/**
103-
* Returns all header groups for the table.
104-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getheadergroups)
105-
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
106-
*/
107-
getHeaderGroups: () => HeaderGroup<TData>[]
108-
/**
109-
* Returns the footer groups for the table.
110-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getfootergroups)
111-
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
112-
*/
113-
getFooterGroups: () => HeaderGroup<TData>[]
117+
export interface Header_Core<TData extends RowData, TValue>
118+
extends Header_CoreProperties<TData, TValue> {
114119
/**
115-
* Returns headers for all columns in the table, including parent headers.
116-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getflatheaders)
120+
* Returns the rendering context (or props) for column-based components like headers, footers and filters.
121+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#getcontext)
117122
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
118123
*/
119-
getFlatHeaders: () => Header<TData, unknown>[]
124+
getContext: () => HeaderContext<TData, TValue>
120125
/**
121-
* Returns headers for all leaf columns in the table, (not including parent headers).
122-
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/headers#getleafheaders)
126+
* Returns the leaf headers hierarchically nested under this header.
127+
* @link [API Docs](https://tanstack.com/table/v8/docs/api/core/header#getleafheaders)
123128
* @link [Guide](https://tanstack.com/table/v8/docs/guide/headers)
124129
*/
125130
getLeafHeaders: () => Header<TData, unknown>[]
126131
}
132+
133+
export interface HeaderGroup_Core<TData extends RowData> {
134+
depth: number
135+
headers: Header<TData, unknown>[]
136+
id: string
137+
}

packages/table-core/src/core/headers/CreateHeader.ts renamed to packages/table-core/src/core/headers/_createHeader.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Column, Header, RowData, Table } from '../../types'
2-
import { Header_Core } from './Headers.types'
2+
import { Header_CoreProperties } from './Headers.types'
33

44
export function _createHeader<TData extends RowData, TValue>(
55
table: Table<TData>,
@@ -12,38 +12,17 @@ export function _createHeader<TData extends RowData, TValue>(
1212
depth: number
1313
}
1414
): Header<TData, TValue> {
15-
const id = options.id ?? column.id
16-
17-
let header: Header_Core<TData, TValue> = {
18-
id,
15+
const header: Header_CoreProperties<TData, TValue> = {
16+
colSpan: 0,
1917
column,
18+
depth: options.depth,
19+
headerGroup: null!,
20+
id: options.id ?? column.id,
2021
index: options.index,
2122
isPlaceholder: !!options.isPlaceholder,
2223
placeholderId: options.placeholderId,
23-
depth: options.depth,
24-
subHeaders: [],
25-
colSpan: 0,
2624
rowSpan: 0,
27-
headerGroup: null!,
28-
getLeafHeaders: (): Header<TData, unknown>[] => {
29-
const leafHeaders: Header<TData, unknown>[] = []
30-
31-
const recurseHeader = (h: Header_Core<TData, any>) => {
32-
if (h.subHeaders && h.subHeaders.length) {
33-
h.subHeaders.map(recurseHeader)
34-
}
35-
leafHeaders.push(h as Header<TData, unknown>)
36-
}
37-
38-
recurseHeader(header)
39-
40-
return leafHeaders
41-
},
42-
getContext: () => ({
43-
table,
44-
header: header as Header<TData, TValue>,
45-
column,
46-
}),
25+
subHeaders: [],
4726
}
4827

4928
table._features.forEach(feature => {

0 commit comments

Comments
 (0)