Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional keyboard control #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 57 additions & 52 deletions lib/DataSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,60 +494,63 @@ var DataSheet = (function (_PureComponent) {
if (e.isPropagationStopped && e.isPropagationStopped()) {
return;
}
var keyCode = e.which || e.keyCode;

var _getState5 = this.getState(),
start = _getState5.start,
end = _getState5.end,
editing = _getState5.editing;

var isEditing = editing && !isEmpty(editing);
var noCellsSelected = !start || isEmpty(start);
var ctrlKeyPressed = e.ctrlKey || e.metaKey;
var deleteKeysPressed =
keyCode === _keys.DELETE_KEY || keyCode === _keys.BACKSPACE_KEY;
var enterKeyPressed = keyCode === _keys.ENTER_KEY;
var numbersPressed = keyCode >= 48 && keyCode <= 57;
var lettersPressed = keyCode >= 65 && keyCode <= 90;
var latin1Supplement = keyCode >= 160 && keyCode <= 255;
var numPadKeysPressed = keyCode >= 96 && keyCode <= 105;
var currentCell = !noCellsSelected && this.props.data[start.i][start.j];
var equationKeysPressed =
[
187 /* equal */,
189 /* substract */,
190 /* period */,
107 /* add */,
109 /* decimal point */,
110,
].indexOf(keyCode) > -1;

