UNPKG

@nutui/nutui-react-taro

Version:

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

122 lines (121 loc) 4.85 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, { useState, useEffect, useMemo } from "react"; import classNames from "classnames"; import { CSSTransition } from "react-transition-group"; import { View } from "@tarojs/components"; import { Close } from "@nutui/icons-react-taro"; import { web } from "../../utils/taro/platform"; import { pxTransform } from "../../utils/taro/px-transform"; import { ComponentDefaults } from "../../utils/typings"; import { customEvents, useCustomEvent, useCustomEventsPath } from "../../hooks/taro/use-custom-event"; import { mergeProps } from "../../utils/merge-props"; var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), { id: '', distance: 8, navHeight: 57, position: 'top', visible: false, closeable: false, leftIcon: null, rightIcon: null, duration: 3000, onClose: function() {}, onClick: function() {} }); var classPrefix = 'nut-notify'; export var Notify = function(props) { var _mergeProps = mergeProps(defaultProps, props), id = _mergeProps.id, style = _mergeProps.style, children = _mergeProps.children, distance = _mergeProps.distance, navHeight = _mergeProps.navHeight, closeable = _mergeProps.closeable, leftIcon = _mergeProps.leftIcon, rightIcon = _mergeProps.rightIcon, position = _mergeProps.position, visible = _mergeProps.visible, duration = _mergeProps.duration, className = _mergeProps.className, onClose = _mergeProps.onClose, onClick = _mergeProps.onClick; useCustomEvent(id, function(status) { status ? show() : hide(); }); var timer; var _useState = _sliced_to_array(useState(false), 2), showNotify = _useState[0], setShowNotify = _useState[1]; useEffect(function() { if (visible) { show(); } else { hide(); } }, [ visible ]); var show = function() { setShowNotify(true); clearTimer(); if (duration) { timer = window.setTimeout(function() { hide(); }, duration); } }; var clearTimer = function() { if (timer) { clearTimeout(timer); timer = null; } }; var hide = function() { setShowNotify(false); onClose(); }; var getDistance = useMemo(function() { if (position === 'top') { return { top: pxTransform(web() ? Number(distance) + navHeight : Number(distance)) }; } return { bottom: pxTransform(Number(distance)) }; }, [ distance, position ]); var _obj; var classes = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix), true), _define_property(_obj, "".concat(className), true), _obj)); var handleClick = function() { onClick === null || onClick === void 0 ? void 0 : onClick(); }; var handleClickIcon = function() { hide(); }; var _obj1; return /*#__PURE__*/ React.createElement(CSSTransition, { in: showNotify, timeout: 300, classNames: "fade", unmountOnExit: true, appear: true, position: position, id: id }, /*#__PURE__*/ React.createElement(View, { className: classes, style: _object_spread({}, style, getDistance), onClick: handleClick }, leftIcon ? /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-left-icon") }, leftIcon) : null, /*#__PURE__*/ React.createElement(View, { className: classNames((_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-content"), true), _define_property(_obj1, "".concat(classPrefix, "-ellipsis"), closeable || rightIcon), _define_property(_obj1, "".concat(classPrefix, "-layout-left"), leftIcon || rightIcon), _obj1)) }, children), rightIcon || closeable ? /*#__PURE__*/ React.createElement(View, { className: "".concat(classPrefix, "-right-icon"), onClick: handleClickIcon }, rightIcon || (closeable ? /*#__PURE__*/ React.createElement(Close, { size: 12 }) : null)) : null)); }; export function open(selector) { // eslint-disable-next-line react-hooks/rules-of-hooks var path = useCustomEventsPath(selector); customEvents.trigger(path, true); } export function close(selector) { // eslint-disable-next-line react-hooks/rules-of-hooks var path = useCustomEventsPath(selector); customEvents.trigger(path, false); } Notify.displayName = 'NutNotify'; Notify.open = open; Notify.close = close;