-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.jsx
321 lines (286 loc) · 8.14 KB
/
index.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const React = require('react')
const mui = require('material-ui')
const ModeEdit = require('material-ui/svg-icons/editor/mode-edit').default
const Check = require('material-ui/svg-icons/navigation/check').default
const Delete = require('material-ui/svg-icons/action/delete').default
const times = require('lodash.times')
const {IconButton, Toggle, TextField, RaisedButton, DatePicker} = mui
const PropTypes = require('prop-types')
const createReactClass = require('create-react-class')
const injectTapEventPlugin = require('react-tap-event-plugin')
injectTapEventPlugin()
module.exports = createReactClass({
getDefaultProps: () => {
return {
headerColumns: [],
rows: [],
enableDelete: true,
onChange: function () {},
onDelete: function () {}
}
},
getInitialState: function () {
return {
rows: this.props.rows,
hoverValue: false,
currentRow: false
}
},
contextTypes: {
muiTheme: PropTypes.object.isRequired
},
update: function () {
const row = this.state.rows.filter((row) => {
return row.selected
})
this.props.onChange(row[0])
},
getCellValue: function (cell) {
const self = this
const id = cell && cell.id
const type = this.props.headerColumns.map((header) => {
return header.type
})[id]
const selected = cell && cell.selected
const value = cell && cell.value
const rowId = cell && cell.rowId
const header = cell && cell.header
const width = cell && cell.width
const textFieldId = [id, rowId, header, 'text'].join('-')
const datePickerId = [id, rowId, header, 'date'].join('-')
const textFieldStyle = {
width: width
}
const datePickerStyle = {
width: width
}
const onTextFieldChange = (e) => {
const target = e.target
const value = target.value
var rows = self.state.rows
rows[rowId].columns[id].value = value
self.setState({rows: rows})
}
const onDatePickerChange = (e, date) => {
var rows = self.state.rows
rows[rowId].columns[id].value = date
self.setState({rows: rows})
}
const onToggle = (e) => {
var rows = self.state.rows
rows[rowId].columns[id].value = !rows[rowId].columns[id].value
self.setState({rows: rows})
}
if (header || (type && type === 'ReadOnly')) {
return <p style={{color: '#888'}}>{value}</p>
}
if (type) {
if (selected) {
if (type === 'TextField') {
return <TextField
id={textFieldId}
onChange={onTextFieldChange}
style={textFieldStyle}
value={value}
/>
}
if (type === 'DatePicker') {
return <DatePicker
id={datePickerId}
onChange={onDatePickerChange}
mode='landscape'
style={datePickerStyle}
value={value}
/>
}
if (type === 'Toggle') {
return <Toggle onToggle={onToggle} toggled={value} />
}
} else {
if (type === 'Toggle') {
return <Toggle disabled onToggle={onToggle} toggled={value} />
}
if (type === 'DatePicker') {
return <DatePicker
id={datePickerId}
onChange={onDatePickerChange}
mode='landscape'
style={datePickerStyle}
value={value}
disabled={Boolean(true)}
/>
}
}
}
return <TextField
id={textFieldId}
style={textFieldStyle}
disabled
value={value}
/>
},
renderHeader: function () {
const headerColumns = this.props.headerColumns
const columns = headerColumns.map((column, id) => {
return {value: column.value}
})
const row = {columns: columns, header: true}
return this.renderRow(row)
},
renderRow: function (row) {
const self = this
const columns = row.columns
const rowStyle = {
width: '100%',
display: 'flex',
flexFlow: 'row nowrap',
padding: row.header ? 0 : 12,
border: 0,
borderBottom: '1px solid #ccc',
height: 50
}
const checkboxStyle = {
display: 'flex',
flexFlow: 'row nowrap',
width: 50,
height: 24,
alignItems: 'center'
}
const deleteButtonStyle = {
display: 'flex',
flexFlow: 'row nowrap',
width: 50,
height: 24,
alignItems: 'center',
padding: '0 12 0'
}
const rowId = row.id
const rowKey = ['row', rowId].join('-')
const onRowClick = function (e) {
var rows = self.state.rows
rows.forEach((row, i) => {
if (rowId !== i) row.selected = false
})
rows[rowId].selected = !rows[rowId].selected
self.setState({rows: rows})
}
const r = self.state.rows[rowId]
const selected = (r && r.selected) || false
const button = selected ? <Check /> : <ModeEdit />
const tooltip = selected ? 'Done' : 'Edit'
const onDeleteRow = function (e) {
var rows = self.state.rows
var deleteEvent = {}
rows.forEach((row, i) => {
if (rowId === i) {
rows.splice(i, 1)
deleteEvent = {rowId, row}
}
})
rows.forEach((row, i) => {
row.id = i
})
self.setState({rows: rows})
if (deleteEvent !== {}) self.props.onDelete(deleteEvent)
}
const onClick = function (e) {
if (selected) {
self.update()
}
onRowClick(e)
}
const deleteButton = (!this.props.enableDelete || selected || row.header) ? <div style={deleteButtonStyle} />
: <IconButton style={deleteButtonStyle} tooltip={'Delete this row'} onClick={onDeleteRow}>
<Delete />
</IconButton>
const checkbox = row.header ? <div style={checkboxStyle} />
: <IconButton style={checkboxStyle} tooltip={tooltip} onClick={onClick}>
{button}
</IconButton>
return (
<div key={rowKey} className='row' style={rowStyle}>
{checkbox}
{columns.map((column, id) => {
const width = this.props.headerColumns.map((header) => {
return (header && header.width) || false
})[id]
const cellStyle = {
display: 'flex',
flexFlow: 'row nowrap',
flexGrow: 0.15,
flexBasis: 'content',
alignItems: 'center',
height: 30,
width: width || 200
}
const columnKey = ['column', id].join('-')
column.selected = selected
column.rowId = rowId
column.id = id
column.header = row.header
column.width = cellStyle.width
return (
<div key={columnKey} className='cell' style={cellStyle}>
<div>
{this.getCellValue(column)}
</div>
</div>
)
})}
{deleteButton}
</div>
)
},
render: function () {
const self = this
const style = {
display: 'flex',
flexFlow: 'column nowrap',
justifyContent: 'space-between',
alignItems: 'center',
fontFamily: 'Roboto, sans-serif'
}
const buttonStyle = {
display: 'flex',
flexFlow: 'row nowrap',
marginTop: 10
}
const rows = this.state.rows
const columnTypes = this.props.headerColumns.map((header) => {
return header.type
})
const onButtonClick = (e) => {
const newColumns = times(columnTypes.length, (index) => {
const defaults = {
'TextField': '',
'Toggle': true
}
const value = defaults[columnTypes[index]]
return {value: value}
})
const updatedRows = rows.map((row) => {
if (row.selected) {
self.update()
row.selected = false
}
return row
})
updatedRows.push({columns: newColumns, selected: true})
self.setState({rows: updatedRows})
}
return (
<div className='container' style={style}>
{this.renderHeader()}
{rows.map((row, id) => {
row.id = id
return this.renderRow(row)
})}
<RaisedButton
onClick={onButtonClick}
style={buttonStyle}
label='Add Row'
/>
</div>
)
}
})