all files / demo/ App.tsx

78.38% Statements 58/74
67.74% Branches 21/31
70.83% Functions 17/24
82.09% Lines 55/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                    
import React from 'react';
import './App.css';
import { GridLayout } from '../react-infinitegrid';
 
class Item extends React.Component<{
  groupKey: any,
  num: number,
  onClick: any,
}> {
  render() {
    const no = this.props.num;
    const text = `egjs ${no}`;
 
    return (<div className="item" onClick={this.props.onClick}>
      <div className="thumbnail">
        <img src={`https://naver.github.io/egjs-infinitegrid/assets/image/${no % 59}.jpg`} />
      </div>
      <div className="info">
        {text}
      </div>
    </div>);
  }
}
 
class App extends React.Component<{}, {
  list: any[],
}> {
  public start = 0;
  constructor(prop) {
    super(prop);
    this.state = {
      list: [
        // ...this.loadItems("1", 10),
        // ...this.loadItems("2", 10),
      ],
    };
  }
  loadItems(groupKey, num) {
    const items = [];
    const start = this.start || 0;
 
    for (let i = 0; i < num; ++i) {
      items.push(<Item groupKey={groupKey} num={start + i} key={start + i}
        onClIick={(itemKey => (e => this.remove(itemKey)))(start + i)} />);
    }
    this.start += num;
    return items;
  }
  remove(itemKey) {
    const list = this.state.list.slice();
    const index = list.map(component => parseFloat(component.key)).indexOf(itemKey);

    list.splice(index, 1);
    this.setState({ list });
  }
  onAppend = ({ currentTarget, groupKey, startLoading }) => {
    if (currentTarget.isLoading()) {
      return;
    }
    startLoading();
    const list = this.state.list;
    const items = this.loadItems(parseFloat(groupKey || 0) + 1, 5);
 
    this.setState({ list: list.concat(items) });
  }
  onLayoutComplete = ({ isAppend, isLayout, endLoading, fromCache }) => {
    !isLayout && endLoading();
  }
  onImageError = ({ totalIndex }) => {
    this.state.list.splice(totalIndex, 1);
    this.setState({ list: this.state.list });
  }
  render() {
    const { list } = this.state;
 
    return (
      <GridLayout
        ref={e => {
          (window as any).a = e;
        }}
        options = {{
          transitionDuration: 0,
          isConstantSize: false,
        }}
        layoutOptions={{
          margin: 10,
          align: "center",
        }}
        onAppend={this.onAppend}
        onLayoutComplete={this.onLayoutComplete}
        onImageError={this.onImageError}
        useFirstRender={true}
        loading={<div className="loading" style={{textAlign: "center", height: "100px", backgroundColor: "#f55", position: "absolute"}}>LOADING</div>}
        // status={status}
      >
        {list}
      </GridLayout>
    );
  }
}
 
 
export default App;