Skip to content

Commit

Permalink
chore(suite): update used address table to use Table component
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhavel committed Oct 8, 2024
1 parent 7babc8f commit 7919015
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 175 deletions.
19 changes: 5 additions & 14 deletions packages/components/src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default meta;

interface TableProps {
colWidths?: { minWidth?: string | undefined; maxWidth?: string | undefined }[];
highlightRowOnHover?: boolean;
isRowHighlightedOnHover?: boolean;
}

export const Table: StoryObj = {
Expand All @@ -33,7 +33,7 @@ export const Table: StoryObj = {
{EXAMPLE_TOKENS.map((token, i) => (
<TableComponent.Row
key={i}
onClick={props.highlightRowOnHover ? () => {} : undefined}
onClick={props.isRowHighlightedOnHover ? () => {} : undefined}
>
<TableComponent.Cell>{token.name}</TableComponent.Cell>
<TableComponent.Cell>{token.balance}</TableComponent.Cell>
Expand All @@ -46,7 +46,7 @@ export const Table: StoryObj = {
args: {
...getFramePropsStory(allowedTableFrameProps).args,
colWidths: 'none',
highlightRowOnHover: 'true',
isRowHighlightedOnHover: true,
},
argTypes: {
...getFramePropsStory(allowedTableFrameProps).argTypes,
Expand All @@ -64,18 +64,9 @@ export const Table: StoryObj = {
},
},
},
highlightRowOnHover: {
options: ['true', 'false'],
mapping: {
true: true,
false: false,
},
isRowHighlightedOnHover: {
control: {
type: 'select',
labels: {
true: 'true',
false: 'false',
},
type: 'boolean',
},
},
},
Expand Down
15 changes: 10 additions & 5 deletions packages/components/src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const allowedTableFrameProps = ['margin'] as const satisfies FramePropsKe
type AllowedFrameProps = Pick<FrameProps, (typeof allowedTableFrameProps)[number]>;

interface TableContextProps {
highlightRowOnHover: boolean;
isRowHighlightedOnHover: boolean;
}

const TableContext = createContext<TableContextProps>({ highlightRowOnHover: false });
const TableContext = createContext<TableContextProps>({ isRowHighlightedOnHover: false });

export const useTable = () => useContext(TableContext);

Expand All @@ -41,15 +41,20 @@ export type TableProps = AllowedFrameProps & {
minWidth?: string;
maxWidth?: string;
}[];
highlightRowOnHover?: boolean;
isRowHighlightedOnHover?: boolean;
};

export const Table = ({ children, margin, colWidths, highlightRowOnHover = false }: TableProps) => {
export const Table = ({
children,
margin,
colWidths,
isRowHighlightedOnHover = false,
}: TableProps) => {
const { scrollElementRef, onScroll, ShadowContainer, ShadowRight } = useScrollShadow();
const { parentElevation } = useElevation();

return (
<TableContext.Provider value={{ highlightRowOnHover }}>
<TableContext.Provider value={{ isRowHighlightedOnHover }}>
<ShadowContainer>
<ScrollContainer onScroll={onScroll} ref={scrollElementRef}>
<Container {...makePropsTransient({ margin })}>
Expand Down
5 changes: 1 addition & 4 deletions packages/components/src/components/Table/TableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { typography, spacingsPx, Elevation, mapElevationToBackground } from '@tr
import { useTableHeader } from './TableHeader';
import { UIHorizontalAlignment } from '../../config/types';
import { useElevation } from '../ElevationContext/ElevationContext';
import { useTable } from './Table';

export type TableCellProps = {
children?: ReactNode;
Expand All @@ -24,7 +23,7 @@ const mapAlignmentToJustifyContent = (align: UIHorizontalAlignment) => {
return map[align];
};

const Cell = styled.td<{ $isHeader: boolean; $elevation: Elevation; $highlight: boolean }>`
const Cell = styled.td<{ $isHeader: boolean; $elevation: Elevation }>`
${({ $isHeader }) => ($isHeader ? typography.hint : typography.body)}
color: ${({ theme, $isHeader }) => ($isHeader ? theme.textSubdued : theme.textDefault)};
text-align: left;
Expand All @@ -48,15 +47,13 @@ const Content = styled.div<{ $align: UIHorizontalAlignment }>`
export const TableCell = ({ children, colSpan = 1, align = 'left' }: TableCellProps) => {
const isHeader = useTableHeader();
const { parentElevation } = useElevation();
const { highlightRowOnHover } = useTable();

return (
<Cell
as={isHeader ? 'th' : 'td'}
colSpan={colSpan}
$isHeader={isHeader}
$elevation={parentElevation}
$highlight={highlightRowOnHover}
>
<Content $align={align}>{children}</Content>
</Cell>
Expand Down
22 changes: 16 additions & 6 deletions packages/components/src/components/Table/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useTableHeader } from './TableHeader';
export const Row = styled.tr<{
$elevation: Elevation;
$isCollapsed: boolean;
$highlight: boolean;
$isHighlighted: boolean;
$isHeader: boolean;
}>`
border-top: 1px solid ${mapElevationToBorder};
Expand All @@ -24,8 +24,8 @@ export const Row = styled.tr<{
border-top: 0;
}
${({ $highlight, theme, $elevation, $isHeader }) =>
$highlight &&
${({ $isHighlighted, theme, $elevation, $isHeader }) =>
$isHighlighted &&
!$isHeader &&
css`
&:hover {
Expand Down Expand Up @@ -68,21 +68,31 @@ export const Row = styled.tr<{
export interface TableRowProps {
children: ReactNode;
isCollapsed?: boolean;
isHighlightedOnHover?: boolean;
onClick?: () => void;
onHover?: (isHovering: boolean) => void;
}

export const TableRow = ({ children, isCollapsed = false, onClick }: TableRowProps) => {
export const TableRow = ({
children,
isCollapsed = false,
onClick,
onHover,
isHighlightedOnHover,
}: TableRowProps) => {
const { elevation } = useElevation();
const isHeader = useTableHeader();
const { highlightRowOnHover } = useTable();
const { isRowHighlightedOnHover } = useTable();

return (
<Row
$elevation={elevation}
$isCollapsed={isCollapsed}
$highlight={highlightRowOnHover}
$isHighlighted={isHighlightedOnHover ?? isRowHighlightedOnHover}
$isHeader={isHeader}
onClick={onClick}
onMouseEnter={() => onHover?.(true)}
onMouseLeave={() => onHover?.(false)}
>
{children}
</Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const AssetTable = ({
assetsData,
assetsFiatBalances,
}: AssetTableProps) => (
<Table highlightRowOnHover={true} margin={{ top: spacings.md, bottom: spacings.md }}>
<Table isRowHighlightedOnHover margin={{ top: spacings.md, bottom: spacings.md }}>
<Table.Header>
<Table.Row>
<Table.Cell colSpan={3}>
Expand Down
Loading

0 comments on commit 7919015

Please sign in to comment.