tdesign-react
Version:
TDesign Component for React
227 lines (223 loc) • 10 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _objectWithoutProperties } from '../../_chunks/dep-ed34fbd4.js';
import { _ as _defineProperty } from '../../_chunks/dep-d67deb2c.js';
import { _ as _slicedToArray } from '../../_chunks/dep-10d5731f.js';
import React, { useRef, useMemo, Children, isValidElement, cloneElement } from 'react';
import classNames from 'classnames';
import { isEqual } from 'lodash-es';
import useConfig from '../../hooks/useConfig.js';
import { useLocaleReceiver } from '../../locale/LocalReceiver.js';
import usePanelVirtualScroll from '../hooks/usePanelVirtualScroll.js';
import { g as getKeyMapping, b as getSelectValueArr, O as Option } from '../../_chunks/dep-80300655.js';
import OptionGroup from './OptionGroup.js';
import '../../_chunks/dep-8abcbcbc.js';
import '../../_chunks/dep-74a10cfb.js';
import '../../config-provider/ConfigContext.js';
import '../../locale/zh_CN.js';
import '../../_chunks/dep-751cada9.js';
import 'dayjs';
import '../../_chunks/dep-37c4dbb1.js';
import '../../config-provider/index.js';
import '../../config-provider/ConfigProvider.js';
import '../../config-provider/type.js';
import '../../hooks/useVirtualScroll.js';
import '../../_chunks/dep-a3a3e527.js';
import '../../hooks/useDomRefCallback.js';
import '../../hooks/useRipple.js';
import '../../_chunks/dep-79351403.js';
import '../../hooks/useAnimation.js';
import '../../_chunks/dep-ca8d3fa0.js';
import '../defaultProps.js';
import '../../hooks/useDefaultProps.js';
var _excluded = ["group", "divider"],
_excluded2 = ["value", "label", "disabled", "children"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var PopupContent = /*#__PURE__*/React.forwardRef(function (props, ref) {
var value = props.value,
size = props.size,
max = props.max,
multiple = props.multiple,
showPopup = props.showPopup,
setShowPopup = props.setShowPopup,
empty = props.empty,
loadingText = props.loadingText,
loading = props.loading,
valueType = props.valueType,
children = props.children,
keys = props.keys,
panelTopContent = props.panelTopContent,
panelBottomContent = props.panelBottomContent,
onChange = props.onChange,
onCheckAllChange = props.onCheckAllChange,
getPopupInstance = props.getPopupInstance,
propsOptions = props.options,
propsScroll = props.scroll,
hoverIndex = props.hoverIndex;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
var _useLocaleReceiver = useLocaleReceiver("select"),
_useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 2),
local = _useLocaleReceiver2[0],
t = _useLocaleReceiver2[1];
var emptyText = t(local.empty);
var popupContentRef = useRef(null);
popupContentRef.current = getPopupInstance();
var _usePanelVirtualScrol = usePanelVirtualScroll({
popupContentRef: popupContentRef,
scroll: propsScroll,
options: propsOptions,
size: size
}),
visibleData = _usePanelVirtualScrol.visibleData,
handleRowMounted = _usePanelVirtualScrol.handleRowMounted,
isVirtual = _usePanelVirtualScrol.isVirtual,
panelStyle = _usePanelVirtualScrol.panelStyle,
cursorStyle = _usePanelVirtualScrol.cursorStyle;
var isObjectType = useMemo(function () {
return valueType === "object";
}, [valueType]);
var _useMemo = useMemo(function () {
return getKeyMapping(keys);
}, [keys]),
valueKey = _useMemo.valueKey,
labelKey = _useMemo.labelKey;
var optionsExcludedCheckAll = useMemo(function () {
var uniqueOptions = {};
propsOptions === null || propsOptions === void 0 || propsOptions.forEach(function (option) {
if (option.group) {
option.children.forEach(function (item) {
if (!item.checkAll) {
uniqueOptions[item.value] = item;
}
});
} else if (!option.checkAll) {
uniqueOptions[option.value] = option;
}
});
return Object.values(uniqueOptions);
}, [propsOptions]);
var onSelect = function onSelect(selectedValue, _ref) {
var label = _ref.label,
selected = _ref.selected,
event = _ref.event,
restData = _ref.restData;
var objVal = _objectSpread(_objectSpread({}, isObjectType ? _objectSpread({}, restData) : {}), {}, _defineProperty(_defineProperty({}, labelKey, label), valueKey, selectedValue));
if (multiple) {
var values = getSelectValueArr(value, selectedValue, selected, valueType, keys, objVal);
onChange(values, {
label: label,
value: selectedValue,
e: event,
trigger: selected ? "uncheck" : "check"
});
} else {
var selectVal = valueType === "object" ? objVal : selectedValue;
if (!isEqual(value, selectVal)) {
onChange(selectVal, {
label: label,
value: selectVal,
e: event,
trigger: "check"
});
}
setShowPopup(!showPopup);
}
};
var childrenWithProps = Children.map(children, function (child) {
if (/*#__PURE__*/isValidElement(child)) {
var addedProps = {
size: size,
max: max,
multiple: multiple,
selectedValue: value,
onSelect: onSelect
};
return /*#__PURE__*/cloneElement(child, _objectSpread({}, addedProps));
}
return child;
});
var isEmpty = Array.isArray(childrenWithProps) && !childrenWithProps.length || propsOptions && propsOptions.length === 0;
var _renderOptions = function renderOptions(options) {
var startIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
if (options) {
var currentIndex = startIndex;
return /* @__PURE__ */React.createElement("ul", {
className: "".concat(classPrefix, "-select__list")
}, options.map(function (item, index) {
var group = item.group,
divider = item.divider,
rest = _objectWithoutProperties(item, _excluded);
if (group) {
var groupElement = /* @__PURE__ */React.createElement(OptionGroup, _objectSpread({
label: group,
divider: divider,
key: index
}, rest), _renderOptions(rest.children, currentIndex));
currentIndex += rest.children.length;
return groupElement;
}
var optionValue = item.value,
label = item.label,
disabled = item.disabled,
children2 = item.children,
restData = _objectWithoutProperties(item, _excluded2);
var content = item.content;
var shouldOmitContent = Object.values(keys || {}).includes("content");
var isKeyboardHovered = hoverIndex === currentIndex;
currentIndex += 1;
var optionElement = /* @__PURE__ */React.createElement(Option, _objectSpread(_objectSpread(_objectSpread({
key: index,
max: max,
label: label,
value: optionValue,
onSelect: onSelect,
selectedValue: value,
optionLength: optionsExcludedCheckAll.length,
multiple: multiple,
size: size,
disabled: disabled,
restData: restData,
keys: keys,
isKeyboardHovered: isKeyboardHovered,
onCheckAllChange: onCheckAllChange,
onRowMounted: handleRowMounted
}, isVirtual ? {
isVirtual: isVirtual,
bufferSize: propsScroll === null || propsScroll === void 0 ? void 0 : propsScroll.bufferSize,
scrollType: propsScroll === null || propsScroll === void 0 ? void 0 : propsScroll.type
} : {}), restData), {}, {
content: shouldOmitContent ? null : content
}), children2);
return optionElement;
}));
}
return /* @__PURE__ */React.createElement("ul", {
className: "".concat(classPrefix, "-select__list")
}, childrenWithProps);
};
var renderPanel = function renderPanel(renderedOptions, extraStyle) {
return /* @__PURE__ */React.createElement("div", {
ref: ref,
className: classNames("".concat(classPrefix, "-select__dropdown-inner"), _defineProperty(_defineProperty(_defineProperty({}, "".concat(classPrefix, "-select__dropdown-inner--size-s"), size === "small"), "".concat(classPrefix, "-select__dropdown-inner--size-l"), size === "large"), "".concat(classPrefix, "-select__dropdown-inner--size-m"), size === "medium")),
style: extraStyle
}, loading && /* @__PURE__ */React.createElement("div", {
className: "".concat(classPrefix, "-select__loading-tips")
}, loadingText), !loading && isEmpty && /* @__PURE__ */React.createElement("div", {
className: "".concat(classPrefix, "-select__empty")
}, empty ? empty : /* @__PURE__ */React.createElement("p", null, emptyText)), !loading && !isEmpty && _renderOptions(renderedOptions));
};
if (isVirtual) {
return /* @__PURE__ */React.createElement(React.Fragment, null, panelTopContent, /* @__PURE__ */React.createElement("div", null, /* @__PURE__ */React.createElement("div", {
style: cursorStyle
}), renderPanel(visibleData, panelStyle)), panelBottomContent);
}
if (!children && !propsOptions) return null;
return /* @__PURE__ */React.createElement(React.Fragment, null, panelTopContent, renderPanel(propsOptions), panelBottomContent);
});
export { PopupContent as default };
//# sourceMappingURL=PopupContent.js.map