tdesign-react
Version:
TDesign Component for React
228 lines (224 loc) • 9.71 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _objectWithoutProperties } from '../../_chunks/dep-6b660ef0.js';
import { _ as _defineProperty } from '../../_chunks/dep-cb0a3966.js';
import { _ as _slicedToArray } from '../../_chunks/dep-48805ab8.js';
import React, { useRef, useMemo, Children, isValidElement, cloneElement } from 'react';
import classNames from 'classnames';
import { isEqual } from 'lodash-es';
import { useLocaleReceiver } from '../../locale/LocalReceiver.js';
import { g as getSelectValueArr } from '../../_chunks/dep-c7ed701c.js';
import useConfig from '../../hooks/useConfig.js';
import usePanelVirtualScroll from '../hooks/usePanelVirtualScroll.js';
import Option from './Option.js';
import OptionGroup from './OptionGroup.js';
import '../../_chunks/dep-eca3a3de.js';
import '../../_chunks/dep-026a4c6b.js';
import '../../config-provider/index.js';
import '../../config-provider/ConfigProvider.js';
import '../../config-provider/ConfigContext.js';
import '../../locale/zh_CN.js';
import '../../_chunks/dep-e29214cb.js';
import 'dayjs';
import '../../_chunks/dep-3c9ab31a.js';
import '../../config-provider/type.js';
import '../../_chunks/dep-87d110df.js';
import '../../hooks/useVirtualScroll.js';
import '../../hooks/useDomRefCallback.js';
import '../../hooks/useRipple.js';
import '../../_chunks/dep-c48e2ca1.js';
import '../../hooks/useAnimation.js';
import '../../_chunks/dep-3a09424a.js';
import '../defaultProps.js';
import '../../hooks/useDefaultProps.js';
var _excluded = ["group", "divider"],
_excluded2 = ["value", "label", "disabled", "content", "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;
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 selectableOptions = useMemo(function () {
var uniqueOptions = {};
propsOptions === null || propsOptions === void 0 || propsOptions.forEach(function (option) {
if (option.group) {
option.children.forEach(function (item) {
if (!item.disabled && !item.checkAll) {
uniqueOptions[item.value] = item;
}
});
} else if (!option.disabled && !option.checkAll) {
uniqueOptions[option.value] = option;
}
});
return Object.values(uniqueOptions);
}, [propsOptions]);
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
if (!children && !propsOptions) {
return null;
}
var onSelect = function onSelect(selectedValue, _ref) {
var label = _ref.label,
selected = _ref.selected,
event = _ref.event,
restData = _ref.restData;
var isValObj = valueType === "object";
var objVal = {};
if (isValObj) {
objVal = _objectSpread({}, restData);
if (!(keys !== null && keys !== void 0 && keys.label)) {
Object.assign(objVal, {
label: label
});
}
if (!(keys !== null && keys !== void 0 && keys.value)) {
Object.assign(objVal, {
value: selectedValue
});
}
}
if (!Object.keys(objVal).length) {
Object.assign(objVal, _defineProperty(_defineProperty({}, (keys === null || keys === void 0 ? void 0 : keys.label) || "label", label), (keys === null || keys === void 0 ? void 0 : keys.value) || "value", 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 _renderOptions = function renderOptions(options) {
if (options) {
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) {
return /* @__PURE__ */React.createElement(OptionGroup, {
label: group,
divider: divider,
key: index
}, _renderOptions(rest.children));
}
var optionValue = item.value,
label = item.label,
disabled = item.disabled,
content = item.content,
children2 = item.children,
restData = _objectWithoutProperties(item, _excluded2);
return /* @__PURE__ */React.createElement(Option, _objectSpread(_objectSpread({
key: index,
max: max,
label: label,
value: optionValue,
onSelect: onSelect,
selectedValue: value,
optionLength: selectableOptions.length,
multiple: multiple,
size: size,
disabled: disabled,
restData: restData,
keys: keys,
content: content,
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), children2);
}));
}
return /* @__PURE__ */React.createElement("ul", {
className: "".concat(classPrefix, "-select__list")
}, childrenWithProps);
};
var isEmpty = Array.isArray(childrenWithProps) && !childrenWithProps.length || propsOptions && propsOptions.length === 0;
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);
}
return /* @__PURE__ */React.createElement(React.Fragment, null, panelTopContent, renderPanel(propsOptions), panelBottomContent);
});
export { PopupContent as default };
//# sourceMappingURL=PopupContent.js.map