Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate 'LegendProportion' component from makeStyles to styled-components + cleanup #635

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 35 additions & 58 deletions packages/react-ui/src/widgets/legend/LegendProportion.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
import React from 'react';
import { Box, Grid } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import { Box, Grid, styled } from '@mui/material';
import PropTypes from 'prop-types';
import React from 'react';
import Typography from '../../components/atoms/Typography';

const useStyles = makeStyles((theme) => ({
circles: {
position: 'relative',
marginBottom: 4
},
avg: {
width: 12,
height: 2,
const sizes = {
0: 12,
1: 9,
2: 6,
3: 3
};

const Circle = styled(Box, {
shouldForwardProp: (prop) => prop !== 'index'
})(({ index = 0, theme }) => {
const width = theme.spacing(sizes[index]);
const height = theme.spacing(sizes[index]);

return {
border: `solid 1px ${theme.palette.grey[100]}`,
backgroundColor: theme.palette.grey[50],
borderRadius: '50%',
position: 'absolute',
// TODO change color
border: `1px solid ${theme.palette.grey[900]}`,
borderRadius: 4,
backgroundColor: theme.palette.grey[900],
right: 0,
boxSizing: 'border-box'
},
errorContainer: {
maxWidth: 240
}
bottom: 0,
width,
height
};
});

const ProportionalGrid = styled(Grid)(({ theme: { spacing } }) => ({
justifyContent: 'flex-end',
marginBottom: spacing(0.5),
position: 'relative'
}));

function LegendProportion({ legend }) {
const classes = useStyles();

const { min, max, error } = calculateRange(legend);
const [step1, step2] = !error ? calculateSteps(min, max) : [0, 0];

return (
<Grid container item direction='row' spacing={2} data-testid='proportion-legend'>
<Grid container item xs={6} justifyContent='flex-end' className={classes.circles}>
<Circle index={0}></Circle>
<Circle index={1}></Circle>
<Circle index={2}></Circle>
<Circle index={3}></Circle>
</Grid>
<ProportionalGrid container item xs={6}>
{[...Array(4)].map((circle, index) => (
<Circle index={index} component='span'></Circle>
))}
</ProportionalGrid>
<Grid
container
item
Expand All @@ -48,7 +54,7 @@ function LegendProportion({ legend }) {
spacing={1}
>
{error ? (
<Grid item className={classes.errorContainer}>
<Grid item maxWidth={240}>
<Typography variant='overline'>
You need to specify valid numbers for the labels property
</Typography>
Expand Down Expand Up @@ -122,32 +128,3 @@ function calculateSteps(min, max) {

return [step1, step2];
}

const useStylesCircle = makeStyles((theme) => ({
circle: {
border: `solid 1px ${theme.palette.grey[100]}`,
backgroundColor: theme.palette.grey[50],
borderRadius: '50%',
position: 'absolute',
right: 0,
bottom: 0
}
}));

function Circle({ index = 0 }) {
const classes = useStylesCircle();

const sizes = {
0: 96,
1: 72,
2: 48,
3: 24
};

const width = sizes[index];
const height = sizes[index];

return (
<Box component='span' className={classes.circle} style={{ width, height }}></Box>
);
}