Skip to content

Commit 5a89944

Browse files
committed
type: notification type
1 parent 17d2d33 commit 5a89944

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/Notification/index.ts

+35-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import Notification from './Notification.svelte';
2-
import type { SvelteComponent } from 'svelte';
2+
import type { ComponentConstructorOptions, SvelteComponent } from 'svelte';
33

44
type NotifyPlacement = 'right-top' | 'left-top' | 'right-bottom' | 'left-bottom' | 'center';
55
type NotifyType = 'info' | 'warning' | 'error' | 'success';
6+
type Extend = {
7+
__notify_index: number;
8+
__notify_placement: NotifyPlacement;
9+
index: number;
10+
show: boolean;
11+
};
12+
type NotifyComponent = SvelteComponent<NotifyOptions & Extend>;
613
export interface NotifyOptions {
714
customClass?: string;
815
close?: boolean;
916
title?: string;
1017
type?: NotifyType;
1118
placement?: NotifyPlacement;
12-
target?: HTMLElement;
19+
target?: Element;
1320
onClose?: () => void;
1421
slotTitle?: string | SvelteComponent; // svelte sfc or html sting
1522
slot?: string | SvelteComponent; // svelte sfc or html sting
@@ -28,11 +35,11 @@ const defaultNotifyOptions: NotifyOptions = {
2835
};
2936

3037
const notifyMap = {
31-
'right-top': [],
32-
center: [],
33-
'left-bottom': [],
34-
'right-bottom': [],
35-
'left-top': []
38+
'right-top': [] as NotifyComponent[],
39+
center: [] as NotifyComponent[],
40+
'left-bottom': [] as NotifyComponent[],
41+
'right-bottom': [] as NotifyComponent[],
42+
'left-top': [] as NotifyComponent[]
3643
};
3744

3845
const resolveNotifyOptions = (options: NotifyOptions) => {
@@ -65,7 +72,7 @@ function mountNotify(
6572
evt: Record<string, any>,
6673
context: Map<string, string | SvelteComponent>
6774
) {
68-
const notifyArray = notifyMap[options.placement];
75+
const notifyArray = notifyMap[options.placement || 'right-top'];
6976
let index = 0;
7077
if (!notifyArray) {
7178
console.error(
@@ -77,13 +84,17 @@ function mountNotify(
7784

7885
index = notifyArray.length;
7986
const NotificationInst = new Notification({
80-
target: options.target,
87+
target: options.target!,
8188
props: {
8289
...options,
8390
show: false,
8491
index,
8592
onClose: async () => {
86-
await unmountNotify(options.placement, NotificationInst, 0);
93+
await unmountNotify(
94+
options.placement || 'right-top',
95+
NotificationInst as unknown as NotifyComponent,
96+
0
97+
);
8798
evt.onClose && evt.onClose();
8899
}
89100
},
@@ -94,26 +105,30 @@ function mountNotify(
94105

95106
NotificationInst.$set({ show: true });
96107
// auto close
97-
autoUnmountNotify(options, NotificationInst);
108+
autoUnmountNotify(options, NotificationInst as unknown as NotifyComponent);
98109
// cache NotificationInst
99-
notifyArray.push(NotificationInst);
110+
notifyArray.push(NotificationInst as unknown as NotifyComponent);
100111

101112
return NotificationInst;
102113
}
103114

104-
async function autoUnmountNotify(options: NotifyOptions, inst) {
115+
async function autoUnmountNotify(options: NotifyOptions, inst: NotifyComponent) {
105116
if (options.autoClose) {
106-
await durationUnmountNotify(options.placement, inst, options.duration);
117+
await durationUnmountNotify(options.placement || 'right-top', inst, options.duration || 0);
107118
}
108119
}
109120

110-
async function durationUnmountNotify(placement: NotifyPlacement, inst, duration: number) {
121+
async function durationUnmountNotify(
122+
placement: NotifyPlacement,
123+
inst: NotifyComponent,
124+
duration: number
125+
) {
111126
setTimeout(() => {
112127
unmountNotify(placement, inst, 300);
113128
}, duration);
114129
}
115130

116-
async function unmountNotify(placement: NotifyPlacement, inst, duration: number) {
131+
async function unmountNotify(placement: NotifyPlacement, inst: NotifyComponent, duration: number) {
117132
inst.$set({ show: false });
118133
setTimeout(() => {
119134
notifyMap[placement].splice(inst.__notify_index, 1);
@@ -123,7 +138,7 @@ async function unmountNotify(placement: NotifyPlacement, inst, duration: number)
123138
}
124139

125140
function updatedNotifyByIndex(placement: NotifyPlacement) {
126-
notifyMap[placement].forEach((inst, index) => {
141+
notifyMap[placement].forEach((inst: NotifyComponent, index) => {
127142
inst.$set({ index });
128143
inst.__notify_index = index;
129144
});
@@ -158,19 +173,19 @@ NotifyFn.success = (options: NotifyOptions = {}) => {
158173
return mountNotify(finalOptions, evt, context);
159174
};
160175

161-
NotifyFn.clear = async (inst) => {
176+
NotifyFn.clear = async (inst: NotifyComponent) => {
162177
await unmountNotify(inst.__notify_placment, inst, 300);
163178
};
164179

165180
NotifyFn.clearAll = () => {
166181
Object.keys(notifyMap).forEach((instArr) => {
167-
notifyMap[instArr].forEach((inst) => {
182+
notifyMap[instArr as NotifyPlacement].forEach((inst: NotifyComponent) => {
168183
unmountNotify(inst.__notify_placment, inst, 0);
169184
});
170185
});
171186
};
172187

173-
NotifyFn.update = async (inst, options: NotifyOptions = {}) => {
188+
NotifyFn.update = async (inst: NotifyComponent, options: NotifyOptions = {}) => {
174189
inst.$set({ ...options });
175190
};
176191

0 commit comments

Comments
 (0)