-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
181 lines (163 loc) · 4.79 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
import React from 'react'
import PropTypes from 'prop-types'
import createReactClass from 'create-react-class'
export const STATE = {
LOADING: 'loading',
DISABLED: 'disabled',
SUCCESS: 'success',
ERROR: 'error',
NOTHING: ''
}
const ProgressButton = createReactClass({
propTypes: {
classNamespace: PropTypes.string,
controlled: PropTypes.bool,
durationError: PropTypes.number,
durationSuccess: PropTypes.number,
form: PropTypes.string,
onClick: PropTypes.func,
onError: PropTypes.func,
onSuccess: PropTypes.func,
state: PropTypes.oneOf(Object.keys(STATE).map(k => STATE[k])),
type: PropTypes.string,
shouldAllowClickOnLoading: PropTypes.bool
},
getDefaultProps () {
return {
classNamespace: 'pb-',
controlled: false,
durationError: 1200,
durationSuccess: 500,
onClick () {},
onError () {},
onSuccess () {},
shouldAllowClickOnLoading: false
}
},
getInitialState () {
return {
currentState: this.props.state || STATE.NOTHING
}
},
componentWillReceiveProps (nextProps) {
if (nextProps.state === this.props.state) { return }
switch (nextProps.state) {
case STATE.SUCCESS:
this.success()
return
case STATE.ERROR:
this.error()
return
case STATE.LOADING:
this.loading()
return
case STATE.DISABLED:
this.disable()
return
case STATE.NOTHING:
this.notLoading()
return
default:
return
}
},
componentWillUnmount () {
clearTimeout(this._timeout)
},
render () {
const {
className,
classNamespace,
children,
type,
form,
durationError, // eslint-disable-line no-unused-vars
durationSuccess, // eslint-disable-line no-unused-vars
onClick, // eslint-disable-line no-unused-vars
onError, // eslint-disable-line no-unused-vars
onSuccess, // eslint-disable-line no-unused-vars
state, // eslint-disable-line no-unused-vars
shouldAllowClickOnLoading, // eslint-disable-line no-unused-vars
controlled, // eslint-disable-line no-unused-vars
...containerProps
} = this.props
containerProps.className = classNamespace + 'container ' + this.state.currentState + ' ' + className
containerProps.onClick = this.handleClick
return (
<div {...containerProps}>
<button disabled={state === STATE.DISABLED} type={type} form={form} className={classNamespace + 'button'}>
<span>{children}</span>
<svg className={classNamespace + 'progress-circle'} viewBox='0 0 41 41'>
<path d='M38,20.5 C38,30.1685093 30.1685093,38 20.5,38' />
</svg>
<svg className={classNamespace + 'checkmark'} viewBox='0 0 70 70'>
<path d='m31.5,46.5l15.3,-23.2' />
<path d='m31.5,46.5l-8.5,-7.1' />
</svg>
<svg className={classNamespace + 'cross'} viewBox='0 0 70 70'>
<path d='m35,35l-9.3,-9.3' />
<path d='m35,35l9.3,9.3' />
<path d='m35,35l-9.3,9.3' />
<path d='m35,35l9.3,-9.3' />
</svg>
</button>
</div>
)
},
handleClick (e) {
const shouldAllowClick = (this.props.shouldAllowClickOnLoading ||
this.state.currentState !== STATE.LOADING) &&
this.state.currentState !== STATE.DISABLED
if (this.props.controlled && shouldAllowClick) {
this.props.onClick(e)
return true
}
if (shouldAllowClick) {
this.loading()
const ret = this.props.onClick(e)
this.handlePromise(ret)
} else {
e.preventDefault()
}
},
handlePromise (promise) {
if (promise && promise.then && promise.catch) {
promise
.then(() => {
this.success()
})
.catch((err) => {
this.error(null, err)
})
}
},
loading () {
this.setState({currentState: STATE.LOADING})
},
notLoading () {
this.setState({currentState: STATE.NOTHING})
},
enable () {
this.setState({currentState: STATE.NOTHING})
},
disable () {
this.setState({currentState: STATE.DISABLED})
},
success (callback, dontRemove) {
this.setState({currentState: STATE.SUCCESS})
this._timeout = setTimeout(() => {
if (!dontRemove) { this.setState({currentState: STATE.NOTHING}) }
callback = callback || this.props.onSuccess
if (typeof callback === 'function') { callback() }
}, this.props.durationSuccess)
},
error (callback, err) {
this.setState({currentState: STATE.ERROR})
this._timeout = setTimeout(() => {
this.setState({currentState: STATE.NOTHING})
callback = callback || this.props.onError
if (typeof callback === 'function') { callback(err) }
}, this.props.durationError)
}
})
export default ProgressButton