forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.jsx
More file actions
188 lines (153 loc) · 3.72 KB
/
button.jsx
File metadata and controls
188 lines (153 loc) · 3.72 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
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
/**
* MUI React button module
* @module react/button
*/
'use strict';
import React from 'react';
import * as jqLite from '../js/lib/jqLite';
import * as util from '../js/lib/util';
let rippleIter = 0;
const PropTypes = React.PropTypes,
btnClass = 'mui-btn',
rippleClass = 'mui-ripple-effect',
btnAttrs = {color: 1, variant: 1, size: 1};
/**
* Button element
* @class
*/
class Button extends React.Component {
state = {
ripples: {}
};
static propTypes = {
color: PropTypes.oneOf(['default', 'primary', 'danger', 'dark', 'accent']),
disabled: PropTypes.bool,
size: PropTypes.oneOf(['default', 'small', 'large']),
type: PropTypes.oneOf(['submit', 'button']),
variant: PropTypes.oneOf(['default', 'flat', 'raised', 'fab']),
onClick: PropTypes.func
};
static defaultProps = {
className: '',
color: 'default',
disabled: false,
size: 'default',
type: null,
variant: 'default',
onClick: null
};
componentDidMount() {
// disable MUI js
let el = this.refs.buttonEl;
el._muiDropdown = true;
el._muiRipple = true;
}
onClick(ev) {
let onClickFn = this.props.onClick;
onClickFn && onClickFn(ev);
}
onMouseDown(ev) {
// get (x, y) position of click
let offset = jqLite.offset(this.refs.buttonEl);
// choose diameter
let diameter = offset.height;
if (this.props.variant === 'fab') diameter = diameter / 2;
// add ripple to state
let ripples = this.state.ripples;
let key = Date.now();
ripples[key] = {
xPos: ev.pageX - offset.left,
yPos: ev.pageY - offset.top,
diameter: diameter,
teardownFn: this.teardownRipple.bind(this, key)
};
this.setState({ ripples });
}
onTouchStart(ev) {
}
teardownRipple(key) {
// delete ripple
let ripples = this.state.ripples;
delete ripples[key];
this.setState({ ripples });
}
render() {
let cls = btnClass,
k,
v;
const ripples = this.state.ripples;
// button attributes
for (k in btnAttrs) {
v = this.props[k];
if (v !== 'default') cls += ' ' + btnClass + '--' + v;
}
return (
<button
{ ...this.props }
ref="buttonEl"
className={cls + ' ' + this.props.className}
onClick={this.onClick.bind(this)}
onMouseDown={this.onMouseDown.bind(this)}
>
{this.props.children}
{
Object.keys(ripples).map((k, i) => {
let v = ripples[k];
return (
<Ripple
key={k}
xPos={v.xPos}
yPos={v.yPos}
diameter={v.diameter}
onTeardown={v.teardownFn}
/>
);
})
}
</button>
);
}
}
/**
* Ripple component
* @class
*/
class Ripple extends React.Component {
static propTypes = {
xPos: PropTypes.number,
yPos: PropTypes.number,
diameter: PropTypes.number,
onTeardown: PropTypes.func
};
static defaultProps = {
xPos: 0,
yPos: 0,
diameter: 0,
onTeardown: null
};
componentDidMount() {
// trigger teardown in 2 sec
let teardownTimer = setTimeout(() => {
let fn = this.props.onTeardown;
fn && fn();
}, 2000);
this.setState({ teardownTimer });
}
componentWillUnmount() {
// clear timeout
clearTimeout(this.state.teardownTimer);
}
render() {
const diameter = this.props.diameter,
radius = diameter / 2;
const style = {
height: diameter,
width: diameter,
top: this.props.yPos - radius || 0,
left: this.props.xPos - radius || 0
};
return <div className={rippleClass} style={style} />;
}
}
/** Define module API */
export default Button;