Skip to content

Commit

Permalink
fully reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenStruchev committed Jul 20, 2023
1 parent 3357f30 commit 763ae20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
62 changes: 15 additions & 47 deletions src/components/Breakdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,25 @@ const BreakdownItem = ({
color = "default",
...rest
}) => {
const containerRef = useRef(null);
const textRef = useRef(null);
const [rowWidthExceeds20Percent, setRowWidthExceeds20Percent] = useState(false);
const dateOrInfoExist = !!date || !!info;
const currency = useContext(CurrencyContext);

useEffect(() => {
const containerElement = containerRef.current;
const containerRect = containerElement.getBoundingClientRect();
const containerWidth = containerRect.width;

const textElement = textRef.current;
const textRect = textElement.getBoundingClientRect();
const textWidth = textRect.width;

const percentage = (textWidth / containerWidth) * 100;
if (!rowWidthExceeds20Percent) {
setRowWidthExceeds20Percent(percentage > 20);
}
}, [info, date]);

return (
<tr className={clsx("ui-breakdown-item", colors[color], className)} {...rest}>
<td ref={containerRef} colSpan={2} className="text-left leading-none">
<span className="mr-0.5">{methodIcon}</span>
<span ref={textRef} className={clsx(!rowWidthExceeds20Percent && "whitespace-nowrap")}>
{children}
</span>
<span className="ml-1 text-sm">
{secondary && <span className="empty:hidden">{secondary}</span>}
{rowWidthExceeds20Percent && dateOrInfoExist ? (
<p className="mt-1.5 mb-1 flex items-center">
{info && (
<span className="mr-2 rounded bg-white p-1 uppercase text-black empty:hidden">
{info}
</span>
)}

{date && <span className="empty:hidden">{date}</span>}
</p>
) : (
<>
{info && (
<span className="mr-2 rounded bg-white p-1 uppercase text-black empty:hidden">
{info}
</span>
)}
{date && <span className="empty:hidden">{date}</span>}
</>
)}
</span>
<td colSpan={2} className="text-left leading-none">
<div className="flex flex-wrap items-center leading-3.5">
<span className="break-word">
<span className="mr-0.5">{methodIcon}</span>
{children}
</span>
<span className="ml-1 text-sm">
{secondary && <span className="empty:hidden">{secondary}</span>}
{info && (
<span className="mr-2 ml-1 rounded bg-white p-1 uppercase leading-6 text-black empty:hidden">
{info}
</span>
)}
</span>
</div>
</td>

<td className="w-[1%] whitespace-nowrap pl-4 text-right">
Expand Down
10 changes: 5 additions & 5 deletions src/stories/Other/Breakdown.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Default = () => {
<Breakdown currency="USD">
<Breakdown.Item value={100}>Line item caption</Breakdown.Item>
<Breakdown.Item value={29} secondary="($29.00 x 1)">
Children
Very long really Children
</Breakdown.Item>
<Breakdown.Item value={29} secondary="($29.00 x 1)">
Adults
Expand All @@ -42,16 +42,16 @@ export const Default = () => {
</Button>
</Breakdown.SubtotalItem>

<Breakdown.Item value={1230} date="12/18/2019" info="*0259" methodIcon={<CardIcon />}>
<Breakdown.Item value={1230} secondary="12/18/2019" info="*0259" methodIcon={<CardIcon />}>
Payment
</Breakdown.Item>

<Breakdown.Item color="primary" info="*0259" date="07/23/2021" value={-62} methodIcon={<CardIcon />}>
<Breakdown.Item color="primary" info="*0259" secondary="07/23/2021" value={-62} methodIcon={<CardIcon />}>
Return Payment
</Breakdown.Item>

<Breakdown.Item date="07/23/2021" info={<EmptyComponent />} value={0} methodIcon={<CardIcon />}>
This is a really long message that should wrap somehow
<Breakdown.Item secondary="07/23/2021" info={<EmptyComponent />} value={0} methodIcon={<CardIcon />}>
This is a really long message that should wrap somehow more long
</Breakdown.Item>

<Breakdown.Separator />
Expand Down

0 comments on commit 763ae20

Please sign in to comment.