-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathLaddaButton.jsx
More file actions
102 lines (85 loc) · 2.44 KB
/
Copy pathLaddaButton.jsx
File metadata and controls
102 lines (85 loc) · 2.44 KB
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { create } from 'ladda'
import { SIZES, STYLES } from './constants'
const isUndefined = value => typeof value === 'undefined'
const OMITTED_PROPS = [
'loading',
'progress',
]
const omit = (data, keys) => {
const result = {}
Object.keys(data).forEach((key) => {
if (keys.indexOf(key) === -1) {
result[key] = data[key]
}
})
return result
}
export default
class LaddaButton extends Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
progress: PropTypes.number,
loading: PropTypes.bool,
disabled: PropTypes.bool,
// Ladda props
// eslint-disable-next-line react/no-unused-prop-types
'data-color': PropTypes.oneOf(['green', 'red', 'blue', 'purple', 'mint']),
// eslint-disable-next-line react/no-unused-prop-types
'data-size': PropTypes.oneOf(SIZES),
// eslint-disable-next-line react/no-unused-prop-types
'data-style': PropTypes.oneOf(STYLES),
// eslint-disable-next-line react/no-unused-prop-types
'data-spinner-size': PropTypes.number,
// eslint-disable-next-line react/no-unused-prop-types
'data-spinner-color': PropTypes.string,
// eslint-disable-next-line react/no-unused-prop-types
'data-spinner-lines': PropTypes.number,
};
componentDidMount() {
this.laddaInstance = create(this.node)
if (this.props.loading) {
this.laddaInstance.start()
}
if (!isUndefined(this.props.progress)) {
this.laddaInstance.setProgress(this.props.progress)
}
}
componentWillReceiveProps(nextProps) {
this.updateLaddaInstance(nextProps)
}
componentWillUnmount() {
this.laddaInstance.remove()
}
setNode = (node) => {
this.node = node
}
updateLaddaInstance = (props) => {
if (props.loading !== this.props.loading) {
if (props.loading) {
this.laddaInstance.start()
} else {
this.laddaInstance.stop()
}
}
if (props.progress !== this.props.progress) {
this.laddaInstance.setProgress(props.progress)
}
}
render() {
return (
<button
{...omit(this.props, OMITTED_PROPS)}
className={`ladda-button ${this.props.className || ''}`}
ref={this.setNode}
disabled={this.props.disabled || this.props.loading}
>
<span className="ladda-label">
{this.props.children}
</span>
</button>
)
}
}