You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

  1. import Status from './status';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PureRenderMixin from 'react-addons-pure-render-mixin';
  4. import { ScrollContainer } from 'react-router-scroll';
  5. import StatusContainer from '../containers/status_container';
  6. const StatusList = React.createClass({
  7. propTypes: {
  8. statusIds: ImmutablePropTypes.list.isRequired,
  9. onScrollToBottom: React.PropTypes.func,
  10. onScrollToTop: React.PropTypes.func,
  11. onScroll: React.PropTypes.func,
  12. trackScroll: React.PropTypes.bool
  13. },
  14. getDefaultProps () {
  15. return {
  16. trackScroll: true
  17. };
  18. },
  19. mixins: [PureRenderMixin],
  20. handleScroll (e) {
  21. const { scrollTop, scrollHeight, clientHeight } = e.target;
  22. this._oldScrollPosition = scrollHeight - scrollTop;
  23. if (scrollTop === scrollHeight - clientHeight && this.props.onScrollToBottom) {
  24. this.props.onScrollToBottom();
  25. } else if (scrollTop < 100 && this.props.onScrollToTop) {
  26. this.props.onScrollToTop();
  27. } else if (this.props.onScroll) {
  28. this.props.onScroll();
  29. }
  30. },
  31. componentDidUpdate (prevProps) {
  32. if (prevProps.statusIds.size < this.props.statusIds.size && prevProps.statusIds.first() !== this.props.statusIds.first() && this._oldScrollPosition) {
  33. const node = ReactDOM.findDOMNode(this);
  34. if (node.scrollTop > 0) {
  35. node.scrollTop = node.scrollHeight - this._oldScrollPosition;
  36. }
  37. }
  38. },
  39. render () {
  40. const { statusIds, onScrollToBottom, trackScroll } = this.props;
  41. const scrollableArea = (
  42. <div className='scrollable' onScroll={this.handleScroll}>
  43. <div>
  44. {statusIds.map((statusId) => {
  45. return <StatusContainer key={statusId} id={statusId} />;
  46. })}
  47. </div>
  48. </div>
  49. );
  50. if (trackScroll) {
  51. return (
  52. <ScrollContainer scrollKey='status-list'>
  53. {scrollableArea}
  54. </ScrollContainer>
  55. );
  56. } else {
  57. return scrollableArea;
  58. }
  59. }
  60. });
  61. export default StatusList;