The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

69 lines
1.8 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import ImmutablePureComponent from 'react-immutable-pure-component';
  5. import ConversationContainer from '../containers/conversation_container';
  6. import ScrollableList from '../../../components/scrollable_list';
  7. import { debounce } from 'lodash';
  8. export default class ConversationsList extends ImmutablePureComponent {
  9. static propTypes = {
  10. conversationIds: ImmutablePropTypes.list.isRequired,
  11. hasMore: PropTypes.bool,
  12. isLoading: PropTypes.bool,
  13. onLoadMore: PropTypes.func,
  14. shouldUpdateScroll: PropTypes.func,
  15. };
  16. getCurrentIndex = id => this.props.conversationIds.indexOf(id)
  17. handleMoveUp = id => {
  18. const elementIndex = this.getCurrentIndex(id) - 1;
  19. this._selectChild(elementIndex);
  20. }
  21. handleMoveDown = id => {
  22. const elementIndex = this.getCurrentIndex(id) + 1;
  23. this._selectChild(elementIndex);
  24. }
  25. _selectChild (index) {
  26. const element = this.node.node.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  27. if (element) {
  28. element.focus();
  29. }
  30. }
  31. setRef = c => {
  32. this.node = c;
  33. }
  34. handleLoadOlder = debounce(() => {
  35. const last = this.props.conversationIds.last();
  36. if (last) {
  37. this.props.onLoadMore(last);
  38. }
  39. }, 300, { leading: true })
  40. render () {
  41. const { conversationIds, onLoadMore, ...other } = this.props;
  42. return (
  43. <ScrollableList {...other} onLoadMore={onLoadMore && this.handleLoadOlder} scrollKey='direct' ref={this.setRef}>
  44. {conversationIds.map(item => (
  45. <ConversationContainer
  46. key={item}
  47. conversationId={item}
  48. onMoveUp={this.handleMoveUp}
  49. onMoveDown={this.handleMoveDown}
  50. />
  51. ))}
  52. </ScrollableList>
  53. );
  54. }
  55. }