-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(table): rowHeight override with colDefs
- Loading branch information
Showing
22 changed files
with
241 additions
and
93 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
24 changes: 24 additions & 0 deletions
24
packages/table/src/helpers/definitions/col/getColDef.test.ts
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,24 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
import { TColDef } from '../../../declarations'; | ||
import { getColDef } from './getColDef'; | ||
|
||
describe('helpers', () => { | ||
describe('definitions', () => { | ||
describe('col', () => { | ||
describe('getColDef', () => { | ||
const cases: [string, TColDef[], string, TColDef][] = [ | ||
['empty col defs return undefined', [], '1', undefined], | ||
['id null return undefined', [{ id: '1' }], null, undefined], | ||
['colDef null return undefined', null, '1', undefined], | ||
['return element colDef', [{ id: '1' }], '1', { id: '1' }], | ||
['id not find in colDef', [{ id: '1' }], '2', undefined], | ||
]; | ||
|
||
test.each(cases)('%s', (_title, rowDefs, id, expected) => { | ||
expect(getColDef(rowDefs, id)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
import type { TColDef } from '../../../declarations'; | ||
|
||
export const getColDef = (colDefs: TColDef[], id: string) => | ||
colDefs?.find((col) => col.id === id); |
24 changes: 24 additions & 0 deletions
24
packages/table/src/helpers/definitions/col/getColDefByIndex.test.ts
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,24 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
import { TColDef } from '../../../declarations'; | ||
import { getColDefByIndex } from './getColDefByIndex'; | ||
|
||
describe('helpers', () => { | ||
describe('definitions', () => { | ||
describe('col', () => { | ||
describe('getColDefByIndex', () => { | ||
const cases: [string, TColDef[], number, TColDef][] = [ | ||
['empty col defs return undefined', [], 1, undefined], | ||
['id null return undefined', [{ id: '1' }], null, undefined], | ||
['colDef null return undefined', null, 1, undefined], | ||
['return element colDef', [{ id: '1' }], 0, { id: '1' }], | ||
['id not find in colDef', [{ id: '1' }], 2, undefined], | ||
]; | ||
|
||
test.each(cases)('%s', (_title, rowDefs, id, expected) => { | ||
expect(getColDefByIndex(rowDefs, id)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/table/src/helpers/definitions/col/getColDefByIndex.ts
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,4 @@ | ||
import type { TColDef } from '../../../declarations'; | ||
|
||
export const getColDefByIndex = (colDefs: TColDef[], index: number) => | ||
colDefs ? colDefs[index] : undefined; |
40 changes: 40 additions & 0 deletions
40
packages/table/src/helpers/definitions/col/getRowHeightFromColDefs.test.ts
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,40 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
import { TColDef } from '../../../declarations'; | ||
import { getRowHeightFromColDefs } from './getRowHeightFromColDefs'; | ||
|
||
describe('helpers', () => { | ||
describe('definitions', () => { | ||
describe('col', () => { | ||
describe('getRowHeightFromColDefs', () => { | ||
const cases: [string, TColDef[], number][] = [ | ||
['empty col defs return 0', [], undefined], | ||
['col defs without rowHeight', [{ id: '1' }, { id: '2' }], undefined], | ||
['one col with rowHeight', [{ id: '1', rowHeight: 100 }], 100], | ||
[ | ||
'several columns with the same rowHeight', | ||
[ | ||
{ id: '1', rowHeight: 100 }, | ||
{ id: '2', rowHeight: 100 }, | ||
{ id: '3', rowHeight: 100 }, | ||
], | ||
100, | ||
], | ||
[ | ||
'several columns with diferents rowHeight', | ||
[ | ||
{ id: '1', rowHeight: 10 }, | ||
{ id: '2', rowHeight: 100 }, | ||
{ id: '3', rowHeight: 50 }, | ||
], | ||
100, | ||
], | ||
]; | ||
|
||
test.each(cases)('%s', (_title, rowDefs, expected) => { | ||
expect(getRowHeightFromColDefs(rowDefs)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/table/src/helpers/definitions/col/getRowHeightFromColDefs.ts
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,7 @@ | ||
import type { TColDef } from '../../../declarations'; | ||
|
||
export const getRowHeightFromColDefs = (colDefs: TColDef[]) => | ||
colDefs.reduce( | ||
(prev: number, col) => (col?.rowHeight > prev ? col.rowHeight : prev), | ||
0, | ||
) || undefined; |
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,3 @@ | ||
export * from './getColDef'; | ||
export * from './getColDefByIndex'; | ||
export * from './getRowHeightFromColDefs'; |
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,3 +1,4 @@ | ||
export * from './cell'; | ||
export * from './col'; | ||
export * from './row'; | ||
export * from './data'; |
2 changes: 1 addition & 1 deletion
2
packages/table/src/helpers/definitions/row/deletePresetRowDefs.test.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useTableVirtualizationColumn'; |
2 changes: 1 addition & 1 deletion
2
...src/hooks/useTableVirtualizationColumn.ts → ...ionColumn/useTableVirtualizationColumn.ts
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 was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './useTableVirtualizationRow'; |
34 changes: 34 additions & 0 deletions
34
packages/table/src/hooks/useTableVirtualizationRow/useTableVirtualizationRow.ts
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,34 @@ | ||
import * as React from 'react'; | ||
import { useVirtualizer } from '@tanstack/react-virtual'; | ||
|
||
import { TableContext } from '../../context'; | ||
import { ROW_HEIGHT_MD } from '../../constants'; | ||
import { getRowDef, getRowHeightFromColDefs } from '../../helpers'; | ||
|
||
export const useTableVirtualizationRow = ({ | ||
ref, | ||
}: { | ||
ref: React.MutableRefObject<HTMLDivElement>; | ||
}) => { | ||
const { rowHeight, colDefs, data, rowDefs } = React.useContext(TableContext); | ||
|
||
// The rowHeight from the colDefs is the same for each row | ||
const colDefsRowHeight = React.useMemo( | ||
() => getRowHeightFromColDefs(colDefs), | ||
[colDefs], | ||
); | ||
|
||
return useVirtualizer({ | ||
count: data.length, | ||
getScrollElement: () => ref.current, | ||
estimateSize: (index: number) => { | ||
const rowDef = getRowDef(rowDefs, data[index].id as string); | ||
// The height is a preference of: rowDef, colDef, table:rowHeight or the | ||
// default size | ||
const height = | ||
rowDef?.height || colDefsRowHeight || rowHeight || ROW_HEIGHT_MD; | ||
return rowDef?.hide ? 0 : height; | ||
}, | ||
overscan: 10, | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
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,34 @@ | ||
import { Meta, Story, Source } from '@storybook/blocks'; | ||
|
||
import * as Stories from './LongText.stories'; | ||
|
||
<Meta of={Stories} /> | ||
|
||
# Preset `longText` | ||
|
||
<Story of={Stories.Base} /> | ||
|
||
The preset long text is a variation of the `text` preset that do several things: | ||
|
||
- Use the `TextRenderer` renderer | ||
- Use the `TextAreaEditor` as editor | ||
- Use the `TexFilter` as filter | ||
- Define a height of twice height for rowHeight (defined at column level) | ||
|
||
In the example you are viewing a modified `longText` preset with the next | ||
configuration: | ||
|
||
<Source | ||
language="tsx" | ||
code={`{ | ||
... | ||
preset: 'longText', | ||
rowHeight: 200, | ||
truncateLine: 0, | ||
... | ||
}`} | ||
/> | ||
|
||
Here the `truncateLine: 0` is telling to the `TextRenderer` to not truncate the | ||
line. And the `rowHeight: 200` is defining the height for each element of the | ||
column. |
40 changes: 40 additions & 0 deletions
40
packages/table/src/presets/column/longText/LongText.stories.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,40 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { BasicTable } from '../../../recipes'; | ||
|
||
const meta: Meta<typeof BasicTable> = { | ||
title: 'Components/Layout/Table/Presets/Column/LongText', | ||
component: BasicTable, | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof BasicTable>; | ||
|
||
export const Base: Story = { | ||
args: { | ||
colDefs: [ | ||
{ | ||
id: 'id', | ||
headerName: 'id', | ||
preset: 'text', | ||
}, | ||
{ | ||
id: 'text', | ||
headerName: 'Long text', | ||
preset: 'longText', | ||
rowHeight: 260, | ||
truncateLine: 0, | ||
}, | ||
], | ||
data: [ | ||
{ id: '1', text: 'short text' }, | ||
{ | ||
id: '2', | ||
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris scelerisque laoreet lectus et vestibulum. Curabitur varius accumsan metus sed interdum. Mauris nec est at eros pharetra congue. Donec malesuada erat eget libero efficitur, nec convallis enim egestas. Mauris vitae ante quis purus ornare iaculis et vitae nibh. Cras pharetra suscipit quam, vel semper eros bibendum eget. Sed felis ipsum, maximus eu elit non, vehicula maximus tortor. Praesent congue ultricies libero ut ultricies. Donec eu elementum lacus. Quisque ac suscipit sem, vitae luctus tellus.', | ||
}, | ||
], | ||
}, | ||
}; |
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 @@ | ||
export * from './longText'; |
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,14 @@ | ||
import { ROW_HEIGHT_LG } from '../../../constants'; | ||
|
||
import { TColDef } from '../../../declarations'; | ||
import { TextAreaEditor } from '../../../editors'; | ||
import { TextFilter } from '../../../filters'; | ||
import { TextRenderer } from '../../../renderers'; | ||
|
||
export const longText: TColDef = { | ||
id: 'longText', | ||
cellRenderer: TextRenderer, | ||
cellEditor: TextAreaEditor, | ||
rowHeight: ROW_HEIGHT_LG, | ||
cellFilter: TextFilter, | ||
}; |
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