From c94447219c2c80889c00aff55c091efa2269ecfe Mon Sep 17 00:00:00 2001 From: Johan Lindskogen Date: Fri, 13 Oct 2023 12:42:22 +0200 Subject: [PATCH] Cleaner fallback syntax, and use unknown as TValue --- .../components/StandardTableCell.tsx | 26 +++++++++---------- .../config/StandardTableColumnConfig.ts | 17 +++--------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/packages/grid/src/features/standard-table/components/StandardTableCell.tsx b/packages/grid/src/features/standard-table/components/StandardTableCell.tsx index 6b13a8b2d..6f6489076 100644 --- a/packages/grid/src/features/standard-table/components/StandardTableCell.tsx +++ b/packages/grid/src/features/standard-table/components/StandardTableCell.tsx @@ -20,6 +20,7 @@ import { getCellBorder } from "../util/CellBorderCalculator"; import { formatValueLabel } from "../util/LabelFormatter"; import { StandardTableCellUi } from "./StandardTableCellUi"; import { TextCell } from "./TextCell"; +import { DefaultStandardTableCellRenderer } from "../config/StandardTableColumnConfig"; export interface StandardTableCellProps { columnId: string; @@ -31,6 +32,11 @@ export interface StandardTableCellProps { disableBorderLeft?: boolean; } +const fallbackCellRenderer: DefaultStandardTableCellRenderer = ({ + label, + textSize, +}) => ; + export const StandardTableCell = React.memo(function StandardTableCell({ columnId, item, @@ -58,7 +64,8 @@ export const StandardTableCell = React.memo(function StandardTableCell({ return selectedIds.indexOf(itemKey) >= 0; }, [itemKey, selectedIds]); - const { defaultCellRenderer, defaultTextSize } = useStandardTableConfig(); + const { defaultCellRenderer = fallbackCellRenderer, defaultTextSize } = + useStandardTableConfig(); const { itemValueResolver, @@ -67,7 +74,7 @@ export const StandardTableCell = React.memo(function StandardTableCell({ minWidth, justifyContentCell = "flex-start", borderLeft, - renderCell, + renderCell = defaultCellRenderer, gridCellOptions: gridCellOptionsForColumn, isEditable, onChange, @@ -134,7 +141,7 @@ export const StandardTableCell = React.memo(function StandardTableCell({ const content = useMemo( () => - renderCell?.({ + renderCell({ label, value: itemValue, item, @@ -142,17 +149,9 @@ export const StandardTableCell = React.memo(function StandardTableCell({ isEditable: editable, isSelected, zIndex: currentZIndex, + textSize: defaultTextSize, itemKey, - }) ?? - defaultCellRenderer?.({ - label, - item, - gridCell, - isEditable: editable, - isSelected, - zIndex: currentZIndex, - itemKey, - }) ?? , + }), [ renderCell, label, @@ -163,7 +162,6 @@ export const StandardTableCell = React.memo(function StandardTableCell({ isSelected, currentZIndex, itemKey, - defaultCellRenderer, defaultTextSize, ] ); diff --git a/packages/grid/src/features/standard-table/config/StandardTableColumnConfig.ts b/packages/grid/src/features/standard-table/config/StandardTableColumnConfig.ts index 5774ce0e6..101bf9ad1 100644 --- a/packages/grid/src/features/standard-table/config/StandardTableColumnConfig.ts +++ b/packages/grid/src/features/standard-table/config/StandardTableColumnConfig.ts @@ -6,6 +6,7 @@ import { } from "../../grid-cell/hooks/UseGridCell"; import { SortOrderIconVariant } from "../../table-ui/components/table/SortOrderIcon"; import { StandardTableOnKeyDownArgs } from "./StandardTableConfig"; +import { TextSize } from "@stenajs-webui/core"; export type StandardTableColumnConfig< TItem, @@ -211,6 +212,7 @@ export interface StandardTableCellRendererArgObject { gridCell: UseGridCellResult; isEditable?: boolean; isSelected: boolean; + textSize?: TextSize; /** * The z-index used for that cell. Usable if the cell has a popover which should get same z-index for example. */ @@ -222,22 +224,9 @@ export interface StandardTableCellRendererArgObject { * Therefor, it can not know "value: TItemValue", since it has not been defined yet. */ export type DefaultStandardTableCellRenderer = ( - arg: DefaultStandardTableCellRendererArgObject + arg: StandardTableCellRendererArgObject ) => ReactNode; -export interface DefaultStandardTableCellRendererArgObject { - label: string; - item: TItem; - itemKey: string; - gridCell: UseGridCellResult; - isEditable?: boolean; - isSelected: boolean; - /** - * The z-index used for that cell. Usable if the cell has a popover which should get same z-index for example. - */ - zIndex?: number | string; -} - export type BackgroundResolver = (item: TItem) => string | undefined; export interface ItemValueResolver {