tdesign-react
Version:
TDesign Component for React
139 lines (135 loc) • 6.52 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _defineProperty } from '../_chunks/dep-d67deb2c.js';
import React, { useRef, useEffect } from 'react';
import classNames from 'classnames';
import { CSSTransition } from 'react-transition-group';
import { useCollapseContext } from './CollapseContext.js';
import FakeArrow from '../common/FakeArrow.js';
import useConfig from '../hooks/useConfig.js';
import { collapsePanelDefaultProps } from './defaultProps.js';
import useDefaultProps from '../hooks/useDefaultProps.js';
import '../_chunks/dep-8abcbcbc.js';
import '../config-provider/ConfigContext.js';
import 'lodash-es';
import '../_chunks/dep-6b235a32.js';
import '../_chunks/dep-b9afa32b.js';
import 'dayjs';
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 CollapsePanel = function CollapsePanel(props) {
var _useDefaultProps = useDefaultProps(props, collapsePanelDefaultProps),
value = _useDefaultProps.value,
disabled = _useDefaultProps.disabled,
destroyOnCollapse = _useDefaultProps.destroyOnCollapse,
expandIcon = _useDefaultProps.expandIcon,
className = _useDefaultProps.className,
style = _useDefaultProps.style,
header = _useDefaultProps.header,
headerRightContent = _useDefaultProps.headerRightContent,
children = _useDefaultProps.children,
index = _useDefaultProps.index;
var _useCollapseContext = useCollapseContext(),
disableAll = _useCollapseContext.disabled,
defaultExpandAll = _useCollapseContext.defaultExpandAll,
expandIconPlacement = _useCollapseContext.expandIconPlacement,
expandOnRowClick = _useCollapseContext.expandOnRowClick,
expandIconAll = _useCollapseContext.expandIcon,
updateCollapseValue = _useCollapseContext.updateCollapseValue,
collapseValue = _useCollapseContext.collapseValue;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix;
var componentName = "".concat(classPrefix, "-collapse-panel");
var innerValue = value || index;
var finalExpandIcon = (expandIcon === void 0 ? expandIconAll : expandIcon) || null;
var headRef = useRef(null);
var iconRef = useRef(null);
var contentRef = useRef(null);
var bodyRef = useRef(null);
var isDisabled = disabled || !!disableAll;
useEffect(function () {
if (defaultExpandAll) {
updateCollapseValue(innerValue);
}
}, []);
var isActive = Array.isArray(collapseValue) ? collapseValue.includes(innerValue) : collapseValue === innerValue;
var classes = classNames(componentName, _defineProperty({}, "".concat(classPrefix, "-is-disabled"), isDisabled), className);
var handleClick = function handleClick(e) {
var canExpand = expandOnRowClick && e.currentTarget === headRef.current || e.currentTarget === iconRef.current;
if (canExpand && !isDisabled) {
updateCollapseValue(innerValue, {
e: e
});
}
e.stopPropagation();
};
var renderIcon = function renderIcon() {
var iconNode = null;
if (/*#__PURE__*/React.isValidElement(finalExpandIcon)) {
iconNode = finalExpandIcon;
} else if (finalExpandIcon) {
iconNode = /* @__PURE__ */React.createElement(FakeArrow, {
className: classNames("".concat(componentName, "__icon--default"))
});
}
return iconNode && /* @__PURE__ */React.createElement("div", {
className: "".concat(componentName, "__icon ").concat(componentName, "__icon--").concat(expandIconPlacement, " ").concat(isActive ? "".concat(componentName, "__icon--active") : ""),
ref: iconRef,
onClick: handleClick
}, iconNode);
};
var renderHeader = function renderHeader() {
var cls = ["".concat(componentName, "__header"), _defineProperty({}, "".concat(classPrefix, "-is-clickable"), expandOnRowClick && !isDisabled)];
return /* @__PURE__ */React.createElement("div", {
ref: headRef,
className: classNames(cls),
onClick: handleClick
}, expandIconPlacement === "left" && renderIcon(), header, /* @__PURE__ */React.createElement("div", {
className: "".concat(componentName, "__header--blank")
}), headerRightContent, expandIconPlacement === "right" && renderIcon());
};
var renderBody = function renderBody() {
var transitionCallbacks = {
onEnter: function onEnter() {
bodyRef.current.style.height = "".concat(contentRef === null || contentRef === void 0 ? void 0 : contentRef.current.clientHeight, "px");
},
onEntered: function onEntered() {
bodyRef.current.style.height = "auto";
},
onExit: function onExit() {
bodyRef.current.style.height = "".concat(contentRef === null || contentRef === void 0 ? void 0 : contentRef.current.clientHeight, "px");
},
onExiting: function onExiting() {
bodyRef.current.style.height = "0px";
}
};
return /* @__PURE__ */React.createElement(CSSTransition, _objectSpread({
"in": isActive,
appear: true,
timeout: 300,
nodeRef: bodyRef,
unmountOnExit: destroyOnCollapse
}, transitionCallbacks), /* @__PURE__ */React.createElement("div", {
style: {
height: 0
},
className: classNames("".concat(componentName, "__body"), "".concat(classPrefix, "-slide-down-enter-active"), _defineProperty({}, "".concat(componentName, "__body--collapsed"), !isActive)),
ref: bodyRef
}, /* @__PURE__ */React.createElement("div", {
className: "".concat(componentName, "__content"),
ref: contentRef
}, children)));
};
return /* @__PURE__ */React.createElement("div", {
className: classes,
style: _objectSpread({}, style)
}, /* @__PURE__ */React.createElement("div", {
className: "".concat(componentName, "__wrapper")
}, renderHeader(), renderBody()));
};
CollapsePanel.displayName = "CollapsePanel";
export { CollapsePanel as default };
//# sourceMappingURL=CollapsePanel.js.map