UNPKG

@braineet/ui

Version:

Braineet design system

174 lines (173 loc) 5.75 kB
var _excluded = ["closeToast", "text", "icon", "children", "onClick", "appareance", "isLoading"], _excluded2 = ["icon"], _excluded3 = ["icon"]; 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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } /* eslint-disable react/prop-types */ import React from 'react'; import isFunction from 'lodash/isFunction'; import isString from 'lodash/isString'; import { toast as toastReactToastigy } from 'react-toastify'; import Alert from '../alert'; import Box from '../box'; import Pane from '../pane'; import Spinner from '../spinner'; import { Wrapper } from './styles'; import { jsx as _jsx } from "react/jsx-runtime"; var iconByType = { success: 'tick', error: 'error' }; var Msg = function Msg(_ref) { var closeToast = _ref.closeToast, text = _ref.text, icon = _ref.icon, children = _ref.children, onClick = _ref.onClick, _ref$appareance = _ref.appareance, appareance = _ref$appareance === void 0 ? 'default' : _ref$appareance, isLoading = _ref.isLoading, restOptions = _objectWithoutPropertiesLoose(_ref, _excluded); return /*#__PURE__*/_jsx(Wrapper, { children: /*#__PURE__*/_jsx(Pane, { display: "flex", alignY: "center", className: "content", children: /*#__PURE__*/_jsx(Alert, { icon: isLoading ? /*#__PURE__*/_jsx(Box, { mr: 4, children: /*#__PURE__*/_jsx(Spinner, { color: "black", type: "ring", size: 16 }) }) : icon, color: appareance === 'default' ? 'black' : appareance, textStyles: { marginRight: 0 }, isClosable: !(restOptions != null && restOptions.closeButton), onClose: closeToast, children: text }) }) }); }; var createToast = function createToast(toastify, _ref2) { var icon = _ref2.icon, options = _objectWithoutPropertiesLoose(_ref2, _excluded2); return toastify( /*#__PURE__*/_jsx(Msg, _extends({}, options, { icon: icon })), _extends({}, options, { containerId: 'toast' })); }; var createAsyncToast = function createAsyncToast(promise, _ref3, options) { var pending = _ref3.pending, success = _ref3.success, error = _ref3.error; var id; if (pending) { id = createToast(toastReactToastigy.loading, _extends({}, options, { text: pending, isLoading: true })); } var resetParams = { isLoading: null, autoClose: null, closeOnClick: null, closeButton: null, draggable: null }; var resolver = function resolver(type, input, result) { // Remove the toast if the input has not been provided. This prevents the toast from hanging // in the pending state if a success/error toast has not been provided. if (input == null) { toastReactToastigy.dismiss(id); return; } var baseParams = _extends({ type: type, appareance: type, icon: iconByType[type] }, resetParams, options, { data: result, autoClose: false }); var params = isString(input) ? { text: input } : input; // if the id is set we know that it's an update if (id) { updateToast(id, _extends({}, baseParams, params)); } else { // using toast.promise without loading createToast(params.render, _extends({}, baseParams, params)); } return result; }; var p = isFunction(promise) ? promise() : promise; p.then(function (result) { return resolver('success', success, result); }).catch(function (err) { return resolver('error', error, err); }); return p; }; var updateToast = function updateToast(toastId, _ref4) { var icon = _ref4.icon, options = _objectWithoutPropertiesLoose(_ref4, _excluded3); return toastReactToastigy.update(toastId, _extends({}, options, { render: /*#__PURE__*/_jsx(Msg, _extends({ icon: icon }, options)), containerId: 'toast' })); }; var toast = function toast(options) { return createToast(toastReactToastigy, options); }; toast.success = function (options) { return createToast(toastReactToastigy.success, _extends({ appareance: 'success', icon: iconByType.success }, options)); }; toast.error = function (options) { return createToast(toastReactToastigy.error, _extends({ appareance: 'error', icon: iconByType.error }, options)); }; toast.warning = function (options) { return createToast(toastReactToastigy.warning, _extends({ appareance: 'warning' }, options)); }; toast.info = function (options) { return createToast(toastReactToastigy.info, _extends({ appareance: 'action' }, options)); }; toast.loading = function (options) { return createToast(toastReactToastigy.loading, options); }; toast.update = function (toastId, options) { return updateToast(toastId, _extends({}, options)); }; toast.promise = function (promise, _ref5, options) { var pending = _ref5.pending, success = _ref5.success, error = _ref5.error; return createAsyncToast(promise, { pending: pending, success: success, error: error }, options); }; toast.defaultProps = { appareance: 'black', onClick: function onClick() {} }; export default toast;