|
| 1 | +import { render, signal, batch, For } from "pota"; |
| 2 | +import { useSelector } from "pota/hooks"; |
| 3 | + |
| 4 | +let idCounter = 1; |
| 5 | +const adjectives = [ |
| 6 | + "pretty", |
| 7 | + "large", |
| 8 | + "big", |
| 9 | + "small", |
| 10 | + "tall", |
| 11 | + "short", |
| 12 | + "long", |
| 13 | + "handsome", |
| 14 | + "plain", |
| 15 | + "quaint", |
| 16 | + "clean", |
| 17 | + "elegant", |
| 18 | + "easy", |
| 19 | + "angry", |
| 20 | + "crazy", |
| 21 | + "helpful", |
| 22 | + "mushy", |
| 23 | + "odd", |
| 24 | + "unsightly", |
| 25 | + "adorable", |
| 26 | + "important", |
| 27 | + "inexpensive", |
| 28 | + "cheap", |
| 29 | + "expensive", |
| 30 | + "fancy", |
| 31 | + ], |
| 32 | + colours = [ |
| 33 | + "red", |
| 34 | + "yellow", |
| 35 | + "blue", |
| 36 | + "green", |
| 37 | + "pink", |
| 38 | + "brown", |
| 39 | + "purple", |
| 40 | + "brown", |
| 41 | + "white", |
| 42 | + "black", |
| 43 | + "orange", |
| 44 | + ], |
| 45 | + nouns = [ |
| 46 | + "table", |
| 47 | + "chair", |
| 48 | + "house", |
| 49 | + "bbq", |
| 50 | + "desk", |
| 51 | + "car", |
| 52 | + "pony", |
| 53 | + "cookie", |
| 54 | + "sandwich", |
| 55 | + "burger", |
| 56 | + "pizza", |
| 57 | + "mouse", |
| 58 | + "keyboard", |
| 59 | + ]; |
| 60 | + |
| 61 | +function _random(max) { |
| 62 | + return Math.round(Math.random() * 1000) % max; |
| 63 | +} |
| 64 | + |
| 65 | +function buildData(count) { |
| 66 | + let data = new Array(count); |
| 67 | + for (let i = 0; i < count; i++) { |
| 68 | + const [label, setLabel] = signal( |
| 69 | + `${adjectives[_random(adjectives.length)]} ${ |
| 70 | + colours[_random(colours.length)] |
| 71 | + } ${nouns[_random(nouns.length)]}`, |
| 72 | + ); |
| 73 | + data[i] = { |
| 74 | + id: idCounter++, |
| 75 | + label, |
| 76 | + setLabel, |
| 77 | + }; |
| 78 | + } |
| 79 | + return data; |
| 80 | +} |
| 81 | + |
| 82 | +const Button = ({ id, text, fn }) => ( |
| 83 | + <div class="col-sm-6 smallpad"> |
| 84 | + <button |
| 85 | + id={id} |
| 86 | + class="btn btn-primary btn-block" |
| 87 | + type="button" |
| 88 | + onClick={fn} |
| 89 | + > |
| 90 | + {text} |
| 91 | + </button> |
| 92 | + </div> |
| 93 | +); |
| 94 | + |
| 95 | +const App = () => { |
| 96 | + const [data, setData] = signal([]), |
| 97 | + [selected, setSelected] = signal(null), |
| 98 | + run = () => setData(buildData(1000)), |
| 99 | + runLots = () => { |
| 100 | + setData(buildData(10000)); |
| 101 | + }, |
| 102 | + add = () => setData((d) => [...d, ...buildData(1000)]), |
| 103 | + update = () => |
| 104 | + batch(() => { |
| 105 | + for (let i = 0, d = data(), len = d.length; i < len; i += 10) |
| 106 | + d[i].setLabel((l) => l + " !!!"); |
| 107 | + }), |
| 108 | + swapRows = () => { |
| 109 | + const d = data().slice(); |
| 110 | + if (d.length > 998) { |
| 111 | + let tmp = d[1]; |
| 112 | + d[1] = d[998]; |
| 113 | + d[998] = tmp; |
| 114 | + setData(d); |
| 115 | + } |
| 116 | + }, |
| 117 | + clear = () => setData([]), |
| 118 | + remove = (id) => |
| 119 | + setData((d) => { |
| 120 | + const idx = d.findIndex((datum) => datum.id === id); |
| 121 | + d.splice(idx, 1); |
| 122 | + return [...d]; |
| 123 | + }), |
| 124 | + isSelected = useSelector(selected); |
| 125 | + |
| 126 | + return ( |
| 127 | + <div class="container"> |
| 128 | + <div class="jumbotron"> |
| 129 | + <div class="row"> |
| 130 | + <div class="col-md-6"> |
| 131 | + <h1>pota Keyed</h1> |
| 132 | + </div> |
| 133 | + <div class="col-md-6"> |
| 134 | + <div class="row"> |
| 135 | + <Button id="run" text="Create 1,000 rows" fn={run} /> |
| 136 | + <Button id="runlots" text="Create 10,000 rows" fn={runLots} /> |
| 137 | + <Button id="add" text="Append 1,000 rows" fn={add} /> |
| 138 | + <Button id="update" text="Update every 10th row" fn={update} /> |
| 139 | + <Button id="clear" text="Clear" fn={clear} /> |
| 140 | + <Button id="swaprows" text="Swap Rows" fn={swapRows} /> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + <table |
| 146 | + class="table table-hover table-striped test-data" |
| 147 | + onClick={(e) => { |
| 148 | + const element = e.target; |
| 149 | + if (element.setSelected !== undefined) { |
| 150 | + setSelected(element.setSelected); |
| 151 | + } else if (element.removeRow !== undefined) { |
| 152 | + remove(element.removeRow); |
| 153 | + } |
| 154 | + }} |
| 155 | + > |
| 156 | + <tbody> |
| 157 | + <For each={data}> |
| 158 | + {(row) => { |
| 159 | + const { id, label } = row; |
| 160 | + |
| 161 | + return ( |
| 162 | + <tr class:danger={isSelected(id)}> |
| 163 | + <td class="col-md-1" textContent={id} /> |
| 164 | + <td class="col-md-4"> |
| 165 | + <a textContent={label} prop:setSelected={id} /> |
| 166 | + </td> |
| 167 | + <td class="col-md-1"> |
| 168 | + <a> |
| 169 | + <span |
| 170 | + class="glyphicon glyphicon-remove" |
| 171 | + aria-hidden="true" |
| 172 | + prop:removeRow={id} |
| 173 | + /> |
| 174 | + </a> |
| 175 | + </td> |
| 176 | + <td class="col-md-6" /> |
| 177 | + </tr> |
| 178 | + ); |
| 179 | + }} |
| 180 | + </For> |
| 181 | + </tbody> |
| 182 | + </table> |
| 183 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true" /> |
| 184 | + </div> |
| 185 | + ); |
| 186 | +}; |
| 187 | + |
| 188 | +render(App, document.getElementById("main")); |
0 commit comments