1
1
import React , { PureComponent } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
4
- import { ENTER_KEY , ESCAPE_KEY , TAB_KEY , RIGHT_KEY , LEFT_KEY , UP_KEY , DOWN_KEY } from './keys'
4
+ import {
5
+ ENTER_KEY ,
6
+ ESCAPE_KEY ,
7
+ TAB_KEY ,
8
+ RIGHT_KEY ,
9
+ LEFT_KEY ,
10
+ UP_KEY ,
11
+ DOWN_KEY
12
+ } from './keys'
5
13
6
14
import Cell from './Cell'
7
15
import CellShape from './CellShape'
8
16
import DataEditor from './DataEditor'
9
17
import ValueViewer from './ValueViewer'
10
18
import { renderValue , renderData } from './renderHelpers'
11
19
12
- function initialData ( { cell, row, col, valueRenderer, dataRenderer} ) {
20
+ function initialData ( { cell, row, col, valueRenderer, dataRenderer } ) {
13
21
return renderData ( cell , row , col , valueRenderer , dataRenderer )
14
22
}
15
23
16
- function initialValue ( { cell, row, col, valueRenderer} ) {
24
+ function initialValue ( { cell, row, col, valueRenderer } ) {
17
25
return renderValue ( cell , row , col , valueRenderer )
18
26
}
19
27
@@ -35,26 +43,31 @@ export default class DataCell extends PureComponent {
35
43
this . handleContextMenu = this . handleContextMenu . bind ( this )
36
44
this . handleDoubleClick = this . handleDoubleClick . bind ( this )
37
45
38
- this . state = { updated : false , reverting : false , value : '' , committing : false }
46
+ this . state = {
47
+ updated : false ,
48
+ reverting : false ,
49
+ value : '' ,
50
+ committing : false
51
+ }
39
52
}
40
53
41
- componentWillReceiveProps ( nextProps ) {
42
- if ( initialValue ( nextProps ) !== initialValue ( this . props ) ) {
43
- this . setState ( { updated : true } )
44
- this . timeout = setTimeout ( ( ) => this . setState ( { updated : false } ) , 700 )
54
+ componentDidUpdate ( prevProps ) {
55
+ if ( initialValue ( prevProps ) !== initialValue ( this . props ) ) {
56
+ this . setState ( { updated : true } )
57
+ this . timeout = setTimeout ( ( ) => this . setState ( { updated : false } ) , 700 )
45
58
}
46
- if ( nextProps . editing === true && this . props . editing === false ) {
47
- const value = nextProps . clearing ? '' : initialData ( nextProps )
59
+ if ( this . props . editing === true && prevProps . editing === false ) {
60
+ const value = this . props . clearing ? '' : initialData ( this . props )
48
61
this . setState ( { value, reverting : false } )
49
62
}
50
- }
51
63
52
- componentDidUpdate ( prevProps ) {
53
- if ( prevProps . editing === true &&
54
- this . props . editing === false &&
55
- ! this . state . reverting &&
56
- ! this . state . committing &&
57
- this . state . value !== initialData ( this . props ) ) {
64
+ if (
65
+ prevProps . editing === true &&
66
+ this . props . editing === false &&
67
+ ! this . state . reverting &&
68
+ ! this . state . committing &&
69
+ this . state . value !== initialData ( this . props )
70
+ ) {
58
71
this . props . onChange ( this . props . row , this . props . col , this . state . value )
59
72
}
60
73
}
@@ -68,7 +81,7 @@ export default class DataCell extends PureComponent {
68
81
}
69
82
70
83
handleCommit ( value , e ) {
71
- const { onChange, onNavigate} = this . props
84
+ const { onChange, onNavigate } = this . props
72
85
if ( value !== initialData ( this . props ) ) {
73
86
this . setState ( { value, committing : true } )
74
87
onChange ( this . props . row , this . props . col , value )
@@ -82,33 +95,33 @@ export default class DataCell extends PureComponent {
82
95
}
83
96
84
97
handleRevert ( ) {
85
- this . setState ( { reverting : true } )
98
+ this . setState ( { reverting : true } )
86
99
this . props . onRevert ( )
87
100
}
88
101
89
102
handleMouseDown ( e ) {
90
- const { row, col, onMouseDown, cell} = this . props
103
+ const { row, col, onMouseDown, cell } = this . props
91
104
if ( ! cell . disableEvents ) {
92
105
onMouseDown ( row , col , e )
93
106
}
94
107
}
95
108
96
109
handleMouseOver ( e ) {
97
- const { row, col, onMouseOver, cell} = this . props
110
+ const { row, col, onMouseOver, cell } = this . props
98
111
if ( ! cell . disableEvents ) {
99
112
onMouseOver ( row , col )
100
113
}
101
114
}
102
115
103
116
handleDoubleClick ( e ) {
104
- const { row, col, onDoubleClick, cell} = this . props
117
+ const { row, col, onDoubleClick, cell } = this . props
105
118
if ( ! cell . disableEvents ) {
106
119
onDoubleClick ( row , col )
107
120
}
108
121
}
109
122
110
123
handleContextMenu ( e ) {
111
- const { row, col, onContextMenu, cell} = this . props
124
+ const { row, col, onContextMenu, cell } = this . props
112
125
if ( ! cell . disableEvents ) {
113
126
onContextMenu ( e , row , col )
114
127
}
@@ -119,9 +132,14 @@ export default class DataCell extends PureComponent {
119
132
if ( keyCode === ESCAPE_KEY ) {
120
133
return this . handleRevert ( )
121
134
}
122
- const { cell : { component} , forceEdit} = this . props
135
+ const {
136
+ cell : { component } ,
137
+ forceEdit
138
+ } = this . props
123
139
const eatKeys = forceEdit || ! ! component
124
- const commit = keyCode === ENTER_KEY || keyCode === TAB_KEY ||
140
+ const commit =
141
+ keyCode === ENTER_KEY ||
142
+ keyCode === TAB_KEY ||
125
143
( ! eatKeys && [ LEFT_KEY , RIGHT_KEY , UP_KEY , DOWN_KEY ] . includes ( keyCode ) )
126
144
127
145
if ( commit ) {
@@ -130,7 +148,7 @@ export default class DataCell extends PureComponent {
130
148
}
131
149
132
150
renderComponent ( editing , cell ) {
133
- const { component, readOnly, forceComponent} = cell
151
+ const { component, readOnly, forceComponent } = cell
134
152
if ( ( editing && ! readOnly ) || forceComponent ) {
135
153
return component
136
154
}
@@ -161,23 +179,37 @@ export default class DataCell extends PureComponent {
161
179
}
162
180
163
181
render ( ) {
164
- const { row, col, cell, cellRenderer : CellRenderer ,
165
- valueRenderer, dataEditor, valueViewer, attributesRenderer,
166
- selected, editing, onKeyUp} = this . props
167
- const { updated} = this . state
182
+ const {
183
+ row,
184
+ col,
185
+ cell,
186
+ cellRenderer : CellRenderer ,
187
+ valueRenderer,
188
+ dataEditor,
189
+ valueViewer,
190
+ attributesRenderer,
191
+ selected,
192
+ editing,
193
+ onKeyUp
194
+ } = this . props
195
+ const { updated } = this . state
168
196
169
- const content = this . renderComponent ( editing , cell ) ||
170
- this . renderEditor ( editing , cell , row , col , dataEditor ) ||
171
- this . renderViewer ( cell , row , col , valueRenderer , valueViewer )
197
+ const content =
198
+ this . renderComponent ( editing , cell ) ||
199
+ this . renderEditor ( editing , cell , row , col , dataEditor ) ||
200
+ this . renderViewer ( cell , row , col , valueRenderer , valueViewer )
172
201
173
202
const className = [
174
203
cell . className ,
175
- 'cell' , cell . overflow ,
204
+ 'cell' ,
205
+ cell . overflow ,
176
206
selected && 'selected' ,
177
207
editing && 'editing' ,
178
208
cell . readOnly && 'read-only' ,
179
209
updated && 'updated'
180
- ] . filter ( a => a ) . join ( ' ' )
210
+ ]
211
+ . filter ( a => a )
212
+ . join ( ' ' )
181
213
182
214
return (
183
215
< CellRenderer
@@ -195,7 +227,7 @@ export default class DataCell extends PureComponent {
195
227
onDoubleClick = { this . handleDoubleClick }
196
228
onContextMenu = { this . handleContextMenu }
197
229
onKeyUp = { onKeyUp }
198
- >
230
+ >
199
231
{ content }
200
232
</ CellRenderer >
201
233
)
0 commit comments