tdesign-react
Version:
TDesign Component for React
164 lines (160 loc) • 5.95 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _slicedToArray } from '../../_chunks/dep-10d5731f.js';
import { useRef, useState, useMemo, useEffect } from 'react';
import useConfig from '../../hooks/useConfig.js';
import { g as getKeyMapping, b as getSelectValueArr } from '../../_chunks/dep-80300655.js';
import '../../_chunks/dep-74a10cfb.js';
import '../../config-provider/ConfigContext.js';
import '../../_chunks/dep-d67deb2c.js';
import '../../_chunks/dep-8abcbcbc.js';
import 'lodash-es';
import '../../locale/zh_CN.js';
import '../../_chunks/dep-751cada9.js';
import 'dayjs';
import '../../_chunks/dep-37c4dbb1.js';
import '../../_chunks/dep-a3a3e527.js';
import 'classnames';
import '../../hooks/useDomRefCallback.js';
import '../../hooks/useRipple.js';
import '../../_chunks/dep-79351403.js';
import '../../hooks/useAnimation.js';
import '../../_chunks/dep-ca8d3fa0.js';
import '../base/OptionGroup.js';
import '../defaultProps.js';
import '../../hooks/useDefaultProps.js';
function useKeyboardControl(_ref) {
var max = _ref.max,
multiple = _ref.multiple,
keys = _ref.keys,
value = _ref.value,
valueType = _ref.valueType,
handleChange = _ref.handleChange,
innerPopupVisible = _ref.innerPopupVisible,
handlePopupVisibleChange = _ref.handlePopupVisibleChange,
displayOptions = _ref.displayOptions,
onCheckAllChange = _ref.onCheckAllChange,
selectInputRef = _ref.selectInputRef,
toggleIsScrolling = _ref.toggleIsScrolling;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
var isCheckAll = useRef(false);
var _useState = useState(-1),
_useState2 = _slicedToArray(_useState, 2),
hoverIndex = _useState2[0],
changeHoverIndex = _useState2[1];
var isObjectType = useMemo(function () {
return valueType === "object";
}, [valueType]);
var _useMemo = useMemo(function () {
return getKeyMapping(keys);
}, [keys]),
valueKey = _useMemo.valueKey,
disabledKey = _useMemo.disabledKey;
useEffect(function () {
if (!innerPopupVisible) {
changeHoverIndex(-1);
} else if (!multiple) {
var index = displayOptions.findIndex(function (option) {
return option.value === value;
});
changeHoverIndex(index >= 0 ? index : -1);
} else {
changeHoverIndex(-1);
}
}, [innerPopupVisible, displayOptions]);
useEffect(function () {
if (!Array.isArray(value)) return;
isCheckAll.current = value.length === displayOptions.filter(function (v) {
return !((v.disabled || v.checkAll) && !value.includes(v.value));
}).length;
}, [value, displayOptions]);
var handleKeyboardScroll = function handleKeyboardScroll(hoverIndex2) {
var popupContent = selectInputRef.current.getPopupContentElement();
var optionSelector = ".".concat(classPrefix, "-select-option");
var selector = ".".concat(classPrefix, "-select-option__hover");
var firstSelectedNode = popupContent.querySelector(selector);
if (firstSelectedNode) {
toggleIsScrolling(true);
var scrollHeight = popupContent.querySelector(optionSelector).clientHeight * hoverIndex2;
popupContent.scrollTo({
top: scrollHeight,
behavior: "smooth"
});
}
};
var handleKeyDown = function handleKeyDown(_value, _ref2) {
var _displayOptions$newIn, _displayOptions$newIn2;
var e = _ref2.e;
var optionsListLength = displayOptions.length;
var newIndex = hoverIndex;
switch (e.code) {
case "ArrowUp":
e.preventDefault();
if (hoverIndex === -1) newIndex = 0;else if (hoverIndex === 0 || hoverIndex > optionsListLength - 1) newIndex = optionsListLength - 1;else newIndex -= 1;
if ((_displayOptions$newIn = displayOptions[newIndex]) !== null && _displayOptions$newIn !== void 0 && _displayOptions$newIn[disabledKey]) newIndex -= 1;
changeHoverIndex(newIndex);
handleKeyboardScroll(newIndex);
break;
case "ArrowDown":
e.preventDefault();
if (hoverIndex === -1 || hoverIndex >= optionsListLength - 1) newIndex = 0;else newIndex += 1;
if ((_displayOptions$newIn2 = displayOptions[newIndex]) !== null && _displayOptions$newIn2 !== void 0 && _displayOptions$newIn2.disabled) newIndex += 1;
changeHoverIndex(newIndex);
handleKeyboardScroll(newIndex);
break;
case "Enter":
{
if (!innerPopupVisible) {
handlePopupVisibleChange(true, {
e: e
});
break;
}
if (hoverIndex === -1) return;
if (displayOptions[hoverIndex].checkAll) {
onCheckAllChange(!isCheckAll.current, e);
return;
}
var selectedOptions = displayOptions[hoverIndex];
var optionValue = isObjectType ? selectedOptions : selectedOptions[valueKey];
if (!multiple) {
handleChange(optionValue, {
trigger: "check",
e: e
});
handlePopupVisibleChange(false, {
e: e
});
changeHoverIndex(-1);
handleKeyboardScroll(0);
} else {
var valueIndex = value.indexOf(optionValue);
var isSelected = valueIndex > -1;
var values = getSelectValueArr(value, optionValue, isSelected, valueType, keys, selectedOptions);
if (max > 0 && values.length > max) return;
handleChange(values, {
trigger: !isSelected ? "check" : "uncheck",
e: e
});
}
break;
}
case "Escape":
handlePopupVisibleChange(false, {
e: e
});
changeHoverIndex(-1);
handleKeyboardScroll(0);
}
};
return {
hoverIndex: hoverIndex,
handleKeyDown: handleKeyDown
};
}
export { useKeyboardControl as default };
//# sourceMappingURL=useKeyboardControl.js.map