1
+ import { Component , createElement , render } from 'nervjs'
1
2
2
- class TableCell extends Nerv . PureComponent {
3
+ class TableCell extends Component {
3
4
constructor ( props ) {
4
- super ( props ) ;
5
- this . onClick = this . onClick . bind ( this ) ;
5
+ super ( props )
6
+ this . onClick = this . onClick . bind ( this )
7
+ }
8
+
9
+ shouldComponentUpdate ( nextProps , nextState ) {
10
+ return this . props . text !== nextProps . text
6
11
}
7
12
8
13
onClick ( e ) {
9
- console . log ( 'Clicked' + this . props . text ) ;
10
- e . stopPropagation ( ) ;
14
+ console . log ( 'Clicked' + this . props . text )
15
+ e . stopPropagation ( )
11
16
}
12
17
13
18
render ( ) {
14
- return Nerv . createElement (
15
- 'td' ,
16
- { className : 'TableCell' , onClick : this . onClick } ,
17
- this . props . text
18
- ) ;
19
+ return (
20
+ < td className = "TableCell" onClick = { this . onClick } >
21
+ { this . props . text }
22
+ </ td >
23
+ )
19
24
}
20
25
}
21
26
22
- class TableRow extends Nerv . PureComponent {
27
+ class TableRow extends Component {
28
+ shouldComponentUpdate ( nextProps , nextState ) {
29
+ return this . props . data !== nextProps . data
30
+ }
31
+
23
32
render ( ) {
24
- const { data } = this . props ;
33
+ const { data } = this . props
25
34
26
35
// Interned strings
27
- const classes = data . active ? 'TableRow active' : 'TableRow' ;
36
+ const classes = data . active ? 'TableRow active' : 'TableRow'
28
37
29
- const cells = data . props ;
38
+ const cells = data . props
30
39
31
- const children = [ ] ;
40
+ const children = [ ]
32
41
for ( let i = 0 ; i < cells . length ; i ++ ) {
33
- // Key is used because Nerv prints warnings that there should be a key, libraries that can detect that children
42
+ // Key is used because React prints warnings that there should be a key, libraries that can detect that children
34
43
// shape isn't changing should render cells without keys.
35
- children . push ( Nerv . createElement ( TableCell , { key : i , text : cells [ i ] } ) ) ;
44
+ children . push ( < TableCell key = { i } text = { cells [ i ] } /> )
36
45
}
37
46
38
- // First table cell is inserted this way to prevent Nerv from printing warning that it doesn't have key property
39
- return Nerv . createElement (
40
- 'tr' ,
41
- { className : classes , 'data-id' : data . id } ,
42
- Nerv . createElement ( TableCell , { text : '#' + data . id } ) ,
43
- children
44
- ) ;
47
+ // First table cell is inserted this way to prevent react from printing warning that it doesn't have key property
48
+ return (
49
+ < tr className = { classes } data-id = { data . id } >
50
+ < TableCell text = { '#' + data . id } />
51
+ { children }
52
+ </ tr >
53
+ )
45
54
}
46
55
}
47
56
48
- class Table extends Nerv . PureComponent {
57
+ class Table extends Component {
58
+ shouldComponentUpdate ( nextProps , nextState ) {
59
+ return this . props . data !== nextProps . data
60
+ }
61
+
49
62
render ( ) {
50
- const items = this . props . data . items ;
63
+ const items = this . props . data . items
51
64
52
- const children = [ ] ;
65
+ const children = [ ]
53
66
for ( let i = 0 ; i < items . length ; i ++ ) {
54
- const item = items [ i ] ;
55
- children . push ( Nerv . createElement ( TableRow , { key : item . id , data : item } ) ) ;
67
+ const item = items [ i ]
68
+ children . push ( < TableRow key = { item . id } data = { item } /> )
56
69
}
57
70
58
- return Nerv . createElement (
59
- 'table' ,
60
- { className : 'Table' } ,
61
- Nerv . createElement (
62
- 'tbody' ,
63
- null ,
64
- children
65
- )
66
- ) ;
71
+ return (
72
+ < table className = "Table" >
73
+ < tbody > { children } </ tbody >
74
+ </ table >
75
+ )
67
76
}
68
77
}
69
78
70
- class AnimBox extends Nerv . PureComponent {
79
+ class AnimBox extends Component {
80
+ shouldComponentUpdate ( nextProps , nextState ) {
81
+ return this . props . data !== nextProps . data
82
+ }
83
+
71
84
render ( ) {
72
- const { data } = this . props ;
73
- const time = data . time ;
85
+ const { data } = this . props
86
+ const time = data . time
74
87
const style = {
75
- ' borderRadius' : ( time % 10 ) . toString ( ) + 'px' ,
76
- ' background' : 'rgba(0,0,0,' + ( 0.5 + time % 10 / 10 ) . toString ( ) + ')'
77
- } ;
88
+ borderRadius : ( time % 10 ) . toString ( ) + 'px' ,
89
+ background : 'rgba(0,0,0,' + ( 0.5 + ( time % 10 ) / 10 ) . toString ( ) + ')'
90
+ }
78
91
79
- return Nerv . createElement ( ' div' , { className : ' AnimBox' , ' data-id' : data . id , style : style } ) ;
92
+ return < div className = " AnimBox" data-id = { data . id } style = { style } />
80
93
}
81
94
}
82
95
83
- class Anim extends Nerv . PureComponent {
96
+ class Anim extends Component {
97
+ shouldComponentUpdate ( nextProps , nextState ) {
98
+ return this . props . data !== nextProps . data
99
+ }
100
+
84
101
render ( ) {
85
- const items = this . props . data . items ;
102
+ const items = this . props . data . items
86
103
87
- const children = [ ] ;
104
+ const children = [ ]
88
105
for ( let i = 0 ; i < items . length ; i ++ ) {
89
- const item = items [ i ] ;
90
- children . push ( Nerv . createElement ( AnimBox , { key : item . id , data : item } ) ) ;
106
+ const item = items [ i ]
107
+ children . push ( < AnimBox key = { item . id } data = { item } /> )
91
108
}
92
109
93
- return Nerv . createElement (
94
- 'div' ,
95
- { className : 'Anim' } ,
96
- children
97
- ) ;
110
+ return < div className = "Anim" > { children } </ div >
98
111
}
99
112
}
100
113
101
- class TreeLeaf extends Nerv . PureComponent {
114
+ class TreeLeaf extends Component {
115
+ shouldComponentUpdate ( nextProps , nextState ) {
116
+ return this . props . data !== nextProps . data
117
+ }
118
+
102
119
render ( ) {
103
- return Nerv . createElement (
104
- 'li' ,
105
- { className : 'TreeLeaf' } ,
106
- this . props . data . id
107
- ) ;
120
+ return < li className = "TreeLeaf" > { this . props . data . id } </ li >
108
121
}
109
122
}
110
123
111
- class TreeNode extends Nerv . PureComponent {
124
+ class TreeNode extends Component {
125
+ shouldComponentUpdate ( nextProps , nextState ) {
126
+ return this . props . data !== nextProps . data
127
+ }
128
+
112
129
render ( ) {
113
- const { data } = this . props ;
114
- const children = [ ] ;
130
+ const { data } = this . props
131
+ const children = [ ]
115
132
116
133
for ( let i = 0 ; i < data . children . length ; i ++ ) {
117
- const n = data . children [ i ] ;
134
+ const n = data . children [ i ]
118
135
if ( n . container ) {
119
- children . push ( Nerv . createElement ( TreeNode , { key : n . id , data : n } ) ) ;
136
+ children . push ( < TreeNode key = { n . id } data = { n } /> )
120
137
} else {
121
- children . push ( Nerv . createElement ( TreeLeaf , { key : n . id , data : n } ) ) ;
138
+ children . push ( < TreeLeaf key = { n . id } data = { n } /> )
122
139
}
123
140
}
124
141
125
- return Nerv . createElement (
126
- 'ul' ,
127
- { className : 'TreeNode' } ,
128
- children
129
- ) ;
142
+ return < ul className = "TreeNode" > { children } </ ul >
130
143
}
131
144
}
132
145
133
- class Tree extends Nerv . PureComponent {
146
+ class Tree extends Component {
147
+ shouldComponentUpdate ( nextProps , nextState ) {
148
+ return this . props . data !== nextProps . data
149
+ }
150
+
134
151
render ( ) {
135
- return Nerv . createElement (
136
- ' div' ,
137
- { className : 'Tree' } ,
138
- Nerv . createElement ( TreeNode , { data : this . props . data . root } )
139
- ) ;
152
+ return (
153
+ < div className = "Tree" >
154
+ < TreeNode data = { this . props . data . root } />
155
+ </ div >
156
+ )
140
157
}
141
158
}
142
159
143
- class Main extends Nerv . PureComponent {
160
+ class Main extends Component {
161
+ shouldComponentUpdate ( nextProps , nextState ) {
162
+ return this . props . data !== nextProps . data
163
+ }
164
+
144
165
render ( ) {
145
- const { data } = this . props ;
146
- const location = data . location ;
166
+ const { data } = this . props
167
+ const location = data . location
147
168
148
- var section ;
169
+ var section
149
170
if ( location === 'table' ) {
150
- section = Nerv . createElement ( Table , { data : data . table } ) ;
171
+ section = < Table data = { data . table } />
151
172
} else if ( location === 'anim' ) {
152
- section = Nerv . createElement ( Anim , { data : data . anim } ) ;
173
+ section = < Anim data = { data . anim } />
153
174
} else if ( location === 'tree' ) {
154
- section = Nerv . createElement ( Tree , { data : data . tree } ) ;
175
+ section = < Tree data = { data . tree } />
155
176
}
156
177
157
- return Nerv . createElement (
158
- 'div' ,
159
- { className : 'Main' } ,
160
- section
161
- ) ;
178
+ return < div className = "Main" > { section } </ div >
162
179
}
163
180
}
164
181
165
- uibench . init ( 'Nerv[PC]' , Nerv . version ) ;
166
-
167
- document . addEventListener ( 'DOMContentLoaded' , function ( e ) {
168
- const container = document . querySelector ( '#App' ) ;
169
-
170
- uibench . run ( function ( state ) {
171
- Nerv . render ( Nerv . createElement ( Main , { data : state } ) , container ) ;
172
- } , function ( samples ) {
173
- Nerv . render ( Nerv . createElement (
174
- 'pre' ,
175
- null ,
176
- JSON . stringify ( samples , null , ' ' )
177
- ) , container ) ;
178
- } ) ;
179
- } ) ;
182
+ uibench . init ( 'Nerv' , '1.0.0-alpha' )
183
+
184
+ document . addEventListener ( 'DOMContentLoaded' , function ( e ) {
185
+ const container = document . querySelector ( '#App' )
186
+
187
+ uibench . run (
188
+ function ( state ) {
189
+ render ( < Main data = { state } /> , container )
190
+ } ,
191
+ function ( samples ) {
192
+ render (
193
+ < pre > { JSON . stringify ( samples , null , ' ' ) } </ pre > ,
194
+ container
195
+ )
196
+ }
197
+ )
198
+ } )
0 commit comments