Skip to content
Snippets Groups Projects
Commit c056d55a authored by Esko Ikkala's avatar Esko Ikkala
Browse files

Testing CellMeasurer

parent 6d406140
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,8 @@ import { withStyles } from '@material-ui/core/styles'; ...@@ -8,6 +8,8 @@ import { withStyles } from '@material-ui/core/styles';
// import PlaceIcon from '@material-ui/icons/Place'; // import PlaceIcon from '@material-ui/icons/Place';
import { import {
AutoSizer, AutoSizer,
CellMeasurer,
CellMeasurerCache,
Column, Column,
Table, Table,
//SortIndicator //SortIndicator
...@@ -27,15 +29,13 @@ const styles = () => ({ ...@@ -27,15 +29,13 @@ const styles = () => ({
width: '100%', width: '100%',
flexDirection: 'column' flexDirection: 'column'
}, },
resultsInfo: { tableColumn: {
flexGrow: 0 padding: '5px 15px 5px 0'
},
searchField: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: 70
}, },
valueList: {
marginTop: 0,
marginBottom: 0,
}
}); });
const tableStyles = { const tableStyles = {
...@@ -46,45 +46,64 @@ const tableStyles = { ...@@ -46,45 +46,64 @@ const tableStyles = {
textTransform: 'none', textTransform: 'none',
borderBottom: '1px solid rgba(224, 224, 224, 1)' borderBottom: '1px solid rgba(224, 224, 224, 1)'
}, },
evenRow: { tableRow: {
borderBottom: '1px solid rgba(224, 224, 224, 1)', borderBottom: '1px solid rgba(224, 224, 224, 1)'
//backgroundColor: '#fafafa'
},
oddRow: {
borderBottom: '1px solid rgba(224, 224, 224, 1)',
}, },
noRows: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontSize: '1em',
color: '#bdbdbd',
}
};
const calculateRowStyle = ({ index }) => {
if (index < 0) {
return tableStyles.headerRow;
} else {
return index % 2 === 0 ? tableStyles.evenRow : tableStyles.oddRow;
}
}; };
class VirtualizedTable extends React.PureComponent { class VirtualizedTable extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
this._noRowsRenderer = this._noRowsRenderer.bind(this);
this._sort = this._sort.bind(this);
} }
idRenderer = ({cellData, rowData}) => { cache = new CellMeasurerCache({
if (cellData == null) return ''; fixedWidth: true,
minHeight: 40,
});
columnCellRenderer = ({dataKey, parent, rowIndex}) => {
const {list} = this.props;
const rowData = list.get(rowIndex % list.size);
const cellData = rowData[dataKey];
let cellContent = '';
if (cellData == null | cellData === '-') {
cellContent = '-';
} else {
switch(dataKey) {
case 'prefLabel':
cellContent = this.stringListRenderer(cellData);
break;
case 'author':
cellContent = this.objectListRenderer(cellData);
break;
}
}
return (
<CellMeasurer
cache={this.cache}
columnIndex={0}
key={dataKey}
parent={parent}
rowIndex={rowIndex}>
<div
className={this.props.classes.tableColumn}
style={{
whiteSpace: 'normal',
}}>
{cellContent}
</div>
</CellMeasurer>
);
};
idRenderer = ({dataKey, parent, rowIndex}) => {
const {list} = this.props;
const rowData = list.get(rowIndex % list.size);
let cellData = rowData[dataKey];
cellData = cellData.replace('http://ldf.fi/mmm/manifestation_singleton/', '');
let sdbmUrl = ''; let sdbmUrl = '';
let id = ''; let id = '';
if (rowData.manuscriptRecord == '-') { if (rowData.manuscriptRecord == '-') {
...@@ -95,104 +114,106 @@ class VirtualizedTable extends React.PureComponent { ...@@ -95,104 +114,106 @@ class VirtualizedTable extends React.PureComponent {
sdbmUrl = rowData.manuscriptRecord; sdbmUrl = rowData.manuscriptRecord;
} }
id = id.replace('part_', ''); id = id.replace('part_', '');
const idLink = <a target='_blank' rel='noopener noreferrer' href={sdbmUrl}>{id}</a>;
return ( return (
<div key={cellData}> <div className={this.props.classes.tableColumn}>
{idLink} <a target='_blank' rel='noopener noreferrer' href={sdbmUrl}>{id}</a>
</div> </div>
); );
}; };
objectListRenderer = ({cellData}) => { objectListRenderer = (cellData) => {
if (cellData == null || cellData === '-' ) { return (
return ( <div key={cellData}>{'-'}</div> <ul className={this.props.classes.valueList}>
); {cellData.map((item, i) =>
} else { <li key={i}>
return ( <a
<div key={cellData}> target='_blank' rel='noopener noreferrer'
{cellData.map((item, i) => <span key={i}> href={'https://sdbm.library.upenn.edu/' + item.sdbmType + '/' + item.id}
{!!i && <br />} >
<a target='_blank' rel='noopener noreferrer' {item.prefLabel}
href={'https://sdbm.library.upenn.edu/' + item.sdbmType + '/' + item.id}>{item.prefLabel}
</a> </a>
</span>)} </li>
</div> )}
); </ul>
} );
};
stringListRenderer = (cellData) => {
return (
<ul className={this.props.classes.valueList}>
{cellData.map((item, i) => <li key={i}>{item}</li>)}
</ul>
);
}; };
stringListRenderer = ({cellData}) => { rowGetter = ({index}) => this.getDatum(this.props.list, index);
if (cellData == null || cellData === '-' ) {
return ( <div key={cellData}>{'-'}</div> getDatum = (list, index) => {
); return list.get(index % list.size);
}
calculateRowStyle = ({ index }) => {
if (index < 0) {
return tableStyles.headerRow;
} else { } else {
return ( return tableStyles.tableRow;
<div key={cellData}>
{cellData.map((item, i) => <span key={i}>
{!!i && <br />}
{item}
</span>)}
</div>
);
} }
}; };
// <Column
// label="Creation place"
// dataKey="creationPlace"
// cellRenderer={this.columnCellRenderer}
// width={300}
// />
//
render() { render() {
const { classes, list } = this.props; const { classes } = this.props;
const rowGetter = ({index}) => this._getDatum(list, index);
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Grid container className={classes.container}> <Grid container className={classes.container}>
<div className={classes.resultsInfo}> <div className={classes.resultsInfo}>
</div> </div>
{this.props.list.size > 0 && {this.props.list.size > 0 &&
<div style={{ flex: '1 1 auto' }}> <div style={{ flex: '1 1 auto' }}>
<AutoSizer> <AutoSizer>
{({ height, width }) => ( {({ height, width }) => (
<Table <Table
deferredMeasurementCache={this.cache}
rowHeight={this.cache.rowHeight}
overscanRowCount={10} overscanRowCount={10}
rowHeight={150} rowClassName={'tableRow'}
rowGetter={rowGetter} rowGetter={this.rowGetter}
rowCount={this.props.list.size} rowCount={this.props.list.size}
sortDirection={this.props.search.sortDirection.toUpperCase()} sortDirection={this.props.search.sortDirection.toUpperCase()}
width={width} width={width}
height={height} height={height}
headerHeight={50} headerHeight={50}
noRowsRenderer={this._noRowsRenderer}
style={tableStyles.tableRoot} style={tableStyles.tableRoot}
rowStyle={calculateRowStyle} rowStyle={this.calculateRowStyle}
> >
<Column <Column
label="ID" label="ID"
cellDataGetter={({rowData}) => rowData.id.replace('http://ldf.fi/mmm/manifestation_singleton/', '')}
dataKey="id" dataKey="id"
cellRenderer={this.idRenderer} cellRenderer={this.idRenderer}
width={70} width={70}
/> />
<Column <Column
label="Title" label="Title"
cellDataGetter={({rowData}) => rowData.prefLabel}
dataKey="prefLabel" dataKey="prefLabel"
cellRenderer={this.stringListRenderer} cellRenderer={this.columnCellRenderer}
width={400} width={400}
/> />
<Column <Column
label="Author" label="Author"
cellDataGetter={({rowData}) => rowData.author}
dataKey="author" dataKey="author"
cellRenderer={this.objectListRenderer} cellRenderer={this.columnCellRenderer}
width={400} width={400}
/> />
<Column
label="Creation place"
cellDataGetter={({rowData}) => rowData.creationPlace}
dataKey="creationPlace"
cellRenderer={this.objectListRenderer}
width={300}
/>
</Table> </Table>
)} )}
</AutoSizer> </AutoSizer>
...@@ -202,44 +223,6 @@ class VirtualizedTable extends React.PureComponent { ...@@ -202,44 +223,6 @@ class VirtualizedTable extends React.PureComponent {
</div> </div>
); );
} }
_getDatum(list, index) {
return list.get(index % list.size);
}
_getRowHeight({index}) {
const list = this.props.list;
return this._getDatum(list, index).size;
}
_noRowsRenderer() {
return <div className={tableStyles.noRows}>No rows</div>;
}
// _onScrollToRowChange(event) {
// const {rowCount} = this.state;
// let scrollToIndex = Math.min(
// rowCount - 1,
// parseInt(event.target.value, 10),
// );
//
// if (isNaN(scrollToIndex)) {
// scrollToIndex = undefined;
// }
//
// this.setState({scrollToIndex});
// }
// https://stackoverflow.com/questions/40412114/how-to-do-proper-column-filtering-with-react-virtualized-advice-needed
_sort({ event, sortBy, sortDirection }) {
// console.log(event.target)
if (event.target.id.startsWith('filter') || event.target.className.startsWith('Mui')) {
event.stopPropagation();
} else {
this.props.sortResults({ sortBy, sortDirection: sortDirection.toLowerCase() });
}
}
} }
VirtualizedTable.propTypes = { VirtualizedTable.propTypes = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment