@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
218 lines (217 loc) • 9.17 kB
JavaScript
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import classNames from "classnames";
import { usePageScroll } from "@tarojs/taro";
import { View } from "@tarojs/components";
import { CSSTransition } from "react-transition-group";
import { Check } from "@nutui/icons-react-taro";
import { getWindowInfo } from "../../utils/taro/get-system-info";
import { Overlay } from "../overlay/overlay";
import { getRectInMultiPlatform } from "../../utils/taro/get-rect";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
titleIcon: null,
columns: 1,
direction: 'down',
icon: null,
closeOnClickAway: true,
activeTitleClass: '',
inactiveTitleClass: '',
onChange: function(value) {
return undefined;
}
});
export var MenuItem = /*#__PURE__*/ forwardRef(function(props, ref) {
var _$_object_spread = _object_spread({}, defaultProps, props), className = _$_object_spread.className, style = _$_object_spread.style, options = _$_object_spread.options, value = _$_object_spread.value, defaultValue = _$_object_spread.defaultValue, columns = _$_object_spread.columns, title = _$_object_spread.title, icon = _$_object_spread.icon, direction = _$_object_spread.direction, onChange = _$_object_spread.onChange, activeTitleClass = _$_object_spread.activeTitleClass, inactiveTitleClass = _$_object_spread.inactiveTitleClass, closeOnClickAway = _$_object_spread.closeOnClickAway, children = _$_object_spread.children, activeColor = _$_object_spread.activeColor, show = _$_object_spread.show, parent = _$_object_spread.parent, index = _$_object_spread.index;
var _useState = _sliced_to_array(useState(show), 2), showPopup = _useState[0], setShowPopup = _useState[1];
var _usePropsValue = _sliced_to_array(usePropsValue({
defaultValue: defaultValue,
value: value,
finalValue: undefined,
onChange: function(v) {
var _options_filter = _sliced_to_array(options.filter(function(o) {
return o.value === v;
}), 1), option = _options_filter[0];
onChange === null || onChange === void 0 ? void 0 : onChange(option);
}
}), 2), innerValue = _usePropsValue[0], setValue = _usePropsValue[1];
var cssRef = useRef(null);
useEffect(function() {
setShowPopup(show);
}, [
show
]);
var getParentOffset = useCallback(function() {
setTimeout(function() {
return _async_to_generator(function() {
var p, rect;
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
p = parent.menuRef.current;
return [
4,
getRectInMultiPlatform(p)
];
case 1:
rect = _state.sent();
setPosition({
height: rect.height,
top: rect.top
});
return [
2
];
}
});
})();
}, 100);
}, [
parent.menuRef
]);
useEffect(function() {
getParentOffset();
}, [
showPopup,
getParentOffset
]);
var windowHeight = useMemo(function() {
return getWindowInfo().windowHeight;
}, []);
var updateItemOffset = useCallback(function() {
if (!parent.lockScroll) return;
var p = parent.menuRef.current;
getRectInMultiPlatform(p).then(function(rect) {
if (rect) {
setPosition({
height: rect.height,
top: rect.top
});
}
});
}, [
direction,
windowHeight,
parent.lockScroll,
parent.menuRef
]);
usePageScroll(updateItemOffset);
useImperativeHandle(ref, function() {
return {
toggle: function(s) {
var from = 'REF';
s ? parent.toggleMenuItem(index, from) : parent.hideMenuItem(index, from);
}
};
});
var getIconCName = function(optionVal, value) {
var _obj;
return classNames((_obj = {}, _define_property(_obj, activeTitleClass, optionVal === value), _define_property(_obj, inactiveTitleClass, optionVal !== value), _obj));
};
var setTitle = function(text) {
if (!title) {
parent.updateTitle(text, index);
}
};
var handleClick = function(item) {
parent.toggleMenuItem(index);
setTitle(item.text);
setValue(item.value);
};
var _useState1 = _sliced_to_array(useState({
top: 0,
height: 0
}), 2), position = _useState1[0], setPosition = _useState1[1];
var isShow = function() {
if (showPopup) return {};
return {
display: 'none'
};
};
var getPosition = function() {
return direction === 'down' ? {
top: "".concat(position.top + position.height, "px"),
bottom: '0',
height: 'initial'
} : {
bottom: "".concat(getWindowInfo().windowHeight - position.top, "px"),
top: '0',
height: 'initial'
};
};
var placeholderStyle = function() {
if (direction === 'down') {
return _object_spread({
height: "".concat(position.top + position.height, "px")
}, isShow());
}
return _object_spread({
height: "".concat(getWindowInfo().windowHeight - position.top, "px"),
top: 'auto'
}, isShow());
};
return /*#__PURE__*/ React.createElement(View, {
className: "nut-menu-container"
}, closeOnClickAway ? /*#__PURE__*/ React.createElement(View, {
className: "nut-menu-placeholder-element ".concat(classNames({
up: direction === 'up'
})),
style: placeholderStyle(),
onClick: function() {
return parent.toggleMenuItem(index);
}
}) : null, parent.overlay ? /*#__PURE__*/ React.createElement(Overlay, {
className: "nut-menu-container-overlay",
style: getPosition(),
lockScroll: parent.lockScroll,
visible: showPopup,
closeOnOverlayClick: parent.closeOnOverlayClick,
onClick: function() {
parent.closeOnOverlayClick && parent.hideMenuItem(index);
}
}) : null, /*#__PURE__*/ React.createElement(View, {
className: classNames({
'nut-menu-container-wrap': direction === 'down',
'nut-menu-container-wrap-up': direction !== 'down'
}, className),
style: _object_spread({}, style, isShow())
}, /*#__PURE__*/ React.createElement(CSSTransition, {
nodeRef: cssRef,
in: showPopup,
timeout: 100,
classNames: direction === 'down' ? 'nut-menu-container-down' : 'nut-menu-container-up'
}, /*#__PURE__*/ React.createElement(View, {
className: "nut-menu-container-content"
}, options === null || options === void 0 ? void 0 : options.map(function(item, index) {
return /*#__PURE__*/ React.createElement(View, {
className: "nut-menu-container-item ".concat(classNames({
active: item.value === innerValue
})),
key: item.text,
style: {
flexBasis: "".concat(100 / columns, "%")
},
onClick: function() {
handleClick(item);
}
}, item.value === innerValue ? /*#__PURE__*/ React.createElement(React.Fragment, null, icon ? /*#__PURE__*/ React.cloneElement(icon, {
className: "nut-menu-container-item-icon ".concat(icon.props.className || '')
}) : /*#__PURE__*/ React.createElement(Check, {
color: activeColor,
size: 16,
className: "nut-menu-container-item-icon ".concat(getIconCName(item.value, value))
})) : null, /*#__PURE__*/ React.createElement(View, {
className: "nut-menu-container-item-title ".concat(getIconCName(item.value, value)),
style: {
color: "".concat(item.value === innerValue ? activeColor : '')
}
}, item.text));
}), children))));
});
MenuItem.displayName = 'NutMenuItem';