UNPKG

@ozen-ui/kit

Version:

React component library

36 lines (35 loc) 1.53 kB
import { useEffect } from 'react'; import { usePrevious } from '../../../../hooks/usePrevious'; import { scrollContainerToElement } from '../../../../utils/scrollContainerToElement'; export var useDataListBaseScroll = function (_a) { var dataListRef = _a.dataListRef, valueControl = _a.valueControl, active = _a.active, optionsStore = _a.optionsStore; var scrollToOption = function (optionId) { var optionInfo = optionsStore.getOptionFullInfo(optionId); if (!optionInfo) { return; } var hasFocusedElement = !!optionsStore.focused.current; scrollContainerToElement({ container: dataListRef.current, element: optionInfo.ref.current, behavior: hasFocusedElement ? 'smooth' : 'instant', }); }; var previousCurrent = usePrevious(optionsStore.currentOption.current); useEffect(function () { var _a; if (!active) { return; } var firstCurrent = previousCurrent === null ? optionsStore.currentOption.current : null; var selected = (_a = optionsStore.focused.current) !== null && _a !== void 0 ? _a : firstCurrent; if (selected !== null) { // Прокрутка списка setTimeout(function () { // Прокрутка списка scrollToOption(selected); }); } }, [active, optionsStore.focused.current, valueControl.lastSelected]); return { scrollToOption: scrollToOption }; };