@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
156 lines • 6.76 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["size", "name", "title", "value", "icon", "label", "disabled", "clickable", "border", "isLink", "renderTitle", "renderIcon", "renderRightIcon", "renderValue", "style", "className", "children", "index", "isOpen", "handleToggle", "rectWrapper"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useState, useEffect, useRef, useCallback } from 'react';
import { View } from '@tarojs/components';
import { nextTick } from '@tarojs/taro';
import * as utils from '../wxs/utils';
import { getRect } from '../common/utils';
import VanCell from '../cell/index';
import { get } from '../default-props';
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var d = get().CollapseItem;
var compIndex = 0;
export function CollapseItem(props) {
var _d$props = _objectSpread(_objectSpread({}, d), props),
size = _d$props.size,
_d$props$name = _d$props.name,
name = _d$props$name === void 0 ? null : _d$props$name,
_d$props$title = _d$props.title,
title = _d$props$title === void 0 ? '' : _d$props$title,
_d$props$value = _d$props.value,
value = _d$props$value === void 0 ? '' : _d$props$value,
icon = _d$props.icon,
label = _d$props.label,
_d$props$disabled = _d$props.disabled,
disabled = _d$props$disabled === void 0 ? false : _d$props$disabled,
_d$props$clickable = _d$props.clickable,
clickable = _d$props$clickable === void 0 ? false : _d$props$clickable,
_d$props$border = _d$props.border,
border = _d$props$border === void 0 ? true : _d$props$border,
_d$props$isLink = _d$props.isLink,
isLink = _d$props$isLink === void 0 ? true : _d$props$isLink,
renderTitle = _d$props.renderTitle,
renderIcon = _d$props.renderIcon,
renderRightIcon = _d$props.renderRightIcon,
renderValue = _d$props.renderValue,
style = _d$props.style,
className = _d$props.className,
children = _d$props.children,
index = _d$props.index,
isOpen = _d$props.isOpen,
handleToggle = _d$props.handleToggle,
_d$props$rectWrapper = _d$props.rectWrapper,
rectWrapper = _d$props$rectWrapper === void 0 ? '.van-collapse' : _d$props$rectWrapper,
others = _objectWithoutProperties(_d$props, _excluded);
var _useState = useState('auto'),
_useState2 = _slicedToArray(_useState, 2),
currHeight = _useState2[0],
setCurrHeight = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
init = _useState4[0],
setInit = _useState4[1];
var _useState5 = useState(compIndex++),
_useState6 = _slicedToArray(_useState5, 1),
curCompIndex = _useState6[0];
var nextActionTimeout = useRef(null);
var _useState7 = useState(-1),
_useState8 = _slicedToArray(_useState7, 2),
domHeight = _useState8[0],
setDomHeight = _useState8[1];
var isFirstRender = useRef(true);
useEffect(function () {
setTimeout(function () {
var targetId = "#content-class".concat(curCompIndex);
getRect(null, "".concat(targetId), rectWrapper).then(function (res) {
if (res) {
setDomHeight(res.height);
nextTick(function () {
setInit(true);
});
}
});
}, 33.33);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children]);
useEffect(function () {
if (domHeight !== -1) {
if (nextActionTimeout.current) clearTimeout(nextActionTimeout.current);
if (isOpen) {
setCurrHeight("".concat(domHeight, "px"));
isFirstRender.current = false;
nextActionTimeout.current = setTimeout(function () {
setCurrHeight('auto');
}, 200);
} else {
if (!isFirstRender.current) {
setCurrHeight("".concat(domHeight, "px"));
nextActionTimeout.current = setTimeout(function () {
setCurrHeight('0px');
}, 200);
}
}
} else {
setCurrHeight('0px');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isOpen, domHeight, init]);
var handleToggle_ = useCallback(function () {
if (disabled) return;
isFirstRender.current = false;
handleToggle && handleToggle(isOpen, name);
}, [handleToggle, disabled, isOpen, name]);
return /*#__PURE__*/_jsxs(View, _objectSpread(_objectSpread({
className: 'van-collapse-item ' + (index !== 0 ? 'van-hairline--top' : '') + " ".concat(className || ''),
style: style
}, others), {}, {
children: [/*#__PURE__*/_jsx(VanCell, {
title: title,
icon: icon,
value: value,
label: label,
isLink: isLink,
clickable: clickable,
size: size,
border: border && isOpen,
className: utils.bem('collapse-item__title', {
disabled: disabled,
expanded: isOpen
}) + ' van-cell',
onClick: handleToggle_,
renderTitle: /*#__PURE__*/_jsx(_Fragment, {
children: renderTitle
}),
renderIcon: /*#__PURE__*/_jsx(_Fragment, {
children: renderIcon
}),
renderRightIcon: /*#__PURE__*/_jsx(_Fragment, {
children: renderRightIcon
}),
children: renderValue
}), /*#__PURE__*/_jsx(View, {
style: {
height: init ? 'auto' : 0,
overflow: 'hidden'
},
children: /*#__PURE__*/_jsx(View, {
className: "van-collapse-item__content content-class",
style: {
height: currHeight
},
children: /*#__PURE__*/_jsx(View, {
id: "content-class".concat(curCompIndex),
className: "van-collapse-item__content_wrapper",
children: children
})
})
})]
}));
}
export default CollapseItem;