|
| 1 | +/** |
| 2 | + * @jsx Rax.createElement |
| 3 | + */ |
| 4 | +var Rax = require('rax'); |
| 5 | + |
| 6 | +function random(max) { |
| 7 | + return Math.round(Math.random() * 1000) % max; |
| 8 | +} |
| 9 | + |
| 10 | +const A = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", |
| 11 | + "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", |
| 12 | + "cheap", "expensive", "fancy"]; |
| 13 | +const C = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
| 14 | +const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", |
| 15 | + "keyboard"]; |
| 16 | + |
| 17 | +let nextId = 1; |
| 18 | + |
| 19 | +function buildData(count) { |
| 20 | + const data = new Array(count); |
| 21 | + for (let i = 0; i < count; i++) { |
| 22 | + data[i] = { |
| 23 | + id: nextId++, |
| 24 | + label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}`, |
| 25 | + }; |
| 26 | + } |
| 27 | + return data; |
| 28 | +} |
| 29 | + |
| 30 | +const GlyphIcon = <span className="glyphicon glyphicon-remove" aria-hidden="true"></span>; |
| 31 | + |
| 32 | +class Row extends Rax.Component { |
| 33 | + onSelect = () => { |
| 34 | + this.props.select(this.props.item); |
| 35 | + } |
| 36 | + |
| 37 | + onRemove = () => { |
| 38 | + this.props.remove(this.props.item); |
| 39 | + } |
| 40 | + |
| 41 | + shouldComponentUpdate(nextProps) { |
| 42 | + return nextProps.item !== this.props.item || nextProps.selected !== this.props.selected; |
| 43 | + } |
| 44 | + |
| 45 | + render() { |
| 46 | + let { selected, item } = this.props; |
| 47 | + return (<tr className={selected ? "danger" : ""}> |
| 48 | + <td className="col-md-1">{item.id}</td> |
| 49 | + <td className="col-md-4"><a onClick={this.onSelect}>{item.label}</a></td> |
| 50 | + <td className="col-md-1"><a onClick={this.onRemove}>{GlyphIcon}</a></td> |
| 51 | + <td className="col-md-6"></td> |
| 52 | + </tr>); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function Button({ id, cb, title }) { |
| 57 | + return ( |
| 58 | + <div className="col-sm-6 smallpad"> |
| 59 | + <button type="button" className="btn btn-primary btn-block" id={id} onClick={cb}>{title}</button> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +class Jumbotron extends Rax.Component { |
| 65 | + shouldComponentUpdate() { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + render() { |
| 70 | + const { run, runLots, add, update, clear, swapRows } = this.props; |
| 71 | + return ( |
| 72 | + <div className="jumbotron"> |
| 73 | + <div className="row"> |
| 74 | + <div className="col-md-6"> |
| 75 | + <h1>Rax keyed</h1> |
| 76 | + </div> |
| 77 | + <div className="col-md-6"> |
| 78 | + <div className="row"> |
| 79 | + <Button id="run" title="Create 1,000 rows" cb={run} /> |
| 80 | + <Button id="runlots" title="Create 10,000 rows" cb={runLots} /> |
| 81 | + <Button id="add" title="Append 1,000 rows" cb={add} /> |
| 82 | + <Button id="update" title="Update every 10th row" cb={update} /> |
| 83 | + <Button id="clear" title="Clear" cb={clear} /> |
| 84 | + <Button id="swaprows" title="Swap Rows" cb={swapRows} /> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class Main extends Rax.Component { |
| 94 | + state = { |
| 95 | + data: [], |
| 96 | + selected: 0, |
| 97 | + }; |
| 98 | + |
| 99 | + run = () => { |
| 100 | + this.setState({ data: buildData(1000), selected: 0 }); |
| 101 | + } |
| 102 | + |
| 103 | + runLots = () => { |
| 104 | + this.setState({ data: buildData(10000), selected: 0 }); |
| 105 | + } |
| 106 | + |
| 107 | + add = () => { |
| 108 | + this.setState({ data: this.state.data.concat(buildData(1000)), selected: this.state.selected }); |
| 109 | + } |
| 110 | + |
| 111 | + update = () => { |
| 112 | + const data = this.state.data; |
| 113 | + for (let i = 0; i < data.length; i += 10) { |
| 114 | + const item = data[i]; |
| 115 | + data[i] = { id: item.id, label: item.label + ' !!!' }; |
| 116 | + } |
| 117 | + this.forceUpdate(); |
| 118 | + } |
| 119 | + |
| 120 | + select = (item) => { |
| 121 | + this.setState({ selected: item.id }); |
| 122 | + } |
| 123 | + |
| 124 | + remove = (item) => { |
| 125 | + const data = this.state.data; |
| 126 | + data.splice(data.indexOf(item), 1); |
| 127 | + this.forceUpdate(); |
| 128 | + } |
| 129 | + |
| 130 | + clear = () => { |
| 131 | + this.setState({ data: [], selected: 0 }); |
| 132 | + } |
| 133 | + |
| 134 | + swapRows = () => { |
| 135 | + const data = this.state.data; |
| 136 | + if (data.length > 998) { |
| 137 | + let temp = data[1]; |
| 138 | + data[1] = data[998]; |
| 139 | + data[998] = temp; |
| 140 | + } |
| 141 | + this.forceUpdate(); |
| 142 | + } |
| 143 | + |
| 144 | + render() { |
| 145 | + return (<div className="container"> |
| 146 | + <Jumbotron run={this.run} runLots={this.runLots} add={this.add} update={this.update} clear={this.clear} swapRows={this.swapRows} /> |
| 147 | + <table className="table table-hover table-striped test-data"><tbody> |
| 148 | + {this.state.data.map((item) => ( |
| 149 | + <Row key={item.id} item={item} selected={this.state.selected === item.id} select={this.select} remove={this.remove}></Row> |
| 150 | + ))} |
| 151 | + </tbody></table> |
| 152 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 153 | + </div>); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +Rax.render( |
| 158 | + <Main />, |
| 159 | + document.getElementById('main'), |
| 160 | +); |
0 commit comments