wealth/packages/island/src/molecules/list-row/ListRow.tsx

180 lines
4.4 KiB
TypeScript

import NextLink from 'next/link';
import { FlexBox } from '../../atoms/flex-box';
import { GridBox } from '../../atoms/grid-box';
import { Icon } from '../../atoms/icon';
import { Typography } from '../../atoms/typography';
import { Element } from '../../primitives/element';
import { cn } from '../../tools/cn';
import type { ListRowProps } from './types';
import { listRowIconVariants, listRowVariants } from './variants';
/**
* One row of a List (DS ListRow): a lead (avatar or a 40px icon tile), the title with an optional
* subtitle and meta, an end column (an Amount, a StatusBadge, a caption) and a chevron for a link.
* With `href` the whole row is a link, with `onClick` a button (such as one that opens a detail
* sheet), otherwise a plain row. Renders its own `li`, so it always sits in a List. Every text
* comes from the caller.
*/
export function ListRow(props: ListRowProps) {
const {
title,
subtitle,
meta,
children,
leading,
icon,
iconTone,
end,
keepEnd = false,
chevron,
unread = false,
unreadLabel,
dimmed = false,
disabled = false,
selected = false,
className,
} = props;
const hasSecondLine = subtitle != null || meta != null || children != null;
const showChevron = chevron ?? props.href !== undefined;
const classes = cn(
listRowVariants({
unread,
dimmed,
disabled,
selected,
interactive: props.href !== undefined || props.onClick !== undefined,
}),
className,
);
const layout = {
alignItems: hasSecondLine ? 'items-start' : 'items-center',
gap: 'gap-3',
'aria-current': selected || undefined,
} as const;
const content = (
<>
{unread && (
<Element
tag="span"
role={unreadLabel ? 'img' : undefined}
aria-label={unreadLabel}
aria-hidden={unreadLabel ? undefined : true}
className="absolute start-1.5 top-4.5 size-1.5 rounded-full bg-action-primary"
/>
)}
{leading ??
(icon && (
<GridBox
tag="span"
aria-hidden
justifyItems="justify-items-center"
alignItems="items-center"
className={listRowIconVariants({ tone: iconTone })}
>
<Icon name={icon} size={20} />
</GridBox>
))}
<FlexBox
tag="span"
direction="flex-col"
className="min-w-0 flex-1 gap-0.5"
>
<Typography
tag="span"
variant="inherit"
color="inherit"
className="text-body-compact leading-6 font-semibold"
>
{title}
</Typography>
{subtitle != null && (
<Typography
tag="span"
variant="body-sm"
color="secondary"
className="leading-[1.375rem]"
>
{subtitle}
</Typography>
)}
{children}
{meta != null && (
<Typography
tag="span"
variant="inherit"
color="tertiary"
className="text-[0.75rem] leading-5"
>
{meta}
</Typography>
)}
</FlexBox>
{end != null && (
<FlexBox
tag="span"
direction="flex-col"
alignItems="items-end"
gap="gap-1"
className="flex-none"
data-keep={keepEnd || undefined}
>
{end}
</FlexBox>
)}
{showChevron && (
<Icon
name="chevron-left"
size={20}
className="self-center text-text-tertiary"
/>
)}
</>
);
let row;
if (props.href !== undefined && !disabled) {
row = (
<FlexBox
tag={NextLink}
href={props.href}
onClick={props.onClick}
{...layout}
className={classes}
>
{content}
</FlexBox>
);
} else if (props.href === undefined && props.onClick !== undefined) {
row = (
<FlexBox
tag="button"
type="button"
onClick={props.onClick}
disabled={disabled}
{...layout}
className={classes}
>
{content}
</FlexBox>
);
} else {
row = (
<FlexBox
aria-disabled={disabled || undefined}
{...layout}
className={classes}
>
{content}
</FlexBox>
);
}
return (
<Element tag="li" className="min-w-0">
{row}
</Element>
);
}