UNPKG

@douyinfe/semi-ui

Version:

A modern, comprehensive, flexible design system and UI library. Connect DesignOps & DevOps. Quickly build beautiful React apps. Maintained by Douyin-fe team.

191 lines 6.07 kB
import _noop from "lodash/noop"; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React from 'react'; import cls from 'classnames'; import PropTypes from 'prop-types'; import ConfigContext from '../configProvider/context'; import { cssClasses, numbers, strings } from '@douyinfe/semi-foundation/lib/es/notification/constants'; import NotificationFoundation from '@douyinfe/semi-foundation/lib/es/notification/notificationFoundation'; import Button from '../iconButton'; import BaseComponent from '../_base/baseComponent'; import { getDefaultPropsFromGlobalConfig, isSemiIcon } from '../_utils'; import { IconAlertCircle, IconAlertTriangle, IconClose, IconInfoCircle, IconTickCircle } from '@douyinfe/semi-icons'; import { getUuidShort } from '@douyinfe/semi-foundation/lib/es/utils/uuid'; const prefixCls = cssClasses.NOTICE; const { duration } = numbers; const { types, themes, directions } = strings; class Notice extends BaseComponent { get adapter() { return Object.assign(Object.assign({}, super.adapter), { notifyWrapperToRemove: id => { this.props.close(id); }, notifyClose: () => { this.props.onClose(); this.props.onHookClose && this.props.onHookClose(); } }); } constructor(props) { super(props); this.clearCloseTimer = () => { this.foundation._clearCloseTimer(); }; this.startCloseTimer = () => { this.foundation._startCloseTimer(); }; this.close = e => { this.props.onCloseClick(this.props.id); this.foundation.close(e); }; this.notifyClick = e => { this.props.onClick(e); }; this.state = { visible: true }; this.foundation = new NotificationFoundation(this.adapter); } componentWillUnmount() { this.foundation.destroy(); } renderTypeIcon() { const { type, icon } = this.props; const iconMap = { warning: /*#__PURE__*/React.createElement(IconAlertTriangle, { size: "large" }), success: /*#__PURE__*/React.createElement(IconTickCircle, { size: "large" }), info: /*#__PURE__*/React.createElement(IconInfoCircle, { size: "large" }), error: /*#__PURE__*/React.createElement(IconAlertCircle, { size: "large" }) }; let iconType = iconMap[type]; const iconCls = cls({ [`${prefixCls}-icon`]: true, [`${prefixCls}-${type}`]: true }); if (icon) { iconType = icon; } if (iconType) { return /*#__PURE__*/React.createElement("div", { className: iconCls, "x-semi-prop": "icon" }, isSemiIcon(iconType) ? /*#__PURE__*/React.cloneElement(iconType, { size: iconType.props.size || 'large' }) : iconType); } return null; } render() { const direction = this.props.direction || this.context.direction; const defaultPosition = direction === 'rtl' ? 'topLeft' : 'topRight'; const _a = this.props, { content, title, theme, position = defaultPosition, type, id, onCloseClick, className, showClose, style } = _a, attr = __rest(_a, ["content", "title", "theme", "position", "type", "id", "onCloseClick", "className", "showClose", "style"]); const { visible } = this.state; const wrapper = cls(prefixCls, className, { [`${prefixCls}-close`]: !visible, [`${prefixCls}-icon-show`]: types.includes(type), [`${prefixCls}-${type}`]: true, [`${prefixCls}-${theme}`]: theme === 'light', [`${prefixCls}-rtl`]: direction === 'rtl' }); const titleID = getUuidShort({}); return /*#__PURE__*/React.createElement("div", { className: wrapper, style: style, onMouseEnter: this.clearCloseTimer, onMouseLeave: this.startCloseTimer, onClick: this.notifyClick, "aria-labelledby": titleID, role: 'alert', onAnimationEnd: this.props.onAnimationEnd, onAnimationStart: this.props.onAnimationStart }, /*#__PURE__*/React.createElement("div", null, this.renderTypeIcon()), /*#__PURE__*/React.createElement("div", { className: `${prefixCls}-inner` }, /*#__PURE__*/React.createElement("div", { className: `${prefixCls}-content-wrapper` }, title ? ( /*#__PURE__*/React.createElement("div", { id: titleID, className: `${prefixCls}-title`, "x-semi-prop": "title" }, title)) : '', content ? ( /*#__PURE__*/React.createElement("div", { className: `${prefixCls}-content`, "x-semi-prop": "content" }, content)) : ''), showClose && ( /*#__PURE__*/React.createElement(Button, { className: `${prefixCls}-icon-close`, type: "tertiary", icon: /*#__PURE__*/React.createElement(IconClose, null), theme: "borderless", size: "small", onClick: this.close })))); } } Notice.contextType = ConfigContext; Notice.propTypes = { duration: PropTypes.number, id: PropTypes.string, title: PropTypes.node, content: PropTypes.node, type: PropTypes.oneOf(types), theme: PropTypes.oneOf(themes), icon: PropTypes.node, onClick: PropTypes.func, onClose: PropTypes.func, onCloseClick: PropTypes.func, showClose: PropTypes.bool, // private props close: PropTypes.func, direction: PropTypes.oneOf(directions) }; Notice.__SemiComponentName__ = "Notification"; Notice.defaultProps = getDefaultPropsFromGlobalConfig(Notice.__SemiComponentName__, { duration, id: '', close: _noop, onClose: _noop, onClick: _noop, onCloseClick: _noop, content: '', title: '', showClose: true, theme: 'normal' }); export default Notice;