Guest User

Untitled

a guest
Jan 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2.  
  3. export default class SearchTable extends Component {
  4.  
  5.   constructor(props) {
  6.     super(props);
  7.  
  8.     this.state = {
  9.       equities         : props.equities,
  10.       defaultNumResults: 30,
  11.       numResults       : 30,
  12.     };
  13.  
  14.     this.registerScrollSpy();
  15.   }
  16.  
  17.   componentWillUnmount() {
  18.     window.removeEventListener('scroll', this.isScrolledToBottom);
  19.   }
  20.  
  21.   registerScrollSpy = () => {
  22.     window.addEventListener('scroll', this.isScrolledToBottom);
  23.   }
  24.  
  25.   isScrolledToBottom = () => {
  26.     const offset = 5;
  27.     const scrollTop = (document.documentElement && document.documentElement.scrollTop)
  28.       || document.body.scrollTop;
  29.     const scrollHeight = (document.documentElement && document.documentElement.scrollHeight)
  30.       || document.body.scrollHeight;
  31.     const scrolledToBottom = (scrollTop + window.innerHeight + offset) >= scrollHeight;
  32.  
  33.     if (scrolledToBottom) {
  34.       this.showTenMoreEquities();
  35.     }
  36.   }
  37.  
  38.   showTenMoreEquities = () => {
  39.     const numResults = this.state.numResults;
  40.     this.setState({
  41.       show      : false,
  42.       numResults: numResults + 10,
  43.     });
  44.   }
  45.  
  46.   render() {
  47.     const numResults = this.state.numResults;
  48.     return (
  49.       <div>
  50.         <table>
  51.           <thead>
  52.             <tr>
  53.               <th>Navn</th>
  54.               <th className="hide-on-500px">Oppdatert</th>
  55.               <th>Siste dag</th>
  56.               <th>Kurs</th>
  57.               <th />
  58.             </tr>
  59.           </thead>
  60.           <tbody>
  61.             {
  62.               this.state.equities
  63.                 .map((equity, i) => {
  64.                   if (i < numResults) {
  65.                     return (
  66.                       <SearchEquity
  67.                         key={i}
  68.                         showModal={() => this.showModal(equity)}
  69.                         equity={equity}
  70.                       />
  71.                     );
  72.                   } return null;
  73.                 })
  74.             }
  75.           </tbody>
  76.         </table>
  77.       </div>
  78.     );
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment