forked from mathieudutour/react-progress-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-progress-button.js
154 lines (139 loc) · 4.96 KB
/
react-progress-button.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
;(function (root, factory) {
if (typeof module !== "undefined" && module.exports) {
module.exports = factory(require("react"));
} else if (typeof define === "function" && define.amd) {
define(["react"], factory);
} else {
root.ProgressButton = factory(root.React);
}
})(this, function (React) {
"use strict";
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var ProgressButton = React.createClass({displayName: "ProgressButton",
propTypes: {
classNamespace: React.PropTypes.string,
durationError: React.PropTypes.number,
durationSuccess: React.PropTypes.number,
form: React.PropTypes.string,
onClick: React.PropTypes.func,
onError: React.PropTypes.func,
onSuccess: React.PropTypes.func,
state: React.PropTypes.string,
type: React.PropTypes.string,
shouldAllowClickOnLoading: React.PropTypes.bool,
},
getDefaultProps: function() {
return {
classNamespace: 'pb-',
durationError: 1200,
durationSuccess: 500,
onClick: function() {},
onError: function() {},
onSuccess: function() {},
shouldAllowClickOnLoading: false,
};
},
getInitialState: function() {
return {
currentState: this.props.state || ''
};
},
render: function() {
var className = this.props.className;
var classNamespace = this.props.classNamespace;
var children = this.props.children;
var type = this.props.type;
var form = this.props.form;
var containerProps = _objectWithoutProperties(this.props, ["classNamespace", "children", "type", "form", "durationError", "durationSuccess", "onClick", "onError", "state", "shouldAllowClickOnLoading"]);
containerProps.className = classNamespace + "container " + this.state.currentState + ' ' + className;
containerProps.onClick = this.handleClick;
return (
React.createElement("div", containerProps,
React.createElement("button", {type: type, form: form, className: classNamespace + "button"},
React.createElement("span", null, children),
React.createElement("svg", {className: classNamespace + "progress-circle",
viewBox: "0 0 41 41"},
React.createElement("path", {d: "M38,20.5 C38,30.1685093 30.1685093,38 20.5,38"})
),
React.createElement("svg", {className: classNamespace + "checkmark",
viewBox: "0 0 70 70"},
React.createElement("path", {d: "m31.5,46.5l15.3,-23.2"}),
React.createElement("path", {d: "m31.5,46.5l-8.5,-7.1"})
),
React.createElement("svg", {className: classNamespace + "cross",
viewBox: "0 0 70 70"},
React.createElement("path", {d: "m35,35l-9.3,-9.3"}),
React.createElement("path", {d: "m35,35l9.3,9.3"}),
React.createElement("path", {d: "m35,35l-9.3,9.3"}),
React.createElement("path", {d: "m35,35l9.3,-9.3"})
)
)
)
);
},
handleClick: function(e) {
if ((this.props.shouldAllowClickOnLoading ||
this.state.currentState !== 'loading') &&
this.state.currentState !== 'disabled'
) {
this.props.onClick(e);
} else {
e.preventDefault();
}
},
loading: function() {
this.setState({currentState: 'loading'});
},
notLoading: function() {
this.setState({currentState: ''});
},
enable: function() {
this.setState({currentState: ''});
},
disable: function() {
this.setState({currentState: 'disabled'});
},
success: function(callback, dontRemove) {
this.setState({currentState: 'success'});
this._timeout = setTimeout(function() {
callback = callback || this.props.onSuccess;
callback();
if (dontRemove === true) { return; }
this.setState({currentState: ''});
}.bind(this), this.props.durationSuccess);
},
error: function(callback) {
this.setState({currentState: 'error'});
this._timeout = setTimeout(function() {
callback = callback || this.props.onError;
callback();
this.setState({currentState: ''});
}.bind(this), this.props.durationError);
},
componentWillReceiveProps: function(nextProps) {
switch(nextProps.state) {
case 'success':
this.success();
return;
case 'error':
this.error();
return;
case 'loading':
this.loading();
return;
case 'disabled':
this.disable();
return;
case '':
this.notLoading();
return;
default:
return;
}
},
componentWillUnmount: function() {
clearTimeout(this._timeout);
}
});
return ProgressButton;
});