UNPKG

@nutui/nutui-react

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

186 lines (185 loc) 7.79 kB
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 React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react"; import classNames from "classnames"; import { CSSTransition } from "react-transition-group"; import { Check } from "@nutui/icons-react"; import { Overlay } from "../overlay/overlay"; import useClickAway from "../../hooks/use-click-away"; import { ComponentDefaults } from "../../utils/typings"; import { usePropsValue } from "../../hooks/use-props-value"; import { getScrollParent } from "../../utils/get-scroll-parent"; 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]; useEffect(function() { setShowPopup(show); }, [ show ]); var getParentOffset = useCallback(function() { setTimeout(function() { var p = parent.menuRef.current; if (p) { var rect = p.getBoundingClientRect(); setPosition({ height: rect.height, top: rect.top }); } }); }, [ parent.menuRef ]); useEffect(function() { getParentOffset(); }, [ showPopup, getParentOffset ]); 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 isShow = function() { if (showPopup) return {}; return { display: 'none' }; }; var _useState1 = _sliced_to_array(useState({ top: 0, height: 0 }), 2), position = _useState1[0], setPosition = _useState1[1]; var scrollParent = useMemo(function() { return getScrollParent(parent.menuRef, window); }, [ parent.menuRef ]); useEffect(function() { if (!parent.lockScroll) { scrollParent === null || scrollParent === void 0 ? void 0 : scrollParent.addEventListener('scroll', getParentOffset, false); return function() { scrollParent === null || scrollParent === void 0 ? void 0 : scrollParent.removeEventListener('scroll', getParentOffset, false); }; } }, [ parent.lockScroll, scrollParent, getParentOffset ]); var getPosition = function() { return direction === 'down' ? { top: "".concat(position.top + position.height, "px"), bottom: '0', height: 'initial' } : { bottom: "".concat(window.innerHeight - position.top, "px"), top: 'auto', height: 'initial' }; }; var micRef = useRef(null); var cssRef = useRef(null); var targetSet = [ micRef.current ]; useClickAway(function() { parent.hideMenuItem(index); }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore targetSet, 'click', false, showPopup, closeOnClickAway); return /*#__PURE__*/ React.createElement("div", { className: "nut-menu-container", ref: micRef }, 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("div", { 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("div", { className: "nut-menu-container-content" }, options === null || options === void 0 ? void 0 : options.map(function(item) { return /*#__PURE__*/ React.createElement("div", { 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("i", { className: "nut-menu-container-item-icon" }, icon || /*#__PURE__*/ React.createElement(Check, { color: activeColor, className: getIconCName(item.value, value) })) : null, /*#__PURE__*/ React.createElement("div", { className: "nut-menu-container-item-title ".concat(getIconCName(item.value, value)), style: { color: "".concat(item.value === innerValue ? activeColor : '') } }, item.text)); }), children)))); }); MenuItem.displayName = 'NutMenuItem';