|
| 1 | +import React, { useCallback, useEffect, useState, useRef } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import "./MultiSlider.css"; |
| 4 | + |
| 5 | +export interface SliderProps { |
| 6 | + min: number, |
| 7 | + max: number, |
| 8 | + onChange: (min: number, max: number) => void, |
| 9 | + labelFn?: (value: number) => string, |
| 10 | +} |
| 11 | + |
| 12 | +const MultiRangeSlider = ({ min, max, onChange, labelFn }: SliderProps) => { |
| 13 | + if (!labelFn) { |
| 14 | + labelFn = (_val) => ''; |
| 15 | + } |
| 16 | + const [minVal, setMinVal] = useState<number>(min); |
| 17 | + const [maxVal, setMaxVal] = useState<number>(max); |
| 18 | + const minValRef = useRef<number>(min); |
| 19 | + const maxValRef = useRef<number>(max); |
| 20 | + const range = useRef<HTMLInputElement | null>(null); |
| 21 | + |
| 22 | + // Convert to percentage |
| 23 | + const getPercent = useCallback( |
| 24 | + (value) => Math.round(((value - min) / (max - min)) * 100), |
| 25 | + [min, max] |
| 26 | + ); |
| 27 | + |
| 28 | + // Set width of the range to decrease from the left side |
| 29 | + useEffect(() => { |
| 30 | + const minPercent = getPercent(minVal); |
| 31 | + const maxPercent = getPercent(maxValRef.current); |
| 32 | + |
| 33 | + if (range.current) { |
| 34 | + |
| 35 | + range.current.style.left = `${minPercent}%`; |
| 36 | + range.current.style.width = `${maxPercent - minPercent}%`; |
| 37 | + } |
| 38 | + }, [minVal, getPercent]); |
| 39 | + |
| 40 | + // Set width of the range to decrease from the right side |
| 41 | + useEffect(() => { |
| 42 | + const minPercent = getPercent(minValRef.current); |
| 43 | + const maxPercent = getPercent(maxVal); |
| 44 | + |
| 45 | + if (range.current) { |
| 46 | + range.current.style.width = `${maxPercent - minPercent}%`; |
| 47 | + } |
| 48 | + }, [maxVal, getPercent]); |
| 49 | + |
| 50 | + // Get min and max values when their state changes |
| 51 | + useEffect(() => { |
| 52 | + onChange(minVal, maxVal); |
| 53 | + }, [minVal, maxVal, onChange]); |
| 54 | + |
| 55 | + return ( |
| 56 | + <span className="container"> |
| 57 | + <input |
| 58 | + type="range" |
| 59 | + min={min} |
| 60 | + max={max} |
| 61 | + value={minVal} |
| 62 | + onChange={(event) => { |
| 63 | + const value = Math.min(Number(event.target.value), maxVal - 1); |
| 64 | + setMinVal(value); |
| 65 | + minValRef.current = value; |
| 66 | + }} |
| 67 | + className="thumb thumb--left" |
| 68 | + style={{ zIndex: (minVal > max - 100) ? "5" : undefined }} |
| 69 | + /> |
| 70 | + <input |
| 71 | + type="range" |
| 72 | + min={min} |
| 73 | + max={max} |
| 74 | + value={maxVal} |
| 75 | + onChange={(event) => { |
| 76 | + const value = Math.max(Number(event.target.value), minVal + 1); |
| 77 | + setMaxVal(value); |
| 78 | + maxValRef.current = value; |
| 79 | + }} |
| 80 | + className="thumb thumb--right" |
| 81 | + /> |
| 82 | + |
| 83 | + <span className="slider"> |
| 84 | + <span className="slider__left-input"> |
| 85 | + <input type="number" value={minVal} onChange={(event) => { |
| 86 | + const value = Math.max(Math.min(Number(event.target.value), maxVal - 1), min); |
| 87 | + setMinVal(value); |
| 88 | + minValRef.current = value; |
| 89 | + }}/> |
| 90 | + </span> |
| 91 | + <span className="slider__right-input"> |
| 92 | + <input type="number" value={maxVal} onChange={(event) => { |
| 93 | + const value = Math.min(Math.max(Number(event.target.value), minVal + 1), max); |
| 94 | + setMaxVal(value); |
| 95 | + maxValRef.current = value; |
| 96 | + }}/> |
| 97 | + </span> |
| 98 | + <span className="slider__track" /> |
| 99 | + <span ref={range} className="slider__range" /> |
| 100 | + <span className="slider__left-value"> |
| 101 | + {labelFn(minVal)} |
| 102 | + </span> |
| 103 | + <span className="slider__right-value"> |
| 104 | + {labelFn(maxVal)} |
| 105 | + </span> |
| 106 | + </span> |
| 107 | + </span> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +MultiRangeSlider.propTypes = { |
| 112 | + min: PropTypes.number.isRequired, |
| 113 | + max: PropTypes.number.isRequired, |
| 114 | + onChange: PropTypes.func.isRequired |
| 115 | +}; |
| 116 | + |
| 117 | +export default MultiRangeSlider; |
0 commit comments