Skip to content

Commit

Permalink
feat(Meter): added extra prop (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyunreal authored Feb 17, 2025
1 parent b5e4739 commit 631591b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 21 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 27 additions & 20 deletions src/__screenshot_tests__/meter-screenshot-test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import {openStoryPage, screen} from '../test-utils';

test.each`
values | type | themeVariant
${[0]} | ${'linear'} | ${'default'}
${[0]} | ${'circular'} | ${'default'}
${[0]} | ${'angular'} | ${'default'}
${[100]} | ${'linear'} | ${'default'}
${[100]} | ${'circular'} | ${'default'}
${[100]} | ${'angular'} | ${'default'}
${[33, 33]} | ${'linear'} | ${'default'}
${[33, 33]} | ${'circular'} | ${'default'}
${[33, 33]} | ${'angular'} | ${'default'}
${[33, 33]} | ${'linear'} | ${'inverse'}
${[33, 33]} | ${'circular'} | ${'inverse'}
${[33, 33]} | ${'angular'} | ${'inverse'}
${[33, 33]} | ${'linear'} | ${'media'}
${[33, 33]} | ${'circular'} | ${'media'}
${[33, 33]} | ${'angular'} | ${'media'}
${[20, 20, 20, 20, 0]} | ${'linear'} | ${'default'}
${[20, 20, 20, 20, 0]} | ${'circular'} | ${'default'}
${[20, 20, 20, 20, 0]} | ${'angular'} | ${'default'}
`('Meter $themeVariant $type $values', async ({themeVariant, values, type}) => {
values | type | themeVariant | extraContent
${[0]} | ${'linear'} | ${'default'} | ${''}
${[0]} | ${'circular'} | ${'default'} | ${''}
${[0]} | ${'angular'} | ${'default'} | ${''}
${[100]} | ${'linear'} | ${'default'} | ${''}
${[100]} | ${'circular'} | ${'default'} | ${''}
${[100]} | ${'angular'} | ${'default'} | ${''}
${[33, 33]} | ${'linear'} | ${'default'} | ${''}
${[33, 33]} | ${'circular'} | ${'default'} | ${''}
${[33, 33]} | ${'angular'} | ${'default'} | ${''}
${[33, 33]} | ${'linear'} | ${'inverse'} | ${''}
${[33, 33]} | ${'circular'} | ${'inverse'} | ${''}
${[33, 33]} | ${'angular'} | ${'inverse'} | ${''}
${[33, 33]} | ${'linear'} | ${'media'} | ${''}
${[33, 33]} | ${'circular'} | ${'media'} | ${''}
${[33, 33]} | ${'angular'} | ${'media'} | ${''}
${[20, 20, 20, 20, 0]} | ${'linear'} | ${'default'} | ${''}
${[20, 20, 20, 20, 0]} | ${'circular'} | ${'default'} | ${''}
${[20, 20, 20, 20, 0]} | ${'angular'} | ${'default'} | ${''}
${[33, 33]} | ${'linear'} | ${'default'} | ${'This is a small extra'}
${[33, 33]} | ${'linear'} | ${'default'} | ${'This is a super long extra This is a super long extra This is a super long extra This is a super long extra'}
${[33, 33]} | ${'angular'} | ${'default'} | ${'This is a small extra'}
${[33, 33]} | ${'angular'} | ${'default'} | ${'This is a super long extra This is a super long extra This is a super long extra This is a super long extra'}
${[33, 33]} | ${'circular'} | ${'default'} | ${'This is a small extra'}
${[33, 33]} | ${'circular'} | ${'default'} | ${'This is a super long extra This is a super long extra This is a super long extra This is a super long extra'}
`('Meter $themeVariant $type $values', async ({themeVariant, values, type, extraContent}) => {
await openStoryPage({
id: 'components-data-visualizations-meter--meter-story',
args: {
Expand All @@ -35,6 +41,7 @@ test.each`
}),
{}
),
extraContent,
},
});

Expand Down
25 changes: 25 additions & 0 deletions src/__stories__/meter-story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import beachImg from './images/beach.jpg';

