|
1 | | -import { memo } from "react"; |
2 | | -import { render } from "react-dom"; |
3 | | -import { createState , observer, compute } from "react-tagged-state"; |
| 1 | +import {memo} from "react"; |
| 2 | +import {render} from "react-dom"; |
| 3 | +import {batch, mutated, useTagged} from "react-tagged-state"; |
4 | 4 |
|
5 | 5 | const A = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", |
6 | | - "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", |
7 | | - "cheap", "expensive", "fancy"]; |
| 6 | + "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", |
| 7 | + "cheap", "expensive", "fancy"]; |
8 | 8 | const C = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
9 | 9 | const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", |
10 | | - "keyboard"]; |
| 10 | + "keyboard"]; |
11 | 11 |
|
12 | 12 | const random = (max) => Math.round(Math.random() * 1000) % max; |
13 | 13 |
|
14 | 14 | let nextId = 1; |
15 | 15 |
|
16 | | -const buildData = (count) => { |
17 | | - const res = []; |
18 | | - let index = 0; |
19 | | - |
20 | | - for (; index < count; ++index) { |
21 | | - res.push({ |
22 | | - id: nextId++, |
23 | | - label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}` |
24 | | - }) |
25 | | - } |
26 | | - |
27 | | - return res; |
| 16 | +const buildData = (res, count) => { |
| 17 | + for (let index = 0; index < count; index++) { |
| 18 | + res.push({ |
| 19 | + id: nextId++, |
| 20 | + label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}` |
| 21 | + }) |
| 22 | + } |
28 | 23 | } |
29 | 24 |
|
30 | | -const dataState = createState([]); |
31 | | - |
32 | | -const selectedState = createState(0); |
33 | | - |
34 | | -const update = (data) => { |
35 | | - const newData = data.slice(); |
36 | | - let index = 0; |
37 | | - let length = newData.length; |
38 | | - let temp; |
39 | | - |
40 | | - for (; index < length; index += 10) { |
41 | | - temp = newData[index]; |
42 | | - |
43 | | - newData[index] = { id: temp.id, label: temp.label + " !!!" }; |
44 | | - } |
45 | | - |
46 | | - return newData; |
47 | | -}; |
48 | | - |
49 | | -const remove = (item) => (data) => { |
50 | | - const newData = data.slice(); |
51 | | - |
52 | | - newData.splice(data.indexOf(item), 1); |
53 | | - |
54 | | - return newData; |
| 25 | +const data = []; |
| 26 | + |
| 27 | +let selected = 0; |
| 28 | + |
| 29 | +const Row = memo(({item}) => { |
| 30 | + useTagged(item); |
| 31 | + |
| 32 | + return ( |
| 33 | + <tr className={selected === item.id ? "danger" : ""}> |
| 34 | + <td className="col-md-1">{item.id}</td> |
| 35 | + <td className="col-md-4"> |
| 36 | + <a onClick={() => batch(() => { |
| 37 | + if (selected) { |
| 38 | + mutated(data.find(({id}) => id === selected)); |
| 39 | + } |
| 40 | + |
| 41 | + selected = item.id; |
| 42 | + mutated(item); |
| 43 | + })}>{item.label}</a> |
| 44 | + </td> |
| 45 | + <td className="col-md-1"> |
| 46 | + <a onClick={() => { |
| 47 | + data.splice(data.indexOf(item), 1); |
| 48 | + mutated(data); |
| 49 | + }}><span className="glyphicon glyphicon-remove" aria-hidden="true"/></a> |
| 50 | + </td> |
| 51 | + <td className="col-md-6"/> |
| 52 | + </tr> |
| 53 | + ) |
| 54 | +}); |
| 55 | + |
| 56 | +const RowList = () => { |
| 57 | + useTagged(data); |
| 58 | + |
| 59 | + return data.map((item) => <Row key={item.id} item={item}/>) |
55 | 60 | }; |
56 | 61 |
|
57 | | -const swapRows = (data) => { |
58 | | - const newData = data.slice(); |
59 | | - const tmp = newData[1]; |
60 | | - |
61 | | - newData[1] = newData[998]; |
62 | | - newData[998] = tmp; |
63 | | - |
64 | | - return newData; |
65 | | -}; |
66 | | - |
67 | | -const Row = memo(observer(({ item }) => ( |
68 | | - <tr className={compute(() => selectedState() === item.id) ? "danger" : ""}> |
69 | | - <td className="col-md-1">{item.id}</td> |
70 | | - <td className="col-md-4"> |
71 | | - <a onClick={() => selectedState(item.id)}>{item.label}</a> |
72 | | - </td> |
73 | | - <td className="col-md-1"> |
74 | | - <a onClick={() => dataState(remove(item))}><span className="glyphicon glyphicon-remove" aria-hidden="true"/></a> |
75 | | - </td> |
76 | | - <td className="col-md-6"/> |
77 | | - </tr> |
78 | | -))); |
79 | | - |
80 | | -const RowList = observer(() => dataState().map((item) => <Row key={item.id} item={item} />)); |
81 | | - |
82 | | -const Button = ({ id, title, cb }) => ( |
83 | | - <div className="col-sm-6 smallpad"> |
84 | | - <button type="button" className="btn btn-primary btn-block" id={id} onClick={cb}>{title}</button> |
85 | | - </div> |
| 62 | +const Button = ({id, title, cb}) => ( |
| 63 | + <div className="col-sm-6 smallpad"> |
| 64 | + <button type="button" className="btn btn-primary btn-block" id={id} onClick={cb}>{title}</button> |
| 65 | + </div> |
86 | 66 | ); |
87 | 67 |
|
88 | 68 | const Main = () => ( |
89 | | - <div className="container"> |
90 | | - <div className="jumbotron"> |
91 | | - <div className="row"> |
92 | | - <div className="col-md-6"><h1>React Tagged State </h1></div> |
93 | | - <div className="col-md-6"><div className="row"> |
94 | | - <Button id="run" title="Create 1,000 rows" cb={() => dataState(buildData(1000))} /> |
95 | | - <Button id="runlots" title="Create 10,000 rows" cb={() => dataState(buildData(10000))} /> |
96 | | - <Button id="add" title="Append 1,000 rows" cb={() => dataState((data) => data.concat(buildData(1000)))} /> |
97 | | - <Button id="update" title="Update every 10th row" cb={() => dataState(update)} /> |
98 | | - <Button id="clear" title="Clear" cb={() => dataState([])} /> |
99 | | - <Button id="swaprows" title="Swap Rows" cb={() => dataState(swapRows)} /> |
100 | | - </div></div> |
101 | | - </div> |
| 69 | + <div className="container"> |
| 70 | + <div className="jumbotron"> |
| 71 | + <div className="row"> |
| 72 | + <div className="col-md-6"><h1>React Tagged State </h1></div> |
| 73 | + <div className="col-md-6"> |
| 74 | + <div className="row"> |
| 75 | + <Button id="run" title="Create 1,000 rows" cb={() => { |
| 76 | + data.length = 0; |
| 77 | + selected = 0; |
| 78 | + buildData(data, 1000); |
| 79 | + mutated(data); |
| 80 | + }}/> |
| 81 | + <Button id="runlots" title="Create 10,000 rows" cb={() => { |
| 82 | + data.length = 0; |
| 83 | + selected = 0; |
| 84 | + buildData(data, 10000); |
| 85 | + mutated(data); |
| 86 | + }}/> |
| 87 | + <Button id="add" title="Append 1,000 rows" cb={() => { |
| 88 | + buildData(data, 1000); |
| 89 | + mutated(data); |
| 90 | + }}/> |
| 91 | + <Button id="update" title="Update every 10th row" cb={() => batch(() => { |
| 92 | + for (let index = 0; index < data.length; index += 10) { |
| 93 | + mutated(data[index]).label += " !!!"; |
| 94 | + } |
| 95 | + })}/> |
| 96 | + <Button id="clear" title="Clear" cb={() => { |
| 97 | + data.length = 0; |
| 98 | + selected = 0; |
| 99 | + mutated(data); |
| 100 | + }}/> |
| 101 | + <Button id="swaprows" title="Swap Rows" cb={() => { |
| 102 | + [data[1], data[998]] = [data[998], data[1]]; |
| 103 | + mutated(data); |
| 104 | + }}/> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + <table className="table table-hover table-striped test-data"> |
| 110 | + <tbody> |
| 111 | + <RowList/> |
| 112 | + </tbody> |
| 113 | + </table> |
| 114 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"/> |
102 | 115 | </div> |
103 | | - <table className="table table-hover table-striped test-data"> |
104 | | - <tbody> |
105 | | - <RowList /> |
106 | | - </tbody> |
107 | | - </table> |
108 | | - <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"/> |
109 | | - </div> |
110 | 116 | ); |
111 | 117 |
|
112 | 118 | render( |
113 | | - <Main />, |
114 | | - document.getElementById("main") |
| 119 | + <Main/>, |
| 120 | + document.getElementById("main") |
115 | 121 | ); |
0 commit comments