UNPKG

@ozen-ui/kit

Version:

React component library

80 lines (79 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useDataListBaseControls = void 0; var react_1 = require("react"); var getNextIndex_1 = require("../../../../utils/getNextIndex"); var getPrevIndex_1 = require("../../../../utils/getPrevIndex"); var isKey_1 = require("../../../../utils/isKey"); var isKeys_1 = require("../../../../utils/isKeys"); var utils_1 = require("../../utils"); var useDataListBaseControls = function (_a) { var active = _a.active, valueControl = _a.valueControl, optionsStore = _a.optionsStore; var findOption = function (value) { var _a, _b; if (value === null) { return null; } var lastId = (0, utils_1.lastSelectedValue)(value !== null && value !== void 0 ? value : null); return ((_b = (_a = optionsStore.options.current.find(function (option) { return option.id === lastId; })) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null); }; (0, react_1.useEffect)(function () { var _a, _b; if (!active) { return; } optionsStore.setFocused(findOption(optionsStore.focused.current)); optionsStore.setCurrentOption(valueControl.value ? findOption((0, utils_1.lastSelectedValue)(valueControl.value)) : (_b = (_a = optionsStore.options.current[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null); }, [active]); // Сброс (0, react_1.useEffect)(function () { if (!active) { return undefined; } return function () { optionsStore.setFocused(null); optionsStore.setCurrentOption(null); }; }, [active]); var onKeyDown = function (event) { var _a, _b, _c; if (!active || !(0, isKeys_1.isKeys)(event, ['Enter', 'ArrowUp', 'ArrowDown'])) { return; } var currentOption = (_a = optionsStore.focused.current) !== null && _a !== void 0 ? _a : optionsStore.currentOption.current; if ((0, isKey_1.isKey)(event, 'Enter')) { // Не обрабатываем ввод в пустом списке if (!optionsStore.options.current.length) { return; } if (!currentOption) { return; } var optionFullInfo = optionsStore.getOptionFullInfo(currentOption); if (!optionFullInfo || optionFullInfo.disabled) { return; } event.preventDefault(); optionsStore.setFocused(optionFullInfo.id); optionsStore.setCurrentOption(optionFullInfo.id); valueControl.setValue(event, optionFullInfo.id); } if ((0, isKeys_1.isKeys)(event, ['ArrowUp', 'ArrowDown'])) { event.preventDefault(); var availableOptions = optionsStore.availableOptions.current; var currentIndex = currentOption ? (0, utils_1.getOptionIndex)(availableOptions, currentOption) : null; var isArrowUp = (0, isKey_1.isKey)(event, 'ArrowUp'); var newIndex = isArrowUp ? (0, getPrevIndex_1.getPrevIndex)(currentIndex !== null && currentIndex !== void 0 ? currentIndex : 0, availableOptions.length) : (0, getNextIndex_1.getNextIndex)(currentIndex !== null && currentIndex !== void 0 ? currentIndex : -1, availableOptions.length); var newItem = (_c = (_b = availableOptions[newIndex]) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : null; optionsStore.setFocused(newItem); } }; return { onKeyDown: onKeyDown }; }; exports.useDataListBaseControls = useDataListBaseControls;