广州矛调粤政易端
xusd
7 days ago d27794814b69d18aeb8ee96a46cae91d5613570c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * @Company: hugeInfo
 * @Author: lwh
 * @Date: 2023-03-07 15:45:59
 * @LastEditTime: 2023-03-07 15:46:10
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: 
 */
import React, { Component } from 'react';
 
class ToastBox extends Component {
    constructor() {
        super();
        this.transitionTime = 300;
        this.state = { notices: [] };
        this.removeNotice = this.removeNotice.bind(this);
    }
 
    getNoticeKey() {
        const { notices } = this.state;
        return `notice-${new Date().getTime()}-${notices.length}`;
    }
 
    addNotice(notice) {
        const { notices } = this.state;
        notice.key = this.getNoticeKey();
 
        // notices.push(notice);//展示所有的提示
        notices[0] = notice; //仅展示最后一个提示
 
        this.setState({ notices });
        if (notice.duration > 0) {
            setTimeout(() => {
                this.removeNotice(notice.key);
            }, notice.duration);
        }
        return () => {
            this.removeNotice(notice.key);
        };
    }
 
    removeNotice(key) {
        const { notices } = this.state;
        this.setState({
            notices: notices.filter((notice) => {
                if (notice.key === key) {
                    if (notice.onClose) setTimeout(notice.onClose, this.transitionTime);
                    return false;
                }
                return true;
            }),
        });
    }
 
    render() {
        const { notices } = this.state;
        const icons = {
            info: 'toast_info',
            success: 'toast_success',
            error: 'toast_error',
            loading: 'toast_loading',
        };
        return (
            <div className="toast">
                {notices.map((notice) => (
                    <div className="toast_bg" key={notice.key}>
                        <div className="toast_box">
                            <div className={`toast_icon ${icons[notice.type]}`}></div>
                            <div className="toast_text">{notice.content}</div>
                        </div>
                    </div>
                ))}
            </div>
        );
    }
}
 
export default ToastBox;