import React, { PureComponent, createRef } from 'react'; import { VariableSizeList as List } from 'react-window'; import AutoSizer from 'react-virtualized-auto-sizer'; import classnames from 'classnames'; import UserListItem from './UserListItem'; export default class UserList extends PureComponent { list = createRef(); getSnapshotBeforeUpdate(prevProps) { if (this.list.current) { const { users } = this.props; if (prevProps.users.length !== users.length) { this.list.current.resetAfterIndex( Math.min(prevProps.users.length, users.length) + 1 ); } else { this.list.current.forceUpdate(); } } return null; } getItemHeight = index => { const { users } = this.props; if (index === 0) { return 12; } if (index === users.length + 1) { return 10; } return 24; }; getItemKey = index => { const { users } = this.props; if (index === 0) { return 'top'; } if (index === users.length + 1) { return 'bottom'; } return index; }; renderUser = ({ index, style }) => { const { users, coloredNicks, onNickClick } = this.props; if (index === 0 || index === users.length + 1) { return null; } return ( ); }; render() { const { users, showUserList } = this.props; const className = classnames('userlist', { 'off-canvas': showUserList }); return (
{({ height }) => ( {this.renderUser} )}
); } }