Skip to content

Commit a3f0a04

Browse files
authored
Merge pull request #22 from patrick-jones/escape
Adds the ability to revert an edit by hitting the escape key.
2 parents 95ebb2c + 56ffb3d commit a3f0a04

6 files changed

Lines changed: 141 additions & 131 deletions

File tree

lib/DataCell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var DataCell = function (_PureComponent) {
4949
}, {
5050
key: 'componentDidUpdate',
5151
value: function componentDidUpdate(prevProps) {
52-
if (prevProps.editing === true && this.props.editing === false) {
52+
if (prevProps.editing === true && this.props.editing === false && this.props.reverting === false) {
5353
this.onChange(this._input.value);
5454
}
5555
if (prevProps.editing === false && this.props.editing === true) {

lib/DataSheet.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
3434

3535
var TAB_KEY = 9;
3636
var ENTER_KEY = 13;
37+
var ESCAPE_KEY = 27;
3738
var LEFT_KEY = 37;
3839
var UP_KEY = 38;
3940
var RIGHT_KEY = 39;
@@ -81,6 +82,7 @@ var DataSheet = function (_PureComponent) {
8182
selecting: false,
8283
forceEdit: false,
8384
editing: {},
85+
reverting: {},
8486
clear: {}
8587
};
8688
_this.state = _this.defaultState;
@@ -231,6 +233,7 @@ var DataSheet = function (_PureComponent) {
231233
var ctrlKeyPressed = e.ctrlKey || e.metaKey;
232234
var deleteKeysPressed = e.keyCode === DELETE_KEY || e.keyCode === BACKSPACE_KEY;
233235
var enterKeyPressed = e.keyCode === ENTER_KEY;
236+
var escapeKeyPressed = e.keyCode === ESCAPE_KEY;
234237
var numbersPressed = e.keyCode >= 48 && e.keyCode <= 57;
235238
var lettersPressed = e.keyCode >= 65 && e.keyCode <= 90;
236239
var numPadKeysPressed = e.keyCode >= 96 && e.keyCode <= 105;
@@ -255,15 +258,18 @@ var DataSheet = function (_PureComponent) {
255258
});
256259
e.preventDefault();
257260
} else if (enterKeyPressed && isEditing) {
258-
this.setState({ editing: {} });
261+
this.setState({ editing: {}, reverting: {} });
262+
} else if (escapeKeyPressed && isEditing) {
263+
this.setState({ editing: {}, reverting: editing });
259264
} else if (enterKeyPressed && !isEditing && !cell.readOnly) {
260-
this.setState({ editing: start, clear: {}, forceEdit: true });
265+
this.setState({ editing: start, clear: {}, reverting: {}, forceEdit: true });
261266
} else if (numbersPressed || numPadKeysPressed || lettersPressed || equationKeysPressed || enterKeyPressed) {
262267
//empty out cell if user starts typing without pressing enter
263268
if (!isEditing && !cell.readOnly) {
264269
this.setState({
265270
editing: start,
266271
clear: start,
272+
reverting: {},
267273
forceEdit: false
268274
});
269275
}
@@ -350,6 +356,9 @@ var DataSheet = function (_PureComponent) {
350356
var isEditing = function isEditing(i, j) {
351357
return _this4.state.editing.i === i && _this4.state.editing.j === j;
352358
};
359+
var isReverting = function isReverting(i, j) {
360+
return _this4.state.reverting.i === i && _this4.state.reverting.j === j;
361+
};
353362
var shouldClear = function shouldClear(i, j) {
354363
return _this4.state.clear.i === i && _this4.state.clear.j === j;
355364
};
@@ -376,7 +385,9 @@ var DataSheet = function (_PureComponent) {
376385
onMouseDown: cell.disableEvents ? nullFtn : _this4.onMouseDown,
377386
onDoubleClick: cell.disableEvents ? nullFtn : _this4.onDoubleClick,
378387
onMouseOver: cell.disableEvents ? nullFtn : _this4.onMouseOver,
379-
onContextMenu: cell.disableEvents ? nullFtn : _this4.onContextMenu, editing: isEditing(i, j),
388+
onContextMenu: cell.disableEvents ? nullFtn : _this4.onContextMenu,
389+
editing: isEditing(i, j),
390+
reverting: isReverting(i, j),
380391
colSpan: cell.colSpan,
381392
value: valueRenderer(cell)
382393
};

src/DataCell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class DataCell extends PureComponent {
1515
}
1616

1717
componentDidUpdate(prevProps) {
18-
if (prevProps.editing === true && this.props.editing === false) {
18+
if (prevProps.editing === true && this.props.editing === false && this.props.reverting === false) {
1919
this.onChange(this._input.value);
2020
}
2121
if (prevProps.editing === false && this.props.editing === true) {

src/DataSheet.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ComponentCell from './ComponentCell';
55

66
const TAB_KEY = 9;
77
const ENTER_KEY = 13;
8+
const ESCAPE_KEY = 27;
89
const LEFT_KEY = 37;
910
const UP_KEY = 38;
1011
const RIGHT_KEY = 39;
@@ -46,6 +47,7 @@ export default class DataSheet extends PureComponent {
4647
selecting: false,
4748
forceEdit: false,
4849
editing: {},
50+
reverting: {},
4951
clear: {}
5052
};
5153
this.state = this.defaultState;
@@ -96,7 +98,7 @@ export default class DataSheet extends PureComponent {
9698
handlePaste(e) {
9799
if(isEmpty(this.state.editing)) {
98100
const start = this.state.start;
99-
101+
100102
const pastedMap = [];
101103
const pasteData = e.clipboardData
102104
.getData('text/plain')
@@ -113,7 +115,7 @@ export default class DataSheet extends PureComponent {
113115
this.onChange(start.i + i, start.j + j, pastedData);
114116
end = {i: start.i + i, j: start.j + j};
115117
}
116-
118+
117119
});
118120
pastedMap.push(rowData);
119121
});
@@ -176,6 +178,7 @@ export default class DataSheet extends PureComponent {
176178
const ctrlKeyPressed = e.ctrlKey || e.metaKey;
177179
const deleteKeysPressed = (e.keyCode === DELETE_KEY || e.keyCode === BACKSPACE_KEY);
178180
const enterKeyPressed = e.keyCode === ENTER_KEY;
181+
const escapeKeyPressed = e.keyCode === ESCAPE_KEY;
179182
const numbersPressed = (e.keyCode >= 48 && e.keyCode <= 57);
180183
const lettersPressed = (e.keyCode >= 65 && e.keyCode <= 90);
181184
const numPadKeysPressed = (e.keyCode >= 96 && e.keyCode <= 105);
@@ -200,9 +203,11 @@ export default class DataSheet extends PureComponent {
200203
);
201204
e.preventDefault();
202205
} else if (enterKeyPressed && isEditing) {
203-
this.setState({editing: {}});
206+
this.setState({editing: {}, reverting: {}});
207+
} else if (escapeKeyPressed && isEditing) {
208+
this.setState({editing: {}, reverting: editing});
204209
} else if (enterKeyPressed && !isEditing && !cell.readOnly) {
205-
this.setState({editing: start, clear: {}, forceEdit: true});
210+
this.setState({editing: start, clear: {}, reverting: {}, forceEdit: true});
206211
} else if (numbersPressed
207212
|| numPadKeysPressed
208213
|| lettersPressed
@@ -214,6 +219,7 @@ export default class DataSheet extends PureComponent {
214219
this.setState({
215220
editing: start,
216221
clear: start,
222+
reverting: {},
217223
forceEdit: false
218224
});
219225
}
@@ -288,6 +294,7 @@ export default class DataSheet extends PureComponent {
288294
};
289295

290296
const isEditing = (i, j) => this.state.editing.i === i && this.state.editing.j === j;
297+
const isReverting = (i, j) => this.state.reverting.i === i && this.state.reverting.j === j;
291298
const shouldClear = (i, j) => this.state.clear.i === i && this.state.clear.j === j;
292299

293300
return <table ref={(r) => this.dgDom = r} className={'data-grid ' + (className ? className : '')}>
@@ -305,7 +312,9 @@ export default class DataSheet extends PureComponent {
305312
onMouseDown: cell.disableEvents ? nullFtn : this.onMouseDown,
306313
onDoubleClick: cell.disableEvents ? nullFtn : this.onDoubleClick,
307314
onMouseOver: cell.disableEvents ? nullFtn : this.onMouseOver,
308-
onContextMenu: cell.disableEvents ? nullFtn : this.onContextMenu,editing: isEditing(i, j),
315+
onContextMenu: cell.disableEvents ? nullFtn : this.onContextMenu,
316+
editing: isEditing(i, j),
317+
reverting: isReverting(i, j),
309318
colSpan: cell.colSpan,
310319
value: valueRenderer(cell),
311320
};

0 commit comments

Comments
 (0)