|
| 1 | +import { createRoot, createSignal, freeze, selectWhen } from 'solid-js' |
| 2 | + |
| 3 | +let idCounter = 1; |
| 4 | +const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"], |
| 5 | + colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"], |
| 6 | + nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
| 7 | + |
| 8 | +function _random (max) { return Math.round(Math.random() * 1000) % max; }; |
| 9 | + |
| 10 | +function buildData(count) { |
| 11 | + let data = new Array(count); |
| 12 | + for (let i = 0; i < count; i++) { |
| 13 | + const [label, setLabel] = createSignal(`${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`); |
| 14 | + data[i] = { |
| 15 | + id: idCounter++, |
| 16 | + label, setLabel |
| 17 | + } |
| 18 | + } |
| 19 | + return data; |
| 20 | +} |
| 21 | + |
| 22 | +const Button = ({ id, text, fn }) => |
| 23 | + <div class='col-sm-6 smallpad'> |
| 24 | + <button id={ id } class='btn btn-primary btn-block' type='button' onClick={ fn }>{ text }</button> |
| 25 | + </div> |
| 26 | + |
| 27 | +const App = () => { |
| 28 | + const [data, setData] = createSignal([]), |
| 29 | + [selected, setSelected] = createSignal(null, (a, b) => a === b); |
| 30 | + |
| 31 | + return <div class='container'> |
| 32 | + <div class='jumbotron'><div class='row'> |
| 33 | + <div class='col-md-6'><h1>SolidJS Keyed</h1></div> |
| 34 | + <div class='col-md-6'><div class='row'> |
| 35 | + <Button id='run' text='Create 1,000 rows' fn={ run } /> |
| 36 | + <Button id='runlots' text='Create 10,000 rows' fn={ runLots } /> |
| 37 | + <Button id='add' text='Append 1,000 rows' fn={ add } /> |
| 38 | + <Button id='update' text='Update every 10th row' fn={ update } /> |
| 39 | + <Button id='clear' text='Clear' fn={ clear } /> |
| 40 | + <Button id='swaprows' text='Swap Rows' fn={ swapRows } /> |
| 41 | + </div></div> |
| 42 | + </div></div> |
| 43 | + <table class='table table-hover table-striped test-data'><tbody> |
| 44 | + <$ each={ data() } afterRender={ selectWhen(selected, 'danger') }>{ row => |
| 45 | + <tr model={ row.id }> |
| 46 | + <td class='col-md-1' textContent={ row.id } /> |
| 47 | + <td class='col-md-4'><a onClick={ select }>{ row.label }</a></td> |
| 48 | + <td class='col-md-1'><a onClick={ remove }><span class='glyphicon glyphicon-remove' /></a></td> |
| 49 | + <td class='col-md-6'/> |
| 50 | + </tr> |
| 51 | + }</$> |
| 52 | + </tbody></table> |
| 53 | + <span class='preloadicon glyphicon glyphicon-remove' aria-hidden="true" /> |
| 54 | + </div> |
| 55 | + |
| 56 | + function select(e, id) { setSelected(id); } |
| 57 | + |
| 58 | + function remove(e, id) { |
| 59 | + const d = data(); |
| 60 | + d.splice(d.findIndex(d => d.id === id), 1); |
| 61 | + setData(d); |
| 62 | + } |
| 63 | + |
| 64 | + function run() { |
| 65 | + freeze(() => { |
| 66 | + setData(buildData(1000)); |
| 67 | + setSelected(null); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + function runLots() { |
| 72 | + freeze(() => { |
| 73 | + setData(buildData(10000)); |
| 74 | + setSelected(null); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + function add() { setData(data().concat(buildData(1000))); } |
| 79 | + |
| 80 | + function update() { |
| 81 | + freeze(() => { |
| 82 | + const d = data(); |
| 83 | + let index = 0; |
| 84 | + while (index < d.length) { |
| 85 | + d[index].setLabel(d[index].label() + ' !!!'); |
| 86 | + index += 10; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + function swapRows() { |
| 92 | + const d = data(); |
| 93 | + if (d.length > 998) { |
| 94 | + let tmp = d[1]; |
| 95 | + d[1] = d[998]; |
| 96 | + d[998] = tmp; |
| 97 | + setData(d); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + function clear() { |
| 102 | + freeze(() => { |
| 103 | + setData([]); |
| 104 | + setSelected(null); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +createRoot(() => document.getElementById("main").appendChild(<App />)) |
0 commit comments