if (noCellsSelected || ctrlKeyPressed) {
return true;
}
if (this.props.keyboardEnabled) {
var keyCode = e.which || e.keyCode;

var _getState5 = this.getState(),
start = _getState5.start,
end = _getState5.end,
editing = _getState5.editing;

var isEditing = editing && !isEmpty(editing);
var noCellsSelected = !start || isEmpty(start);
var ctrlKeyPressed = e.ctrlKey || e.metaKey;
var deleteKeysPressed =
keyCode === _keys.DELETE_KEY || keyCode === _keys.BACKSPACE_KEY;
var enterKeyPressed = keyCode === _keys.ENTER_KEY;
var numbersPressed = keyCode >= 48 && keyCode <= 57;
var lettersPressed = keyCode >= 65 && keyCode <= 90;
var latin1Supplement = keyCode >= 160 && keyCode <= 255;
var numPadKeysPressed = keyCode >= 96 && keyCode <= 105;
var currentCell =
!noCellsSelected && this.props.data[start.i][start.j];
var equationKeysPressed =
[
187 /* equal */,
189 /* substract */,
190 /* period */,
107 /* add */,
109 /* decimal point */,
110,
].indexOf(keyCode) > -1;

if (noCellsSelected || ctrlKeyPressed) {
return true;
}

if (!isEditing) {
this.handleKeyboardCellMovement(e);
if (deleteKeysPressed) {
e.preventDefault();
this.clearSelectedCells(start, end);
} else if (currentCell && !currentCell.readOnly) {
if (enterKeyPressed) {
this._setState({ editing: start, clear: {}, forceEdit: true });
if (!isEditing) {
this.handleKeyboardCellMovement(e);
if (deleteKeysPressed) {
e.preventDefault();
} else if (
numbersPressed ||
numPadKeysPressed ||
lettersPressed ||
latin1Supplement ||
equationKeysPressed
) {
// empty out cell if user starts typing without pressing enter
this._setState({
editing: start,
clear: start,
forceEdit: false,
});
this.clearSelectedCells(start, end);
} else if (currentCell && !currentCell.readOnly) {
if (enterKeyPressed) {
this._setState({ editing: start, clear: {}, forceEdit: true });
e.preventDefault();
} else if (
numbersPressed ||
numPadKeysPressed ||
lettersPressed ||
latin1Supplement ||
equationKeysPressed
) {
// empty out cell if user starts typing without pressing enter
this._setState({
editing: start,
clear: start,
forceEdit: false,
});
}
}
}
}
Expand Down Expand Up @@ -1063,6 +1066,7 @@ DataSheet.propTypes = {
attributesRenderer: _propTypes2.default.func,
keyFn: _propTypes2.default.func,
handleCopy: _propTypes2.default.func,
keyboardEnabled: _propTypes2.default.bool,
};

DataSheet.defaultProps = {
Expand All @@ -1071,4 +1075,5 @@ DataSheet.defaultProps = {
cellRenderer: _Cell2.default,
valueViewer: _ValueViewer2.default,
dataEditor: _DataEditor2.default,
keyboardEnabled: true,
};
90 changes: 47 additions & 43 deletions src/DataSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,51 +304,53 @@ export default class DataSheet extends PureComponent {
if (e.isPropagationStopped && e.isPropagationStopped()) {
return;
}
const keyCode = e.which || e.keyCode;
const { start, end, editing } = this.getState();
const isEditing = editing && !isEmpty(editing);
const noCellsSelected = !start || isEmpty(start);
const ctrlKeyPressed = e.ctrlKey || e.metaKey;
const deleteKeysPressed =
keyCode === DELETE_KEY || keyCode === BACKSPACE_KEY;
const enterKeyPressed = keyCode === ENTER_KEY;
const numbersPressed = keyCode >= 48 && keyCode <= 57;
const lettersPressed = keyCode >= 65 && keyCode <= 90;
const latin1Supplement = keyCode >= 160 && keyCode <= 255;
const numPadKeysPressed = keyCode >= 96 && keyCode <= 105;
const currentCell = !noCellsSelected && this.props.data[start.i][start.j];
const equationKeysPressed =
[
187 /* equal */,
189 /* substract */,
190 /* period */,
107 /* add */,
109 /* decimal point */,
110,
].indexOf(keyCode) > -1;

if (noCellsSelected || ctrlKeyPressed) {
return true;
}
if (this.props.keyboardEnabled) {
const keyCode = e.which || e.keyCode;
const { start, end, editing } = this.getState();
const isEditing = editing && !isEmpty(editing);
const noCellsSelected = !start || isEmpty(start);
const ctrlKeyPressed = e.ctrlKey || e.metaKey;
const deleteKeysPressed =
keyCode === DELETE_KEY || keyCode === BACKSPACE_KEY;
const enterKeyPressed = keyCode === ENTER_KEY;
const numbersPressed = keyCode >= 48 && keyCode <= 57;
const lettersPressed = keyCode >= 65 && keyCode <= 90;
const latin1Supplement = keyCode >= 160 && keyCode <= 255;
const numPadKeysPressed = keyCode >= 96 && keyCode <= 105;
const currentCell = !noCellsSelected && this.props.data[start.i][start.j];
const equationKeysPressed =
[
187 /* equal */,
189 /* substract */,
190 /* period */,
107 /* add */,
109 /* decimal point */,
110,
].indexOf(keyCode) > -1;

if (noCellsSelected || ctrlKeyPressed) {
return true;
}

if (!isEditing) {
this.handleKeyboardCellMovement(e);
if (deleteKeysPressed) {
e.preventDefault();
this.clearSelectedCells(start, end);
} else if (currentCell && !currentCell.readOnly) {
if (enterKeyPressed) {
this._setState({ editing: start, clear: {}, forceEdit: true });
if (!isEditing) {
this.handleKeyboardCellMovement(e);
if (deleteKeysPressed) {
e.preventDefault();
} else if (
numbersPressed ||
numPadKeysPressed ||
lettersPressed ||
latin1Supplement ||
equationKeysPressed
) {
// empty out cell if user starts typing without pressing enter
this._setState({ editing: start, clear: start, forceEdit: false });
this.clearSelectedCells(start, end);
} else if (currentCell && !currentCell.readOnly) {
if (enterKeyPressed) {
this._setState({ editing: start, clear: {}, forceEdit: true });
e.preventDefault();
} else if (
numbersPressed ||
numPadKeysPressed ||
lettersPressed ||
latin1Supplement ||
equationKeysPressed
) {
// empty out cell if user starts typing without pressing enter
this._setState({ editing: start, clear: start, forceEdit: false });
}
}
}
}
Expand Down Expand Up @@ -739,6 +741,7 @@ DataSheet.propTypes = {
attributesRenderer: PropTypes.func,
keyFn: PropTypes.func,
handleCopy: PropTypes.func,
keyboardEnabled: PropTypes.bool,
};

DataSheet.defaultProps = {
Expand All @@ -747,4 +750,5 @@ DataSheet.defaultProps = {
cellRenderer: Cell,
valueViewer: ValueViewer,
dataEditor: DataEditor,
keyboardEnabled: true,
};
2 changes: 2 additions & 0 deletions types/react-datasheet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ declare namespace ReactDataSheet {
dataEditor?: DataEditor<T, V>;
/** Method to render the underlying value of the cell function(cell, i, j). This data is visible once in edit mode. */
dataRenderer?: DataRenderer<T, V>;
/** If set to 'false', all key bindings are disabled. Defaults to 'true'. */
keyboardEnabled?: boolean;
/** onCellsChanged handler: function(arrayOfChanges[, arrayOfAdditions]) {}, where changes is an array of objects of the shape {cell, row, col, value}. */
onCellsChanged?: CellsChangedHandler<T, V>;
/** Context menu handler : function(event, cell, i, j). */
Expand Down