SliderRange.jsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React from "react";
  2. import { Range } from "rc-slider";
  3. import styled from "styled-components";
  4. import "rc-slider/assets/index.css";
  5. const StyledSliderRange = styled.div`
  6. display: flex;
  7. align-items: center;
  8. justify-content: center;
  9. > span {
  10. margin: 1em;
  11. color: var(--font-color2);
  12. }
  13. `;
  14. const sliderRangeStyling = {
  15. trackStyle: [
  16. {
  17. backgroundColor: "var(--color-primary)",
  18. height: "5px",
  19. },
  20. ],
  21. railStyle: {
  22. backgroundColor: "var(--font-color2)",
  23. },
  24. handleStyle: [
  25. {
  26. backgroundColor: "white",
  27. borderColor: "white",
  28. },
  29. {
  30. backgroundColor: "white",
  31. borderColor: "white",
  32. },
  33. ],
  34. };
  35. // Please check https://github.com/react-component/slider#user-content-common-api
  36. // for all available props.
  37. const SliderRange = ({
  38. defaultValue: [defaultMin, defaultMax],
  39. plusSignMode = true,
  40. ...props
  41. }) => {
  42. const [minMaxValues, setMinMaxValues] = React.useState([
  43. props.value.length ? props.value[0] : defaultMin,
  44. props.value.length ? props.value[1] : defaultMax,
  45. ]);
  46. const onChangeCustom = function (values) {
  47. setMinMaxValues(values);
  48. props.onChange(values);
  49. };
  50. return (
  51. <StyledSliderRange className={props.className}>
  52. <span>{`${minMaxValues[0]}`}</span>
  53. <Range
  54. {...props}
  55. {...sliderRangeStyling}
  56. value={props.value.length ? props.value : [defaultMin, defaultMax]}
  57. onChange={onChangeCustom}
  58. />
  59. <span>{`${minMaxValues[1]}${
  60. plusSignMode && minMaxValues[1] == props.max ? "+" : ""
  61. }`}</span>
  62. </StyledSliderRange>
  63. );
  64. };
  65. export default SliderRange;