| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import React, { useRef, useState } from "react";
- import PropTypes from "prop-types";
- import {
- VerticalScrollerContainer,
- ListContainer,
- ArrowIconContainer,
- } from "./VerticalScroller.styled";
- import { ArrowButton } from "../Buttons/ArrowButton/ArrowButton";
-
- const VerticalScroller = (props) => {
- const scrollRef = useRef(null);
- const [isDisabledUpButton, setIsDisabledUpButton] = useState(true);
- const [isDisabledDownButton, setIsDisabledDownButton] = useState(false);
-
- const handleScroll = (event) => {
- if (!event.external) {
- if (scrollRef.current.scrollTop === 0) {
- if (isDisabledUpButton !== true) setIsDisabledUpButton(true);
- } else {
- if (isDisabledUpButton !== false) setIsDisabledUpButton(false);
- }
- if (
- scrollRef.current.scrollHeight - scrollRef.current.scrollTop ===
- scrollRef.current.clientHeight
- ) {
- if (isDisabledDownButton !== true) setIsDisabledDownButton(true);
- } else {
- if (isDisabledDownButton !== false) setIsDisabledDownButton(false);
- }
- }
- };
-
- const handleRight = () => {
- if (scrollRef.current.scrollTop + 50 !== 0) {
- if (isDisabledUpButton !== false) setIsDisabledUpButton(false);
- }
- if (
- scrollRef.current.scrollHeight - scrollRef.current.scrollTop - 50 <=
- scrollRef.current.clientHeight
- ) {
- if (isDisabledDownButton !== true) setIsDisabledDownButton(true);
- }
- scrollRef.current.scrollBy({ top: 50, behaviour: "smooth" });
- };
- const handleLeft = () => {
- if (scrollRef.current.scrollHeight - 50 <= 0) {
- if (isDisabledUpButton !== true) setIsDisabledUpButton(true);
- }
- if (
- scrollRef.current.scrollHeight - scrollRef.current.scrollTop + 50 !==
- scrollRef.current.clientHeight
- ) {
- if (isDisabledDownButton !== false) setIsDisabledDownButton(false);
- }
- scrollRef.current.scrollBy({ top: -50, behaviour: "smooth" });
- };
- return (
- <VerticalScrollerContainer
- style={props.containerStyle}
- className={props.className}
- >
- {!props.hideArrows && (
- <ArrowIconContainer side="up">
- <ArrowButton
- onClick={handleLeft}
- disabled={isDisabledUpButton}
- side={"up"}
- ></ArrowButton>
- </ArrowIconContainer>
- )}
- <ListContainer
- innerRef={scrollRef}
- style={props.listStyle}
- onScroll={handleScroll}
- >
- {props.children}
- </ListContainer>
- {!props.hideArrows && (
- <ArrowIconContainer>
- <ArrowButton
- onClick={handleRight}
- disabled={isDisabledDownButton}
- side={"down"}
- ></ArrowButton>
- </ArrowIconContainer>
- )}
- </VerticalScrollerContainer>
- );
- };
-
- VerticalScroller.propTypes = {
- children: PropTypes.node,
- className: PropTypes.string,
- containerStyle: PropTypes.any,
- listStyle: PropTypes.any,
- hideArrows: PropTypes.bool,
- };
-
- export default VerticalScroller;
|