Skip to content

Commit

Permalink
Migrate 'LegendWidgetUI' component from makeStyles to styled-componen…
Browse files Browse the repository at this point in the history
…t + cleanup (#637)
  • Loading branch information
jantolg authored Apr 28, 2023
1 parent eee9346 commit 738ee11
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- LegendCategories component migrated from makeStyles to styled-components + cleanup [#634](https://github.com/CartoDB/carto-react/pull/634)
- LegendProportion component migrated from makeStyles to styled-components + cleanup [#635](https://github.com/CartoDB/carto-react/pull/635)
- LegendRamp component migrated from makeStyles to styled-components + cleanup [#636](https://github.com/CartoDB/carto-react/pull/636)
- LegendWidgetUI component migrated from makeStyles to styled-components + cleanup [#637](https://github.com/CartoDB/carto-react/pull/637)

## 2.0

Expand Down
2 changes: 1 addition & 1 deletion packages/react-ui/src/widgets/legend/LegendProportion.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function LegendProportion({ legend }) {
<Grid container item direction='row' spacing={2} data-testid='proportion-legend'>
<ProportionalGrid container item xs={6}>
{[...Array(4)].map((circle, index) => (
<Circle index={index} component='span'></Circle>
<Circle key={index} index={index} component='span'></Circle>
))}
</ProportionalGrid>
<Grid
Expand Down
80 changes: 34 additions & 46 deletions packages/react-ui/src/widgets/legend/LegendWidgetUI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { createRef, Fragment } from 'react';
import { Box, Button, Collapse, Divider, Grid, SvgIcon } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import { Box, Button, Collapse, Divider, Grid, styled, SvgIcon } from '@mui/material';
import LegendWrapper from './LegendWrapper';
import LegendCategories from './LegendCategories';
import LegendIcon from './LegendIcon';
Expand Down Expand Up @@ -28,13 +27,11 @@ const LayersIcon = () => (
</SvgIcon>
);

const useStyles = makeStyles((theme) => ({
legend: {
minWidth: '240px',
backgroundColor: theme.palette.background.paper,
boxShadow: theme.shadows[1],
borderRadius: 4
}
const LegendBox = styled(Box)(({ theme }) => ({
minWidth: theme.spacing(30),
backgroundColor: theme.palette.background.paper,
boxShadow: theme.shadows[1],
borderRadius: theme.spacing(0.5)
}));

function LegendWidgetUI({
Expand All @@ -49,11 +46,10 @@ function LegendWidgetUI({
onChangeLegendRowCollapsed,
title
}) {
const classes = useStyles();
const isSingle = layers.length === 1;

return (
<Box className={`${classes.legend} ${className}`}>
<LegendBox className={className}>
<LegendContainer
isSingle={isSingle}
collapsed={collapsed}
Expand All @@ -69,7 +65,7 @@ function LegendWidgetUI({
onChangeCollapsed={onChangeLegendRowCollapsed}
/>
</LegendContainer>
</Box>
</LegendBox>
);
}

Expand All @@ -96,22 +92,25 @@ LegendWidgetUI.propTypes = {

export default LegendWidgetUI;

const useStylesLegendContainer = makeStyles((theme) => ({
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
height: '36px'
},
button: {
flex: '1 1 auto',
justifyContent: 'space-between',
padding: theme.spacing(0.75, 1.25, 0.75, 3),
borderTop: ({ collapsed }) =>
collapsed ? 'none' : `1px solid ${theme.palette.divider}`,
cursor: 'pointer'
},
wrapperInner: {
const Header = styled(Grid)(({ theme }) => ({
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
height: theme.spacing(4.5)
}));

const HeaderButton = styled(Button, {
shouldForwardProp: (prop) => prop !== 'collapsed'
})(({ theme, collapsed }) => ({
flex: '1 1 auto',
justifyContent: 'space-between',
padding: theme.spacing(0.75, 1.25, 0.75, 3),
borderTop: collapsed ? 'none' : `1px solid ${theme.palette.divider}`,
cursor: 'pointer'
}));

const CollapseWrapper = styled(Collapse)(() => ({
'.MuiCollapse-wrapperInner': {
maxHeight: 'max(350px, 80vh)',
overflowY: 'auto',
overflowX: 'hidden'
Expand All @@ -120,9 +119,6 @@ const useStylesLegendContainer = makeStyles((theme) => ({

function LegendContainer({ isSingle, children, collapsed, onChangeCollapsed, title }) {
const wrapper = createRef();
const classes = useStylesLegendContainer({
collapsed
});

const handleExpandClick = () => {
if (onChangeCollapsed) onChangeCollapsed(!collapsed);
Expand All @@ -132,26 +128,18 @@ function LegendContainer({ isSingle, children, collapsed, onChangeCollapsed, tit
children
) : (
<>
<Collapse
ref={wrapper}
in={!collapsed}
timeout='auto'
unmountOnExit
classes={{
wrapperInner: classes.wrapperInner
}}
>
<CollapseWrapper ref={wrapper} in={!collapsed} timeout='auto' unmountOnExit>
{children}
</Collapse>
<Grid container className={classes.header}>
<Button
className={classes.button}
</CollapseWrapper>
<Header container>
<HeaderButton
collapsed={collapsed}
endIcon={<LayersIcon />}
onClick={handleExpandClick}
>
<Typography variant='subtitle1'>{title}</Typography>
</Button>
</Grid>
</HeaderButton>
</Header>
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState } from 'react';
import LegendWidgetUI from '../../../src/widgets/legend/LegendWidgetUI';
import CartoIcon from '../../assets/carto-symbol.svg';

const options = {
title: 'Organisms/Widgets/LegendWidgetUI',
Expand Down

0 comments on commit 738ee11

Please sign in to comment.