Skip to content

Commit 51ae7af

Browse files
committed
react-app: Update colorBar style and allow user input total value
refs oursky#128
1 parent 3980247 commit 51ae7af

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

react-app/src/components/common/ColorBar/ColorBar.tsx

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,32 @@ const ColorBarSection: React.FC<ColorBarSectionProps> = (props) => {
3131
});
3232

3333
const percentage = useMemo(() => {
34+
if (total.isZero()) {
35+
return 0;
36+
}
3437
return `${data.value.div(total).shiftedBy(2).toFixed(1)}%`;
3538
}, [data, total]);
3639

3740
const handlePercentageDisplay = useCallback(() => {
3841
if (percentageEl) {
3942
const parentEle = percentageEl.parentElement;
43+
44+
if (
45+
parentEle &&
46+
parentEle.offsetWidth === 0 &&
47+
percentageEl.offsetWidth !== 0
48+
) {
49+
// Component is still being initialized
50+
return;
51+
}
52+
4053
if (
4154
!showPercentage ||
4255
(parentEle && parentEle.offsetWidth < percentageEl.offsetWidth)
4356
) {
4457
percentageEl.hidden = true;
58+
} else {
59+
percentageEl.hidden = false;
4560
}
4661
}
4762
}, [percentageEl, showPercentage]);
@@ -108,34 +123,52 @@ const ColorBarSection: React.FC<ColorBarSectionProps> = (props) => {
108123
interface ColorBarProps {
109124
className?: string;
110125
showPercentage?: boolean;
126+
total?: BigNumber;
111127
data: ColorBarData[];
112128
}
113129

114130
const ColorBar: React.FC<ColorBarProps> = (props) => {
115-
const { className, data, showPercentage } = props;
131+
const { className, data, showPercentage, total } = props;
116132

117-
const total = useMemo(
133+
const calculatedTotal = useMemo(
118134
() =>
135+
total ??
119136
data.reduce((total, data) => total.plus(data.value), new BigNumber(0)),
120-
[data]
137+
[data, total]
121138
);
122139

140+
// Using grid to overlap the bars without using relative which breaks the tooltips
123141
return (
124-
<div className={cn("flex", "h-full", "overflow-hidden", className)}>
125-
{!total.eq(0) ? (
126-
data.map((data, index) => (
142+
<div
143+
className={cn(
144+
"grid",
145+
"grid-rows-1",
146+
"grid-cols-1",
147+
"h-full",
148+
"overflow-hidden",
149+
className
150+
)}
151+
>
152+
<div
153+
className={cn(
154+
"row-start-1",
155+
"col-start-1",
156+
"block",
157+
"h-full",
158+
"w-full",
159+
"bg-likecoin-grey"
160+
)}
161+
/>
162+
<div className={cn("row-start-1", "col-start-1", "flex")}>
163+
{data.map((data, index) => (
127164
<ColorBarSection
128165
key={index}
129166
data={data}
130-
total={total}
167+
total={calculatedTotal}
131168
showPercentage={showPercentage}
132169
/>
133-
))
134-
) : (
135-
<div
136-
className={cn("inline-block", "h-full", "w-full", "bg-likecoin-grey")}
137-
/>
138-
)}
170+
))}
171+
</div>
139172
</div>
140173
);
141174
};

0 commit comments

Comments
 (0)