From 174e53ed514f9890734d7b343c13ea61a47b437f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Zag=C3=B3rski?= Date: Wed, 13 Jul 2022 13:26:33 +0200 Subject: [PATCH] LegendCategories: support for custom markers. --- .../widgets/legend/LegendCategories.test.js | 9 ++-- .../src/widgets/legend/LegendCategories.js | 49 +++++++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/packages/react-ui/__tests__/widgets/legend/LegendCategories.test.js b/packages/react-ui/__tests__/widgets/legend/LegendCategories.test.js index 59d43d768..f97a8c3d5 100644 --- a/packages/react-ui/__tests__/widgets/legend/LegendCategories.test.js +++ b/packages/react-ui/__tests__/widgets/legend/LegendCategories.test.js @@ -20,21 +20,22 @@ describe('LegendCategories', () => { }); test('renders colors (CARTOColors) correctly', () => { render(); - const elements = document.querySelectorAll('[class*="circle"]'); + const elements = document.querySelectorAll('[class*="markerCircle"]'); getPalette(COLOR, 2).forEach((color, idx) => expect(elements[idx]).toHaveStyle(`background-color: ${color}`) ); }); test('renders colors (hex) correctly', () => { render(); - const [firstCategory, secondCategory] = - document.querySelectorAll('[class*="circle"]'); + const [firstCategory, secondCategory] = document.querySelectorAll( + '[class*="markerCircle"]' + ); expect(firstCategory).toHaveStyle('background-color: #000;'); expect(secondCategory).toHaveStyle('background-color: #fff;'); }); test('renders stroked colors correctly', () => { render(); - const elements = document.querySelectorAll('[class*="circle"]'); + const elements = document.querySelectorAll('[class*="markerCircle"]'); getPalette(COLOR, 2).forEach((color, idx) => expect(elements[idx]).toHaveStyle(`border-color: ${color}`) ); diff --git a/packages/react-ui/src/widgets/legend/LegendCategories.js b/packages/react-ui/src/widgets/legend/LegendCategories.js index 38c87f998..2754e0e86 100644 --- a/packages/react-ui/src/widgets/legend/LegendCategories.js +++ b/packages/react-ui/src/widgets/legend/LegendCategories.js @@ -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); @@ -14,6 +14,9 @@ function LegendCategories({ legend }) { isMax={false} label={label} color={palette[idx]} + icon={ + customMarkers && Array.isArray(customMarkers) ? customMarkers[idx] : customMarkers + } isStrokeColor={isStrokeColor} /> )); @@ -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 }; @@ -56,14 +63,11 @@ const useStyles = makeStyles((theme) => ({ '& $circle': {} } }, - circle: { + marker: { whiteSpace: 'nowrap', display: 'block', - width: '12px', - height: '12px', - borderRadius: '50%', position: 'relative', - border: '2px solid transparent', + '&::after': { position: 'absolute', display: ({ isMax }) => (isMax ? 'block' : 'none'), @@ -76,6 +80,16 @@ const useStyles = makeStyles((theme) => ({ boxSizing: 'content-box' } }, + markerCircle: { + width: '12px', + height: '12px', + border: '2px solid transparent', + borderRadius: '50%' + }, + markerIcon: { + width: '16px', + height: '16px' + }, flexParent: { display: 'flex', alignItems: 'center' @@ -94,7 +108,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); @@ -121,8 +135,25 @@ function Row({ label, isMax, isStrokeColor, color = '#000' }) {