11import React from 'react' ;
2- import { Grid , MultiGrid , AutoSizer , CellMeasurer } from 'react-virtualized' ;
2+ import ReactDOM from 'react-dom' ;
3+ import { MultiGrid , AutoSizer , CellMeasurer } from 'react-virtualized' ;
34import 'react-virtualized/styles.css' ;
45// hack: `stream.Transform` (stream-browserify) is undefined in `csv-parse` when
56// built with @jupyterlabextension -builder
67import infer from 'jsontableschema/lib/infer' ;
78// import { infer } from 'jsontableschema';
89import './index.css' ;
910
10- const ROW_HEIGHT = 34 ;
11+ const ROW_HEIGHT = 42 ;
12+ const GRID_MAX_HEIGHT = 336 ;
1113
1214function inferSchema ( data ) {
13- const headers = data . reduce (
14- ( result , row ) => [ ...new Set ( [ ...result , ...Object . keys ( row ) ] ) ] ,
15+ // Take a sampling of rows from data
16+ const range = Array . from ( { length : 10 } , ( v , i ) =>
17+ Math . floor ( Math . random ( ) * data . length ) ) ;
18+ // Separate headers and values
19+ const headers = range . reduce (
20+ ( result , row ) => [ ...new Set ( [ ...result , ...Object . keys ( data [ row ] ) ] ) ] ,
1521 [ ]
1622 ) ;
17- const values = data . map ( row => Object . values ( row ) ) ;
23+ const values = range . map ( row => Object . values ( data [ row ] ) ) ;
24+ // Infer column types and return schema for data
1825 return infer ( headers , values ) ;
1926}
2027
@@ -32,27 +39,30 @@ export default class VirtualizedGrid extends React.Component {
3239 }
3340
3441 render ( ) {
42+ const rowCount = this . data . length + 1
43+ const height = rowCount * ROW_HEIGHT ;
3544 return (
3645 < AutoSizer disableHeight >
3746 { ( { width } ) => (
3847 < CellMeasurer
3948 cellRenderer = { this . cellRenderer }
4049 columnCount = { this . schema . fields . length }
4150 height = { ROW_HEIGHT }
42- rowCount = { this . data . length + 1 }
51+ rowCount = { rowCount }
4352 >
4453 { ( { getColumnWidth } ) => (
4554 < MultiGrid
4655 cellRenderer = { this . cellRenderer }
4756 columnCount = { this . schema . fields . length }
48- columnWidth = { getColumnWidth }
57+ columnWidth = { index => getColumnWidth ( index ) + 10 }
58+ fixedColumnCount = { 1 }
4959 fixedRowCount = { 1 }
5060 height = {
51- ( this . data . length + 1 ) * ( ROW_HEIGHT + 8 ) < 400
52- ? ( this . data . length + 1 ) * ( ROW_HEIGHT + 8 )
53- : 400
61+ height < GRID_MAX_HEIGHT
62+ ? height
63+ : GRID_MAX_HEIGHT
5464 }
55- rowCount = { this . data . length + 1 }
65+ rowCount = { rowCount }
5666 rowHeight = { ROW_HEIGHT }
5767 width = { width }
5868 />
@@ -70,13 +80,13 @@ export default class VirtualizedGrid extends React.Component {
7080 style = { {
7181 ...style ,
7282 fontFamily : '-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"' ,
73- fontSize : 16 ,
83+ fontSize : 12 ,
7484 fontWeight : rowIndex === 0 ? 600 : 'normal' ,
7585 backgroundColor : rowIndex % 2 === 0 && rowIndex !== 0
7686 ? '#f8f8f8'
7787 : '#fff' ,
78- border : '1px solid #ddd ' ,
79- padding : '6px 13px '
88+ padding : '6px 13px ' ,
89+ boxSizing : 'border-box '
8090 } }
8191 >
8292 {
0 commit comments