-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
190 lines (172 loc) · 4.36 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
var React = require('react');
var PropTypes = require('prop-types');
var CreateReactClass = require('create-react-class');
var noop = function() {}
function formatButtons(buttons) {
if (typeof buttons === 'string')
buttons = [buttons];
if (buttons) {
buttons = buttons.map(function (button) {
if (typeof button === 'string')
return {
name: button
};
return button;
});
}
return buttons || null;
}
module.exports = CreateReactClass({
displayName: 'react-crouton',
propTypes: {
id: PropTypes.number.isRequired,
onDismiss: PropTypes.func,
hidden: PropTypes.bool,
timeout: PropTypes.number,
autoMiss: PropTypes.bool,
message: PropTypes.oneOfType([
PropTypes.string,
PropTypes.array
]),
type: PropTypes.string.isRequired,
/**
* Buttons can be either `string` or `array`
*
* Example:
*
* ```
* <Crouton buttons='close'/>
* or
* <Crouton buttons=[{name: 'close'}, {name: 'retry'}] />
* or
* <Crouton buttons=[{name: 'close', listener: somefunction}] />
* or
* <Crouton buttons=[{name: 'close', className: 'btn close', listener: somefunction}] />
* ```
*/
buttons: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string
})),
PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
className: PropTypes.string,
listener: PropTypes.func
}))
])
},
getDefaultProps: function () {
return {
onDismiss: noop,
// 2000 ms
timeout: 2000,
autoMiss: true
};
},
getInitialState: function () {
return {
hidden: false,
ttd: null
};
},
dismiss: function () {
this.setState({
hidden: true
});
this.props.onDismiss();
this.clearTimeout();
return this;
},
clearTimeout: function () {
if (this.state.ttd) {
clearTimeout(this.state.ttd);
this.setState({
ttd: null
});
}
return this;
},
handleClick: function (event) {
this.dismiss();
var item = this.state.buttons.filter(function (button) {
return button.name.toLowerCase() === event.target.id
})[0];
if (item && item.listener) {
item.listener(event);
}
},
componentWillMount: function () {
this.changeState(this.props);
},
componentWillUnmount: function () {
this.clearTimeout();
},
changeState: function (nextProps) {
var buttons = formatButtons(nextProps.buttons);
var message = nextProps.message
if (typeof message === 'string')
message = [message];
var autoMiss = nextProps.autoMiss;
if (autoMiss && !nextProps.hidden) {
var ttd = setTimeout(this.dismiss, nextProps.timeout || this.props.timeout);
// this.dismiss();
this.setState({
ttd: ttd,
hidden: nextProps.hidden,
buttons: buttons,
message: message,
type: nextProps.type
});
} else {
this.setState({
hidden: nextProps.hidden,
buttons: buttons,
message: message,
type: nextProps.type
});
}
},
componentWillReceiveProps: function (nextProps) {
this.changeState(nextProps);
},
shouldComponentUpdate: function (nextProps, nextState) {
if (nextProps.id === this.props.id) {
return !!nextState.hidden;
}
return true
},
renderChildren: function () {
if (this.props.children) return this.props.children;
return this.state.message.map(function (msg, i) {
return React.createElement('span', {
key: i
}, msg);
});
},
render: function () {
return React.createElement('div', {
className: 'crouton',
hidden: this.state.hidden
},
React.createElement('div', {
className: this.state.type
},
this.renderChildren()
,
this.state.buttons ? React.createElement('div', {
className: 'buttons'
},
this.state.buttons.map(function (button, i) {
return React.createElement('button', {
id: button.name.toLowerCase(),
key: i,
className: button.className ? button.className : button.name.toLowerCase(),
onClick: this.handleClick
}, button.name)
}, this)
) : null
)
)
}
})