mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
* merge develop * Add eslint dependencies, configs and scripts to plugins project * run lint with Github action * ignore tests and dist folders * fun eslint with --fix and manual fixes, renamed __tests_ to __tests__ * add plugins packages folder to lint-staged config * fix lint issue
96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
import React, { useEffect, useRef, useState } from 'react';
|
|
import Slider, { Range } from 'rc-slider';
|
|
import 'rc-slider/assets/index.css';
|
|
|
|
export const RangeSlider = function RangeSlider({ height, properties, styles, setExposedVariable }) {
|
|
const { value, min, max, enableTwoHandle } = properties;
|
|
const { trackColor, handleColor, lineColor, visibility } = styles;
|
|
const sliderRef = useRef(null);
|
|
const [sliderValue, setSliderValue] = useState(0);
|
|
const [rangeValue, setRangeValue] = useState([0, 100]);
|
|
|
|
const toArray = (data) => (Array.isArray(data) ? data : [data, max]);
|
|
const singleHandleValue = !enableTwoHandle ? (Array.isArray(value) ? value[0] : value) : 50;
|
|
const twoHandlesArray = enableTwoHandle ? toArray(value) : [0, 100];
|
|
|
|
const computedStyles = {
|
|
height,
|
|
display: visibility ? 'flex' : 'none',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
padding: '0px 2px',
|
|
};
|
|
|
|
useEffect(() => {
|
|
setSliderValue(singleHandleValue);
|
|
setExposedVariable('value', singleHandleValue);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [singleHandleValue]);
|
|
|
|
useEffect(() => {
|
|
setRangeValue(twoHandlesArray);
|
|
setExposedVariable('value', twoHandlesArray);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [...twoHandlesArray]);
|
|
|
|
useEffect(() => {
|
|
setExposedVariable('value', enableTwoHandle ? twoHandlesArray : singleHandleValue);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [sliderRef.current, enableTwoHandle]);
|
|
|
|
const onSliderChange = (value) => {
|
|
setExposedVariable('value', value);
|
|
setSliderValue(value);
|
|
};
|
|
|
|
const onRangeChange = (value) => {
|
|
setExposedVariable('value', value);
|
|
setRangeValue(value);
|
|
};
|
|
|
|
const rangeStyles = {
|
|
handleStyle: toArray(sliderValue).map(() => {
|
|
return {
|
|
backgroundColor: handleColor,
|
|
borderColor: handleColor,
|
|
};
|
|
}),
|
|
trackStyle: toArray(sliderValue).map(() => {
|
|
return { backgroundColor: trackColor };
|
|
}),
|
|
railStyle: { backgroundColor: lineColor },
|
|
};
|
|
|
|
return (
|
|
<div style={computedStyles} className="range-slider">
|
|
{enableTwoHandle ? (
|
|
<Range
|
|
min={min}
|
|
max={max}
|
|
defaultValue={toArray(rangeValue)}
|
|
onChange={onRangeChange}
|
|
value={toArray(rangeValue)}
|
|
ref={sliderRef}
|
|
trackStyle={rangeStyles.trackStyle}
|
|
railStyle={rangeStyles.railStyle}
|
|
handleStyle={rangeStyles.handleStyle}
|
|
/>
|
|
) : (
|
|
<Slider
|
|
min={min}
|
|
max={max}
|
|
defaultValue={sliderValue}
|
|
value={sliderValue}
|
|
ref={sliderRef}
|
|
onChange={onSliderChange}
|
|
trackStyle={{ backgroundColor: trackColor }}
|
|
railStyle={{ backgroundColor: lineColor }}
|
|
handleStyle={{
|
|
backgroundColor: handleColor,
|
|
borderColor: handleColor,
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|