|
1 |
| -import { createSignal, createSelector, batch } from 'solid-js'; |
2 |
| -import { render } from 'solid-js/web'; |
| 1 | +import { createSignal, createSelector, batch } from "solid-js"; |
| 2 | +import { render } from "solid-js/web"; |
3 | 3 |
|
4 |
| -let idCounter = 1; |
5 |
| -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"], |
6 |
| - colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"], |
7 |
| - nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
| 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"]; // prettier-ignore |
| 5 | +const colors = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; // prettier-ignore |
| 6 | +const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; // prettier-ignore |
8 | 7 |
|
9 |
| -function _random (max) { return Math.round(Math.random() * 1000) % max; }; |
| 8 | +const random = (max) => Math.round(Math.random() * 1000) % max; |
10 | 9 |
|
11 |
| -function buildData(count) { |
| 10 | +let nextId = 1; |
| 11 | + |
| 12 | +const buildData = (count) => { |
12 | 13 | let data = new Array(count);
|
13 | 14 | for (let i = 0; i < count; i++) {
|
14 |
| - const [label, setLabel] = createSignal(`${adjectives[_random(adjectives.length)]} ${colours[_random(colours.length)]} ${nouns[_random(nouns.length)]}`); |
15 |
| - data[i] = { |
16 |
| - id: idCounter++, |
17 |
| - label, setLabel |
18 |
| - } |
| 15 | + const [label, setLabel] = createSignal( |
| 16 | + `${adjectives[random(adjectives.length)]} ${colors[random(colors.length)]} ${nouns[random(nouns.length)]}` |
| 17 | + ); |
| 18 | + data[i] = { id: nextId++, label, setLabel }; |
19 | 19 | }
|
20 | 20 | return data;
|
21 |
| -} |
| 21 | +}; |
22 | 22 |
|
23 |
| -const Button = ({ id, text, fn }) => |
24 |
| - <div class='col-sm-6 smallpad'> |
25 |
| - <button id={ id } class='btn btn-primary btn-block' type='button' onClick={ fn }>{ text }</button> |
| 23 | +const Button = ([id, text, fn]) => ( |
| 24 | + <div class="col-sm-6 smallpad"> |
| 25 | + <button prop:id={id} class="btn btn-primary btn-block" type="button" onClick={fn}> |
| 26 | + {text} |
| 27 | + </button> |
26 | 28 | </div>
|
| 29 | +); |
27 | 30 |
|
28 |
| -const App = () => { |
29 |
| - const [data, setData] = createSignal([]), |
30 |
| - [selected, setSelected] = createSignal(null), |
31 |
| - run = () => setData(buildData(1000)), |
32 |
| - runLots = () => setData(buildData(10000)), |
33 |
| - add = () => setData(d => [...d, ...buildData(1000)]), |
34 |
| - update = () => batch(() => { |
35 |
| - for(let i = 0, d = data(), len = d.length; i < len; i += 10) |
36 |
| - d[i].setLabel(l => l + ' !!!'); |
37 |
| - }), |
38 |
| - swapRows = () => { |
39 |
| - const d = data().slice(); |
40 |
| - if (d.length > 998) { |
41 |
| - let tmp = d[1]; |
42 |
| - d[1] = d[998]; |
43 |
| - d[998] = tmp; |
44 |
| - setData(d); |
45 |
| - } |
46 |
| - }, |
47 |
| - clear = () => setData([]), |
48 |
| - remove = id => setData(d => { |
49 |
| - const idx = d.findIndex(d => d.id === id); |
50 |
| - return [...d.slice(0, idx), ...d.slice(idx + 1)]; |
51 |
| - }), |
52 |
| - isSelected = createSelector(selected); |
53 |
| - |
54 |
| - return <div class='container'> |
55 |
| - <div class='jumbotron'><div class='row'> |
56 |
| - <div class='col-md-6'><h1>SolidJS Keyed</h1></div> |
57 |
| - <div class='col-md-6'><div class='row'> |
58 |
| - <Button id='run' text='Create 1,000 rows' fn={ run } /> |
59 |
| - <Button id='runlots' text='Create 10,000 rows' fn={ runLots } /> |
60 |
| - <Button id='add' text='Append 1,000 rows' fn={ add } /> |
61 |
| - <Button id='update' text='Update every 10th row' fn={ update } /> |
62 |
| - <Button id='clear' text='Clear' fn={ clear } /> |
63 |
| - <Button id='swaprows' text='Swap Rows' fn={ swapRows } /> |
64 |
| - </div></div> |
65 |
| - </div></div> |
66 |
| - <table class='table table-hover table-striped test-data'><tbody> |
67 |
| - <For each={ data() }>{ row => { |
68 |
| - let rowId = row.id; |
69 |
| - return <tr class={isSelected(rowId) ? "danger": ""}> |
70 |
| - <td class='col-md-1' textContent={ rowId } /> |
71 |
| - <td class='col-md-4'><a onClick={[setSelected, rowId]} textContent={ row.label() } /></td> |
72 |
| - <td class='col-md-1'><a onClick={[remove, rowId]}><span class='glyphicon glyphicon-remove' aria-hidden="true" /></a></td> |
73 |
| - <td class='col-md-6'/> |
74 |
| - </tr> |
75 |
| - }}</For> |
76 |
| - </tbody></table> |
77 |
| - <span class='preloadicon glyphicon glyphicon-remove' aria-hidden="true" /> |
78 |
| - </div>; |
79 |
| -} |
| 31 | +render(() => { |
| 32 | + const [data, setData] = createSignal([]); |
| 33 | + const [selected, setSelected] = createSignal(null); |
| 34 | + const run = () => setData(buildData(1_000)); |
| 35 | + const runLots = () => setData(buildData(10_000)); |
| 36 | + const add = () => setData((d) => [...d, ...buildData(1_000)]); |
| 37 | + const update = () => |
| 38 | + batch(() => { |
| 39 | + for (let i = 0, d = data(), len = d.length; i < len; i += 10) d[i].setLabel((l) => l + " !!!"); |
| 40 | + }); |
| 41 | + const clear = () => setData([]); |
| 42 | + const swapRows = () => { |
| 43 | + const list = data().slice(); |
| 44 | + if (list.length > 998) { |
| 45 | + let item = list[1]; |
| 46 | + list[1] = list[998]; |
| 47 | + list[998] = item; |
| 48 | + setData(list); |
| 49 | + } |
| 50 | + }; |
| 51 | + const isSelected = createSelector(selected); |
80 | 52 |
|
81 |
| -render(App, document.getElementById("main")); |
| 53 | + return ( |
| 54 | + <div class="container"> |
| 55 | + <div class="jumbotron"> |
| 56 | + <div class="row"> |
| 57 | + <div class="col-md-6"> |
| 58 | + <h1>Solid</h1> |
| 59 | + </div> |
| 60 | + <div class="col-md-6"> |
| 61 | + <div class="row"> |
| 62 | + <Button {...["run", "Create 1,000 rows", run]} /> |
| 63 | + <Button {...["runlots", "Create 10,000 rows", runLots]} /> |
| 64 | + <Button {...["add", "Append 1,000 rows", add]} /> |
| 65 | + <Button {...["update", "Update every 10th row", update]} /> |
| 66 | + <Button {...["clear", "Clear", clear]} /> |
| 67 | + <Button {...["swaprows", "Swap Rows", swapRows]} /> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + <table class="table table-hover table-striped test-data"> |
| 73 | + <tbody> |
| 74 | + <For each={data()}> |
| 75 | + {(row) => { |
| 76 | + let rowId = row.id; |
| 77 | + return ( |
| 78 | + <tr class={isSelected(rowId) ? "danger" : ""}> |
| 79 | + <td class="col-md-1" textContent={rowId} /> |
| 80 | + <td class="col-md-4"> |
| 81 | + <a onClick={() => setSelected(rowId)} textContent={row.label()} /> |
| 82 | + </td> |
| 83 | + <td class="col-md-1"> |
| 84 | + <a onClick={() => setData((d) => d.toSpliced(d.findIndex((d) => d.id === rowId), 1))}> |
| 85 | + <span class="glyphicon glyphicon-remove" aria-hidden="true" /> |
| 86 | + </a> |
| 87 | + </td> |
| 88 | + <td class="col-md-6" /> |
| 89 | + </tr> |
| 90 | + ); // prettier-ignore |
| 91 | + }} |
| 92 | + </For> |
| 93 | + </tbody> |
| 94 | + </table> |
| 95 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true" /> |
| 96 | + </div> |
| 97 | + ); |
| 98 | +}, document.getElementById("main")); |
0 commit comments