generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(BaseTable): calc aria-rowindex fixed
- Loading branch information
Showing
6 changed files
with
93 additions
and
4 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
54 changes: 54 additions & 0 deletions
54
src/components/BaseTable/__stories__/stories/GroupingWithVirtualizationStory.tsx
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,54 @@ | ||
import React from 'react'; | ||
|
||
import type {ExpandedState, Row} from '@tanstack/react-table'; | ||
|
||
import {useTable, useWindowRowVirtualizer} from '../../../../hooks'; | ||
import {BaseTable} from '../../BaseTable'; | ||
import {columns} from '../constants/columns'; | ||
import type {Item} from '../types'; | ||
import {generateData} from '../utils'; | ||
|
||
import {cnVirtualizationStory} from './VirtualizationStory.classname'; | ||
|
||
const data = generateData(300); | ||
const grouping: Array<keyof Item> = ['status', 'age']; | ||
|
||
import './VirtualizationStory.scss'; | ||
|
||
const getGroupTitle = (row: Row<Item>) => row.getValue<string>('name'); | ||
const getIsGroupHeaderRow = (row: Row<Item>) => row.getIsGrouped(); | ||
|
||
export const GroupingWithVirtualizationStory = () => { | ||
const [expanded, setExpanded] = React.useState<ExpandedState>({}); | ||
|
||
const table = useTable({ | ||
columns, | ||
data, | ||
getRowId: (item) => item.id, | ||
enableExpanding: true, | ||
enableGrouping: true, | ||
onExpandedChange: setExpanded, | ||
state: { | ||
expanded, | ||
grouping, | ||
}, | ||
}); | ||
|
||
const rowVirtualizer = useWindowRowVirtualizer({ | ||
count: table.getRowModel().rows.length, | ||
estimateSize: () => 20, | ||
overscan: 5, | ||
}); | ||
|
||
return ( | ||
<BaseTable | ||
table={table} | ||
rowVirtualizer={rowVirtualizer} | ||
getGroupTitle={getGroupTitle} | ||
getIsGroupHeaderRow={getIsGroupHeaderRow} | ||
className={cnVirtualizationStory()} | ||
headerClassName={cnVirtualizationStory('header')} | ||
stickyHeader | ||
/> | ||
); | ||
}; |
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,20 @@ | ||
import type {Row} from '@tanstack/react-table'; | ||
|
||
export const getAriaRowIndexMap = <TData>(rows: Row<TData>[]) => { | ||
let rowIndex = 1; | ||
|
||
return rows.reduce<Record<string, number>>((acc, row, index, arr) => { | ||
const newMap = { | ||
...acc, | ||
[row.id]: rowIndex, | ||
}; | ||
|
||
const nextRow = arr[index + 1]; | ||
if (nextRow?.parentId !== row.id) { | ||
rowIndex += row.getLeafRows().length; | ||
} | ||
rowIndex++; | ||
|
||
return newMap; | ||
}, {}); | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type {HeaderGroup} from '@tanstack/react-table'; | ||
|
||
export const shouldRenderFooterRow = <TData>(footerGroup: HeaderGroup<TData>) => { | ||
return footerGroup.headers.every((header) => !header.column.columnDef.footer); | ||
return footerGroup.headers.some((header) => header.column.columnDef.footer); | ||
}; |