-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFlatButton.react.js
234 lines (205 loc) · 5.81 KB
/
FlatButton.react.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// @flow
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import MuiFlatButton from 'material-ui/FlatButton';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
type Props = {
backgroundColor?: string,
children?: Node,
className?: string,
disableTouchRipple?: boolean,
disabled?: boolean,
fireEvent?: () => void,
fullWidth?: boolean,
hoverColor?: string,
href?: string,
icon?: Node,
id?: string,
label: string,
labelPosition?: string,
labelStyle?: Object,
n_clicks?: number,
primary?: boolean,
rippleColor?: string,
secondary?: boolean,
setProps?: () => void,
style?: Object,
};
const propTypes = {
/**
* Color of button when mouse is not hovering over it.
*/
backgroundColor: PropTypes.string,
/**
* This is what will be displayed inside the button.
* If a label is specified, the text within the label prop will
* be displayed. Otherwise, the component will expect children
* which will then be displayed. (In our example,
* we are nesting an `<input type="file" />` and a `span`
* that acts as our label to be displayed.) This only
* applies to flat and raised buttons.
*/
children: PropTypes.node,
/**
* The CSS class name of the root element.
*/
className: PropTypes.string,
/**
* If true, the element's ripple effect will be disabled.
*/
disableTouchRipple: PropTypes.bool,
/**
* Disables the button if set to true.
*/
disabled: PropTypes.bool,
/**
* A callback for firing events to dash.
*/
fireEvent: PropTypes.func,
/**
* If true, the button will take up the full width of its container.
*/
fullWidth: PropTypes.bool,
/**
* Color of button when mouse hovers over.
*/
hoverColor: PropTypes.string,
/**
* The URL to link to when the button is clicked.
*/
href: PropTypes.string,
/**
* Use this property to display an icon.
*/
icon: PropTypes.node,
/* The element's ID */
id: PropTypes.string.isRequired,
/**
* Label for the button.
*/
label: PropTypes.string.isRequired,
/**
* Place label before or after the passed children.
*/
labelPosition: PropTypes.oneOf(['before', 'after']),
/**
* Override the inline-styles of the button's label element.
*/
labelStyle: PropTypes.objectOf(PropTypes.any),
/**
* An integer that represents the number of times
* that this element has been clicked on.
*/
n_clicks: PropTypes.number,
/**
* If true, colors button according to
* primaryTextColor from the Theme.
*/
primary: PropTypes.bool,
/**
* Color for the ripple after button is clicked.
*/
rippleColor: PropTypes.string,
/**
* If true, colors button according to secondaryTextColor from the theme.
* The primary prop has precendent if set to true.
*/
secondary: PropTypes.bool,
/** Dash callback to update props on the server */
setProps: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.objectOf(PropTypes.any),
};
const defaultProps = {
backgroundColor: '',
children: null,
className: '',
disableTouchRipple: false,
disabled: false,
fireEvent: () => {},
fullWidth: false,
hoverColor: '',
href: '',
icon: null,
labelPosition: 'after',
labelStyle: {},
n_clicks: 0,
primary: false,
rippleColor: '',
secondary: false,
setProps: () => {},
style: {},
};
export default class FlatButton extends Component<Props> {
constructor(props: Props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
if (this.props.setProps) this.props.setProps({n_clicks: this.props.n_clicks + 1});
if (this.props.fireEvent) this.props.fireEvent({event: 'click'});
}
render() {
const { backgroundColor, className, disableTouchRipple, disabled,
fullWidth, hoverColor, href, icon, id, label, labelPosition, labelStyle,
primary, rippleColor, secondary, style} = this.props;
if (this.props.fireEvent || this.props.setProps) {
return (
<div id={id}>
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
<MuiFlatButton
backgroundColor={backgroundColor}
className={className}
disableTouchRipple={disableTouchRipple}
disabled={disabled}
fullWidth={fullWidth}
hoverColor={hoverColor}
href={href}
icon={icon}
label={label}
labelPosition={labelPosition}
labelStyle={labelStyle}
onClick={this.handleClick}
primary={primary}
rippleColor={rippleColor}
secondary={secondary}
style={style}
>
{this.props.children}
</MuiFlatButton>
</MuiThemeProvider>
</div>);
} else {
return (
<div id={id}>
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
<MuiFlatButton
backgroundColor={backgroundColor}
className={className}
disableTouchRipple={disableTouchRipple}
disabled={disabled}
fullWidth={fullWidth}
hoverColor={hoverColor}
href={href}
icon={icon}
label={label}
labelPosition={labelPosition}
labelStyle={labelStyle}
primary={primary}
rippleColor={rippleColor}
secondary={secondary}
style={style}
>
{this.props.children}
</MuiFlatButton>
</MuiThemeProvider>
</div>);
}
}
}
FlatButton.propTypes = propTypes;
FlatButton.defaultProps = defaultProps;