Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(table): [NoTicket] Fix and clean up types #369

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export type BaseCellProps = {
value: number;
type: 'zap' | 'star';
};
cellId?: string;
};

export const BaseCell = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export type ButtonCellProps = {
isSelected?: boolean;
onClick: () => void;
price?: ReactNode;
cellId?: string;
};

export const ButtonCell = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type CTACellProps = {
grey?: boolean;
narrow?: boolean;
href: string;
cellId?: string;
};
import styles from './CTACell.module.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export type CardCellProps = {
icon?: ReactNode;
onClick?: () => void;
href: string;
cellId?: string;
};

export const CardCell = ({
title,
description,
Expand All @@ -18,10 +18,10 @@ export const CardCell = ({
}: CardCellProps) => {
return (
<div className="ta-left">
<Card
<Card
title={title}
description={description}
density='balanced'
density="balanced"
icon={icon}
onClick={onClick}
{...(href && { href, as: 'a' })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type ExtraTableCellProps = {
isFirstCellInRow?: boolean;
isTopLeftCell?: boolean;
isNavigation?: boolean;
colSpan?: number;
};

export type TableCellProps = TableCellData & ExtraTableCellProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface TableContentsProps {
className?: string;
collapsibleSections?: boolean;
tableData: TableData;
hideColumns: number[];
hideColumns?: number[];
hideDetails?: boolean;
isMobile?: boolean;
openModal?: ModalFunction;
Expand Down
113 changes: 57 additions & 56 deletions src/lib/components/table/components/TableSection/TableSection.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import classNames from 'classnames';

import styles from './TableSection.module.scss';
import { TableCell, TableCellProps } from '../TableCell/TableCell';
import { TableCell } from '../TableCell/TableCell';
import {
CellReplacements,
isBaseCell,
Expand All @@ -14,7 +14,7 @@ import { useCallback } from 'react';
export interface TableSectionProps {
className?: string;
tableCellRows: TableCellRowData[];
hideColumns: number[];
hideColumns?: number[];
hideHeader?: boolean;
openModal?: ModalFunction;
title: string;
Expand Down Expand Up @@ -62,9 +62,8 @@ const TableSection = ({
};

const isVisibleColumn = useCallback(
(cellIndex: number) => (
!hideColumns.includes(cellIndex)
), [hideColumns]
(cellIndex: number) => !hideColumns.includes(cellIndex),
[hideColumns]
);

return (
Expand Down Expand Up @@ -96,67 +95,69 @@ const TableSection = ({
},
} as TableCellData;

return isVisibleColumn(cellIndex) && (
<TableCell
key={cellIndex}
isHeader
isFirstCellInRow={isFirstCellInRow}
isTopLeftCell={isFirstCellInRow}
{...cellProps}
/>
return (
isVisibleColumn(cellIndex) && (
<TableCell
key={cellIndex}
isHeader
isFirstCellInRow={isFirstCellInRow}
isTopLeftCell={isFirstCellInRow}
{...cellProps}
/>
)
);
})}
</tr>
</thead>
)}

<tbody>
{tableCellRows.map((row, rowIndex) => {

return rowIndex > 0 && (
<tr key={rowIndex} className={styles.tr}>
{row.map((tableCellData, cellIndex) => {
const key = `${rowIndex}-${cellIndex}`;
const isFirstCellInRow = cellIndex === 0;

const titleFromRow = getModalTitleFromRowHeader(row);
const titleFromColumnOrRow =
getModalTitleFromColumnHeader(cellIndex) ||
getModalTitleFromRowHeader(row);

const cellReplacementData =
(tableCellData.cellId &&
cellReplacements?.[tableCellData.cellId]) ||
{};

const cellProps = {
...tableCellData,
...cellReplacementData,
...{
openModal,
modalTitle: isFirstCellInRow
? titleFromRow
: titleFromColumnOrRow,
align: isFirstCellInRow ? 'left' : 'center',
},
} as TableCellData;

return !hideColumns.includes(cellIndex) && (
<TableCell
isFirstCellInRow={isFirstCellInRow}
key={key}
{...cellProps}
/>
);
})}
</tr>
);
})}
{tableCellRows.map(
(row, rowIndex) =>
rowIndex > 0 && (
<tr key={rowIndex} className={styles.tr}>
{row.map((tableCellData, cellIndex) => {
const key = `${rowIndex}-${cellIndex}`;
const isFirstCellInRow = cellIndex === 0;

const titleFromRow = getModalTitleFromRowHeader(row);
const titleFromColumnOrRow =
getModalTitleFromColumnHeader(cellIndex) ||
getModalTitleFromRowHeader(row);

const cellReplacementData =
(tableCellData.cellId &&
cellReplacements?.[tableCellData.cellId]) ||
{};

const cellProps = {
...tableCellData,
...cellReplacementData,
...{
openModal,
modalTitle: isFirstCellInRow
? titleFromRow
: titleFromColumnOrRow,
align: isFirstCellInRow ? 'left' : 'center',
},
} as TableCellData;

return (
!hideColumns.includes(cellIndex) && (
<TableCell
isFirstCellInRow={isFirstCellInRow}
key={key}
{...cellProps}
/>
)
);
})}
</tr>
)
)}
</tbody>
</table>
);
};

export type { TableCellProps };

export { TableSection };
11 changes: 9 additions & 2 deletions src/lib/components/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { CTACellProps } from './components/TableCell/CTACell/CTACell';
import { ButtonCellProps } from './components/TableCell/ButtonCell/ButtonCell';
import { CardCellProps } from './components/TableCell/CardCell/CardCell';

type DefaultCellProps = { colSpan?: number };
type DefaultCellProps = {
cellId?: string;
colSpan?: number;
};

type BaseCellData = BaseCellProps & { type?: undefined } & DefaultCellProps;
type CTACellData = CTACellProps & { type: 'CTA' } & DefaultCellProps;
type ButtonCellData = ButtonCellProps & { type: 'BUTTON' } & DefaultCellProps;
type CardCellData = CardCellProps & { type: 'CARD' } & DefaultCellProps;

export type TableCellData = BaseCellData | CTACellData | ButtonCellData | CardCellData;
export type TableCellData =
| BaseCellData
| CTACellData
| ButtonCellData
| CardCellData;

export const isBaseCell = (
tableCellData: TableCellData
Expand Down
Loading