dispatch/client/src/js/components/UserList.js

78 lines
1.8 KiB
JavaScript
Raw Normal View History

import React, { PureComponent } from 'react';
2016-02-06 00:54:21 +00:00
import { VirtualScroll } from 'react-virtualized';
2015-12-28 23:34:32 +00:00
import UserListItem from './UserListItem';
export default class UserList extends PureComponent {
2015-12-28 23:34:32 +00:00
state = {
height: window.innerHeight - 100
};
2015-12-28 23:34:32 +00:00
componentDidMount() {
window.addEventListener('resize', this.handleResize);
}
2016-02-16 21:43:25 +00:00
componentWillUpdate(nextProps) {
if (nextProps.users.size === this.props.users.size) {
2017-02-16 02:55:50 +00:00
this.list.forceUpdate();
2016-02-16 21:43:25 +00:00
}
}
2015-12-28 23:34:32 +00:00
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
2016-02-06 00:54:21 +00:00
getRowHeight = index => {
if (index === 0 || index === this.props.users.size + 1) {
return 10;
}
return 24;
};
listRef = el => { this.list = el; };
2016-02-16 21:43:25 +00:00
handleResize = () => this.setState({ height: window.innerHeight - 100 });
2015-12-28 23:34:32 +00:00
2016-02-06 00:54:21 +00:00
renderUser = index => {
const { users } = this.props;
if (index === 0 || index === users.size + 1) {
2017-02-16 02:55:50 +00:00
return <span style={{ height: '10px' }} />;
2016-02-06 00:54:21 +00:00
}
const { tab, openPrivateChat, select } = this.props;
const user = users.get(index - 1);
return (
<UserListItem
key={user.nick}
user={user}
tab={tab}
openPrivateChat={openPrivateChat}
select={select}
/>
);
};
2015-12-28 23:34:32 +00:00
render() {
2016-02-06 00:54:21 +00:00
const { tab, showUserList } = this.props;
2016-01-11 22:31:06 +00:00
const className = showUserList ? 'userlist off-canvas' : 'userlist';
2015-12-28 23:34:32 +00:00
const style = {};
if (!tab.channel) {
style.display = 'none';
}
return (
2016-01-11 22:31:06 +00:00
<div className={className} style={style}>
2016-02-06 00:54:21 +00:00
<VirtualScroll
ref={this.listRef}
2016-02-06 00:54:21 +00:00
height={this.state.height}
rowsCount={this.props.users.size + 2}
rowHeight={this.getRowHeight}
rowRenderer={this.renderUser}
/>
2015-12-28 23:34:32 +00:00
</div>
);
}
}