-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.jsx
More file actions
71 lines (58 loc) · 1.8 KB
/
component.jsx
File metadata and controls
71 lines (58 loc) · 1.8 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
import React from 'react';
export class Component extends React.Component {
static alertWhenError = (callback, ...args) => {
try {
return callback(...args);
} catch (error) {
window.alert(error.message || error);
throw error;
}
}
setStateOrAlert = (callback) => {
this.setState((state, props) => Component.alertWhenError(callback, state, props));
}
createEventArgs = (callback, event) => {
return (callback) ? Component.alertWhenError(callback, event) : [];
}
handleChange = (event) => {
if (!this.props.disabled && this.props.onChange) {
const args = this.createEventArgs(this.argsOnChange, event);
Component.alertWhenError(this.props.onChange, ...args);
}
}
handleClick = (event) => {
if (!this.props.disabled && this.props.onClick) {
const args = this.createEventArgs(this.argsOnClick, event);
Component.alertWhenError(this.props.onClick, ...args);
}
}
handleContextMenu = (event) => {
event.preventDefault();
if (!this.props.disabled && this.props.onContextMenu) {
const args = this.createEventArgs(this.argsOnContextMenu, event);
Component.alertWhenError(this.props.onContextMenu, ...args);
}
}
handleSubmit = (event) => {
event.preventDefault();
if (!this.props.disabled && this.props.onSubmit) {
const args = this.createEventArgs(this.argsOnSubmit, event);
Component.alertWhenError(this.props.onSubmit, new FormData(event.target), ...args);
}
}
render() {
try {
const view = this.createView();
if (!view) {
throw new Error("illegal view.");
}
return view;
} catch (error) {
return (
<div className="render-error">
{error.message || error} in {this.constructor.name}
</div>
);
}
}
}