You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VerticalScroller.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { useRef, useState } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. VerticalScrollerContainer,
  5. ListContainer,
  6. ArrowIconContainer,
  7. } from "./VerticalScroller.styled";
  8. import { ArrowButton } from "../Buttons/ArrowButton/ArrowButton";
  9. const VerticalScroller = (props) => {
  10. const scrollRef = useRef(null);
  11. const [isDisabledUpButton, setIsDisabledUpButton] = useState(true);
  12. const [isDisabledDownButton, setIsDisabledDownButton] = useState(false);
  13. const handleScroll = (event) => {
  14. if (!event.external) {
  15. if (scrollRef.current.scrollTop === 0) {
  16. if (isDisabledUpButton !== true) setIsDisabledUpButton(true);
  17. } else {
  18. if (isDisabledUpButton !== false) setIsDisabledUpButton(false);
  19. }
  20. if (
  21. scrollRef.current.scrollHeight - scrollRef.current.scrollTop ===
  22. scrollRef.current.clientHeight
  23. ) {
  24. if (isDisabledDownButton !== true) setIsDisabledDownButton(true);
  25. } else {
  26. if (isDisabledDownButton !== false) setIsDisabledDownButton(false);
  27. }
  28. }
  29. };
  30. const handleRight = () => {
  31. if (scrollRef.current.scrollTop + 50 !== 0) {
  32. if (isDisabledUpButton !== false) setIsDisabledUpButton(false);
  33. }
  34. if (
  35. scrollRef.current.scrollHeight - scrollRef.current.scrollTop - 50 <=
  36. scrollRef.current.clientHeight
  37. ) {
  38. if (isDisabledDownButton !== true) setIsDisabledDownButton(true);
  39. }
  40. scrollRef.current.scrollBy({ top: 50, behaviour: "smooth" });
  41. };
  42. const handleLeft = () => {
  43. if (scrollRef.current.scrollHeight - 50 <= 0) {
  44. if (isDisabledUpButton !== true) setIsDisabledUpButton(true);
  45. }
  46. if (
  47. scrollRef.current.scrollHeight - scrollRef.current.scrollTop + 50 !==
  48. scrollRef.current.clientHeight
  49. ) {
  50. if (isDisabledDownButton !== false) setIsDisabledDownButton(false);
  51. }
  52. scrollRef.current.scrollBy({ top: -50, behaviour: "smooth" });
  53. };
  54. return (
  55. <VerticalScrollerContainer
  56. style={props.containerStyle}
  57. className={props.className}
  58. >
  59. {!props.hideArrows && (
  60. <ArrowIconContainer side="up">
  61. <ArrowButton
  62. onClick={handleLeft}
  63. disabled={isDisabledUpButton}
  64. side={"up"}
  65. ></ArrowButton>
  66. </ArrowIconContainer>
  67. )}
  68. <ListContainer
  69. innerRef={scrollRef}
  70. style={props.listStyle}
  71. onScroll={handleScroll}
  72. >
  73. {props.children}
  74. </ListContainer>
  75. {!props.hideArrows && (
  76. <ArrowIconContainer>
  77. <ArrowButton
  78. onClick={handleRight}
  79. disabled={isDisabledDownButton}
  80. side={"down"}
  81. ></ArrowButton>
  82. </ArrowIconContainer>
  83. )}
  84. </VerticalScrollerContainer>
  85. );
  86. };
  87. VerticalScroller.propTypes = {
  88. children: PropTypes.node,
  89. className: PropTypes.string,
  90. containerStyle: PropTypes.any,
  91. listStyle: PropTypes.any,
  92. hideArrows: PropTypes.bool,
  93. };
  94. export default VerticalScroller;