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

Add customNavigate event #216

Open
wants to merge 3 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
34 changes: 27 additions & 7 deletions src/DataSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,11 @@ export default class DataSheet extends PureComponent {

handleKeyboardCellMovement(e, commit = false) {
const { start, editing } = this.getState();
const { data } = this.props;
const { data, customNavigate } = this.props;
const isEditing = editing && !isEmpty(editing);
const currentCell = data[start.i] && data[start.i][start.j];
let defaultOffset = { i: 1, j: 0 };
let jumpRow = false;

if (isEditing && !commit) {
return false;
Expand All @@ -287,17 +289,34 @@ export default class DataSheet extends PureComponent {
}

if (keyCode === TAB_KEY) {
this.handleNavigate(e, { i: 0, j: e.shiftKey ? -1 : 1 }, true);
defaultOffset = { i: 0, j: e.shiftKey ? -1 : 1 };
jumpRow = true;
} else if (keyCode === RIGHT_KEY) {
this.handleNavigate(e, { i: 0, j: 1 });
defaultOffset = { i: 0, j: 1 };
} else if (keyCode === LEFT_KEY) {
this.handleNavigate(e, { i: 0, j: -1 });
defaultOffset = { i: 0, j: -1 };
} else if (keyCode === UP_KEY) {
this.handleNavigate(e, { i: -1, j: 0 });
defaultOffset = { i: -1, j: 0 };
} else if (keyCode === DOWN_KEY) {
this.handleNavigate(e, { i: 1, j: 0 });
defaultOffset = { i: 1, j: 0 };
} else if (commit && keyCode === ENTER_KEY) {
this.handleNavigate(e, { i: e.shiftKey ? -1 : 1, j: 0 });
defaultOffset = { i: e.shiftKey ? -1 : 1, j: 0 };
}
if (
[TAB_KEY, RIGHT_KEY, LEFT_KEY, UP_KEY, DOWN_KEY, ENTER_KEY].includes(
keyCode,
)
) {
const [newOffset, newJumpRow] = customNavigate
? customNavigate(currentCell, keyCode, e) || [defaultOffset, jumpRow]
: [defaultOffset, jumpRow];
if (keyCode === ENTER_KEY) {
if (commit) {
this.handleNavigate(e, newOffset, newJumpRow);
}
} else {
this.handleNavigate(e, newOffset, newJumpRow);
}
}
}

Expand Down Expand Up @@ -745,6 +764,7 @@ DataSheet.propTypes = {
parsePaste: PropTypes.func,
attributesRenderer: PropTypes.func,
keyFn: PropTypes.func,
customNavigate: PropTypes.func,
handleCopy: PropTypes.func,
};

Expand Down
2 changes: 2 additions & 0 deletions types/react-datasheet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ declare namespace ReactDataSheet {
keyFn?: (row: number) => string | number;
/** Optional: Function that can decide whether navigating to the indicated cell is possible. */
isCellNavigable?: (cell: T, row: number, col: number, jumpNext: boolean) => boolean;

customNavigate?: (cell: T, keyCode: number, e: object) => T[];
}

/** A function to process the raw clipboard data. It should return an array of arrays of strings. This is useful for when the clipboard may have data with irregular field or line delimiters. If not set, rows will be split with line breaks and cells with tabs. To wire it up pass your function to the parsePaste property of the ReactDataSheet component. */
Expand Down