Skip to content

Commit

Permalink
Cleaner fallback syntax, and use unknown as TValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lindskogen committed Oct 13, 2023
1 parent b11c1ba commit c944472
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TItem> {
columnId: string;
Expand All @@ -31,6 +32,11 @@ export interface StandardTableCellProps<TItem> {
disableBorderLeft?: boolean;
}

const fallbackCellRenderer: DefaultStandardTableCellRenderer<unknown> = ({
label,
textSize,
}) => <TextCell label={label} size={textSize} />;

export const StandardTableCell = React.memo(function StandardTableCell<TItem>({
columnId,
item,
Expand Down Expand Up @@ -58,7 +64,8 @@ export const StandardTableCell = React.memo(function StandardTableCell<TItem>({
return selectedIds.indexOf(itemKey) >= 0;
}, [itemKey, selectedIds]);

const { defaultCellRenderer, defaultTextSize } = useStandardTableConfig();
const { defaultCellRenderer = fallbackCellRenderer, defaultTextSize } =
useStandardTableConfig();

const {
itemValueResolver,
Expand All @@ -67,7 +74,7 @@ export const StandardTableCell = React.memo(function StandardTableCell<TItem>({
minWidth,
justifyContentCell = "flex-start",
borderLeft,
renderCell,
renderCell = defaultCellRenderer,
gridCellOptions: gridCellOptionsForColumn,
isEditable,
onChange,
Expand Down Expand Up @@ -134,25 +141,17 @@ export const StandardTableCell = React.memo(function StandardTableCell<TItem>({

const content = useMemo(
() =>
renderCell?.({
renderCell({
label,
value: itemValue,
item,
gridCell,
isEditable: editable,
isSelected,
zIndex: currentZIndex,
textSize: defaultTextSize,
itemKey,
}) ??
defaultCellRenderer?.({
label,
item,
gridCell,
isEditable: editable,
isSelected,
zIndex: currentZIndex,
itemKey,
}) ?? <TextCell label={label} size={defaultTextSize} />,
}),
[
renderCell,
label,
Expand All @@ -163,7 +162,6 @@ export const StandardTableCell = React.memo(function StandardTableCell<TItem>({
isSelected,
currentZIndex,
itemKey,
defaultCellRenderer,
defaultTextSize,
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -211,6 +212,7 @@ export interface StandardTableCellRendererArgObject<TItemValue, TItem> {
gridCell: UseGridCellResult<string>;
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.
*/
Expand All @@ -222,22 +224,9 @@ export interface StandardTableCellRendererArgObject<TItemValue, TItem> {
* Therefor, it can not know "value: TItemValue", since it has not been defined yet.
*/
export type DefaultStandardTableCellRenderer<TItem> = (
arg: DefaultStandardTableCellRendererArgObject<TItem>
arg: StandardTableCellRendererArgObject<unknown, TItem>
) => ReactNode;

export interface DefaultStandardTableCellRendererArgObject<TItem> {
label: string;
item: TItem;
itemKey: string;
gridCell: UseGridCellResult<string>;
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<TItem> = (item: TItem) => string | undefined;

export interface ItemValueResolver<TItem, TItemValue> {
Expand Down

0 comments on commit c944472

Please sign in to comment.