Skip to content

Commit

Permalink
change touch events to use add event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
bprize15 committed Nov 21, 2024
1 parent cf7cb6f commit 6412483
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions web/src/main/javascript/src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OncoTree, {
OncoTreeNode,
OncoTreeSearchOption,
} from "@oncokb/oncotree";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import ReactSelect, {
ClearIndicatorProps,
ControlProps,
Expand Down Expand Up @@ -266,6 +266,9 @@ export default function SearchBar({
GroupBase<OncoTreeSearchOption>
>,
) {
const prevButtonRef = useRef<HTMLDivElement>(null);
const nextButtonRef = useRef<HTMLDivElement>(null);

const inputStyle = props.getStyles("input", { ...props, isHidden: false });
const inputColor = inputStyle.color ?? "black";
const clearIndicatorClass = css(props.getStyles("clearIndicator", props));
Expand Down Expand Up @@ -330,29 +333,45 @@ export default function SearchBar({
setCancerTypeResultsIndex(newIndex);
}

const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
useEffect(() => {
const prevButtonTouchHandler = (event: TouchEvent) => {
getPreviousResult();
event.preventDefault();
};
const nextButtonTouchHandler = (event: TouchEvent) => {
getNextResult();
event.preventDefault();
};
prevButtonRef.current?.addEventListener("touchstart", prevButtonTouchHandler);
nextButtonRef.current?.addEventListener("touchstart", nextButtonTouchHandler)

return () => {
prevButtonRef.current?.removeEventListener("touchstart", prevButtonTouchHandler);

Check warning on line 349 in web/src/main/javascript/src/components/SearchBar/SearchBar.tsx

View workflow job for this annotation

GitHub Actions / build

The ref value 'prevButtonRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'prevButtonRef.current' to a variable inside the effect, and use that variable in the cleanup function
nextButtonRef.current?.removeEventListener("touchstart", nextButtonTouchHandler);

Check warning on line 350 in web/src/main/javascript/src/components/SearchBar/SearchBar.tsx

View workflow job for this annotation

GitHub Actions / build

The ref value 'nextButtonRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'nextButtonRef.current' to a variable inside the effect, and use that variable in the cleanup function
}
}, []);

Check warning on line 352 in web/src/main/javascript/src/components/SearchBar/SearchBar.tsx

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has missing dependencies: 'getNextResult' and 'getPreviousResult'. Either include them or remove the dependency array

return (
<>
{getResultsNumberSpan()}
{resultsAndIndexDefined && cancerTypeResults.length > 0 && (
<div style={{ userSelect: "none", display: "flex" }}>
<div
ref={prevButtonRef}
data-type={PREV_BUTTON_DATA_TYPE}
className={clearIndicatorClass}
onClick={isMobile ? undefined : getPreviousResult}
onTouchStart={isMobile ? getPreviousResult : undefined}
onClick={getPreviousResult}
>
<FontAwesomeIcon
style={{ pointerEvents: "none" }}
icon={faArrowUp}
/>
</div>
<div
ref={nextButtonRef}
data-type={NEXT_BUTTON_DATA_TYPE}
className={clearIndicatorClass}
onClick={isMobile ? undefined : getNextResult}
onTouchStart={isMobile ? getNextResult : undefined}
onClick={getNextResult}
>
<FontAwesomeIcon
style={{ pointerEvents: "none" }}
Expand Down

0 comments on commit 6412483

Please sign in to comment.