@trellixio/roaster-coffee
Version:
Beans' product component library
51 lines (50 loc) • 1.97 kB
JavaScript
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, { useEffect, useRef } from 'react';
import { Toast } from '../Toast';
export function getAutoClose(autoClose, toastAutoClose) {
if (typeof toastAutoClose === 'number') {
return toastAutoClose;
}
if (toastAutoClose === false || autoClose === false) {
return false;
}
return autoClose;
}
export function ToastsContainer({ toast, autoClose, onHide, innerRef, className }) {
const { autoClose: toastAutoClose, message } = toast, toastProps = __rest(toast, ["autoClose", "message"]);
const autoCloseTimeout = getAutoClose(autoClose, toastAutoClose);
const hideTimeout = useRef();
const handleHide = () => {
onHide(toast.id);
window.clearTimeout(hideTimeout.current);
};
const cancelDelayedHide = () => {
clearTimeout(hideTimeout.current);
};
const handleDelayedHide = () => {
if (typeof autoCloseTimeout === 'number') {
hideTimeout.current = window.setTimeout(handleHide, autoCloseTimeout);
}
};
useEffect(() => {
if (typeof toast.onOpen === 'function') {
toast.onOpen(toast);
}
}, []);
useEffect(() => {
handleDelayedHide();
return cancelDelayedHide;
}, [autoClose, toast.autoClose]);
return React.createElement(Toast, Object.assign({}, toastProps, { className: className, onClose: handleHide, ref: innerRef, message: message }));
}
ToastsContainer.displayName = 'ToastContainer';