From a4dd1e2f6e92f90c201287ea6a83a865a7f10d71 Mon Sep 17 00:00:00 2001 From: "Timothy Z." Date: Fri, 3 Jan 2025 13:26:30 +0200 Subject: [PATCH] feat(MultiSelectMenu): scroll into view --- .../MultiselectMenu/Dropdown/Dropdown.tsx | 70 +++++++++++++------ .../Dropdown/Option/Option.tsx | 10 +-- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/common/MultiselectMenu/Dropdown/Dropdown.tsx b/src/common/MultiselectMenu/Dropdown/Dropdown.tsx index cb7de76c2..eea0d679e 100644 --- a/src/common/MultiselectMenu/Dropdown/Dropdown.tsx +++ b/src/common/MultiselectMenu/Dropdown/Dropdown.tsx @@ -1,6 +1,6 @@ // Copyright (C) 2017-2024 Smart code 203358507 -import React from 'react'; +import React, { useRef, useEffect, useCallback } from 'react'; import Button from 'stremio/common/Button'; import { useTranslation } from 'react-i18next'; import classNames from 'classnames'; @@ -19,33 +19,57 @@ type Props = { const Dropdown = ({ level, setLevel, options, onSelect, selectedOption, menuOpen }: Props) => { const { t } = useTranslation(); + const optionsRef = useRef(new Map()); + const containerRef = useRef(null); - const onBackButtonClick = () => { + const handleSetOptionRef = useCallback((value: number) => (node: HTMLButtonElement | null) => { + if (node) { + optionsRef.current.set(value, node); + } else { + optionsRef.current.delete(value); + } + }, []); + + const handleBackClick = useCallback(() => { setLevel(level - 1); - }; + }, [setLevel, level]); - return ( -
- { - level > 0 ? - - : null + useEffect(() => { + if (menuOpen && selectedOption && containerRef.current) { + const selectedNode = optionsRef.current.get(selectedOption.value); + if (selectedNode) { + selectedNode.scrollIntoView({ + behavior: 'smooth', + block: 'nearest' + }); } - { - options - .filter((option: MultiselectMenuOption) => !option.hidden) - .map((option: MultiselectMenuOption, index) => ( -