From 5a2441a5ffee43c9f97604d04630589d8ad55e0d Mon Sep 17 00:00:00 2001 From: Carlos Pinedo Date: Tue, 12 Dec 2023 12:16:43 +0100 Subject: [PATCH] feat(Table): Improved context communication and values update for table body --- packages/table/src/core/Table/Table.tsx | 5 ++- packages/table/src/core/Table/context.tsx | 6 ++- .../table/src/core/TableBody/TableBody.tsx | 12 ++---- .../table/src/core/hooks/useTableMeasures.ts | 38 ++++++++++--------- packages/table/src/core/utils/sizes.ts | 8 ++-- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/packages/table/src/core/Table/Table.tsx b/packages/table/src/core/Table/Table.tsx index cb666d358..32c183170 100644 --- a/packages/table/src/core/Table/Table.tsx +++ b/packages/table/src/core/Table/Table.tsx @@ -34,11 +34,13 @@ export const Table: React.FC = ({ const theme = useTheme(); const ref = React.useRef(); + const refinedColumnDefs: ColDef[] = getCollatedColumns( defaultColumnDef, columnDefs, types, ); + const sizes = getSizes(theme, visualOptions?.density ?? 'default'); const { rowVirtualizer, columnVirtualizer } = useTableVirtualization({ @@ -50,6 +52,7 @@ export const Table: React.FC = ({ }); const { hasScroll } = useTableScroll(rowVirtualizer, ref); + const { measures } = useTableMeasures({ ref, rowVirtualizer, @@ -77,8 +80,6 @@ export const Table: React.FC = ({ scrolled={hasScroll} /> { - const [context] = React.useState({ + const [context, setContext] = React.useState({ ...merge({}, defaultTableContext, value), }); + React.useEffect(() => { + setContext(merge({}, defaultTableContext, value)); + }, [value]); + return ( {children} ); diff --git a/packages/table/src/core/TableBody/TableBody.tsx b/packages/table/src/core/TableBody/TableBody.tsx index 4a0d14c30..81af5cca3 100644 --- a/packages/table/src/core/TableBody/TableBody.tsx +++ b/packages/table/src/core/TableBody/TableBody.tsx @@ -5,14 +5,13 @@ import { StyledTableBody } from './StyledTableBody'; import { VirtualItem, Virtualizer } from '@tanstack/react-virtual'; import { Box, Typography } from '@devoinc/genesys-ui'; import { TableContext } from '../Table/context'; +import { getTableEvalHeight, getTableEvalWidth } from '../utils'; export interface TableBodyProps { columnDefs: ColDef[]; columnVirtualizer: Virtualizer; data: unknown; rowVirtualizer: Virtualizer; - height?: React.CSSProperties['height']; - width?: React.CSSProperties['width']; } const renderMessage = (message: React.ReactNode) => { @@ -45,17 +44,14 @@ export const TableBody: React.FC = ({ columnVirtualizer, data, rowVirtualizer, - height, - width, }) => { - const { texts } = React.useContext(TableContext); + const { visualOptions, texts, measures } = React.useContext(TableContext); const emptyMessage = getEmptyMessage(data, texts, rowVirtualizer); - const { visualOptions } = React.useContext(TableContext); return ( {emptyMessage ? ( diff --git a/packages/table/src/core/hooks/useTableMeasures.ts b/packages/table/src/core/hooks/useTableMeasures.ts index 0e98b7184..36e75f8da 100644 --- a/packages/table/src/core/hooks/useTableMeasures.ts +++ b/packages/table/src/core/hooks/useTableMeasures.ts @@ -1,5 +1,6 @@ +import React, { useEffect } from 'react'; import { Virtualizer } from '@tanstack/react-virtual'; -import { SizesConfig } from '../../declarations'; +import { MeasuresConfig, SizesConfig } from '../../declarations'; interface UseTableMeasuresParams { ref: React.MutableRefObject; @@ -14,26 +15,27 @@ export const useTableMeasures = ({ columnVirtualizer, sizes, }: UseTableMeasuresParams) => { - const tableWrapperHeight = ref?.current?.offsetHeight; - const tableWrapperWidth = ref?.current?.offsetWidth; - const tableVisibleBodyHeight = tableWrapperHeight - sizes.head.height; + const [measures, setMeasures] = React.useState(); - const measures = { - wrapper: { - height: tableWrapperHeight, - width: tableWrapperWidth, - }, - body: { - total: { - height: rowVirtualizer?.getTotalSize(), - width: columnVirtualizer?.getTotalSize(), + useEffect(() => { + setMeasures({ + wrapper: { + height: ref?.current?.offsetHeight, + width: ref?.current?.offsetWidth, }, - visible: { - height: tableVisibleBodyHeight, - width: tableWrapperWidth, + body: { + total: { + height: rowVirtualizer?.getTotalSize(), + width: columnVirtualizer?.getTotalSize(), + }, + visible: { + height: ref?.current?.offsetHeight - sizes.head.height, + width: ref?.current?.offsetWidth, + }, }, - }, - }; + }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [ref.current?.offsetHeight, ref.current?.offsetWidth]); return { measures }; }; diff --git a/packages/table/src/core/utils/sizes.ts b/packages/table/src/core/utils/sizes.ts index 044c12cde..73376bf29 100644 --- a/packages/table/src/core/utils/sizes.ts +++ b/packages/table/src/core/utils/sizes.ts @@ -1,19 +1,17 @@ import { Brand } from '@devoinc/genesys-tokens-types'; import { Density, SizesConfig } from '../../declarations'; import { getPxFromRem } from '@devoinc/genesys-ui'; -import { TableBodyProps } from '../TableBody'; /** * Returns the evaluated height of the table based in the virtualization */ -export const getTableEvalHeight = ( - tableBodyHeight: TableBodyProps['height'], -) => (tableBodyHeight !== 0 ? `${tableBodyHeight}px` : 'auto'); +export const getTableEvalHeight = (tableBodyHeight: number) => + tableBodyHeight !== 0 ? `${tableBodyHeight}px` : 'auto'; /** * Returns the evaluated width of the table based in the virtualization */ -export const getTableEvalWidth = (tableBodyWidth: TableBodyProps['width']) => +export const getTableEvalWidth = (tableBodyWidth: number) => tableBodyWidth !== 0 ? `${tableBodyWidth}px` : '100%'; /**