1
- import _ from 'lodash'
2
- import React , { PropTypes } from 'react'
3
1
import cx from 'classnames'
2
+ import _ from 'lodash'
3
+ import React , { Component , PropTypes } from 'react'
4
4
5
5
import {
6
6
createShorthand ,
@@ -13,175 +13,178 @@ import {
13
13
useKeyOrValueAndKey ,
14
14
} from '../../lib'
15
15
import Icon from '../../elements/Icon'
16
-
17
16
import MessageContent from './MessageContent'
18
17
import MessageHeader from './MessageHeader'
19
18
import MessageList from './MessageList'
20
19
import MessageItem from './MessageItem'
21
20
22
21
/**
23
- * A message displays information that explains nearby content
22
+ * A message displays information that explains nearby content.
24
23
* @see Form
25
24
*/
26
- function Message ( props ) {
27
- const {
28
- children,
29
- className,
30
- content,
31
- header,
32
- icon,
33
- list,
34
- onDismiss,
35
- hidden,
36
- visible,
37
- floating,
38
- compact,
39
- attached,
40
- warning,
41
- info,
42
- positive,
43
- success,
44
- negative,
45
- error,
46
- color,
47
- size,
48
- } = props
49
-
50
- const classes = cx (
51
- 'ui' ,
52
- size ,
53
- color ,
54
- useKeyOnly ( icon , 'icon' ) ,
55
- useKeyOnly ( hidden , 'hidden' ) ,
56
- useKeyOnly ( visible , 'visible' ) ,
57
- useKeyOnly ( floating , 'floating' ) ,
58
- useKeyOnly ( compact , 'compact' ) ,
59
- useKeyOrValueAndKey ( attached , 'attached' ) ,
60
- useKeyOnly ( warning , 'warning' ) ,
61
- useKeyOnly ( info , 'info' ) ,
62
- useKeyOnly ( positive , 'positive' ) ,
63
- useKeyOnly ( success , 'success' ) ,
64
- useKeyOnly ( negative , 'negative' ) ,
65
- useKeyOnly ( error , 'error' ) ,
66
- 'message' ,
67
- className ,
68
- )
69
-
70
- const dismissIcon = onDismiss && < Icon name = 'close' onClick = { onDismiss } />
71
- const rest = getUnhandledProps ( Message , props )
72
- const ElementType = getElementType ( Message , props )
73
-
74
- if ( ! _ . isNil ( children ) ) {
75
- return (
76
- < ElementType { ...rest } className = { classes } >
77
- { dismissIcon }
78
- { children }
79
- </ ElementType >
80
- )
81
- }
25
+ export default class Message extends Component {
26
+ static propTypes = {
27
+ /** An element type to render as (string or function). */
28
+ as : customPropTypes . as ,
82
29
83
- return (
84
- < ElementType { ...rest } className = { classes } >
85
- { dismissIcon }
86
- { Icon . create ( icon ) }
87
- { ( ! _ . isNil ( header ) || ! _ . isNil ( content ) || ! _ . isNil ( list ) ) && (
88
- < MessageContent >
89
- { MessageHeader . create ( header ) }
90
- { MessageList . create ( list ) }
91
- { createShorthand ( 'p' , val => ( { children : val } ) , content ) }
92
- </ MessageContent >
93
- ) }
94
- </ ElementType >
95
- )
96
- }
30
+ /** A message can be formatted to attach itself to other content. */
31
+ attached : PropTypes . oneOfType ( [
32
+ PropTypes . bool ,
33
+ PropTypes . oneOf ( [ 'bottom' ] ) ,
34
+ ] ) ,
97
35
98
- Message . _meta = {
99
- name : 'Message' ,
100
- type : META . TYPES . COLLECTION ,
101
- props : {
102
- attached : [ 'bottom' ] ,
103
- color : SUI . COLORS ,
104
- size : _ . without ( SUI . SIZES , 'medium' ) ,
105
- } ,
106
- }
36
+ /** Primary content. */
37
+ children : PropTypes . node ,
38
+
39
+ /** Additional classes. */
40
+ className : PropTypes . string ,
41
+
42
+ /** A message can be formatted to be different colors. */
43
+ color : PropTypes . oneOf ( SUI . COLORS ) ,
107
44
108
- Message . propTypes = {
109
- /** An element type to render as (string or function). */
110
- as : customPropTypes . as ,
45
+ /** A message can only take up the width of its content. */
46
+ compact : PropTypes . bool ,
111
47
112
- /** Primary content. */
113
- children : PropTypes . node ,
48
+ /** Shorthand for primary content. */
49
+ content : customPropTypes . contentShorthand ,
114
50
115
- /** Additional classes . */
116
- className : PropTypes . string ,
51
+ /** A message may be formatted to display a negative message. Same as `negative` . */
52
+ error : PropTypes . bool ,
117
53
118
- /** Shorthand for primary content. */
119
- content : customPropTypes . contentShorthand ,
54
+ /** A message can float above content that it is related to . */
55
+ floating : PropTypes . bool ,
120
56
121
- /** Shorthand for MessageHeader. */
122
- header : customPropTypes . itemShorthand ,
57
+ /** Shorthand for MessageHeader. */
58
+ header : customPropTypes . itemShorthand ,
123
59
124
- /** A message can contain an icon. */
125
- icon : PropTypes . oneOfType ( [
126
- PropTypes . bool ,
127
- customPropTypes . itemShorthand ,
128
- ] ) ,
60
+ /** A message can be hidden. */
61
+ hidden : PropTypes . bool ,
129
62
130
- /** Array shorthand items for the MessageList. Mutually exclusive with children. */
131
- list : customPropTypes . collectionShorthand ,
63
+ /** A message can contain an icon. */
64
+ icon : PropTypes . oneOfType ( [
65
+ customPropTypes . itemShorthand ,
66
+ PropTypes . bool ,
67
+ ] ) ,
132
68
133
- /**
134
- * A message that the user can choose to hide.
135
- * Called when the user clicks the "x" icon. This also adds the "x" icon.
136
- */
137
- onDismiss : PropTypes . func ,
69
+ /** A message may be formatted to display information. */
70
+ info : PropTypes . bool ,
138
71
139
- /** A message can be hidden . */
140
- hidden : PropTypes . bool ,
72
+ /** Array shorthand items for the MessageList. Mutually exclusive with children . */
73
+ list : customPropTypes . collectionShorthand ,
141
74
142
- /** A message can be set to visible to force itself to be shown . */
143
- visible : PropTypes . bool ,
75
+ /** A message may be formatted to display a negative message. Same as `error` . */
76
+ negative : PropTypes . bool ,
144
77
145
- /** A message can float above content that it is related to. */
146
- floating : PropTypes . bool ,
78
+ /**
79
+ * A message that the user can choose to hide.
80
+ * Called when the user clicks the "x" icon. This also adds the "x" icon.
81
+ *
82
+ * @param {SyntheticEvent } event - React's original SyntheticEvent.
83
+ * @param {object } data - All props.
84
+ */
85
+ onDismiss : PropTypes . func ,
147
86
148
- /** A message can only take up the width of its content . */
149
- compact : PropTypes . bool ,
87
+ /** A message may be formatted to display a positive message. Same as `success` . */
88
+ positive : PropTypes . bool ,
150
89
151
- /** A message can be formatted to attach itself to other content. */
152
- attached : PropTypes . oneOfType ( [
153
- PropTypes . bool ,
154
- PropTypes . oneOf ( Message . _meta . props . attached ) ,
155
- ] ) ,
90
+ /** A message may be formatted to display a positive message. Same as `positive`. */
91
+ success : PropTypes . bool ,
156
92
157
- /** A message may be formatted to display warning messages . */
158
- warning : PropTypes . bool ,
93
+ /** A message can have different sizes . */
94
+ size : PropTypes . oneOf ( _ . without ( SUI . SIZES , 'medium' ) ) ,
159
95
160
- /** A message may be formatted to display information . */
161
- info : PropTypes . bool ,
96
+ /** A message can be set to visible to force itself to be shown . */
97
+ visible : PropTypes . bool ,
162
98
163
- /** A message may be formatted to display a positive message. Same as `success`. */
164
- positive : PropTypes . bool ,
99
+ /** A message may be formatted to display warning messages. */
100
+ warning : PropTypes . bool ,
101
+ }
165
102
166
- /** A message may be formatted to display a positive message. Same as `positive`. */
167
- success : PropTypes . bool ,
103
+ static _meta = {
104
+ name : 'Message' ,
105
+ type : META . TYPES . COLLECTION ,
106
+ }
168
107
169
- /** A message may be formatted to display a negative message. Same as `error`. */
170
- negative : PropTypes . bool ,
108
+ static Content = MessageContent
109
+ static Header = MessageHeader
110
+ static List = MessageList
111
+ static Item = MessageItem
171
112
172
- /** A message may be formatted to display a negative message. Same as `negative`. */
173
- error : PropTypes . bool ,
113
+ handleDismiss = e => {
114
+ const { onDismiss } = this . props
174
115
175
- /** A message can be formatted to be different colors. */
176
- color : PropTypes . oneOf ( Message . _meta . props . color ) ,
116
+ if ( onDismiss ) onDismiss ( e , this . props )
117
+ }
177
118
178
- /** A message can have different sizes. */
179
- size : PropTypes . oneOf ( Message . _meta . props . size ) ,
180
- }
119
+ render ( ) {
120
+ const {
121
+ attached,
122
+ children,
123
+ className,
124
+ color,
125
+ compact,
126
+ content,
127
+ error,
128
+ floating,
129
+ header,
130
+ hidden,
131
+ icon,
132
+ info,
133
+ list,
134
+ negative,
135
+ onDismiss,
136
+ positive,
137
+ size,
138
+ success,
139
+ visible,
140
+ warning,
141
+ } = this . props
142
+
143
+ const classes = cx (
144
+ 'ui' ,
145
+ color ,
146
+ size ,
147
+ useKeyOnly ( compact , 'compact' ) ,
148
+ useKeyOnly ( error , 'error' ) ,
149
+ useKeyOnly ( floating , 'floating' ) ,
150
+ useKeyOnly ( hidden , 'hidden' ) ,
151
+ useKeyOnly ( icon , 'icon' ) ,
152
+ useKeyOnly ( info , 'info' ) ,
153
+ useKeyOnly ( negative , 'negative' ) ,
154
+ useKeyOnly ( positive , 'positive' ) ,
155
+ useKeyOnly ( success , 'success' ) ,
156
+ useKeyOnly ( visible , 'visible' ) ,
157
+ useKeyOnly ( warning , 'warning' ) ,
158
+ useKeyOrValueAndKey ( attached , 'attached' ) ,
159
+ 'message' ,
160
+ className ,
161
+ )
162
+
163
+ const dismissIcon = onDismiss && < Icon name = 'close' onClick = { this . handleDismiss } />
164
+ const rest = getUnhandledProps ( Message , this . props )
165
+ const ElementType = getElementType ( Message , this . props )
181
166
182
- Message . Content = MessageContent
183
- Message . Header = MessageHeader
184
- Message . List = MessageList
185
- Message . Item = MessageItem
167
+ if ( ! _ . isNil ( children ) ) {
168
+ return (
169
+ < ElementType { ...rest } className = { classes } >
170
+ { dismissIcon }
171
+ { children }
172
+ </ ElementType >
173
+ )
174
+ }
186
175
187
- export default Message
176
+ return (
177
+ < ElementType { ...rest } className = { classes } >
178
+ { dismissIcon }
179
+ { Icon . create ( icon ) }
180
+ { ( ! _ . isNil ( header ) || ! _ . isNil ( content ) || ! _ . isNil ( list ) ) && (
181
+ < MessageContent >
182
+ { MessageHeader . create ( header ) }
183
+ { MessageList . create ( list ) }
184
+ { createShorthand ( 'p' , val => ( { children : val } ) , content ) }
185
+ </ MessageContent >
186
+ ) }
187
+ </ ElementType >
188
+ )
189
+ }
190
+ }
0 commit comments