UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

105 lines 7.78 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } import { rpx2vw4style as __rpx2vw__ } from "@alifd/babel-runtime-jsx-style-transform"; function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { __rest } from "tslib"; import React, { createElement, forwardRef } from "react"; import classNames from 'classnames'; import { Text } from "@alifd/meet-react-component-one"; import View from '../view'; import Icon from '../icon'; import { isString, isFunction } from '../utils'; import { useValue } from '../utils/hooks'; var ICON_MAP = { success: 'success', warning: 'warning', error: 'error', notice: 'prompt', help: 'help', loading: 'loading' }; var Message = function Message(props, ref) { var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, className = props.className, _props$shape = props.shape, shape = _props$shape === void 0 ? 'inline' : _props$shape, type = props.type, visibleProp = props.visible, _props$defaultVisible = props.defaultVisible, defaultVisible = _props$defaultVisible === void 0 ? false : _props$defaultVisible, title = props.title, iconType = props.iconType, _props$closable = props.closable, closable = _props$closable === void 0 ? false : _props$closable, onClose = props.onClose, children = props.children, zIndex = props.zIndex, style = props.style, others = __rest(props, ["prefix", "className", "shape", "type", "visible", "defaultVisible", "title", "iconType", "closable", "onClose", "children", "zIndex", "style"]); var newStyle = Object.assign({ zIndex: zIndex }, style); var _useValue = useValue(props, false, { valueName: 'visible' }), _useValue2 = _slicedToArray(_useValue, 3), visible = _useValue2[0], setVisible = _useValue2[1], isControlled = _useValue2[2]; if (!visible) { return null; } var clsPrefix = "".concat(prefix, "message"); var toastContent = title || children; var isToast = shape === 'toast'; var hideIcon = iconType === null || iconType === '' || isToast && !type; var isSimpleMode = shape === 'inline' && !title && children; var handleClose = function handleClose() { if (!isControlled) { setVisible(false); } if (isFunction(onClose)) { onClose(); } }; return /*#__PURE__*/React.createElement(View, _extends({ ref: ref, className: classNames(clsPrefix, "".concat(clsPrefix, "--").concat(type), "".concat(clsPrefix, "--").concat(shape), className, _defineProperty(_defineProperty(_defineProperty({}, "".concat(clsPrefix, "--").concat(shape, "-").concat(type), shape && type), "".concat(clsPrefix, "--inline--no-title"), shape === 'inline' && !toastContent), "".concat(clsPrefix, "--toast-with-icon"), shape === 'toast' && !hideIcon)), style: __rpx2vw__(newStyle) }, others), !hideIcon && /*#__PURE__*/React.createElement(View, { className: "".concat(clsPrefix, "-icon-container--").concat(shape) }, /*#__PURE__*/React.createElement(Icon, { name: iconType || ICON_MAP[type] || type, className: classNames("".concat(clsPrefix, "-icon"), "".concat(clsPrefix, "-icon--").concat(type), "".concat(clsPrefix, "-icon--").concat(shape)) })), /*#__PURE__*/React.createElement(View, { className: "".concat(clsPrefix, "-container ").concat(clsPrefix, "-container--").concat(shape) }, title && !isToast && /*#__PURE__*/React.createElement(View, { className: classNames("".concat(clsPrefix, "-title")) }, isString(title) ? /*#__PURE__*/React.createElement(Text, { className: classNames("".concat(clsPrefix, "-title"), _defineProperty({}, "".concat(clsPrefix, "-title--").concat(type), isSimpleMode)), numberOfLines: 1 }, title || children) : title), /*#__PURE__*/React.createElement(View, { className: classNames("".concat(clsPrefix, "-content-container"), "".concat(clsPrefix, "-content-container--").concat(shape)) }, isToast && isString(toastContent) ? /*#__PURE__*/React.createElement(Text, { className: classNames("".concat(clsPrefix, "-content"), "".concat(clsPrefix, "-content--").concat(shape)), numberOfLines: 20 }, toastContent) : null, isToast && !isString(toastContent) ? toastContent : null, !isToast && children ? /*#__PURE__*/React.createElement(View, { className: classNames("".concat(clsPrefix, "-content--inline"), _defineProperty(_defineProperty(_defineProperty({}, "".concat(clsPrefix, "-content--no-title"), shape === 'inline' && !title), "".concat(clsPrefix, "-content--inline-").concat(type), isSimpleMode), "".concat(clsPrefix, "-content--inline-simple"), isSimpleMode)) }, children) : null)), closable && !isToast && /*#__PURE__*/React.createElement(Icon, { name: "close", size: "s", className: classNames("".concat(clsPrefix, "-close"), _defineProperty({}, "".concat(clsPrefix, "-close--").concat(type), isSimpleMode)), onClick: handleClose })); }; Message.displayName = 'Message'; export default /*#__PURE__*/forwardRef(Message);