@economic/taco 2.26.19 → 2.26.20
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/esm/packages/taco/src/primitives/Table/Core/features/useTableRenderer.js +10 -3
- package/dist/esm/packages/taco/src/primitives/Table/Core/features/useTableRenderer.js.map +1 -1
- package/dist/esm/packages/taco/src/primitives/Table/Core/useTable.js.map +1 -1
- package/dist/primitives/Table/Core/features/useTableRenderer.d.ts +1 -1
- package/dist/primitives/Table/Core/useTable.d.ts +1 -1
- package/dist/taco.cjs.development.js +10 -3
- package/dist/taco.cjs.development.js.map +1 -1
- package/dist/taco.cjs.production.min.js +1 -1
- package/dist/taco.cjs.production.min.js.map +1 -1
- package/package.json +2 -2
@@ -6,10 +6,11 @@ import { Row } from '../components/Row/Row.js';
|
|
6
6
|
const TABLE_SCROLL_PADDING_BOTTOM = 36;
|
7
7
|
const TABLE_PADDING_BOTTOM = 120;
|
8
8
|
function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
9
|
-
var _table$getState$group, _virtualItems
|
9
|
+
var _table$getState$group, _ref, _virtualItems$padding, _virtualItems$padding2, _ref2, _virtualItems;
|
10
10
|
const tableMeta = table.options.meta;
|
11
11
|
const rows = table.getRowModel().rows;
|
12
12
|
const HEADER_OFFSET_TOTAL = HEADER_ROW_HEIGHT_ESTIMATE * table.getHeaderGroups().length;
|
13
|
+
const isTableGrouped = !!((_table$getState$group = table.getState().grouping) !== null && _table$getState$group !== void 0 && _table$getState$group.length);
|
13
14
|
// expanded rows
|
14
15
|
const {
|
15
16
|
createRowMeasurer,
|
@@ -23,7 +24,7 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
23
24
|
getScrollElement: () => tableRef.current,
|
24
25
|
overscan: tableMeta.printing.isPrinting ? rows.length : undefined,
|
25
26
|
rangeExtractor,
|
26
|
-
scrollPaddingStart:
|
27
|
+
scrollPaddingStart: isTableGrouped ? HEADER_ROW_HEIGHT_ESTIMATE : undefined,
|
27
28
|
scrollPaddingEnd: HEADER_OFFSET_TOTAL + TABLE_SCROLL_PADDING_BOTTOM,
|
28
29
|
paddingEnd: TABLE_PADDING_BOTTOM
|
29
30
|
});
|
@@ -56,10 +57,12 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
56
57
|
}
|
57
58
|
}
|
58
59
|
}, [virtualItems.length, tableRef.current]);
|
60
|
+
// use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0
|
61
|
+
const paddingStartIndex = isTableGrouped && rows.length > 1 ? 1 : 0;
|
59
62
|
// styling for offsetting rows - this "is" the virtualisation
|
60
63
|
const [paddingTop, paddingBottom] = virtualItems.length > 0 ? [
|
61
64
|
// use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0
|
62
|
-
Math.max(0, ((_virtualItems$ = virtualItems[
|
65
|
+
Math.max(0, (_ref = ((_virtualItems$padding = virtualItems[paddingStartIndex]) === null || _virtualItems$padding === void 0 ? void 0 : _virtualItems$padding.start) - virtualiser.options.scrollMargin - ((_virtualItems$padding2 = virtualItems[paddingStartIndex]) === null || _virtualItems$padding2 === void 0 ? void 0 : _virtualItems$padding2.size)) !== null && _ref !== void 0 ? _ref : 0), Math.max(0, (_ref2 = virtualiser.getTotalSize() - ((_virtualItems = virtualItems[virtualItems.length - 1]) === null || _virtualItems === void 0 ? void 0 : _virtualItems.end)) !== null && _ref2 !== void 0 ? _ref2 : 0)] : [0, 0];
|
63
66
|
// ensure default active rows are scrolled to
|
64
67
|
React__default.useEffect(() => {
|
65
68
|
if (defaultRowActiveIndex) {
|
@@ -79,6 +82,10 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
79
82
|
paddingTop
|
80
83
|
};
|
81
84
|
content = virtualItems.map(virtualRow => {
|
85
|
+
// there appears to be a react-virtual bug where it inserts a single `undefined` item at the end of the row, which crashes here
|
86
|
+
if (!virtualRow) {
|
87
|
+
return null;
|
88
|
+
}
|
82
89
|
const row = rows[virtualRow.index];
|
83
90
|
const measureRow = createRowMeasurer(virtualiser.resizeItem, virtualRow);
|
84
91
|
return /*#__PURE__*/React__default.createElement(Row, {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useTableRenderer.js","sources":["../../../../../../../../../src/primitives/Table/Core/features/useTableRenderer.tsx"],"sourcesContent":["import React, { CSSProperties } from 'react';\nimport { Table as ReactTable, TableMeta as ReactTableMeta } from '@tanstack/react-table';\nimport {\n useVirtualizer,\n defaultRangeExtractor,\n ScrollToOptions as ReactVirtualScrollToOptions,\n VirtualItem,\n} from '@tanstack/react-virtual';\nimport { HEADER_ROW_HEIGHT_ESTIMATE, ROW_HEIGHT_ESTIMATES } from '../util/rows';\nimport { Row } from '../components/Row/Row';\nimport { useTableRenderers } from '../types';\nimport { TableRef } from '../../types';\n\nconst TABLE_SCROLL_PADDING_BOTTOM = 36;\nconst TABLE_PADDING_BOTTOM = 120;\n\nexport function useTableRenderer<TType = unknown>(\n renderers: useTableRenderers<TType>,\n table: ReactTable<TType>,\n tableRef: React.RefObject<TableRef>,\n defaultRowActiveIndex?: number | undefined\n) {\n const tableMeta = table.options.meta as ReactTableMeta<TType>;\n const rows = table.getRowModel().rows;\n const HEADER_OFFSET_TOTAL = HEADER_ROW_HEIGHT_ESTIMATE * table.getHeaderGroups().length;\n\n // expanded rows\n const { createRowMeasurer, estimateSize } = useRowHeightVirtualisation<TType>(table);\n\n // row groups\n const rangeExtractor = useRowGroupVirtualisation<TType>(table);\n\n const virtualiser = useVirtualizer({\n count: rows.length,\n estimateSize,\n getScrollElement: () => tableRef.current,\n overscan: tableMeta.printing.isPrinting ? rows.length : undefined,\n rangeExtractor,\n scrollPaddingStart: table.getState().grouping?.length ? HEADER_ROW_HEIGHT_ESTIMATE : undefined,\n scrollPaddingEnd: HEADER_OFFSET_TOTAL + TABLE_SCROLL_PADDING_BOTTOM,\n paddingEnd: TABLE_PADDING_BOTTOM,\n });\n\n const virtualItems = virtualiser.getVirtualItems();\n\n const scrollToIndex = React.useCallback(\n (index: number, options: ReactVirtualScrollToOptions = { align: 'auto', behavior: 'smooth' }) => {\n const notSmooth: ReactVirtualScrollToOptions = { ...options, behavior: 'auto' };\n\n if (tableRef.current) {\n if (index === 0) {\n virtualiser.scrollToOffset(0, notSmooth);\n } else if (index >= virtualItems.length - 1) {\n // scrolling to the last row isn't easy when rows have dynamic height\n // because you don't know all the heights until they render...\n // so when you skip to the last you don't know if \"the end\" is actually the end\n virtualiser.scrollToIndex(index, notSmooth);\n\n // https://github.com/TanStack/virtual/issues/216\n setTimeout(() => {\n virtualiser.scrollToIndex(index, notSmooth);\n\n setTimeout(() => {\n virtualiser.scrollToIndex(index, notSmooth);\n }, 150);\n }, 150);\n } else {\n virtualiser.scrollToIndex(index, options);\n }\n }\n },\n [virtualItems.length, tableRef.current]\n );\n\n // styling for offsetting rows - this \"is\" the virtualisation\n const [paddingTop, paddingBottom] =\n virtualItems.length > 0\n ? [\n // use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0\n Math.max(\n 0,\n (virtualItems[1] ?? virtualItems[0]).start - virtualiser.options.scrollMargin - virtualItems[0].size\n ),\n Math.max(0, virtualiser.getTotalSize() - virtualItems[virtualItems.length - 1].end),\n ]\n : [0, 0];\n\n // ensure default active rows are scrolled to\n React.useEffect(() => {\n if (defaultRowActiveIndex) {\n scrollToIndex(defaultRowActiveIndex, { align: 'center', behavior: 'auto' });\n }\n }, []);\n\n // rendered output\n let style: CSSProperties = {};\n let content: JSX.Element[] | null = null;\n\n if (rows.length) {\n style = {\n height: `${virtualiser.getTotalSize()}px`,\n paddingBottom,\n paddingTop,\n };\n content = virtualItems.map(virtualRow => {\n const row = rows[virtualRow.index];\n const measureRow = createRowMeasurer(virtualiser.resizeItem, virtualRow);\n return (\n <Row\n key={row.id}\n row={row}\n index={virtualRow.index}\n table={table}\n measureRow={measureRow}\n renderer={renderers.row}\n cellRenderer={renderers.cell}\n />\n );\n });\n }\n\n return {\n rows: content,\n style,\n scrollToIndex,\n };\n}\n\n// support varying row heights - variable row heights, row expansion\nfunction useRowHeightVirtualisation<TType = unknown>(table: ReactTable<TType>) {\n const tableMeta = table.options.meta as ReactTableMeta<TType>;\n const rows = table.getRowModel().rows;\n const expandedState = table.getState().expanded;\n\n const [expandedRowHeightsCache, setExpandedRowHeightsCache] = React.useState<Record<number, number>>({});\n\n // look at all registered expanded row heights and use them to form a more accurate average size\n // it might be worth considering if taking the max, or an upper bound, would be better\n const averageExpandedRowHeight = React.useMemo(() => {\n const values: number[] = Object.values(expandedRowHeightsCache);\n return values.length ? values.reduce((a, b) => a + b, 0) / values.length : 0;\n }, [expandedRowHeightsCache]);\n\n // row heights\n const estimateSize = React.useCallback(\n (index: number) => {\n const rowHeight = tableMeta.rowHeight.height\n ? ROW_HEIGHT_ESTIMATES[tableMeta.rowHeight.height]\n : ROW_HEIGHT_ESTIMATES.medium;\n\n if (expandedState === true || rows[index]?.getIsExpanded?.()) {\n return rowHeight + averageExpandedRowHeight;\n }\n\n return rowHeight;\n },\n [tableMeta.rowHeight.height, averageExpandedRowHeight, expandedState]\n );\n\n const createRowMeasurer = React.useCallback(\n (resizeItem: (item: VirtualItem, size: number) => void, virtualRow: VirtualItem) =>\n (rowHeight: number, expansionHeight?: number) => {\n // register the expansion height so that it can be used to determine an average\n if (expansionHeight) {\n setExpandedRowHeightsCache(cache => ({ ...cache, [virtualRow.index]: expansionHeight }));\n }\n\n // update the whole row's size to include the expanded height\n resizeItem(virtualRow, rowHeight + (expansionHeight ?? 0));\n },\n []\n );\n\n return { estimateSize, createRowMeasurer };\n}\n\n// support virtualised row groups (where the row group headers are sticky)\nfunction useRowGroupVirtualisation<TType = unknown>(table: ReactTable<TType>) {\n const rows = table.getRowModel().rows;\n const isTableGrouped = !!table.getState().grouping.length;\n\n const rowGroupIndexes = React.useMemo(() => {\n const indexes: number[] = [];\n\n if (isTableGrouped) {\n rows.forEach((row, index) => {\n if (row.getIsGrouped()) {\n indexes.push(index);\n }\n });\n }\n\n return indexes;\n }, []);\n\n // this is taken from the react-virtual docs/examples\n const rangeExtractor = React.useCallback(\n range => {\n const activeRowGroupIndex =\n [...rowGroupIndexes].reverse().find(index => range.startIndex >= index) ?? rowGroupIndexes[0];\n\n const next = new Set([activeRowGroupIndex, ...defaultRangeExtractor(range)]);\n return [...next].sort((a, b) => a - b);\n },\n [rowGroupIndexes]\n );\n\n return isTableGrouped ? rangeExtractor : undefined;\n}\n"],"names":["TABLE_SCROLL_PADDING_BOTTOM","TABLE_PADDING_BOTTOM","useTableRenderer","renderers","table","tableRef","defaultRowActiveIndex","tableMeta","options","meta","rows","getRowModel","HEADER_OFFSET_TOTAL","HEADER_ROW_HEIGHT_ESTIMATE","getHeaderGroups","length","createRowMeasurer","estimateSize","useRowHeightVirtualisation","rangeExtractor","useRowGroupVirtualisation","virtualiser","useVirtualizer","count","getScrollElement","current","overscan","printing","isPrinting","undefined","scrollPaddingStart","_table$getState$group","getState","grouping","scrollPaddingEnd","paddingEnd","virtualItems","getVirtualItems","scrollToIndex","React","useCallback","index","align","behavior","notSmooth","scrollToOffset","setTimeout","paddingTop","paddingBottom","Math","max","_virtualItems$","start","scrollMargin","size","getTotalSize","end","useEffect","style","content","height","map","virtualRow","row","measureRow","resizeItem","Row","key","id","renderer","cellRenderer","cell","expandedState","expanded","expandedRowHeightsCache","setExpandedRowHeightsCache","useState","averageExpandedRowHeight","useMemo","values","Object","reduce","a","b","rowHeight","ROW_HEIGHT_ESTIMATES","medium","_rows$index","_rows$index$getIsExpa","getIsExpanded","call","expansionHeight","cache","isTableGrouped","rowGroupIndexes","indexes","forEach","getIsGrouped","push","range","activeRowGroupIndex","_reverse$find","reverse","find","startIndex","next","Set","defaultRangeExtractor","sort"],"mappings":";;;;;AAaA,MAAMA,2BAA2B,GAAG,EAAE;AACtC,MAAMC,oBAAoB,GAAG,GAAG;SAEhBC,gBAAgBA,CAC5BC,SAAmC,EACnCC,KAAwB,EACxBC,QAAmC,EACnCC,qBAA0C;;EAE1C,MAAMC,SAAS,GAA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K,CAACO,WAAW,EAAE,CAACD,IAAI;EACrC,MAAMmF,cAAc,GAAG,CAAC,CAACzF,KAAK,CAAC4B,QAAQ,EAAE,CAACC,QAAQ,CAAClB,MAAM;EAEzD,MAAM+E,eAAe,GAAGvD,cAAK,CAACuC,OAAO,CAAC;IAClC,MAAMiB,OAAO,GAAa,EAAE;IAE5B,IAAIF,cAAc,EAAE;MAChBnF,IAAI,CAACsF,OAAO,CAAC,CAACjC,GAAG,EAAEtB,KAAK;QACpB,IAAIsB,GAAG,CAACkC,YAAY,EAAE,EAAE;UACpBF,OAAO,CAACG,IAAI,CAACzD,KAAK,CAAC;;OAE1B,CAAC;;IAGN,OAAOsD,OAAO;GACjB,EAAE,EAAE,CAAC;;EAGN,MAAM5E,cAAc,GAAGoB,cAAK,CAACC,WAAW,CACpC2D,KAAK;;IACD,MAAMC,mBAAmB,IAAAC,aAAA,GACrB,CAAC,GAAGP,eAAe,CAAC,CAACQ,OAAO,EAAE,CAACC,IAAI,CAAC9D,KAAK,IAAI0D,KAAK,CAACK,UAAU,IAAI/D,KAAK,CAAC,cAAA4D,aAAA,cAAAA,aAAA,GAAIP,eAAe,CAAC,CAAC,CAAC;IAEjG,MAAMW,IAAI,GAAG,IAAIC,GAAG,CAAC,CAACN,mBAAmB,EAAE,GAAGO,qBAAqB,CAACR,KAAK,CAAC,CAAC,CAAC;IAC5E,OAAO,CAAC,GAAGM,IAAI,CAAC,CAACG,IAAI,CAAC,CAAC1B,CAAC,EAAEC,CAAC,KAAKD,CAAC,GAAGC,CAAC,CAAC;GACzC,EACD,CAACW,eAAe,CAAC,CACpB;EAED,OAAOD,cAAc,GAAG1E,cAAc,GAAGU,SAAS;AACtD;;;;"}
|
1
|
+
{"version":3,"file":"useTableRenderer.js","sources":["../../../../../../../../../src/primitives/Table/Core/features/useTableRenderer.tsx"],"sourcesContent":["import React, { CSSProperties } from 'react';\nimport { Table as ReactTable, TableMeta as ReactTableMeta } from '@tanstack/react-table';\nimport {\n useVirtualizer,\n defaultRangeExtractor,\n ScrollToOptions as ReactVirtualScrollToOptions,\n VirtualItem,\n} from '@tanstack/react-virtual';\nimport { HEADER_ROW_HEIGHT_ESTIMATE, ROW_HEIGHT_ESTIMATES } from '../util/rows';\nimport { Row } from '../components/Row/Row';\nimport { useTableRenderers } from '../types';\nimport { TableRef } from '../../types';\n\nconst TABLE_SCROLL_PADDING_BOTTOM = 36;\nconst TABLE_PADDING_BOTTOM = 120;\n\nexport function useTableRenderer<TType = unknown>(\n renderers: useTableRenderers<TType>,\n table: ReactTable<TType>,\n tableRef: React.RefObject<TableRef>,\n defaultRowActiveIndex?: number | undefined\n) {\n const tableMeta = table.options.meta as ReactTableMeta<TType>;\n const rows = table.getRowModel().rows;\n const HEADER_OFFSET_TOTAL = HEADER_ROW_HEIGHT_ESTIMATE * table.getHeaderGroups().length;\n const isTableGrouped = !!table.getState().grouping?.length;\n\n // expanded rows\n const { createRowMeasurer, estimateSize } = useRowHeightVirtualisation<TType>(table);\n\n // row groups\n const rangeExtractor = useRowGroupVirtualisation<TType>(table);\n\n const virtualiser = useVirtualizer({\n count: rows.length,\n estimateSize,\n getScrollElement: () => tableRef.current,\n overscan: tableMeta.printing.isPrinting ? rows.length : undefined,\n rangeExtractor,\n scrollPaddingStart: isTableGrouped ? HEADER_ROW_HEIGHT_ESTIMATE : undefined,\n scrollPaddingEnd: HEADER_OFFSET_TOTAL + TABLE_SCROLL_PADDING_BOTTOM,\n paddingEnd: TABLE_PADDING_BOTTOM,\n });\n\n const virtualItems = virtualiser.getVirtualItems();\n\n const scrollToIndex = React.useCallback(\n (index: number, options: ReactVirtualScrollToOptions = { align: 'auto', behavior: 'smooth' }) => {\n const notSmooth: ReactVirtualScrollToOptions = { ...options, behavior: 'auto' };\n\n if (tableRef.current) {\n if (index === 0) {\n virtualiser.scrollToOffset(0, notSmooth);\n } else if (index >= virtualItems.length - 1) {\n // scrolling to the last row isn't easy when rows have dynamic height\n // because you don't know all the heights until they render...\n // so when you skip to the last you don't know if \"the end\" is actually the end\n virtualiser.scrollToIndex(index, notSmooth);\n\n // https://github.com/TanStack/virtual/issues/216\n setTimeout(() => {\n virtualiser.scrollToIndex(index, notSmooth);\n\n setTimeout(() => {\n virtualiser.scrollToIndex(index, notSmooth);\n }, 150);\n }, 150);\n } else {\n virtualiser.scrollToIndex(index, options);\n }\n }\n },\n [virtualItems.length, tableRef.current]\n );\n\n // use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0\n const paddingStartIndex = isTableGrouped && rows.length > 1 ? 1 : 0;\n\n // styling for offsetting rows - this \"is\" the virtualisation\n const [paddingTop, paddingBottom] =\n virtualItems.length > 0\n ? [\n // use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0\n Math.max(\n 0,\n virtualItems[paddingStartIndex]?.start -\n virtualiser.options.scrollMargin -\n virtualItems[paddingStartIndex]?.size ?? 0\n ),\n Math.max(0, virtualiser.getTotalSize() - virtualItems[virtualItems.length - 1]?.end ?? 0),\n ]\n : [0, 0];\n\n // ensure default active rows are scrolled to\n React.useEffect(() => {\n if (defaultRowActiveIndex) {\n scrollToIndex(defaultRowActiveIndex, { align: 'center', behavior: 'auto' });\n }\n }, []);\n\n // rendered output\n let style: CSSProperties = {};\n let content: (JSX.Element | null)[] | null = null;\n\n if (rows.length) {\n style = {\n height: `${virtualiser.getTotalSize()}px`,\n paddingBottom,\n paddingTop,\n };\n content = virtualItems.map(virtualRow => {\n // there appears to be a react-virtual bug where it inserts a single `undefined` item at the end of the row, which crashes here\n if (!virtualRow) {\n return null;\n }\n\n const row = rows[virtualRow.index];\n const measureRow = createRowMeasurer(virtualiser.resizeItem, virtualRow);\n return (\n <Row\n key={row.id}\n row={row}\n index={virtualRow.index}\n table={table}\n measureRow={measureRow}\n renderer={renderers.row}\n cellRenderer={renderers.cell}\n />\n );\n });\n }\n\n return {\n rows: content,\n style,\n scrollToIndex,\n };\n}\n\n// support varying row heights - variable row heights, row expansion\nfunction useRowHeightVirtualisation<TType = unknown>(table: ReactTable<TType>) {\n const tableMeta = table.options.meta as ReactTableMeta<TType>;\n const rows = table.getRowModel().rows;\n const expandedState = table.getState().expanded;\n\n const [expandedRowHeightsCache, setExpandedRowHeightsCache] = React.useState<Record<number, number>>({});\n\n // look at all registered expanded row heights and use them to form a more accurate average size\n // it might be worth considering if taking the max, or an upper bound, would be better\n const averageExpandedRowHeight = React.useMemo(() => {\n const values: number[] = Object.values(expandedRowHeightsCache);\n return values.length ? values.reduce((a, b) => a + b, 0) / values.length : 0;\n }, [expandedRowHeightsCache]);\n\n // row heights\n const estimateSize = React.useCallback(\n (index: number) => {\n const rowHeight = tableMeta.rowHeight.height\n ? ROW_HEIGHT_ESTIMATES[tableMeta.rowHeight.height]\n : ROW_HEIGHT_ESTIMATES.medium;\n\n if (expandedState === true || rows[index]?.getIsExpanded?.()) {\n return rowHeight + averageExpandedRowHeight;\n }\n\n return rowHeight;\n },\n [tableMeta.rowHeight.height, averageExpandedRowHeight, expandedState]\n );\n\n const createRowMeasurer = React.useCallback(\n (resizeItem: (item: VirtualItem, size: number) => void, virtualRow: VirtualItem) =>\n (rowHeight: number, expansionHeight?: number) => {\n // register the expansion height so that it can be used to determine an average\n if (expansionHeight) {\n setExpandedRowHeightsCache(cache => ({ ...cache, [virtualRow.index]: expansionHeight }));\n }\n\n // update the whole row's size to include the expanded height\n resizeItem(virtualRow, rowHeight + (expansionHeight ?? 0));\n },\n []\n );\n\n return { estimateSize, createRowMeasurer };\n}\n\n// support virtualised row groups (where the row group headers are sticky)\nfunction useRowGroupVirtualisation<TType = unknown>(table: ReactTable<TType>) {\n const rows = table.getRowModel().rows;\n const isTableGrouped = !!table.getState().grouping.length;\n\n const rowGroupIndexes = React.useMemo(() => {\n const indexes: number[] = [];\n\n if (isTableGrouped) {\n rows.forEach((row, index) => {\n if (row.getIsGrouped()) {\n indexes.push(index);\n }\n });\n }\n\n return indexes;\n }, []);\n\n // this is taken from the react-virtual docs/examples\n const rangeExtractor = React.useCallback(\n range => {\n const activeRowGroupIndex =\n [...rowGroupIndexes].reverse().find(index => range.startIndex >= index) ?? rowGroupIndexes[0];\n\n const next = new Set([activeRowGroupIndex, ...defaultRangeExtractor(range)]);\n return [...next].sort((a, b) => a - b);\n },\n [rowGroupIndexes]\n );\n\n return isTableGrouped ? rangeExtractor : undefined;\n}\n"],"names":["TABLE_SCROLL_PADDING_BOTTOM","TABLE_PADDING_BOTTOM","useTableRenderer","renderers","table","tableRef","defaultRowActiveIndex","tableMeta","options","meta","rows","getRowModel","HEADER_OFFSET_TOTAL","HEADER_ROW_HEIGHT_ESTIMATE","getHeaderGroups","length","isTableGrouped","_table$getState$group","getState","grouping","createRowMeasurer","estimateSize","useRowHeightVirtualisation","rangeExtractor","useRowGroupVirtualisation","virtualiser","useVirtualizer","count","getScrollElement","current","overscan","printing","isPrinting","undefined","scrollPaddingStart","scrollPaddingEnd","paddingEnd","virtualItems","getVirtualItems","scrollToIndex","React","useCallback","index","align","behavior","notSmooth","scrollToOffset","setTimeout","paddingStartIndex","paddingTop","paddingBottom","Math","max","_ref","_virtualItems$padding","start","scrollMargin","_virtualItems$padding2","size","_ref2","getTotalSize","_virtualItems","end","useEffect","style","content","height","map","virtualRow","row","measureRow","resizeItem","Row","key","id","renderer","cellRenderer","cell","expandedState","expanded","expandedRowHeightsCache","setExpandedRowHeightsCache","useState","averageExpandedRowHeight","useMemo","values","Object","reduce","a","b","rowHeight","ROW_HEIGHT_ESTIMATES","medium","_rows$index","_rows$index$getIsExpa","getIsExpanded","call","expansionHeight","cache","rowGroupIndexes","indexes","forEach","getIsGrouped","push","range","activeRowGroupIndex","_reverse$find","reverse","find","startIndex","next","Set","defaultRangeExtractor","sort"],"mappings":";;;;;AAaA,MAAMA,2BAA2B,GAAG,EAAE;AACtC,MAAMC,oBAAoB,GAAG,GAAG;SAEhBC,gBAAgBA,CAC5BC,SAAmC,EACnCC,KAAwB,EACxBC,QAAmC,EACnCC,qBAA0C;;EAE1C,MAAMC,SAAS,GAAGH,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CAAC,CAACG,IAAI,CAAC,CAACzB,CAAC,EAAEC,CAAC,KAAKD,CAAC,GAAGC,CAAC,CAAC;GACzC,EACD,CAACU,eAAe,CAAC,CACpB;EAED,OAAOnF,cAAc,GAAGO,cAAc,GAAGU,SAAS;AACtD;;;;"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useTable.js","sources":["../../../../../../../../src/primitives/Table/Core/useTable.ts"],"sourcesContent":["import { Table as ReactTable, TableMeta as ReactTableMeta, TableState as ReactTableState } from '@tanstack/react-table';\nimport { ScrollToOptions as ReactVirtualScrollToOptions } from '@tanstack/react-virtual';\nimport { useMergedRef } from '../../../hooks/useMergedRef';\nimport { TableCustomSettingsRenderer, TableEmptyStateRenderer, TableProps } from '../types';\nimport { useTableManager } from '../useTableManager/useTableManager';\nimport { useTableStyle } from './features/useTableStyle';\nimport { useTableGlobalShortcuts } from './features/useTableGlobalShortcuts';\nimport { useTableRef } from './features/useTableRef';\nimport { useTableRenderer } from './features/useTableRenderer';\nimport { useTableRowActiveListener } from './listeners/useTableRowActiveListener';\nimport { TableRef } from '../types';\nimport { useTableRenderers } from './types';\nimport { renderer as rowActions } from './components/Columns/Internal/Actions';\nimport { renderer as rowDrag } from './components/Columns/Internal/Drag';\nimport { renderer as rowExpansion } from './components/Columns/Internal/Expansion';\nimport { renderer as rowSelection } from './components/Columns/Internal/Selection';\nimport { useTableManagerInternalColumns } from '../useTableManager/types';\n\nconst INTERNAL_RENDERERS = {\n rowActions,\n rowDrag,\n rowExpansion,\n rowSelection,\n};\n\nexport type useTableReturnValue<TType = unknown> = {\n id: string;\n instance: ReactTable<TType>;\n meta: ReactTableMeta<TType>;\n props: {\n customSettings: TableCustomSettingsRenderer | undefined;\n emptyState: TableEmptyStateRenderer | undefined;\n toolbarLeft: JSX.Element | undefined;\n toolbarPanel: JSX.Element | undefined;\n toolbarRight: JSX.Element | undefined;\n };\n ref: React.RefObject<TableRef>;\n renderer: {\n rows: JSX.Element[] | null;\n style: React.CSSProperties;\n scrollToIndex: (index: number, options?: ReactVirtualScrollToOptions) => void;\n };\n style: React.CSSProperties;\n stylesheet: string;\n state: ReactTableState;\n};\n\nexport function useTable<TType = unknown, TMeta = {}>(\n props: TableProps<TType>,\n externalRef: React.Ref<TableRef>,\n renderers: useTableRenderers<TType>,\n meta?: Partial<ReactTableMeta<TType>> & TMeta\n): useTableReturnValue<TType> {\n // create a ref and merge with the consumer's ref\n const ref = useMergedRef<TableRef>(externalRef);\n // configure the table\n const manager = useTableManager<TType>(props, meta, INTERNAL_RENDERERS as useTableManagerInternalColumns<TType>);\n // configure the virtualised renderer\n const renderer = useTableRenderer<TType>(renderers, manager.instance, ref, props.defaultRowActiveIndex);\n // configure dynamic styling\n const { style, stylesheet } = useTableStyle(manager.id, manager.instance);\n // configure global keyboard shortcuts\n useTableGlobalShortcuts(manager.instance, ref, renderer.scrollToIndex);\n // augment the ref with some helper functions\n useTableRef(manager.instance, ref);\n\n // listeners - they are here and not in useTableManager because they rely on the dom and the table ref\n useTableRowActiveListener(manager.instance, ref);\n\n return {\n ...manager,\n props: {\n customSettings: props.customSettings,\n emptyState: props.emptyState,\n toolbarLeft: props.toolbarLeft,\n toolbarPanel: props.toolbarPanel,\n toolbarRight: props.toolbarRight,\n },\n renderer,\n style,\n stylesheet,\n ref,\n };\n}\n"],"names":["INTERNAL_RENDERERS","rowActions","rowDrag","rowExpansion","rowSelection","useTable","props","externalRef","renderers","meta","ref","useMergedRef","manager","useTableManager","renderer","useTableRenderer","instance","defaultRowActiveIndex","style","stylesheet","useTableStyle","id","useTableGlobalShortcuts","scrollToIndex","useTableRef","useTableRowActiveListener","customSettings","emptyState","toolbarLeft","toolbarPanel","toolbarRight"],"mappings":";;;;;;;;;;;;AAkBA,MAAMA,kBAAkB,GAAG;cACvBC,QAAU;WACVC,UAAO;gBACPC,UAAY;gBACZC;CACH;SAwBeC,QAAQA,CACpBC,KAAwB,EACxBC,WAAgC,EAChCC,SAAmC,EACnCC,IAA6C;;EAG7C,MAAMC,GAAG,GAAGC,YAAY,CAAWJ,WAAW,CAAC;;EAE/C,MAAMK,OAAO,GAAGC,eAAe,CAAQP,KAAK,EAAEG,IAAI,EAAET,kBAA2D,CAAC;;EAEhH,MAAMc,QAAQ,GAAGC,gBAAgB,CAAQP,SAAS,EAAEI,OAAO,CAACI,QAAQ,EAAEN,GAAG,EAAEJ,KAAK,CAACW,qBAAqB,CAAC;;EAEvG,MAAM;IAAEC,KAAK;IAAEC;GAAY,GAAGC,aAAa,CAACR,OAAO,CAACS,EAAE,EAAET,OAAO,CAACI,QAAQ,CAAC;;EAEzEM,uBAAuB,CAACV,OAAO,CAACI,QAAQ,EAAEN,GAAG,EAAEI,QAAQ,CAACS,aAAa,CAAC;;EAEtEC,WAAW,CAACZ,OAAO,CAACI,QAAQ,EAAEN,GAAG,CAAC;;EAGlCe,yBAAyB,CAACb,OAAO,CAACI,QAAQ,EAAEN,GAAG,CAAC;EAEhD,OAAO;IACH,GAAGE,OAAO;IACVN,KAAK,EAAE;MACHoB,cAAc,EAAEpB,KAAK,CAACoB,cAAc;MACpCC,UAAU,EAAErB,KAAK,CAACqB,UAAU;MAC5BC,WAAW,EAAEtB,KAAK,CAACsB,WAAW;MAC9BC,YAAY,EAAEvB,KAAK,CAACuB,YAAY;MAChCC,YAAY,EAAExB,KAAK,CAACwB;KACvB;IACDhB,QAAQ;IACRI,KAAK;IACLC,UAAU;IACVT;GACH;AACL;;;;"}
|
1
|
+
{"version":3,"file":"useTable.js","sources":["../../../../../../../../src/primitives/Table/Core/useTable.ts"],"sourcesContent":["import { Table as ReactTable, TableMeta as ReactTableMeta, TableState as ReactTableState } from '@tanstack/react-table';\nimport { ScrollToOptions as ReactVirtualScrollToOptions } from '@tanstack/react-virtual';\nimport { useMergedRef } from '../../../hooks/useMergedRef';\nimport { TableCustomSettingsRenderer, TableEmptyStateRenderer, TableProps } from '../types';\nimport { useTableManager } from '../useTableManager/useTableManager';\nimport { useTableStyle } from './features/useTableStyle';\nimport { useTableGlobalShortcuts } from './features/useTableGlobalShortcuts';\nimport { useTableRef } from './features/useTableRef';\nimport { useTableRenderer } from './features/useTableRenderer';\nimport { useTableRowActiveListener } from './listeners/useTableRowActiveListener';\nimport { TableRef } from '../types';\nimport { useTableRenderers } from './types';\nimport { renderer as rowActions } from './components/Columns/Internal/Actions';\nimport { renderer as rowDrag } from './components/Columns/Internal/Drag';\nimport { renderer as rowExpansion } from './components/Columns/Internal/Expansion';\nimport { renderer as rowSelection } from './components/Columns/Internal/Selection';\nimport { useTableManagerInternalColumns } from '../useTableManager/types';\n\nconst INTERNAL_RENDERERS = {\n rowActions,\n rowDrag,\n rowExpansion,\n rowSelection,\n};\n\nexport type useTableReturnValue<TType = unknown> = {\n id: string;\n instance: ReactTable<TType>;\n meta: ReactTableMeta<TType>;\n props: {\n customSettings: TableCustomSettingsRenderer | undefined;\n emptyState: TableEmptyStateRenderer | undefined;\n toolbarLeft: JSX.Element | undefined;\n toolbarPanel: JSX.Element | undefined;\n toolbarRight: JSX.Element | undefined;\n };\n ref: React.RefObject<TableRef>;\n renderer: {\n rows: (JSX.Element | null)[] | null;\n style: React.CSSProperties;\n scrollToIndex: (index: number, options?: ReactVirtualScrollToOptions) => void;\n };\n style: React.CSSProperties;\n stylesheet: string;\n state: ReactTableState;\n};\n\nexport function useTable<TType = unknown, TMeta = {}>(\n props: TableProps<TType>,\n externalRef: React.Ref<TableRef>,\n renderers: useTableRenderers<TType>,\n meta?: Partial<ReactTableMeta<TType>> & TMeta\n): useTableReturnValue<TType> {\n // create a ref and merge with the consumer's ref\n const ref = useMergedRef<TableRef>(externalRef);\n // configure the table\n const manager = useTableManager<TType>(props, meta, INTERNAL_RENDERERS as useTableManagerInternalColumns<TType>);\n // configure the virtualised renderer\n const renderer = useTableRenderer<TType>(renderers, manager.instance, ref, props.defaultRowActiveIndex);\n // configure dynamic styling\n const { style, stylesheet } = useTableStyle(manager.id, manager.instance);\n // configure global keyboard shortcuts\n useTableGlobalShortcuts(manager.instance, ref, renderer.scrollToIndex);\n // augment the ref with some helper functions\n useTableRef(manager.instance, ref);\n\n // listeners - they are here and not in useTableManager because they rely on the dom and the table ref\n useTableRowActiveListener(manager.instance, ref);\n\n return {\n ...manager,\n props: {\n customSettings: props.customSettings,\n emptyState: props.emptyState,\n toolbarLeft: props.toolbarLeft,\n toolbarPanel: props.toolbarPanel,\n toolbarRight: props.toolbarRight,\n },\n renderer,\n style,\n stylesheet,\n ref,\n };\n}\n"],"names":["INTERNAL_RENDERERS","rowActions","rowDrag","rowExpansion","rowSelection","useTable","props","externalRef","renderers","meta","ref","useMergedRef","manager","useTableManager","renderer","useTableRenderer","instance","defaultRowActiveIndex","style","stylesheet","useTableStyle","id","useTableGlobalShortcuts","scrollToIndex","useTableRef","useTableRowActiveListener","customSettings","emptyState","toolbarLeft","toolbarPanel","toolbarRight"],"mappings":";;;;;;;;;;;;AAkBA,MAAMA,kBAAkB,GAAG;cACvBC,QAAU;WACVC,UAAO;gBACPC,UAAY;gBACZC;CACH;SAwBeC,QAAQA,CACpBC,KAAwB,EACxBC,WAAgC,EAChCC,SAAmC,EACnCC,IAA6C;;EAG7C,MAAMC,GAAG,GAAGC,YAAY,CAAWJ,WAAW,CAAC;;EAE/C,MAAMK,OAAO,GAAGC,eAAe,CAAQP,KAAK,EAAEG,IAAI,EAAET,kBAA2D,CAAC;;EAEhH,MAAMc,QAAQ,GAAGC,gBAAgB,CAAQP,SAAS,EAAEI,OAAO,CAACI,QAAQ,EAAEN,GAAG,EAAEJ,KAAK,CAACW,qBAAqB,CAAC;;EAEvG,MAAM;IAAEC,KAAK;IAAEC;GAAY,GAAGC,aAAa,CAACR,OAAO,CAACS,EAAE,EAAET,OAAO,CAACI,QAAQ,CAAC;;EAEzEM,uBAAuB,CAACV,OAAO,CAACI,QAAQ,EAAEN,GAAG,EAAEI,QAAQ,CAACS,aAAa,CAAC;;EAEtEC,WAAW,CAACZ,OAAO,CAACI,QAAQ,EAAEN,GAAG,CAAC;;EAGlCe,yBAAyB,CAACb,OAAO,CAACI,QAAQ,EAAEN,GAAG,CAAC;EAEhD,OAAO;IACH,GAAGE,OAAO;IACVN,KAAK,EAAE;MACHoB,cAAc,EAAEpB,KAAK,CAACoB,cAAc;MACpCC,UAAU,EAAErB,KAAK,CAACqB,UAAU;MAC5BC,WAAW,EAAEtB,KAAK,CAACsB,WAAW;MAC9BC,YAAY,EAAEvB,KAAK,CAACuB,YAAY;MAChCC,YAAY,EAAExB,KAAK,CAACwB;KACvB;IACDhB,QAAQ;IACRI,KAAK;IACLC,UAAU;IACVT;GACH;AACL;;;;"}
|
@@ -4,7 +4,7 @@ import { ScrollToOptions as ReactVirtualScrollToOptions } from '@tanstack/react-
|
|
4
4
|
import { useTableRenderers } from '../types';
|
5
5
|
import { TableRef } from '../../types';
|
6
6
|
export declare function useTableRenderer<TType = unknown>(renderers: useTableRenderers<TType>, table: ReactTable<TType>, tableRef: React.RefObject<TableRef>, defaultRowActiveIndex?: number | undefined): {
|
7
|
-
rows: JSX.Element[] | null;
|
7
|
+
rows: (JSX.Element | null)[] | null;
|
8
8
|
style: React.CSSProperties;
|
9
9
|
scrollToIndex: (index: number, options?: ReactVirtualScrollToOptions) => void;
|
10
10
|
};
|
@@ -17,7 +17,7 @@ export declare type useTableReturnValue<TType = unknown> = {
|
|
17
17
|
};
|
18
18
|
ref: React.RefObject<TableRef>;
|
19
19
|
renderer: {
|
20
|
-
rows: JSX.Element[] | null;
|
20
|
+
rows: (JSX.Element | null)[] | null;
|
21
21
|
style: React.CSSProperties;
|
22
22
|
scrollToIndex: (index: number, options?: ReactVirtualScrollToOptions) => void;
|
23
23
|
};
|
@@ -11243,10 +11243,11 @@ function Row(props) {
|
|
11243
11243
|
const TABLE_SCROLL_PADDING_BOTTOM = 36;
|
11244
11244
|
const TABLE_PADDING_BOTTOM = 120;
|
11245
11245
|
function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
11246
|
-
var _table$getState$group, _virtualItems
|
11246
|
+
var _table$getState$group, _ref, _virtualItems$padding, _virtualItems$padding2, _ref2, _virtualItems;
|
11247
11247
|
const tableMeta = table.options.meta;
|
11248
11248
|
const rows = table.getRowModel().rows;
|
11249
11249
|
const HEADER_OFFSET_TOTAL = HEADER_ROW_HEIGHT_ESTIMATE * table.getHeaderGroups().length;
|
11250
|
+
const isTableGrouped = !!((_table$getState$group = table.getState().grouping) !== null && _table$getState$group !== void 0 && _table$getState$group.length);
|
11250
11251
|
// expanded rows
|
11251
11252
|
const {
|
11252
11253
|
createRowMeasurer,
|
@@ -11260,7 +11261,7 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
11260
11261
|
getScrollElement: () => tableRef.current,
|
11261
11262
|
overscan: tableMeta.printing.isPrinting ? rows.length : undefined,
|
11262
11263
|
rangeExtractor,
|
11263
|
-
scrollPaddingStart:
|
11264
|
+
scrollPaddingStart: isTableGrouped ? HEADER_ROW_HEIGHT_ESTIMATE : undefined,
|
11264
11265
|
scrollPaddingEnd: HEADER_OFFSET_TOTAL + TABLE_SCROLL_PADDING_BOTTOM,
|
11265
11266
|
paddingEnd: TABLE_PADDING_BOTTOM
|
11266
11267
|
});
|
@@ -11293,10 +11294,12 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
11293
11294
|
}
|
11294
11295
|
}
|
11295
11296
|
}, [virtualItems.length, tableRef.current]);
|
11297
|
+
// use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0
|
11298
|
+
const paddingStartIndex = isTableGrouped && rows.length > 1 ? 1 : 0;
|
11296
11299
|
// styling for offsetting rows - this "is" the virtualisation
|
11297
11300
|
const [paddingTop, paddingBottom] = virtualItems.length > 0 ? [
|
11298
11301
|
// use row 1 not 0, because 0 might be sticky in grouped tables and it's start value will always be 0
|
11299
|
-
Math.max(0, ((_virtualItems$ = virtualItems[
|
11302
|
+
Math.max(0, (_ref = ((_virtualItems$padding = virtualItems[paddingStartIndex]) === null || _virtualItems$padding === void 0 ? void 0 : _virtualItems$padding.start) - virtualiser.options.scrollMargin - ((_virtualItems$padding2 = virtualItems[paddingStartIndex]) === null || _virtualItems$padding2 === void 0 ? void 0 : _virtualItems$padding2.size)) !== null && _ref !== void 0 ? _ref : 0), Math.max(0, (_ref2 = virtualiser.getTotalSize() - ((_virtualItems = virtualItems[virtualItems.length - 1]) === null || _virtualItems === void 0 ? void 0 : _virtualItems.end)) !== null && _ref2 !== void 0 ? _ref2 : 0)] : [0, 0];
|
11300
11303
|
// ensure default active rows are scrolled to
|
11301
11304
|
React__default.useEffect(() => {
|
11302
11305
|
if (defaultRowActiveIndex) {
|
@@ -11316,6 +11319,10 @@ function useTableRenderer(renderers, table, tableRef, defaultRowActiveIndex) {
|
|
11316
11319
|
paddingTop
|
11317
11320
|
};
|
11318
11321
|
content = virtualItems.map(virtualRow => {
|
11322
|
+
// there appears to be a react-virtual bug where it inserts a single `undefined` item at the end of the row, which crashes here
|
11323
|
+
if (!virtualRow) {
|
11324
|
+
return null;
|
11325
|
+
}
|
11319
11326
|
const row = rows[virtualRow.index];
|
11320
11327
|
const measureRow = createRowMeasurer(virtualiser.resizeItem, virtualRow);
|
11321
11328
|
return /*#__PURE__*/React__default.createElement(Row, {
|