Cupcake/app/components/ui/notice/utils.ts

34 lines
657 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export interface NoticeItem {
key?: string;
title: string;
duration?: number;
content: React.ReactNode;
}
type NoticeFn = (notice: NoticeItem) => void;
const addNoticeFn: NoticeFn[] = [];
export const add = (notice: NoticeItem) => {
addNoticeFn.forEach((item) => {
// 如果没传递 duration默认设置为 5000
if (!("duration" in notice)) {
notice.duration = 5000;
}
item(notice);
});
}
export const addFn = (fn: NoticeFn) => {
return addNoticeFn.push(fn) - 1;
}
export const removeFn = (fn: NoticeFn) => {
const index = addNoticeFn.indexOf(fn);
if (index > -1) {
addNoticeFn.splice(index, 1);
}
}