Skip to content

Commit d23fb22

Browse files
authored
feat: add showMean prop to RangeWithValue component (#306)
1 parent bd6e57c commit d23fb22

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/RangeWithValue/index.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export default {
1919
rangeType: {
2020
control: { type: 'select', options: ['closed', 'open-ended'] },
2121
},
22+
showMean: {
23+
control: { type: 'boolean' },
24+
},
2225
},
2326
};
2427

@@ -27,6 +30,7 @@ const Template: Story<{
2730
expectedMax: number;
2831
actualValue: number;
2932
rangeType: 'closed' | 'open-ended';
33+
showMean: boolean;
3034
}> = function Template(args) {
3135
return (
3236
<div style={{ width: 300 }}>
@@ -41,4 +45,5 @@ Default.args = {
4145
expectedMax: 100,
4246
actualValue: 50,
4347
rangeType: 'closed',
48+
showMean: false,
4449
};

src/RangeWithValue/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type RangeWithValueProps = {
2323
actualValue: number;
2424
rangeType: RangeWithValueType;
2525
bufferPercentage?: number;
26+
showMean?: boolean;
2627
};
2728

2829
export function RangeWithValue({
@@ -31,6 +32,7 @@ export function RangeWithValue({
3132
actualValue,
3233
rangeType,
3334
bufferPercentage = 0.1,
35+
showMean,
3436
}: RangeWithValueProps) {
3537
const theme = useTheme();
3638

@@ -69,11 +71,22 @@ export function RangeWithValue({
6971
theme,
7072
});
7173

74+
const meanValue = (expectedMin + expectedMax) / 2;
75+
const meanLabelWidth = 28;
76+
7277
return (
7378
<Container>
7479
<Scale>
7580
<RangeLine left={`${percentage(expectedMin)}%`} />
7681
<RangeLine left={`${percentage(expectedMax)}%`} />
82+
{showMean && (
83+
<Label
84+
left={`calc(${percentage(meanValue)}% - ${meanLabelWidth / 2}px)`}
85+
>
86+
{meanValue.toFixed(2)}
87+
</Label>
88+
)}
89+
7790
<Tooltip
7891
title={(() => {
7992
if (isOutOfRange) {

0 commit comments

Comments
 (0)