@ozen-ui/kit
Version:
React component library
40 lines (39 loc) • 1.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDataListBaseScroll = void 0;
var react_1 = require("react");
var usePrevious_1 = require("../../../../hooks/usePrevious");
var scrollContainerToElement_1 = require("../../../../utils/scrollContainerToElement");
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;
(0, scrollContainerToElement_1.scrollContainerToElement)({
container: dataListRef.current,
element: optionInfo.ref.current,
behavior: hasFocusedElement ? 'smooth' : 'instant',
});
};
var previousCurrent = (0, usePrevious_1.usePrevious)(optionsStore.currentOption.current);
(0, react_1.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 };
};
exports.useDataListBaseScroll = useDataListBaseScroll;
;