-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProteinTable.jsx
367 lines (351 loc) · 7.53 KB
/
ProteinTable.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { useCallback, useMemo, useState, useEffect, Fragment } from "npm:react";
import { useExpanded, useTable } from 'npm:react-table';
// This could be inlined into SubRowAsync, this this lets you reuse it across tables
function SubRows({ row, rowProps, visibleColumns, data, loading }) {
if (loading) {
return (
<tr>
<td/>
<td colSpan={visibleColumns.length - 1}>
Loading...
</td>
</tr>
);
}
// error handling here :)
return (
<>
{data.map((x, i) => {
return (
<tr
{...rowProps}
key={`${rowProps.key}-expanded-${i}`}
>
{row.cells.map((cell) => {
return (
<td
{...cell.getCellProps()}
>
{cell.render(cell.column.SubCell ? 'SubCell' : 'Cell', {
value:
cell.column.accessor &&
cell.column.accessor(x, i),
row: { ...row, original: x }
})}
</td>
);
})}
</tr>
);
})}
</>
);
}
function SubRowAsync({ row, rowProps, visibleColumns }) {
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
useEffect(() => {
const timer = setTimeout(() => {
setData(dat0);
setLoading(false);
}, 500);
return () => {
clearTimeout(timer);
};
}, []);
return (
<SubRows
row={row}
rowProps={rowProps}
visibleColumns={visibleColumns}
data={data}
loading={loading}
/>
);
}
function Table({ columns: userColumns, data, renderRowSubComponent }) {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
visibleColumns,
state: { expanded } // expanded rowId -> isExpanded:boolean
} = useTable(
{
columns: userColumns,
data
},
useExpanded // We can useExpanded to track the expanded state for sub components too!
)
return <>
<pre>
<code>{JSON.stringify({ expanded: expanded }, null, 2)}</code>
</pre>
<table {...getTableProps()} className={"w-full text-sm text-left text-gray-500 dark:text-gray-400"}>
<thead className={"text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400"}>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th className={"px-6 py-3"} {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
const rowProps = row.getRowProps();
return (
// Use a React.Fragment here so the table markup is still valid
<Fragment key={rowProps.key}>
<tr {...rowProps} className={"bg-white border-b dark:bg-gray-800 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600"}>
{row.cells.map(cell => {
return (
<td className={"px-6 py-4"} {...cell.getCellProps()}>{cell.render('Cell')}</td>
);
})}
</tr>
{/* We could pass anything into this */}
{row.isExpanded &&
renderRowSubComponent({ row, rowProps, visibleColumns })}
</Fragment>
)
})}
</tbody>
</table>
</>
}
const ProteinTable = () => {
const columns = useMemo(
() => [
{
// Make an expander cell
Header: () => null, // No header
id: 'expander', // It needs an ID
Cell: ({ row }) => (
// Use Cell to render an expander for each row.
// We can use the getToggleRowExpandedProps prop-getter
// to build the expander.
<span {...row.getToggleRowExpandedProps()}>
{row.isExpanded ? '👇' : '👉'}
</span>
),
// We can override the cell renderer with a SubCell to be used with an expanded row
SubCell: () => null // No expander on an expanded row
},
{
Header: 'Name',
columns: [
{
Header: 'Protein Accessions',
// We re-map data using accessor functions for subRows
accessor: (d) =>
d[8].map(tt_id => dat0.transcript_translations[tt_id]).join(",")
,
// We can render something different for subRows
SubCell: (cellProps) => (
<>🥳 {cellProps.value} 🎉</>
)
},
{
Header: 'Gene',
accessor: (d) =>
d[6].map(g_id => dat0.genes[g_id]).join(",")
},
{
Header: 'Transcript Accession',
accessor: (d) =>
d[7].map(t_id => dat0.transcripts[t_id]).join(",")
}
]
}
],
[]
);
const data = useMemo(() => dat0, []);
// Create a function that will render our row sub components
const renderRowSubComponent = useCallback(
({ row, rowProps, visibleColumns }) => (
<SubRowAsync
row={row}
rowProps={rowProps}
visibleColumns={visibleColumns}
/>
),
[]
);
return (
<Table
columns={columns}
data={dat0.proteins}
// We added this as a prop for our table component
// Remember, this is not part of the React Table API,
// it's merely a rendering option we created for
// ourselves
renderRowSubComponent={renderRowSubComponent}
/>
);
}
export default ProteinTable
const dat0 = {
"genes": {
"5699447": "FUS3",
"5700469": "FUS1",
"5708188": "FUS2"
},
"transcripts": {
"13736747": "NM_001178256.1",
"13737128": "NM_001178672.1",
"13747109": "NM_001182739.1"
},
"transcript_translations": {
"78647682": "NP_009537.1",
"78656614": "NP_009903.2",
"78656616": "IP_001585",
"78656618": "IP_001586",
"78655645": "NP_013959.1",
"78655648": "IP_012221",
"78655650": "IP_012222",
"78655646": "IP_012223"
},
"proteins": [
[
567,
null,
0,
0,
0,
0,
[
5699447
],
[
13736747
],
[
78647682
]
],
[
1584,
null,
0,
0,
0,
0,
[
5700469
],
[
13737128
],
[
78656614
]
],
[
1585,
null,
0,
0,
0,
0,
[
5700469
],
[
13737128
],
[
78656616
]
],
[
1586,
null,
0,
0,
0,
0,
[
5700469
],
[
13737128
],
[
78656618
]
],
[
12220,
null,
0,
0,
0,
0,
[
5708188
],
[
13747109
],
[
78655645
]
],
[
12221,
null,
0,
0,
0,
0,
[
5708188
],
[
13747109
],
[
78655648
]
],
[
12222,
null,
0,
0,
0,
0,
[
5708188
],
[
13747109
],
[
78655650
]
],
[
12223,
null,
0,
0,
0,
0,
[
5708188
],
[
13747109
],
[
78655646
]
]
]
}