1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ElectronClient/app/gui/ItemList.jsx

70 lines
1.7 KiB
React
Raw Normal View History

const React = require('react');
2017-11-04 18:40:34 +02:00
class ItemList extends React.Component {
constructor() {
super();
this.scrollTop_ = 0;
}
updateStateItemIndexes(props) {
if (typeof props === 'undefined') props = this.props;
const topItemIndex = Math.floor(this.scrollTop_ / props.itemHeight);
const visibleItemCount = Math.ceil(props.style.height / props.itemHeight);
let bottomItemIndex = topItemIndex + visibleItemCount;
if (bottomItemIndex >= props.items.length) bottomItemIndex = props.items.length - 1;
this.setState({
topItemIndex: topItemIndex,
bottomItemIndex: bottomItemIndex,
});
}
componentWillMount() {
this.updateStateItemIndexes();
}
componentWillReceiveProps(newProps) {
this.updateStateItemIndexes(newProps);
}
onScroll(scrollTop) {
this.scrollTop_ = scrollTop;
this.updateStateItemIndexes();
}
2017-11-04 18:40:34 +02:00
render() {
const items = this.props.items;
if (!this.props.itemHeight) throw new Error('itemHeight is required');
const blankItem = function(key, height) {
return <div key={key} style={{height:height}}></div>
}
let itemComps = [blankItem('top', this.state.topItemIndex * this.props.itemHeight)];
for (let i = this.state.topItemIndex; i <= this.state.bottomItemIndex; i++) {
2017-11-10 22:11:48 +02:00
const itemComp = this.props.itemRenderer(items[i]);
2017-11-04 18:40:34 +02:00
itemComps.push(itemComp);
}
itemComps.push(blankItem('bottom', (items.length - this.state.bottomItemIndex - 1) * this.props.itemHeight));
let classes = ['item-list'];
if (this.props.className) classes.push(this.props.className);
const that = this;
2017-11-04 18:40:34 +02:00
return (
<div className={classes.join(' ')} style={this.props.style} onScroll={ (event) => { this.onScroll(event.target.scrollTop) }}>
2017-11-04 18:40:34 +02:00
{ itemComps }
</div>
);
}
}
module.exports = { ItemList };