1
- 'use strict' ;
2
-
3
1
import { Store } from './store.es6'
4
2
import { linkEvent , Component , render } from 'inferno'
5
3
6
- function Row ( { d , id, styleClass , deleteFunc, selectFunc } ) {
4
+ function Row ( { label , id, selected , deleteFunc, selectFunc } ) {
7
5
/*
8
6
* Only <td className="col-md-1"> and <a onClick={linkEvent(id, selectFunc)}/>, nodes needs children shape flags
9
7
* Because they have dynamic children. We can pre-define children type by using ChildFlags
10
8
*/
11
9
return (
12
- < tr className = { styleClass } >
13
- < td className = "col-md-1" $HasTextChildren > { id } </ td >
14
- < td className = "col-md-4" >
15
- < a onClick = { linkEvent ( id , selectFunc ) } $HasTextChildren > { d . label } </ a >
16
- </ td >
17
- < td className = "col-md-1" >
18
- < a onClick = { linkEvent ( id , deleteFunc ) } >
19
- < span className = "glyphicon glyphicon-remove" aria-hidden = "true" />
20
- </ a >
21
- </ td >
22
- < td className = "col-md-6" />
23
- </ tr >
10
+ < tr className = { selected ? 'danger' : null } >
11
+ < td className = "col-md-1" $HasTextChildren > { id } </ td >
12
+ < td className = "col-md-4" >
13
+ < a onClick = { linkEvent ( id , selectFunc ) } $HasTextChildren > { label } </ a >
14
+ </ td >
15
+ < td className = "col-md-1" >
16
+ < a onClick = { linkEvent ( id , deleteFunc ) } >
17
+ < span className = "glyphicon glyphicon-remove" aria-hidden = "true" />
18
+ </ a >
19
+ </ td >
20
+ < td className = "col-md-6" />
21
+ </ tr >
24
22
)
25
23
}
26
24
27
25
// Inferno functional components has hooks, when they are static they can be defined in defaultHooks property
28
26
Row . defaultHooks = {
29
27
onComponentShouldUpdate ( lastProps , nextProps ) {
30
- return nextProps . d !== lastProps . d || nextProps . styleClass !== lastProps . styleClass ;
28
+ return nextProps . label !== lastProps . label || nextProps . selected !== lastProps . selected ;
31
29
}
32
30
} ;
33
31
@@ -41,28 +39,74 @@ function createRows(store, deleteFunc, selectFunc) {
41
39
const id = d . id ;
42
40
43
41
rows . push (
44
- < Row
45
- styleClass = { id === selected ? 'danger' : null }
46
- key = { id }
47
- d = { d }
48
- id = { id }
49
- deleteFunc = { deleteFunc }
50
- selectFunc = { selectFunc }
51
- />
42
+ < Row
43
+ selected = { id === selected }
44
+ key = { id }
45
+ label = { d . label }
46
+ id = { id }
47
+ deleteFunc = { deleteFunc }
48
+ selectFunc = { selectFunc }
49
+ />
52
50
) ;
53
51
}
54
52
55
- /*
56
- * We can optimize rendering rows by pre-defining children types.
57
- * In this case all children are keyed: so we add flag $HasKeyedChildren and $NoNormalize
58
- * when specific shape is used we need to make sure there are no holes in the array and are keys are unique
59
- */
60
- return < tbody $HasKeyedChildren > { rows } </ tbody > ;
53
+ return rows ;
61
54
}
62
55
56
+ function Header ( { run, runLots, add, update, clear, swapRows} ) {
57
+ return (
58
+ < div className = "jumbotron" >
59
+ < div className = "row" >
60
+ < div className = "col-md-6" >
61
+ < h1 > Inferno - keyed</ h1 >
62
+ </ div >
63
+ < div className = "col-md-6" >
64
+ < div className = "row" >
65
+ < div className = "col-sm-6 smallpad" >
66
+ < button type = "button" className = "btn btn-primary btn-block" id = "run" onClick = { run } > Create 1,000
67
+ rows
68
+ </ button >
69
+ </ div >
70
+ < div className = "col-sm-6 smallpad" >
71
+ < button type = "button" className = "btn btn-primary btn-block" id = "runlots" onClick = { runLots } > Create
72
+ 10,000 rows
73
+ </ button >
74
+ </ div >
75
+ < div className = "col-sm-6 smallpad" >
76
+ < button type = "button" className = "btn btn-primary btn-block" id = "add" onClick = { add } > Append 1,000
77
+ rows
78
+ </ button >
79
+ </ div >
80
+ < div className = "col-sm-6 smallpad" >
81
+ < button type = "button" className = "btn btn-primary btn-block" id = "update" onClick = { update } > Update
82
+ every 10th row
83
+ </ button >
84
+ </ div >
85
+ < div className = "col-sm-6 smallpad" >
86
+ < button type = "button" className = "btn btn-primary btn-block" id = "clear" onClick = { clear } > Clear
87
+ </ button >
88
+ </ div >
89
+ < div className = "col-sm-6 smallpad" >
90
+ < button type = "button" className = "btn btn-primary btn-block" id = "swaprows" onClick = { swapRows } > Swap
91
+ Rows
92
+ </ button >
93
+ </ div >
94
+ </ div >
95
+ </ div >
96
+ </ div >
97
+ </ div >
98
+ ) ;
99
+ }
100
+
101
+ Header . defaultHooks = {
102
+ onComponentShouldUpdate ( ) {
103
+ return false ;
104
+ }
105
+ } ;
106
+
63
107
export class Controller extends Component {
64
- constructor ( props ) {
65
- super ( props ) ;
108
+ constructor ( props , context ) {
109
+ super ( props , context ) ;
66
110
this . state = { store : new Store ( ) } ;
67
111
this . select = this . select . bind ( this ) ;
68
112
this . delete = this . delete . bind ( this ) ;
@@ -128,52 +172,24 @@ export class Controller extends Component {
128
172
* Only <table> needs $HasVNodeChildren flag everything else is static
129
173
* tables children is tbody so another vNode, no other flags needed
130
174
*/
131
- return ( < div className = "container" >
132
- < div className = "jumbotron" >
133
- < div className = "row" >
134
- < div className = "col-md-6" >
135
- < h1 > Inferno - keyed</ h1 >
136
- </ div >
137
- < div className = "col-md-6" >
138
- < div className = "row" >
139
- < div className = "col-sm-6 smallpad" >
140
- < button type = "button" className = "btn btn-primary btn-block" id = "run" onClick = { this . run } > Create 1,000
141
- rows
142
- </ button >
143
- </ div >
144
- < div className = "col-sm-6 smallpad" >
145
- < button type = "button" className = "btn btn-primary btn-block" id = "runlots" onClick = { this . runLots } > Create
146
- 10,000 rows
147
- </ button >
148
- </ div >
149
- < div className = "col-sm-6 smallpad" >
150
- < button type = "button" className = "btn btn-primary btn-block" id = "add" onClick = { this . add } > Append 1,000
151
- rows
152
- </ button >
153
- </ div >
154
- < div className = "col-sm-6 smallpad" >
155
- < button type = "button" className = "btn btn-primary btn-block" id = "update" onClick = { this . update } > Update
156
- every 10th row
157
- </ button >
158
- </ div >
159
- < div className = "col-sm-6 smallpad" >
160
- < button type = "button" className = "btn btn-primary btn-block" id = "clear" onClick = { this . clear } > Clear
161
- </ button >
162
- </ div >
163
- < div className = "col-sm-6 smallpad" >
164
- < button type = "button" className = "btn btn-primary btn-block" id = "swaprows" onClick = { this . swapRows } > Swap
165
- Rows
166
- </ button >
167
- </ div >
168
- </ div >
169
- </ div >
175
+ return (
176
+ < div className = "container" >
177
+ < Header
178
+ run = { this . run }
179
+ runLots = { this . runLots }
180
+ add = { this . add }
181
+ update = { this . update }
182
+ clear = { this . clear }
183
+ swapRows = { this . swapRows }
184
+ />
185
+ < table className = "table table-hover table-striped test-data" $HasVNodeChildren >
186
+ < tbody $HasKeyedChildren >
187
+ { createRows ( this . state . store , this . delete , this . select ) }
188
+ </ tbody >
189
+ </ table >
190
+ < span className = "preloadicon glyphicon glyphicon-remove" aria-hidden = "true" />
170
191
</ div >
171
- </ div >
172
- < table className = "table table-hover table-striped test-data" $HasVNodeChildren >
173
- { createRows ( this . state . store , this . delete , this . select ) }
174
- </ table >
175
- < span className = "preloadicon glyphicon glyphicon-remove" aria-hidden = "true" />
176
- </ div > ) ;
192
+ ) ;
177
193
}
178
194
}
179
195
0 commit comments