-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Message.js
179 lines (150 loc) · 4.74 KB
/
Message.js
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
import cx from 'clsx'
import _ from 'lodash'
import PropTypes from 'prop-types'
import * as React from 'react'
import {
childrenUtils,
createHTMLParagraph,
customPropTypes,
getComponentType,
getUnhandledProps,
SUI,
getKeyOnly,
getKeyOrValueAndKey,
useEventCallback,
} from '../../lib'
import Icon from '../../elements/Icon'
import MessageContent from './MessageContent'
import MessageHeader from './MessageHeader'
import MessageList from './MessageList'
import MessageItem from './MessageItem'
/**
* A message displays information that explains nearby content.
* @see Form
*/
const Message = React.forwardRef(function (props, ref) {
const {
attached,
children,
className,
color,
compact,
content,
error,
floating,
header,
hidden,
icon,
info,
list,
negative,
onDismiss,
positive,
size,
success,
visible,
warning,
} = props
const classes = cx(
'ui',
color,
size,
getKeyOnly(compact, 'compact'),
getKeyOnly(error, 'error'),
getKeyOnly(floating, 'floating'),
getKeyOnly(hidden, 'hidden'),
getKeyOnly(icon, 'icon'),
getKeyOnly(info, 'info'),
getKeyOnly(negative, 'negative'),
getKeyOnly(positive, 'positive'),
getKeyOnly(success, 'success'),
getKeyOnly(visible, 'visible'),
getKeyOnly(warning, 'warning'),
getKeyOrValueAndKey(attached, 'attached'),
'message',
className,
)
const rest = getUnhandledProps(Message, props)
const ElementType = getComponentType(props)
const handleDismiss = useEventCallback((e) => {
_.invoke(props, 'onDismiss', e, props)
})
const dismissIcon = onDismiss && <Icon name='close' onClick={handleDismiss} />
if (!childrenUtils.isNil(children)) {
return (
<ElementType {...rest} className={classes} ref={ref}>
{dismissIcon}
{children}
</ElementType>
)
}
return (
<ElementType {...rest} className={classes} ref={ref}>
{dismissIcon}
{Icon.create(icon, { autoGenerateKey: false })}
{(!_.isNil(header) || !_.isNil(content) || !_.isNil(list)) && (
<MessageContent>
{MessageHeader.create(header, { autoGenerateKey: false })}
{MessageList.create(list, { autoGenerateKey: false })}
{createHTMLParagraph(content, { autoGenerateKey: false })}
</MessageContent>
)}
</ElementType>
)
})
Message.displayName = 'Message'
Message.propTypes = {
/** An element type to render as (string or function). */
as: PropTypes.elementType,
/** A message can be formatted to attach itself to other content. */
attached: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['bottom', 'top'])]),
/** Primary content. */
children: PropTypes.node,
/** Additional classes. */
className: PropTypes.string,
/** A message can be formatted to be different colors. */
color: PropTypes.oneOf(SUI.COLORS),
/** A message can only take up the width of its content. */
compact: PropTypes.bool,
/** Shorthand for primary content. */
content: customPropTypes.contentShorthand,
/** A message may be formatted to display a negative message. Same as `negative`. */
error: PropTypes.bool,
/** A message can float above content that it is related to. */
floating: PropTypes.bool,
/** Shorthand for MessageHeader. */
header: customPropTypes.itemShorthand,
/** A message can be hidden. */
hidden: PropTypes.bool,
/** A message can contain an icon. */
icon: PropTypes.oneOfType([customPropTypes.itemShorthand, PropTypes.bool]),
/** A message may be formatted to display information. */
info: PropTypes.bool,
/** Array shorthand items for the MessageList. Mutually exclusive with children. */
list: customPropTypes.collectionShorthand,
/** A message may be formatted to display a negative message. Same as `error`. */
negative: PropTypes.bool,
/**
* A message that the user can choose to hide.
* Called when the user clicks the "x" icon. This also adds the "x" icon.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onDismiss: PropTypes.func,
/** A message may be formatted to display a positive message. Same as `success`. */
positive: PropTypes.bool,
/** A message can have different sizes. */
size: PropTypes.oneOf(_.without(SUI.SIZES, 'medium')),
/** A message may be formatted to display a positive message. Same as `positive`. */
success: PropTypes.bool,
/** A message can be set to visible to force itself to be shown. */
visible: PropTypes.bool,
/** A message may be formatted to display warning messages. */
warning: PropTypes.bool,
}
Message.Content = MessageContent
Message.Header = MessageHeader
Message.List = MessageList
Message.Item = MessageItem
export default Message