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

LegendCategories: support for custom markers #451

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Not released

- LegendCategories: support for custom markers [#451](https://github.com/CartoDB/carto-react/pull/451)

## 1.4
## 1.3

### 1.3.0 (2022-07-11)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,35 @@ describe('LegendCategories', () => {
});
test('renders colors (CARTOColors) correctly', () => {
render(<LegendCategories legend={DEFAULT_LEGEND} />);
const elements = document.querySelectorAll('[class*="circle"]');
const elements = document.querySelectorAll('[class*="marker"]');
getPalette(COLOR, 2).forEach((color, idx) =>
expect(elements[idx]).toHaveStyle(`background-color: ${color}`)
);
});
test('renders colors (hex) correctly', () => {
render(<LegendCategories legend={{ ...DEFAULT_LEGEND, colors: ['#000', '#fff'] }} />);
const [firstCategory, secondCategory] =
document.querySelectorAll('[class*="circle"]');
document.querySelectorAll('[class*="marker"]');
expect(firstCategory).toHaveStyle('background-color: #000;');
expect(secondCategory).toHaveStyle('background-color: #fff;');
});
test('renders stroked colors correctly', () => {
render(<LegendCategories legend={{ ...DEFAULT_LEGEND, isStrokeColor: true }} />);
const elements = document.querySelectorAll('[class*="circle"]');
const elements = document.querySelectorAll('[class*="marker"]');
getPalette(COLOR, 2).forEach((color, idx) =>
expect(elements[idx]).toHaveStyle(`border-color: ${color}`)
);
});
test('renders icons correctly', () => {
render(
<LegendCategories
legend={{ ...DEFAULT_LEGEND, customMarkers: 'https://xyz.com/x.png' }}
/>
);
const elements = document.querySelectorAll('[class*="marker"]');
getPalette(COLOR, 2).forEach((color, idx) => {
expect(elements[idx]).toHaveStyle(`mask-image: url(https://xyz.com/x.png)`);
expect(elements[idx]).toHaveStyle(`background-color: ${color}`);
});
});
});
35 changes: 29 additions & 6 deletions packages/react-ui/src/widgets/legend/LegendCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getPalette } from '../../utils/palette';
import PropTypes from 'prop-types';

function LegendCategories({ legend }) {
const { labels = [], colors = [], isStrokeColor = false } = legend;
const { labels = [], colors = [], isStrokeColor = false, customMarkers } = legend;

const palette = getPalette(colors, labels.length);

Expand All @@ -14,6 +14,9 @@ function LegendCategories({ legend }) {
isMax={false}
label={label}
color={palette[idx]}
icon={
customMarkers && Array.isArray(customMarkers) ? customMarkers[idx] : customMarkers
}
isStrokeColor={isStrokeColor}
/>
));
Expand Down Expand Up @@ -42,6 +45,10 @@ LegendCategories.propTypes = {
legend: PropTypes.shape({
labels: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
colors: PropTypes.oneOfType([PropTypes.arrayOf(ColorType), PropTypes.string]),
customMarkers: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.string
]),
isStrokeColor: PropTypes.bool
}).isRequired
};
Expand All @@ -56,14 +63,16 @@ const useStyles = makeStyles((theme) => ({
'& $circle': {}
}
},
circle: {
marker: {
whiteSpace: 'nowrap',
display: 'block',
width: '12px',
height: '12px',
borderRadius: '50%',
position: 'relative',
border: '2px solid transparent',
border: '2px solid transparent'
},
circle: {
'&::after': {
position: 'absolute',
display: ({ isMax }) => (isMax ? 'block' : 'none'),
Expand All @@ -76,6 +85,10 @@ const useStyles = makeStyles((theme) => ({
boxSizing: 'content-box'
}
},
icon: {
maskRepeat: 'no-repeat',
maskSize: 'cover'
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@VictorVelarde
So update on story:

  • yes, MUI adds proper prefixes, when used in makeStyles like above!
  • but something else (react/mui?) also requires Webkit prefixes ... and filters for inline style={{...}}

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, I didn't notice those were inline styles and they were excluded. This way is better, thx!

flexParent: {
display: 'flex',
alignItems: 'center'
Expand All @@ -94,7 +107,7 @@ const useStyles = makeStyles((theme) => ({
}
}));

function Row({ label, isMax, isStrokeColor, color = '#000' }) {
function Row({ label, isMax, isStrokeColor, color = '#000', icon }) {
const classes = useStyles({ isMax });

const [showTooltip, setShowTooltip] = useState(false);
Expand All @@ -121,8 +134,18 @@ function Row({ label, isMax, isStrokeColor, color = '#000' }) {
<Box
mr={1.5}
component='span'
className={classes.circle}
style={isStrokeColor ? { borderColor: color } : { backgroundColor: color }}
className={[classes.marker, icon ? classes.icon : classes.circle].join(' ')}
style={
icon
? {
backgroundColor: color,
maskImage: `url(${icon})`,
WebkitMaskImage: `url(${icon})`
}
: isStrokeColor
? { borderColor: color }
: { backgroundColor: color }
}
/>
</Tooltip>
<Typography ref={labelRef} variant='overline' className={classes.longTruncate}>
Expand Down