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

Add Clear button to RangeWidget #485

Merged
merged 6 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Update Storybook to v6.5.12 [#487](https://github.com/CartoDB/carto-react/pull/487)

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove extra line here, please

- Add **clear** button to RangeWidget [#485](https://github.com/CartoDB/carto-react/pull/485)

## 1.5

### 1.5.0-alpha.1 (2022-10-05)
Expand Down
74 changes: 48 additions & 26 deletions packages/react-ui/src/widgets/RangeWidgetUI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { Box, makeStyles, Slider, TextField } from '@material-ui/core';
import { Box, Link, makeStyles, Slider, TextField } from '@material-ui/core';
import { debounce } from '@carto/react-core';

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -54,6 +54,15 @@ const useStyles = makeStyles((theme) => ({
margin: 0
}
}
},
clearWrapper: {
display: 'flex',
flexDirection: 'row-reverse',
height: theme.spacing(1.5)
},
clearButton: {
...theme.typography.caption,
cursor: 'pointer'
}
}));

Expand Down Expand Up @@ -97,7 +106,7 @@ function RangeWidgetUI({ data, min, max, limits, onSelectedRangeChange }) {
[onSelectedRangeChange]
);

const handleSliderChange = (_, newValues) => {
const changeSliderValues = (newValues) => {
setInputsValues([...newValues]);
setSliderValues([...newValues]);
debouncedOnSelectedRangeChange && debouncedOnSelectedRangeChange([...newValues]);
Expand All @@ -108,6 +117,8 @@ function RangeWidgetUI({ data, min, max, limits, onSelectedRangeChange }) {
setInputsValues(Object.assign([], inputsValues, { [index]: value }));
};

const hasBeenModified = min !== inputsValues[0] || max !== inputsValues[1];

useEffect(() => {
if (!data) {
return;
Expand All @@ -133,40 +144,51 @@ function RangeWidgetUI({ data, min, max, limits, onSelectedRangeChange }) {
const newValues = Object.assign([], inputsValues, { [index]: value }).sort(
(a, b) => a - b
);
setInputsValues(newValues);
setSliderValues([...newValues]);
onSelectedRangeChange && onSelectedRangeChange(newValues);
changeSliderValues(newValues);
};

const resetSlider = () => {
Josmorsot marked this conversation as resolved.
Show resolved Hide resolved
changeSliderValues([min, max]);
bbecquet marked this conversation as resolved.
Show resolved Hide resolved
};

return (
<Box className={classes.root}>
<Slider
getAriaLabel={(index) => (index === 0 ? 'min value' : 'max value')}
classes={{
rail: classes.sliderWithThumbRail
}}
value={sliderValues}
min={min}
max={max}
onChange={handleSliderChange}
/>
{limits && limits.length === 2 && (
<Box className={classes.clearWrapper}>
{hasBeenModified && (
<Link onClick={resetSlider} className={classes.clearButton}>
Clear
</Link>
)}
</Box>
<Box>
<Slider
getAriaLabel={(index) => (index === 0 ? 'min limit' : 'max limit')}
className={classes.sliderLimit}
getAriaLabel={(index) => (index === 0 ? 'min value' : 'max value')}
classes={{
rail: classes.sliderLimitRail,
thumb: classes.sliderLimitThumb,
track: classes.sliderLimitsTrack,
mark: classes.sliderLimitMarks,
markActive: classes.sliderLimitMarks
rail: classes.sliderWithThumbRail
}}
value={limits}
value={sliderValues}
min={min}
max={max}
marks={limitsMarks}
onChange={(_, values) => changeSliderValues(values)}
/>
)}
{limits && limits.length === 2 && (
<Slider
getAriaLabel={(index) => (index === 0 ? 'min limit' : 'max limit')}
className={classes.sliderLimit}
classes={{
rail: classes.sliderLimitRail,
thumb: classes.sliderLimitThumb,
track: classes.sliderLimitsTrack,
mark: classes.sliderLimitMarks,
markActive: classes.sliderLimitMarks
}}
value={limits}
min={min}
max={max}
marks={limitsMarks}
/>
)}
</Box>
<Box display={'flex'} justifyContent={'space-between'} mb={1}>
<TextField
className={classes.input}
Expand Down