Skip to content

Commit

Permalink
feat: implement windowing horizontal by santiago.trigo
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Pinedo committed Nov 3, 2023
1 parent 1f8b8ea commit 6101940
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 107 deletions.
4 changes: 2 additions & 2 deletions packages/table/src/core/Row/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const Row: React.FC<RowProps> = ({ columnDefs, data, styles }) => {
return (
<Cell
columnDef={columnDef}
key={`cell-${columnDef.colId}`}
data={data[columnDef.field] ?? ''}
key={`cell-${columnDef.id}`}
data={data[columnDef.id] ?? ''}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions packages/table/src/core/Row/StyledTableRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const StyledTableRow = styled.tr<StyledTableRowProps>`
width: 100%;
height: ${({ styles }) => styles.height};
transform: ${({ styles }) => styles.transform};
display: table;
`;
4 changes: 2 additions & 2 deletions packages/table/src/core/Table/StyledTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface StyledTableProps {
}

export const StyledTable = styled.table<StyledTableProps>`
display: block;
height: auto;
position: relative;
height: ${({ height }) => height};
width: 100%;
`;
4 changes: 3 additions & 1 deletion packages/table/src/core/Table/StyledTableWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { scrollbars } from '@devoinc/genesys-ui';
export interface StyledTableWrapperProps {
maxHeight?: React.CSSProperties['maxHeight'];
scrolled?: boolean;
height?: React.CSSProperties['height'];
}

export const StyledTableWrapper = styled.div<StyledTableWrapperProps>`
display: inline-block;
width: 100%;
height: ${({ height }) => height};
overflow: auto;
`;
29 changes: 26 additions & 3 deletions packages/table/src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';

import { TableOptionsProps } from '../../declarations';

Expand All @@ -25,23 +26,45 @@ export const Table: React.FC<TableProps> = ({ tableOptions, data }) => {

const columnsDefs = defineColumnsDefs(defaultColumnDef, columnDefs, types);

const ref = React.useRef();

const rowVirtualizer = useVirtualizer({
count: data.length,
getScrollElement: () => ref.current,
estimateSize: () => 34,
overscan: 5,
});

const columnVirtualizer = useVirtualizer({
count: columnsDefs.length,
getScrollElement: () => ref.current,
estimateSize: () => 34,
overscan: 5,
horizontal: true,
});
debugger;

return (
<StyledTableWrapper
maxHeight={style?.wrapper?.maxHeight}
scrolled={style?.wrapper?.scrolled}
ref={ref}
height={'500px'}
>
<StyledTable
width={'400px'}
width={'100%'}
minWidth={style?.table?.minWidth}
height={'200px'}
height={`${rowVirtualizer.getTotalSize()}px`}
>
<TableHead
columnDefs={columnsDefs}
columnVirtualizer={columnVirtualizer}
scrolled={style?.wrapper?.scrolled}
/>
<TableBody
columnDefs={columnsDefs}
data={data}
columnDefs={columnsDefs}
rowVirtualizer={rowVirtualizer}
height={style?.row?.height}
rowHeight={style?.row?.height}
/>
Expand Down
61 changes: 23 additions & 38 deletions packages/table/src/core/TableBody/TableBody.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';

import { Row } from '../Row';

Expand All @@ -8,54 +7,40 @@ import { ColDef } from '../../declarations';
import { StyledTableBody } from './StyledTableBody';

interface TableBodyProps {
data: { [key: string]: unknown }[];
rowVirtualizer: unknown;
data: unknown;
columnDefs: ColDef[];
height?: React.CSSProperties['height'];
rowHeight?: number;
}

export const TableBody: React.FC<TableBodyProps> = ({
columnDefs,
rowVirtualizer,
data,
height,
rowHeight,
}) => {
const ref = React.useRef();

const rowVirtualizer = useVirtualizer({
count: data.length,
getScrollElement: () => ref.current,
estimateSize: () => 34,
overscan: 10,
});

debugger;
return (
<div
ref={ref}
style={{
height: '400px',
overflow: 'auto',
}}
>
<StyledTableBody height={`${rowVirtualizer.getTotalSize()}px`}>
{rowVirtualizer.getVirtualItems().map((virtualItem) => {
return (
<Row
key={'tb_' + virtualItem.key}
columnDefs={columnDefs}
data={data[virtualItem.index]}
styles={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}}
/>
);
})}
</StyledTableBody>
</div>
<StyledTableBody>
{rowVirtualizer.getVirtualItems().map((virtualItem) => {
return (
<Row
key={'tb_' + virtualItem.key}
columnDefs={columnDefs}
data={data[virtualItem.index]}
styles={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}}
/>
);
})}
</StyledTableBody>
);
};
1 change: 1 addition & 0 deletions packages/table/src/core/TableHead/StyledTableHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const StyledTableHead = styled.thead<StyledTableHeadProps>`
}}
top: 0;
vertical-align: middle;
${({ theme }) => {
return css`
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/core/TableHead/TableHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const TableHead: React.FC<TableHeadProps> = ({
<StyledTableHeadRow scrolled={scrolled}>
{columnDefs.map((columnDef) => (
<HeaderCell
key={columnDef.colId}
key={columnDef.id}
columnDef={columnDef}
scrolled={scrolled}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StyledTableProps } from './core/Table/StyledTable';
import { StyledTableWrapperProps } from './core/Table/StyledTableWrapper';
import { DateContext } from './valueFormatters/date';

export type DefaultColDef = Omit<ColDef, 'colId' | 'field'>;
export type DefaultColDef = Omit<ColDef, 'id'>;

type Types = { id: string };
type TypesColDef = Omit<ColDef, 'colId'>;
Expand Down
149 changes: 90 additions & 59 deletions packages/table/src/recipes/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DropdownMenu, Button, Flex } from '@devoinc/genesys-ui';
import { TableOptionsProps } from '../declarations';
import { BasicTable } from './Table';

let index = 0;
let index = 1;

const data = Holo.of()
.schema({
Expand Down Expand Up @@ -50,13 +50,12 @@ const tableOptions: TableOptionsProps = {
},
columnDefs: [
{
colId: 'id',
field: 'id',
id: 'id',
type: 'text',
headerName: 'id',
},
{
colId: 'menu',
field: 'menu',
id: 'menu',
headerName: 'menu',
CellRenderer: (params) => {
return (
Expand All @@ -81,64 +80,96 @@ const tableOptions: TableOptionsProps = {
},
},
{
colId: 'booleanValue',
field: 'booleanValue',
id: 'booleanValue',
headerName: 'booleanValue',
type: 'tagBoolean',
},
{
id: 'name',
headerName: 'Name',
type: 'text',
editable: true,
},
{
id: 'age',
headerName: 'age',
type: 'number',
},
{
id: 'company',
headerName: 'company',
type: 'text',
},
{
id: 'balance',
headerName: 'balance',
type: 'number',
},
{
id: 'status',
headerName: 'status',
type: 'tag',
},
{
id: 'picture',
headerName: 'picture',
type: 'link',
},
{
id: 'timestamp',
headerName: 'timestamp',
type: 'date',
editable: true,
},
{
id: 'tags',
headerName: 'tags',
type: 'tags',
},
{
id: 'name',
headerName: 'Name',
type: 'text',
editable: true,
},
{
id: 'age',
headerName: 'age',
type: 'number',
},
{
id: 'company',
headerName: 'company',
type: 'text',
},
{
id: 'balance',
headerName: 'balance',
type: 'number',
},
{
id: 'status',
headerName: 'status',
type: 'tag',
},
{
id: 'picture',
headerName: 'picture',
type: 'link',
},
{
id: 'timestamp',
headerName: 'timestamp',
type: 'date',
editable: true,
},
{
id: 'tags',
headerName: 'tags',
type: 'tags',
},
// {
// colId: 'name',
// field: 'name',
// headerName: 'Name',
// type: 'text',
// editable: true,
// },
// {
// colId: 'age',
// field: 'age',
// headerName: 'age',
// type: 'number',
// },
// {
// colId: 'company',
// field: 'company',
// headerName: 'company',
// type: 'text',
// },
// {
// colId: 'balance',
// field: 'balance',
// headerName: 'balance',
// type: 'number',
// },
// {
// colId: 'status',
// field: 'status',
// headerName: 'status',
// type: 'tag',
// },
// {
// colId: 'picture',
// field: 'picture',
// headerName: 'picture',
// type: 'link',
// },
// {
// colId: 'timestamp',
// field: 'timestamp',
// headerName: 'timestamp',
// type: 'date',
// editable: true,
// },
// {
// colId: 'tags',
// field: 'tags',
// headerName: 'tags',
// type: 'tags',
// },
// {
// colId: 'about',
// field: 'about',
// id: 'about',
// headerName: 'about',
// type: 'text',
// },
Expand Down

0 comments on commit 6101940

Please sign in to comment.