@gizwits/vantui
Version:
机智云组件库
197 lines • 7.36 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["title", "titleClass", "value", "popupStyle", "disabled", "setChildrenInstance", "parentInstance", "index", "onOpen", "onOpened", "onClose", "onClosed", "onChange", "options", "className", "style"];
import { View } from '@tarojs/components';
import { useState, useEffect, useCallback, useImperativeHandle, forwardRef, memo } from 'react';
import { nextTick } from '@tarojs/taro';
import * as utils from '../wxs/utils';
import VanIcon from '../icon/index';
import VanCell from '../cell';
import VanPopup from '../popup';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function Index(props, ref) {
var title = props.title,
titleClass = props.titleClass,
value = props.value,
popupStyle = props.popupStyle,
_props$disabled = props.disabled,
disabled = _props$disabled === void 0 ? false : _props$disabled,
setChildrenInstance = props.setChildrenInstance,
parentInstance = props.parentInstance,
index = props.index,
onOpen = props.onOpen,
onOpened = props.onOpened,
onClose = props.onClose,
onClosed = props.onClosed,
_props$onChange = props.onChange,
onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
_props$options = props.options,
options = _props$options === void 0 ? [] : _props$options,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className,
style = props.style,
others = _objectWithoutProperties(props, _excluded);
var _useState = useState({}),
_useState2 = _slicedToArray(_useState, 2),
wrapperStyle = _useState2[0],
setWrapperStyle = _useState2[1];
var _useState3 = useState(true),
_useState4 = _slicedToArray(_useState3, 2),
transition = _useState4[0],
setTransition = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
showPopup = _useState6[0],
setShowPopup = _useState6[1];
var _useState7 = useState(true),
_useState8 = _slicedToArray(_useState7, 2),
showWrapper = _useState8[0],
setShowWrapper = _useState8[1];
var _useState9 = useState(''),
_useState10 = _slicedToArray(_useState9, 2),
displayTitle = _useState10[0],
setDisplayTitle = _useState10[1];
var _useState11 = useState(''),
_useState12 = _slicedToArray(_useState11, 2),
value_ = _useState12[0],
setValue = _useState12[1];
useEffect(function () {
setValue(value);
}, [value]);
var rerender = useCallback(function () {
nextTick(function () {
if (parentInstance) {
parentInstance.updateItemListData();
}
});
}, [parentInstance]);
var toggle = useCallback(function (show) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (typeof show !== 'boolean') {
show = !showPopup;
}
if (show === showPopup) {
return;
}
setTransition(!options.immediate);
setShowPopup(show);
if (show) {
!parentInstance ? void 0 : parentInstance.getChildWrapperStyle().then(function (wrapperStyle) {
var rect = wrapperStyle.rect;
delete wrapperStyle.rect;
if (wrapperStyle) {
wrapperStyle.width = '100vw';
wrapperStyle.position = 'absolute';
}
if (parentInstance.direction === 'down') {
wrapperStyle.top = rect.height + 'PX';
wrapperStyle.height = '100vh';
setWrapperStyle(wrapperStyle);
setShowWrapper(true);
rerender();
}
if (parentInstance.direction === 'up') {
wrapperStyle.height = '100vh';
wrapperStyle.top = 0;
wrapperStyle.transform = 'translateY(-100%)';
wrapperStyle.WebkitTransform = 'translateY(-100%)';
wrapperStyle.MozTransform = 'translateY(-100%)';
wrapperStyle.OTransform = 'translateY(-100%)';
setWrapperStyle(wrapperStyle);
setShowWrapper(true);
rerender();
}
});
} else {
rerender();
}
}, [showPopup, parentInstance, rerender]);
useEffect(function () {
setChildrenInstance(index, {
title: title,
titleClass: titleClass,
disabled: disabled,
transition: transition,
showPopup: showPopup,
index: index,
setDisplayTitle: setDisplayTitle,
displayTitle: displayTitle,
options: options,
value: value_,
toggle: toggle
});
}, [title, titleClass, index, setChildrenInstance, disabled, transition, showPopup, setDisplayTitle, displayTitle, options, value_, toggle]);
var onClosed_ = useCallback(function () {
if (onClosed) onClosed();
setTimeout(function () {
setShowWrapper(false);
}, 10);
}, [onClosed]);
var onOptionTap = function onOptionTap(_event, option) {
var shouldEmitChange = value_ !== option.value;
setShowPopup(false);
setValue(option.value);
if (onClose) onClose();
rerender();
if (shouldEmitChange) {
onChange(option.value);
}
};
useImperativeHandle(ref, function () {
return {
toggle: toggle
};
});
return showWrapper ? /*#__PURE__*/_jsx(View, {
className: utils.bem('dropdown-item', parentInstance.direction) + ' ' + className,
style: utils.style([wrapperStyle, style]),
children: /*#__PURE__*/_jsx(VanPopup, {
show: showPopup,
style: utils.style([{
position: 'absolute'
}, popupStyle]),
overlayStyle: "position: absolute;",
overlay: !!parentInstance.overlay,
position: parentInstance.direction === 'down' ? 'top' : 'bottom',
duration: transition ? parentInstance.duration : 0,
closeOnClickOverlay: parentInstance.closeOnClickOverlay,
onEnter: onOpen,
onLeave: onClose,
onClose: toggle,
onAfterEnter: onOpened,
onAfterLeave: onClosed_,
children: /*#__PURE__*/_jsxs(View, {
children: [(options || []).map(function (item, index) {
return /*#__PURE__*/_jsx(VanCell, {
"data-option": item,
className: utils.bem('dropdown-item__option', {
active: item.value === value_
}),
clickable: true,
icon: item.icon,
onClick: function onClick(e) {
return onOptionTap(e, item);
},
renderTitle: /*#__PURE__*/_jsx(_Fragment, {
children: /*#__PURE__*/_jsx(View, {
className: "van-dropdown-item__title",
style: item.value === value_ ? 'color:' + parentInstance.activeColor : '',
children: item.text
})
}),
children: item.value === value_ && /*#__PURE__*/_jsx(VanIcon, {
name: "success",
className: "van-dropdown-item__icon",
color: parentInstance.activeColor
})
}, "".concat(index, "VanCell"));
}), others.children]
})
})
}) : /*#__PURE__*/_jsx(_Fragment, {});
}
var DropdownItem = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(Index));
export { DropdownItem };
export default DropdownItem;