@@ -82,7 +82,6 @@ var DataSheet = function (_PureComponent) {
82
82
_this . onContextMenu = _this . onContextMenu . bind ( _this ) ;
83
83
_this . handleNavigate = _this . handleNavigate . bind ( _this ) ;
84
84
_this . handleKey = _this . handleKey . bind ( _this ) . bind ( _this ) ;
85
- _this . handleComponentKey = _this . handleComponentKey . bind ( _this ) ;
86
85
_this . handleCopy = _this . handleCopy . bind ( _this ) ;
87
86
_this . handlePaste = _this . handlePaste . bind ( _this ) ;
88
87
_this . pageClick = _this . pageClick . bind ( _this ) ;
@@ -91,6 +90,9 @@ var DataSheet = function (_PureComponent) {
91
90
_this . isSelected = _this . isSelected . bind ( _this ) ;
92
91
_this . isEditing = _this . isEditing . bind ( _this ) ;
93
92
_this . isClearing = _this . isClearing . bind ( _this ) ;
93
+ _this . handleComponentKey = _this . handleComponentKey . bind ( _this ) ;
94
+
95
+ _this . handleKeyboardCellMovement = _this . handleKeyboardCellMovement . bind ( _this ) ;
94
96
95
97
_this . defaultState = {
96
98
start : { } ,
@@ -195,46 +197,51 @@ var DataSheet = function (_PureComponent) {
195
197
}
196
198
} , {
197
199
key : 'handleKeyboardCellMovement' ,
198
- value : function handleKeyboardCellMovement ( e , _ref ) {
199
- var data = _ref . data ,
200
- start = _ref . start ,
201
- isEditing = _ref . isEditing ,
202
- currentCell = _ref . currentCell ;
200
+ value : function handleKeyboardCellMovement ( e ) {
201
+ var commit = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : false ;
202
+ var _state2 = this . state ,
203
+ start = _state2 . start ,
204
+ editing = _state2 . editing ;
205
+ var data = this . props . data ;
203
206
204
- if ( isEditing ) {
205
- return false ;
206
- }
207
- var hasComponent = currentCell && currentCell . component ;
208
- var forceComponent = currentCell && currentCell . forceComponent ;
207
+ var isEditing = editing && ! isEmpty ( editing ) ;
208
+ var currentCell = data [ start . i ] && data [ start . i ] [ start . j ] ;
209
209
210
- if ( hasComponent && ( isEditing || forceComponent ) ) {
210
+ if ( isEditing && ! commit ) {
211
211
return false ;
212
212
}
213
+ var hasComponent = currentCell && currentCell . component ;
213
214
214
215
var keyCode = e . which || e . keyCode ;
215
- var newLocation = null ;
216
216
217
- if ( keyCode === _keys . TAB_KEY && ! e . shiftKey ) {
218
- newLocation = { i : start . i , j : start . j + 1 } ;
219
- newLocation = typeof data [ newLocation . i ] [ newLocation . j ] !== 'undefined' ? newLocation : { i : start . i + 1 , j : 0 } ;
217
+ if ( hasComponent && isEditing ) {
218
+ /*
219
+ if (keyCode === ESCAPE_KEY) {
220
+ e.preventDefault()
221
+ this.onRevert()
222
+ }
223
+ if (keyCode === ENTER_KEY) {
224
+ this.handleNavigate(e, {i: e.shiftKey ? -1 : 1, j: 0})
225
+ } else if (keyCode === TAB_KEY) {
226
+ this.handleNavigate(e, {i: 0, j: e.shiftKey ? -1 : 1}, true)
227
+ } */
228
+ e . preventDefault ( ) ;
229
+ return ;
230
+ }
231
+
232
+ if ( keyCode === _keys . TAB_KEY ) {
233
+ this . handleNavigate ( e , { i : 0 , j : e . shiftKey ? - 1 : 1 } , true ) ;
220
234
} else if ( keyCode === _keys . RIGHT_KEY ) {
221
- newLocation = { i : start . i , j : start . j + 1 } ;
222
- } else if ( keyCode === _keys . LEFT_KEY || keyCode === _keys . TAB_KEY && e . shiftKey ) {
223
- newLocation = { i : start . i , j : start . j - 1 } ;
235
+ this . handleNavigate ( e , { i : 0 , j : 1 } ) ;
236
+ } else if ( keyCode === _keys . LEFT_KEY ) {
237
+ this . handleNavigate ( e , { i : 0 , j : - 1 } ) ;
224
238
} else if ( keyCode === _keys . UP_KEY ) {
225
- newLocation = { i : start . i - 1 , j : start . j } ;
239
+ this . handleNavigate ( e , { i : - 1 , j : 0 } ) ;
226
240
} else if ( keyCode === _keys . DOWN_KEY ) {
227
- newLocation = { i : start . i + 1 , j : start . j } ;
228
- }
229
-
230
- if ( newLocation && data [ newLocation . i ] && typeof data [ newLocation . i ] [ newLocation . j ] !== 'undefined' ) {
231
- this . setState ( { start : newLocation , end : newLocation , editing : { } } ) ;
232
- }
233
- if ( newLocation ) {
234
- e . preventDefault ( ) ;
235
- return true ;
241
+ this . handleNavigate ( e , { i : 1 , j : 0 } ) ;
242
+ } else if ( commit && keyCode === _keys . ENTER_KEY ) {
243
+ this . handleNavigate ( e , { i : e . shiftKey ? - 1 : 1 , j : 0 } ) ;
236
244
}
237
- return false ;
238
245
}
239
246
} , {
240
247
key : 'getSelectedCells' ,
@@ -256,10 +263,10 @@ var DataSheet = function (_PureComponent) {
256
263
return ;
257
264
}
258
265
var keyCode = e . which || e . keyCode ;
259
- var _state2 = this . state ,
260
- start = _state2 . start ,
261
- end = _state2 . end ,
262
- editing = _state2 . editing ;
266
+ var _state3 = this . state ,
267
+ start = _state3 . start ,
268
+ end = _state3 . end ,
269
+ editing = _state3 . editing ;
263
270
264
271
var data = this . props . data ;
265
272
var isEditing = editing && ! isEmpty ( editing ) ;
@@ -278,19 +285,20 @@ var DataSheet = function (_PureComponent) {
278
285
109 , /* decimal point */
279
286
110 ] . indexOf ( keyCode ) > - 1 ;
280
287
281
- if ( noCellsSelected || ctrlKeyPressed || this . handleKeyboardCellMovement ( e , { data : data , start : start , isEditing : isEditing , currentCell : currentCell } ) ) {
288
+ if ( noCellsSelected || ctrlKeyPressed ) {
282
289
return true ;
283
290
}
284
291
285
292
if ( ! isEditing ) {
293
+ this . handleKeyboardCellMovement ( e ) ;
286
294
if ( deleteKeysPressed ) {
287
295
// ugly solution brought to you by https://reactjs.org/docs/react-component.html#setstate
288
296
// setState in a loop is unreliable
289
297
setTimeout ( function ( ) {
290
- _this3 . getSelectedCells ( data , start , end ) . map ( function ( _ref2 ) {
291
- var cell = _ref2 . cell ,
292
- i = _ref2 . i ,
293
- j = _ref2 . j ;
298
+ _this3 . getSelectedCells ( data , start , end ) . map ( function ( _ref ) {
299
+ var cell = _ref . cell ,
300
+ i = _ref . i ,
301
+ j = _ref . j ;
294
302
return ! cell . readOnly ? _this3 . onChange ( i , j , '' ) : null ;
295
303
} ) ;
296
304
} , 0 ) ;
@@ -307,50 +315,68 @@ var DataSheet = function (_PureComponent) {
307
315
}
308
316
}
309
317
} , {
310
- key : 'handleComponentKey ' ,
311
- value : function handleComponentKey ( e ) {
318
+ key : 'handleNavigate ' ,
319
+ value : function handleNavigate ( e , offsets , jumpRow ) {
312
320
var _this4 = this ;
313
321
314
- // handles keyboard events when editing components
315
- var keyCode = e . which || e . keyCode ;
316
- if ( [ _keys . ENTER_KEY , _keys . ESCAPE_KEY , _keys . TAB_KEY ] . includes ( keyCode ) ) {
317
- var editing = this . state . editing ;
318
-
319
- if ( ! isEmpty ( editing ) ) {
320
- var data = this . props . data ;
321
-
322
- var currentCell = data [ editing . i ] [ editing . j ] ;
323
- var offset = e . shiftKey ? - 1 : 1 ;
324
- if ( currentCell && currentCell . component ) {
325
- var func = this . onRevert ; // ESCAPE_KEY
326
- if ( keyCode === _keys . ENTER_KEY ) {
327
- func = function func ( ) {
328
- return _this4 . handleNavigate ( { i : offset , j : 0 } ) ;
329
- } ;
330
- } else if ( keyCode === _keys . TAB_KEY ) {
331
- func = function func ( ) {
332
- return _this4 . handleNavigate ( { i : 0 , j : offset } ) ;
333
- } ;
334
- }
335
- // setTimeout makes sure that component is done handling the event before we take over
336
- setTimeout ( func , 1 ) ;
322
+ if ( offsets && ( offsets . i || offsets . j ) ) {
323
+ var start = this . state . start ;
324
+ var data = this . props . data ;
325
+
326
+ var newLocation = { i : start . i + offsets . i , j : start . j + offsets . j } ;
327
+ var updateLocation = function updateLocation ( ) {
328
+ if ( data [ newLocation . i ] && typeof data [ newLocation . i ] [ newLocation . j ] !== 'undefined' ) {
329
+ _this4 . setState ( { start : newLocation , end : newLocation , editing : { } } ) ;
330
+ e . preventDefault ( ) ;
331
+ return true ;
337
332
}
333
+ return false ;
334
+ } ;
335
+ if ( ! updateLocation ( ) && jumpRow ) {
336
+ if ( offsets . j < 0 ) {
337
+ newLocation = { i : start . i - 1 , j : data [ 0 ] . length - 1 } ;
338
+ } else {
339
+ newLocation = { i : start . i + 1 , j : 0 } ;
340
+ }
341
+ updateLocation ( ) ;
338
342
}
339
343
}
340
344
}
341
345
} , {
342
- key : 'handleNavigate' ,
343
- value : function handleNavigate ( offsets ) {
344
- if ( offsets && ( offsets . i || offsets . j ) ) {
345
- var start = this . state . start ;
346
- var data = this . props . data ;
346
+ key : 'handleComponentKey' ,
347
+ value : function handleComponentKey ( e ) {
348
+ var _this5 = this ;
347
349
348
- var newLocation = { i : start . i + offsets . i , j : start . j + offsets . j } ;
349
- if ( data [ newLocation . i ] && typeof data [ newLocation . i ] [ newLocation . j ] !== 'undefined' ) {
350
- this . setState ( { start : newLocation , end : newLocation , editing : { } } ) ;
350
+ // handles keyboard events when editing components
351
+ var keyCode = e . which || e . keyCode ;
352
+ if ( ! [ _keys . ENTER_KEY , _keys . ESCAPE_KEY , _keys . TAB_KEY ] . includes ( keyCode ) ) {
353
+ return ;
354
+ }
355
+ var editing = this . state . editing ;
356
+ var data = this . props . data ;
357
+
358
+ var isEditing = ! isEmpty ( editing ) ;
359
+ if ( isEditing ) {
360
+ var currentCell = data [ editing . i ] [ editing . j ] ;
361
+ var offset = e . shiftKey ? - 1 : 1 ;
362
+ if ( currentCell && currentCell . component && ! currentCell . forceComponent ) {
363
+ e . preventDefault ( ) ;
364
+ var func = this . onRevert ; // ESCAPE_KEY
365
+ if ( keyCode === _keys . ENTER_KEY ) {
366
+ func = function func ( ) {
367
+ return _this5 . handleNavigate ( e , { i : offset , j : 0 } ) ;
368
+ } ;
369
+ } else if ( keyCode === _keys . TAB_KEY ) {
370
+ func = function func ( ) {
371
+ return _this5 . handleNavigate ( e , { i : 0 , j : offset } , true ) ;
372
+ } ;
373
+ }
374
+ // setTimeout makes sure that component is done handling the event before we take over
375
+ setTimeout ( function ( ) {
376
+ func ( ) ; _this5 . dgDom && _this5 . dgDom . focus ( ) ;
377
+ } , 1 ) ;
351
378
}
352
379
}
353
- this . dgDom && this . dgDom . focus ( ) ;
354
380
}
355
381
} , {
356
382
key : 'onContextMenu' ,
@@ -441,7 +467,7 @@ var DataSheet = function (_PureComponent) {
441
467
} , {
442
468
key : 'render' ,
443
469
value : function render ( ) {
444
- var _this5 = this ;
470
+ var _this6 = this ;
445
471
446
472
var _props2 = this . props ,
447
473
SheetRenderer = _props2 . sheetRenderer ,
@@ -462,7 +488,7 @@ var DataSheet = function (_PureComponent) {
462
488
return _react2 . default . createElement (
463
489
'span' ,
464
490
{ ref : function ref ( r ) {
465
- _this5 . dgDom = r ;
491
+ _this6 . dgDom = r ;
466
492
} , tabIndex : '0' , className : 'data-grid-container' , onKeyDown : this . handleKey } ,
467
493
_react2 . default . createElement (
468
494
SheetRenderer ,
@@ -480,17 +506,17 @@ var DataSheet = function (_PureComponent) {
480
506
col : j ,
481
507
cell : cell ,
482
508
forceEdit : forceEdit ,
483
- onMouseDown : _this5 . onMouseDown ,
484
- onMouseOver : _this5 . onMouseOver ,
485
- onDoubleClick : _this5 . onDoubleClick ,
486
- onContextMenu : _this5 . onContextMenu ,
487
- onChange : _this5 . onChange ,
488
- onRevert : _this5 . onRevert ,
489
- onNavigate : _this5 . handleNavigate ,
490
- onKey : _this5 . handleKey ,
491
- selected : _this5 . isSelected ( i , j ) ,
492
- editing : _this5 . isEditing ( i , j ) ,
493
- clearing : _this5 . isClearing ( i , j ) ,
509
+ onMouseDown : _this6 . onMouseDown ,
510
+ onMouseOver : _this6 . onMouseOver ,
511
+ onDoubleClick : _this6 . onDoubleClick ,
512
+ onContextMenu : _this6 . onContextMenu ,
513
+ onChange : _this6 . onChange ,
514
+ onRevert : _this6 . onRevert ,
515
+ onNavigate : _this6 . handleKeyboardCellMovement ,
516
+ onKey : _this6 . handleKey ,
517
+ selected : _this6 . isSelected ( i , j ) ,
518
+ editing : _this6 . isEditing ( i , j ) ,
519
+ clearing : _this6 . isClearing ( i , j ) ,
494
520
attributesRenderer : attributesRenderer ,
495
521
cellRenderer : cellRenderer ,
496
522
valueRenderer : valueRenderer ,
0 commit comments