Skip to content

Commit

Permalink
refactor(table): [NoTicket] Fix and clean up types (#369)
Browse files Browse the repository at this point in the history
* refactor(table): [NoTicket] Fix and clean up types

* refactor(table): [NoTicket] Use arrow function
  • Loading branch information
kimkwanka authored Jul 24, 2024
1 parent f39d4fe commit 2ea81dc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 66 deletions.
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

0 comments on commit 2ea81dc

Please sign in to comment.