|
| 1 | +import React, { Component } from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | +import easyState from 'react-easy-state' |
| 4 | +import Row from './Row' |
| 5 | +import randomSentence from './randomSentence' |
| 6 | +import {startMeasure, stopMeasure} from './logPerf' |
| 7 | + |
| 8 | +let idCounter = 1 |
| 9 | + |
| 10 | +@easyState |
| 11 | +class Main extends Component { |
| 12 | + constructor (props) { |
| 13 | + super(props) |
| 14 | + this.state = { rows: [] } |
| 15 | + } |
| 16 | + |
| 17 | + componentDidUpdate() { |
| 18 | + stopMeasure() |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount() { |
| 22 | + stopMeasure() |
| 23 | + } |
| 24 | + |
| 25 | + buildRows (numOfRows) { |
| 26 | + const { state } = this |
| 27 | + for (let i = 0; i < numOfRows; i++) { |
| 28 | + state.rows.push({ id: idCounter++, label: randomSentence() }) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + run () { |
| 33 | + startMeasure('run') |
| 34 | + const { state } = this |
| 35 | + state.rows = [] |
| 36 | + state.selected = undefined |
| 37 | + this.buildRows(1000) |
| 38 | + } |
| 39 | + |
| 40 | + add () { |
| 41 | + startMeasure('add') |
| 42 | + this.buildRows(1000) |
| 43 | + } |
| 44 | + |
| 45 | + update () { |
| 46 | + startMeasure('update') |
| 47 | + const { rows } = this.state |
| 48 | + for (let i = 0; i < rows.length; i += 10) { |
| 49 | + rows[i].label += ' !!!' |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + select (row) { |
| 54 | + startMeasure('select') |
| 55 | + const { state } = this |
| 56 | + state.selected = row |
| 57 | + } |
| 58 | + |
| 59 | + delete (row) { |
| 60 | + startMeasure('delete') |
| 61 | + const { rows } = this.state |
| 62 | + rows.splice(rows.indexOf(row), 1) |
| 63 | + } |
| 64 | + |
| 65 | + runLots () { |
| 66 | + startMeasure('runLots') |
| 67 | + const { state } = this |
| 68 | + state.rows = [] |
| 69 | + state.selected = undefined |
| 70 | + this.buildRows(10000) |
| 71 | + } |
| 72 | + |
| 73 | + clear() { |
| 74 | + startMeasure('clear') |
| 75 | + const { state } = this |
| 76 | + state.rows = [] |
| 77 | + state.selected = undefined |
| 78 | + } |
| 79 | + |
| 80 | + swapRows() { |
| 81 | + startMeasure('swapRows') |
| 82 | + const { rows } = this.state |
| 83 | + if (rows.length > 10) { |
| 84 | + const temp = rows[4] |
| 85 | + rows[4] = rows[9] |
| 86 | + rows[9] = temp |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + render() { |
| 91 | + const { rows, selected } = this.state |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="container"> |
| 95 | + <div className="jumbotron"> |
| 96 | + <div className="row"> |
| 97 | + <div className="col-md-6"> |
| 98 | + <h1>React v15.5.4 + Easy State 1.0.3</h1> |
| 99 | + </div> |
| 100 | + <div className="col-md-6"> |
| 101 | + <div className="row"> |
| 102 | + <div className="col-sm-6 smallpad"> |
| 103 | + <button type="button" className="btn btn-primary btn-block" id="run" onClick={this.run}>Create 1,000 rows</button> |
| 104 | + </div> |
| 105 | + <div className="col-sm-6 smallpad"> |
| 106 | + <button type="button" className="btn btn-primary btn-block" id="runlots" onClick={this.runLots}>Create 10,000 rows</button> |
| 107 | + </div> |
| 108 | + <div className="col-sm-6 smallpad"> |
| 109 | + <button type="button" className="btn btn-primary btn-block" id="add" onClick={this.add}>Append 1,000 rows</button> |
| 110 | + </div> |
| 111 | + <div className="col-sm-6 smallpad"> |
| 112 | + <button type="button" className="btn btn-primary btn-block" id="update" onClick={this.update}>Update every 10th row</button> |
| 113 | + </div> |
| 114 | + <div className="col-sm-6 smallpad"> |
| 115 | + <button type="button" className="btn btn-primary btn-block" id="clear" onClick={this.clear}>Clear</button> |
| 116 | + </div> |
| 117 | + <div className="col-sm-6 smallpad"> |
| 118 | + <button type="button" className="btn btn-primary btn-block" id="swaprows" onClick={this.swapRows}>Swap Rows</button> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <table className="table table-hover table-striped test-data"> |
| 125 | + <tbody> |
| 126 | + {rows.map(row => |
| 127 | + <Row onClick={this.select} onDelete={this.delete} |
| 128 | + key={row.id} row={row} styleClass={row === selected ? 'danger' : ''}></Row>)} |
| 129 | + </tbody> |
| 130 | + </table> |
| 131 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 132 | + </div> |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +ReactDOM.render(<Main/>, document.getElementById('main')) |
0 commit comments