/
base.jsx
174 lines (163 loc) · 3.64 KB
/
base.jsx
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
"use strict";
import React from 'react';
import { IControl } from '../base';
/**
* @class UIButton
* @extend UIContril
*/
class UIButton extends IControl {
static propTypes = {
/**
* @property {String} text
*/
text: React.PropTypes.string,
/**
* @property {String} level - primary, warning, error
*/
level: React.PropTypes.oneOf([
'primary', 'warning', 'error', 'default'
]),
/**
* @property {Boolean} block
*/
block: React.PropTypes.bool,
/**
* @property {Boolean} disabled
*/
disabled: React.PropTypes.bool,
/**
* @property {Number} disableInterval
*/
disableInterval: React.PropTypes.number,
};
static styles = {
button: {
display: 'inline-block',
height: '30px',
border: '1px solid #9b9b9b',
borderRadius: '4px',
padding: '0 10px',
color: '#7e7e7e',
backgroundColor: '#fff',
cursor: 'pointer',
transition: 'all .3s ease-in-out',
boxSizing: 'border-box',
},
disabled: {
borderColor: '#eeeeee',
color: '#eeeeee',
cursor: 'no-drop',
},
levels: {
primary: {
color: '#ffffff',
borderColor: '#70CCA1',
backgroundColor: '#70CCA1',
}
},
hovered: {
color: '#ffffff',
borderColor: '#70CCA1',
backgroundColor: '#70CCA1',
}
};
constructor(props) {
super(props);
this.state = {
disabled: false,
hovered: false,
text: props.text,
};
}
/**
* @event onMouseOver
*/
onMouseOver() {
this.setState({
hovered: true,
});
}
/**
* @event onMouseLeave
*/
onMouseLeave() {
this.setState({
hovered: false,
});
}
/**
* Click this button has different behaviors
*
* @event onClick
*/
async onClick(event) {
this.setState({
disabled: true
});
let isValid;
if (typeof this.props.onClick === 'function') {
isValid = await this.props.onClick(event);
}
if ((isValid === undefined || isValid) &&
this.props.disableInterval !== undefined) {
let count = this.props.disableInterval;
let interval = setInterval(() => {
count -= 1;
if (count >= 0) {
this.setState({
text: `等待中(${count})`
});
} else {
clearInterval(interval);
this.setState({
text: this.props.text,
disabled: false,
});
}
}, 1000);
} else {
this.setState({
disabled: false,
});
}
}
render() {
let disabled;
if (this.state.disabled === true) {
disabled = true;
} else {
disabled = this.props.disabled;
}
let srcProps = {};
let style = Object.assign({}, UIButton.styles.button);
if (disabled) {
style = Object.assign(style, UIButton.styles.disabled);
} else {
if (this.state.hovered) {
style = Object.assign(style, UIButton.styles.hovered);
}
srcProps.onMouseOver = this.onMouseOver.bind(this);
srcProps.onMouseLeave = this.onMouseLeave.bind(this);
srcProps.onClick = this.onClick.bind(this);
}
if (this.props.level) {
const level = this.props.level;
style = Object.assign(style, UIButton.styles.levels[level]);
}
if (this.props.block) {
srcProps.flex = 1.0;
} else if (!this.props.flex) {
style = Object.assign(style, {
width: 'auto',
});
}
srcProps.style = style;
let newProps = this.createProps(srcProps);
return (
<button {...newProps}>
{this.state.text}
</button>
);
}
}
module.exports = UIButton;