UNPKG

rsuite

Version:

A suite of react components

111 lines (90 loc) 4.58 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); exports.__esModule = true; exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _taggedTemplateLiteralLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/taggedTemplateLiteralLoose")); var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _utils = require("../utils"); var _CloseButton = _interopRequireDefault(require("../CloseButton")); var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5; var Message = /*#__PURE__*/_react.default.forwardRef(function (props, ref) { var _withClassPrefix; var _props$as = props.as, Component = _props$as === void 0 ? 'div' : _props$as, className = props.className, _props$classPrefix = props.classPrefix, classPrefix = _props$classPrefix === void 0 ? 'message' : _props$classPrefix, children = props.children, closable = props.closable, _props$duration = props.duration, duration = _props$duration === void 0 ? 2000 : _props$duration, full = props.full, header = props.header, _props$type = props.type, type = _props$type === void 0 ? 'info' : _props$type, showIcon = props.showIcon, onClose = props.onClose, rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "className", "classPrefix", "children", "closable", "duration", "full", "header", "type", "showIcon", "onClose"]); var _useState = (0, _react.useState)('show'), display = _useState[0], setDisplay = _useState[1]; var _useClassNames = (0, _utils.useClassNames)(classPrefix), withClassPrefix = _useClassNames.withClassPrefix, merge = _useClassNames.merge, prefix = _useClassNames.prefix; var isMounted = (0, _utils.useIsMounted)(); // Timed close message var _useTimeout = (0, _utils.useTimeout)(onClose, duration, duration > 0), clear = _useTimeout.clear; var handleClose = (0, _react.useCallback)(function (event) { setDisplay('hiding'); onClose === null || onClose === void 0 ? void 0 : onClose(event); clear(); setTimeout(function () { if (isMounted()) { setDisplay('hide'); } }, 1000); }, [clear, isMounted, onClose]); if (display === 'hide') { return null; } var classes = merge(className, withClassPrefix(type, display, (_withClassPrefix = { full: full }, _withClassPrefix['has-title'] = header, _withClassPrefix['has-icon'] = showIcon, _withClassPrefix))); return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({ role: "alert" }, rest, { ref: ref, className: classes }), /*#__PURE__*/_react.default.createElement("div", { className: prefix(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2.default)(["container"]))) }, closable && /*#__PURE__*/_react.default.createElement(_CloseButton.default, { onClick: handleClose }), showIcon && /*#__PURE__*/_react.default.createElement("div", { className: prefix(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2.default)(["icon-wrapper"]))) }, _utils.MESSAGE_STATUS_ICONS[type]), /*#__PURE__*/_react.default.createElement("div", { className: prefix(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteralLoose2.default)(["content"]))) }, header && /*#__PURE__*/_react.default.createElement("div", { className: prefix(_templateObject4 || (_templateObject4 = (0, _taggedTemplateLiteralLoose2.default)(["header"]))) }, header), children && /*#__PURE__*/_react.default.createElement("div", { className: prefix(_templateObject5 || (_templateObject5 = (0, _taggedTemplateLiteralLoose2.default)(["body"]))) }, children)))); }); Message.displayName = 'Message'; Message.propTypes = { type: _propTypes.default.oneOf(_utils.STATUS), className: _propTypes.default.string, onClose: _propTypes.default.func, closable: _propTypes.default.bool, title: _propTypes.default.node, description: _propTypes.default.node, showIcon: _propTypes.default.bool, full: _propTypes.default.bool, classPrefix: _propTypes.default.string }; var _default = Message; exports.default = _default;