forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.jsx
185 lines (172 loc) · 5.03 KB
/
message.jsx
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
import nextLocale from '../locale/zh-cn';
import Icon from '../icon';
import Animate from '../animate';
import ConfigProvider from '../config-provider';
import { obj } from '../util';
const noop = () => {};
/**
* Message
*/
class Message extends Component {
static propTypes = {
prefix: PropTypes.string,
pure: PropTypes.bool,
className: PropTypes.string,
style: PropTypes.object,
/**
* 反馈类型
*/
type: PropTypes.oneOf(['success', 'warning', 'error', 'notice', 'help', 'loading']),
/**
* 反馈外观
*/
shape: PropTypes.oneOf(['inline', 'addon', 'toast']),
/**
* 反馈大小
*/
size: PropTypes.oneOf(['medium', 'large']),
/**
* 标题
*/
title: PropTypes.node,
/**
* 内容
*/
children: PropTypes.node,
/**
* 默认是否显示
*/
defaultVisible: PropTypes.bool,
/**
* 当前是否显示
*/
visible: PropTypes.bool,
/**
* 显示的图标类型,会覆盖内部设置的IconType,传false不显示图标
*/
iconType: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
/**
* 显示关闭按钮
*/
closeable: PropTypes.bool,
/**
* 关闭按钮的回调
*/
onClose: PropTypes.func,
/**
* 关闭之后调用的函数
*/
afterClose: PropTypes.func,
/**
* 是否开启展开收起动画
*/
animation: PropTypes.bool,
locale: PropTypes.object,
rtl: PropTypes.bool,
};
static defaultProps = {
prefix: 'next-',
pure: false,
type: 'success',
shape: 'inline',
size: 'medium',
defaultVisible: true,
closeable: false,
onClose: noop,
afterClose: noop,
animation: true,
locale: nextLocale.Message,
};
state = {
visible: typeof this.props.visible === 'undefined' ? this.props.defaultVisible : this.props.visible,
};
static getDerivedStateFromProps(props) {
if ('visible' in props) {
return {
visible: props.visible,
};
}
return {};
}
onClose = () => {
if (!('visible' in this.props)) {
this.setState({
visible: false,
});
}
this.props.onClose(false);
};
render() {
/* eslint-disable no-unused-vars */
const {
prefix,
pure,
className,
style,
type,
shape,
size,
title,
children,
defaultVisible,
visible: propsVisible,
iconType: icon,
closeable,
onClose,
afterClose,
animation,
rtl,
locale,
} = this.props;
const others = {
...obj.pickOthers(Object.keys(Message.propTypes), this.props),
};
/* eslint-enable */
const { visible } = this.state;
const messagePrefix = `${prefix}message`;
const classes = classNames({
[messagePrefix]: true,
[`${prefix}message-${type}`]: type,
[`${prefix}${shape}`]: shape,
[`${prefix}${size}`]: size,
[`${prefix}title-content`]: !!title,
[`${prefix}only-content`]: !title && !!children,
[className]: className,
});
const newChildren = visible ? (
<div role="alert" style={style} {...others} className={classes} dir={rtl ? 'rtl' : undefined}>
{closeable ? (
<a
role="button"
aria-label={locale.closeAriaLabel}
className={`${messagePrefix}-close`}
onClick={this.onClose}
>
<Icon type="close" />
</a>
) : null}
{icon !== false ? (
<Icon
className={`${messagePrefix}-symbol ${!icon && `${messagePrefix}-symbol-icon`}`}
type={icon}
/>
) : null}
{title ? <div className={`${messagePrefix}-title`}>{title}</div> : null}
{children ? <div className={`${messagePrefix}-content`}>{children}</div> : null}
</div>
) : null;
if (animation) {
return (
<Animate.Expand animationAppear={false} afterLeave={afterClose}>
{newChildren}
</Animate.Expand>
);
}
return newChildren;
}
}
export default ConfigProvider.config(polyfill(Message));