-
Notifications
You must be signed in to change notification settings - Fork 3
/
customisation-demo.jsx
176 lines (167 loc) · 3.63 KB
/
customisation-demo.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
175
176
import React from 'react'
import ReactDom from 'react-dom'
import ObjectTable from 'react-object-table'
import { TextEditor } from 'react-object-table'
class ColourDrawer extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div style={{
textAlign: 'center',
}}>
<span style={{
display: 'inline-block',
borderRadius: '50%',
width: '1.4rem',
height: '1.4rem',
backgroundColor: this.props.value,
boxShadow: 'inset 0.2rem 0.2rem 0.2rem rgba(0, 0, 0, 0.25)',
}}></span>
</div>
);
}
}
ColourDrawer = {
className: 'since-drawer',
component: ColourDrawer,
};
function validate_age(value, props) {
var intAge = parseInt(value);
if (isNaN(intAge)) {
return {valid: false,};
}
if (intAge < 0 || intAge > 125) {
return {valid: false,};
}
return {
valid: true,
cleanedValue: intAge,
};
}
class AgeEditor extends TextEditor.component {
validate(value) {
return validate_age(value, this.props);
}
}
AgeEditor = {
className: 'age-editor',
component: AgeEditor,
validate: validate_age,
};
class CustomisedTable extends React.Component {
constructor(props) {
super(props);
this.state = {
...this.state,
exception: null,
objects: [
{
id: 1,
name: 'Phillip',
favouriteColour: 'black',
age: 75,
},
{
id: 2,
name: 'Michael',
favouriteColour: '#f44',
age: 19,
},
{
id: 3,
name: 'Steven McShane',
favouriteColour: '#05AB45',
age: 22,
},
{
id: 4,
name: 'Aidan',
favouriteColour: 'rebeccapurple',
age: 31,
},
],
};
}
handleUpdate(id, values) {
this.setState(prevState => {
const stateChanges = {
exception: null,
objects: prevState.objects,
}
prevState.objects.map((object, index) => {
if (object.id === id) {
stateChanges.objects[index] = {
...object,
...values,
};
}
});
return stateChanges;
});
}
handleCellError(objectId, columnKey, message) {
this.setState({exception: `Unable to update ${columnKey}:\n${message}`});
}
handleRowError(row, message) {
this.setState({exception: message});
}
render() {
var exception;
if (this.state.exception !== null) {
exception = (
<div style={{
display: 'block',
padding: '1rem',
borderRadius: '0.2rem',
background: '#ff4c4c',
fontSize: '1rem',
color: '#fff',
margin: '0 auto',
maxWidth: '20rem',
whiteSpace: 'pre',
}}>
{this.state.exception}
</div>
);
}
return (
<div>
<ObjectTable
columns={this.props.columns}
objects={this.state.objects}
onUpdate={this.handleUpdate.bind(this)}
onCellError={this.handleCellError.bind(this)}
onRowError={this.handleRowError.bind(this)}
/>
{exception}
</div>
);
}
}
CustomisedTable.defaultProps = {
columns: [
{
name: 'Name',
key: 'name',
},
{
name: 'Age',
key: 'age',
width: 100,
editor: AgeEditor,
},
{
name: 'Favourite HTML Colour',
key: 'favouriteColour',
width: 170,
drawer: ColourDrawer,
},
],
};
var mount = document.querySelectorAll('div.demo-mount-customisation');
ReactDom.render(
<CustomisedTable />,
mount[0]
);