| 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
2×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import React from 'react';
import PropTypes from 'prop-types';
import ReactTable from 'react-table';
import format from 'string-format';
import isEqual from 'lodash/isEqual';
import { format as d3Format } from 'd3-format';
// Material UI
import Checkbox from 'material-ui/Checkbox';
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
const propTypes = {
/** The ID used to identify this compnent in Dash callbacks */
id: PropTypes.string,
/** The value displayed in the input */
data: PropTypes.arrayOf(PropTypes.object),
/** Formatting Array with {col, formatter, header, queryStr, actOnNullLink and isLink} */
columns: PropTypes.arrayOf(PropTypes.object),
/** Are rows selectable via checkbox? */
rowsSelectable: PropTypes.bool,
/** rowsSelected */
rowsSelected: PropTypes.arrayOf(PropTypes.object),
/** Page size options */
pageSizeOptions: PropTypes.arrayOf(PropTypes.number),
/** Default page size */
defaultPageSize: PropTypes.number,
/** Max number checked */
maxNumberChecked: PropTypes.number,
/** Custom style object to pass to ReactTable */
style: PropTypes.object,
/** Filterable */
filterable: PropTypes.bool,
/** Column to use as the index */
colIndex: PropTypes.string,
/** Values to not follow for the link */
doNotFollowLinkValues: PropTypes.arrayOf(PropTypes.any),
/** Dash update props */
setProps: PropTypes.func
};
const defaultProps = {
rowsSelected: [],
defaultPageSize: 10,
pageSizeOptions: [5, 10, 20],
maxNumberChecked: -1,
filterable: false,
doNotFollowLinkValues: [null]
};
/**
* SDDataTable is an example component.
* It takes a property, `label`, and
* displays it.
* It renders an input with the property `value`
* which is editable by the user.
*/
export default class SDDataTable extends React.Component {
/**
* Update the location on the page
* @param event
* @param href
* Reference we are headed to
* @param {boolean} refresh
* Refresh the page?
*/
static updateLocation(event, href, refresh) {
event.preventDefault();
if (refresh) {
window.location.href = href;
} else {
window.history.pushState({}, '', href);
window.dispatchEvent(new Event('onpushstate'));
}
}
static createFormattingFunc(formattingStr, isLink, refreshLink, queryStr, actOnNullLink = true,
doNotFollowLinkValues = [null]) {
Eif (typeof formattingStr === 'undefined')
return;
if (isLink)
return SDDataTable.createLinkFormattingFunc(
formattingStr, refreshLink, queryStr, actOnNullLink, doNotFollowLinkValues);
else
return SDDataTable.createD3FormattingFunc(formattingStr, queryStr);
}
/**
* @param {string} formattingStr
* @param {boolean} refreshLink
* Refresh the page on click
* @param {string} queryStr
* Additional query params
* @param {boolean} actOnNullLink
* Update the location if the value in the column is null?
* @param {Array} doNotFollowLinkValues
* Values to ignore for links
*
* @returns {function}
*/
static createLinkFormattingFunc(formattingStr, refreshLink, queryStr, actOnNullLink = true,
doNotFollowLinkValues = [null]) {
return props => <a
href="#update"
onClick={(event) => {
if (typeof props.value.value !== 'undefined'
&& props.value.value !== null
&& doNotFollowLinkValues.indexOf(props.value.value) > -1)
SDDataTable.updateLocation(
event,
typeof queryStr === 'string'
? `${format(formattingStr, props.value.value)}?${queryStr}`
: format(formattingStr, props.value.value),
refreshLink);
else if (actOnNullLink)
SDDataTable.updateLocation(
event,
typeof queryStr === 'string'
? `${format(formattingStr, props.value.value)}?${queryStr}`
: format(formattingStr, props.value.value),
refreshLink);
else
event.preventDefault()
}}>
{props.value.label}
</a>;
}
/**
* @param {string} formattingStr
* @param {string} queryStr
*
* @returns {function}
*/
static createD3FormattingFunc(formattingStr, queryStr) {
if (typeof queryStr === 'string')
return props => `${d3Format(formattingStr)(props.value)}?${queryStr}`;
return props => d3Format(formattingStr)(props.value);
}
/**
* Convert columns to the format required by React-Table
*
* @param {Array<Object>} columns
* An array of the form [
* {header, col, columns?, isMultilevel?, formatter, isLink, refreshLink, queryStr}]
* @param {Array} doNotFollowLinkValues
* Values to ignore for links
*
* @returns {Array<Object>}
*/
static processColumns(columns, doNotFollowLinkValues = [null]) {
return columns.map(c => (
c.isMultilevel
? {
...c,
Header: c.header,
accessor: c.col,
columns: SDDataTable.processColumns(c.columns)
}
: {
...c,
Header: typeof c.header === 'undefined' ? c.col : c.header,
accessor: c.col,
Cell: SDDataTable.createFormattingFunc(
c.formatter, c.isLink, c.refreshLink, c.queryStr, c.actOnNullLink,
doNotFollowLinkValues),
sortMethod: (a, b) => {
if (c.isLink)
return a.value > b.value ? 1 : -1;
return a > b ? 1 : -1;
},
filterMethod: (filter, row) => {
const val = typeof row[filter.id].label === 'undefined'
? row[filter.id]
: row[filter.id].label;
return val.toString().toLowerCase().startsWith(filter.value.toLowerCase());
}
}));
}
constructor(props) {
super(props);
this.state = {
rowsSelected: props.rowsSelected,
warningOpen: false
};
}
componentWillReceiveProps(nextProps) {
if (typeof nextProps.rowsSelected === 'undefined')
return;
if (!isEqual(nextProps.rowsSelected, this.state.rowsSelected))
this.setState({rowsSelected: nextProps.rowsSelected});
}
get columns() {
const { columns, rowsSelectable } = this.props;
const dataCols = SDDataTable.processColumns(columns);
return rowsSelectable
? [{
header: '',
accessor: 'editButton',
Cell: (props) => (
<div
id={props.row._index}
className="sd-data-table-checkbox"
style={{float: 'left', marginLeft: -2}}
>
<Checkbox
style={{marginLeft: 0, paddingLeft: 0, width: 30}}
inputStyle={{width: 30}}
onCheck={() => this.onButtonClick(props.row)}
checked={this.state.rowsSelected.indexOf(
this.valCheckedInStateRowsSelected(props.row)) > -1}
/>
</div>),
maxWidth: 30,
width: 30,
filterMethod: (filter, row) => {
if (filter.value === 'all') {
return true;
}
if (filter.value === 'true') {
return this.state.rowsSelected.indexOf(
this.valCheckedInStateRowsSelected(row)) > -1;
}
return this.state.rowsSelected.indexOf(
this.valCheckedInStateRowsSelected(row)) === -1;
},
Filter: ({ filter, onChange }) =>
<select
onChange={event => onChange(event.target.value)}
style={{width: '100%'}}
value={filter ? filter.value : 'all'}
>
<option value="all">All</option>
<option value="true">✓</option>
</select>
}].concat(dataCols)
: dataCols;
}
valCheckedInStateRowsSelected(row) {
const { colIndex } = this.props;
const useColIndex = typeof row[colIndex] !== 'undefined';
let valChecked;
if (useColIndex) {
valChecked = typeof row[colIndex].value === 'undefined'
? row[colIndex]
: row[colIndex].value;
} else
valChecked = row._index;
return valChecked;
}
onButtonClick(row) {
const { maxNumberChecked } = this.props;
const valChecked = this.valCheckedInStateRowsSelected(row);
const idxChecked = this.state.rowsSelected.indexOf(valChecked);
const rowsSelected = (idxChecked === -1)
? this.state.rowsSelected.concat([valChecked])
: this.state.rowsSelected.filter(val => val !== valChecked);
if (maxNumberChecked > 0 && rowsSelected.length > maxNumberChecked) {
this.setState({warningOpen: true});
return;
}
this.setState({rowsSelected});
if (this.props.setProps)
this.props.setProps({rowsSelected});
}
render() {
const { id, data, pageSizeOptions, defaultPageSize, style, filterable } = this.props;
const actions = [
<FlatButton
label="OK, got it!"
primary={true}
onClick={() => { this.setState({warningOpen: false}); }}
/>
];
return (
<div id={id}>
<MuiThemeProvider>
<div>
<ReactTable
data={data}
columns={this.columns}
pageSizeOptions={pageSizeOptions}
defaultPageSize={defaultPageSize}
style={style}
filterable={filterable}
/>
<Dialog
title="Too many entries"
actions={actions}
modal={false}
open={this.state.warningOpen}
onRequestClose={() => { this.setState({warningOpen: false}); }}
>
Cannot select more than <strong>{this.props.maxNumberChecked} entries</strong>.
Please uncheck one of your current selections before adding another.
</Dialog>
</div>
</MuiThemeProvider>
</div>);
}
}
SDDataTable.propTypes = propTypes;
SDDataTable.defaultProps = defaultProps;
|