import type {MeterType} from '../meter';

type MeterExtraAlignment = 'start' | 'center' | 'end';

export default {
title: 'Components/Data Visualizations/Meter',
argTypes: {
Expand Down Expand Up @@ -49,6 +51,10 @@ export default {
value8: {
control: {type: 'range', min: 0, max: 100, step: 1},
},
extraAlignment: {
options: ['start', 'center', 'end'] as Array<MeterExtraAlignment>,
control: {type: 'select'},
},
},
parameters: {
fullScreen: true,
Expand All @@ -71,6 +77,8 @@ type MeterStoryArgs = {
value6: number;
value7: number;
value8: number;
extraContent: string;
extraAlignment: MeterExtraAlignment;
};

export const MeterStory: StoryComponent<MeterStoryArgs> = ({
Expand All @@ -81,6 +89,8 @@ export const MeterStory: StoryComponent<MeterStoryArgs> = ({
fullWidth,
width,
ariaLabel,
extraContent,
extraAlignment,
...valuesArgs
}) => {
const values = Object.values(valuesArgs).slice(0, valuesCount);
Expand All @@ -100,6 +110,19 @@ export const MeterStory: StoryComponent<MeterStoryArgs> = ({
reverse={reverse}
values={values}
width={fullWidth ? '100%' : width}
extra={
extraContent ? (
<div
style={{
alignSelf: extraAlignment,
justifyContent: 'center',
...(type === 'linear' ? {} : {margin: '0 auto'}),
}}
>
<div>{extraContent}</div>
</div>
) : undefined
}
/>
</Box>
</ResponsiveLayout>
Expand All @@ -124,4 +147,6 @@ MeterStory.args = {
value6: 10,
value7: 10,
value8: 10,
extraContent: 'This is a small extra',
extraAlignment: 'center',
};
22 changes: 21 additions & 1 deletion src/meter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type MeterProps = {
colors?: Array<string>;
reverse?: boolean;
dataAttributes?: DataAttributes;
extra?: React.ReactNode;
'aria-hidden'?: boolean | 'true' | 'false';
'aria-label'?: string;
'aria-labelledby'?: string;
Expand All @@ -143,6 +144,7 @@ const MeterComponent = ({
'aria-hidden': ariaHidden = false,
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
extra,
}: MeterProps): JSX.Element => {
const {borderRadii, t} = useTheme();
const {ref: containerRef, width: containerWidth} = useElementDimensions();
Expand Down Expand Up @@ -281,10 +283,27 @@ const MeterComponent = ({
' ' +
values.map((v, i) => `${t(meterSectionLabel, i + 1, Math.round(v * 100))}`).join('. ');

const extraStyle = React.useMemo(() => {
if (type === TYPE_LINEAR) {
return {display: 'flex'};
}
const COS_45 = 0.707107;
const sizeFactor = (width / 2 - STROKE_WIDTH_PX) * COS_45;
return {
display: 'flex',
marginTop:
-1 * (type === TYPE_ANGULAR ? sizeFactor + STROKE_WIDTH_PX / 2 : sizeFactor + width / 2),
width: sizeFactor * 2,
minHeight: type === TYPE_ANGULAR ? sizeFactor + STROKE_WIDTH_PX / 2 : sizeFactor * 2,
marginLeft: 'auto',
marginRight: 'auto',
};
}, [width, type]);

return (
<div
ref={containerRef}
style={{width: widthProp}}
style={{width: widthProp, minHeight: height}}
role="meter"
aria-label={ariaLabel || (ariaLabelledBy ? undefined : valueText)}
aria-labelledby={ariaLabelledBy}
Expand Down Expand Up @@ -480,6 +499,7 @@ const MeterComponent = ({
);
})}
</svg>
{extra && <div style={extraStyle}>{extra}</div>}
</div>
);
};
Expand Down

1 comment on commit 631591b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-kaitlaome-flows-projects-65bb050e.vercel.app

Built with commit 631591b.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.