Skip to content

Commit 1b14983

Browse files
committed
Merge branch 'main' into alpha
2 parents 3a20f67 + 7548860 commit 1b14983

File tree

5 files changed

+143
-133
lines changed

5 files changed

+143
-133
lines changed

docs/api/features/row-pinning.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Pinning state is stored on the table using the following shape:
1818
export type RowPinningPosition = false | 'top' | 'bottom'
1919

2020
export type RowPinningState = {
21-
top?: boolean
22-
bottom?: boolean
21+
top?: string[]
22+
bottom?: string[]
2323
}
2424

2525
export type RowPinningRowState = {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { HTMLProps } from 'react'
1+
import React from 'react'
22
import ReactDOM from 'react-dom/client'
33

44
import './index.css'

packages/table-core/src/features/RowPinning.ts

+35-31
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export interface RowPinningRow {
7676
}
7777

7878
export interface RowPinningInstance<TData extends RowData> {
79-
_getPinnedRows: (position: 'top' | 'bottom') => Row<TData>[]
79+
_getPinnedRows: (
80+
visiblePinnedRows: Array<Row<TData>>,
81+
pinnedRowIds: Array<string> | undefined,
82+
position: 'top' | 'bottom'
83+
) => Row<TData>[]
8084
/**
8185
* Returns all bottom pinned rows.
8286
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/row-pinning#getbottomrows)
@@ -199,9 +203,9 @@ export const RowPinning: TableFeature = {
199203
const position = row.getIsPinned()
200204
if (!position) return -1
201205

202-
const visiblePinnedRowIds = table
203-
._getPinnedRows(position)
204-
?.map(({ id }) => id)
206+
const visiblePinnedRowIds = (
207+
position === 'top' ? table.getTopRows() : table.getBottomRows()
208+
)?.map(({ id }) => id)
205209

206210
return visiblePinnedRowIds?.indexOf(row.id) ?? -1
207211
}
@@ -226,36 +230,36 @@ export const RowPinning: TableFeature = {
226230
return Boolean(pinningState[position]?.length)
227231
}
228232

229-
table._getPinnedRows = memo(
230-
position => [
231-
table.getRowModel().rows,
232-
table.getState().rowPinning[position!],
233-
position,
234-
],
235-
(visibleRows, pinnedRowIds, position) => {
236-
const rows =
237-
table.options.keepPinnedRows ?? true
238-
? //get all rows that are pinned even if they would not be otherwise visible
239-
//account for expanded parent rows, but not pagination or filtering
240-
(pinnedRowIds ?? []).map(rowId => {
241-
const row = table.getRow(rowId, true)
242-
return row.getIsAllParentsExpanded() ? row : null
243-
})
244-
: //else get only visible rows that are pinned
245-
(pinnedRowIds ?? []).map(
246-
rowId => visibleRows.find(row => row.id === rowId)!
247-
)
233+
table._getPinnedRows = (visibleRows, pinnedRowIds, position) => {
234+
const rows =
235+
table.options.keepPinnedRows ?? true
236+
? //get all rows that are pinned even if they would not be otherwise visible
237+
//account for expanded parent rows, but not pagination or filtering
238+
(pinnedRowIds ?? []).map(rowId => {
239+
const row = table.getRow(rowId, true)
240+
return row.getIsAllParentsExpanded() ? row : null
241+
})
242+
: //else get only visible rows that are pinned
243+
(pinnedRowIds ?? []).map(
244+
rowId => visibleRows.find(row => row.id === rowId)!
245+
)
248246

249-
return rows
250-
.filter(Boolean)
251-
.map(d => ({ ...d, position })) as Row<TData>[]
252-
},
253-
getMemoOptions(table.options, 'debugRows', '_getPinnedRows')
254-
)
247+
return rows.filter(Boolean).map(d => ({ ...d, position })) as Row<TData>[]
248+
}
255249

256-
table.getTopRows = () => table._getPinnedRows('top')
250+
table.getTopRows = memo(
251+
() => [table.getRowModel().rows, table.getState().rowPinning.top],
252+
(allRows, topPinnedRowIds) =>
253+
table._getPinnedRows(allRows, topPinnedRowIds, 'top'),
254+
getMemoOptions(table.options, 'debugRows', 'getTopRows')
255+
)
257256

258-
table.getBottomRows = () => table._getPinnedRows('bottom')
257+
table.getBottomRows = memo(
258+
() => [table.getRowModel().rows, table.getState().rowPinning.bottom],
259+
(allRows, bottomPinnedRowIds) =>
260+
table._getPinnedRows(allRows, bottomPinnedRowIds, 'bottom'),
261+
getMemoOptions(table.options, 'debugRows', 'getBottomRows')
262+
)
259263

260264
table.getCenterRows = memo(
261265
() => [

packages/table-core/tests/RowPinning.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
getCoreRowModel,
77
getPaginationRowModel,
88
} from '../src'
9-
import * as RowPinning from '../src/features/RowPinning'
109
import { makeData, Person } from './makeTestData'
1110

1211
type personKeys = keyof Person
@@ -23,7 +22,7 @@ function generateColumns(people: Person[]): PersonColumn[] {
2322

2423
describe('RowPinning', () => {
2524
describe('_createTable', () => {
26-
describe('_getPinnedRows', () => {
25+
describe('getTopRows', () => {
2726
it('should return pinned rows when keepPinnedRows is true rows are visible', () => {
2827
const data = makeData(10)
2928
const columns = generateColumns(data)
@@ -48,7 +47,7 @@ describe('RowPinning', () => {
4847
getCoreRowModel: getCoreRowModel(),
4948
})
5049

51-
const result = table._getPinnedRows('top')
50+
const result = table.getTopRows()
5251

5352
expect(result.length).toBe(2)
5453
expect(result[0].id).toBe('0')
@@ -78,7 +77,7 @@ describe('RowPinning', () => {
7877
getCoreRowModel: getCoreRowModel(),
7978
})
8079

81-
const result = table._getPinnedRows('top')
80+
const result = table.getTopRows()
8281

8382
expect(result.length).toBe(2)
8483
expect(result[0].id).toBe('0')
@@ -108,7 +107,7 @@ describe('RowPinning', () => {
108107
getCoreRowModel: getCoreRowModel(),
109108
})
110109

111-
const result = table._getPinnedRows('top')
110+
const result = table.getTopRows()
112111

113112
expect(result.length).toBe(2)
114113
expect(result[0].id).toBe('0')
@@ -138,12 +137,11 @@ describe('RowPinning', () => {
138137
getCoreRowModel: getCoreRowModel(),
139138
})
140139

141-
const result = table._getPinnedRows('top')
140+
const result = table.getTopRows()
142141

143142
expect(result.length).toBe(0)
144143
})
145-
})
146-
describe('getTopRows', () => {
144+
147145
it('should return correct top rows', () => {
148146
const data = makeData(10)
149147
const columns = generateColumns(data)
@@ -174,8 +172,6 @@ describe('RowPinning', () => {
174172
expect(result[0].id).toBe('1')
175173
expect(result[1].id).toBe('3')
176174
})
177-
})
178-
describe('getBottomRows', () => {
179175
it('should return correct bottom rows', () => {
180176
const data = makeData(10)
181177
const columns = generateColumns(data)

0 commit comments

Comments
 (0)