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

[Table/EditableCell] Type in a cell to edit it now #1760

Merged
merged 3 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/table/src/cell/cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export interface ICellProps extends IIntentProps, IProps {
*/
onKeyUp?: React.KeyboardEventHandler<HTMLElement>;

/**
* Callback invoked when a character-key is pressed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need trailing period for consistency.

*/
onKeyPress?: React.KeyboardEventHandler<HTMLElement>;

/**
* A ref handle to capture the outer div of this cell. Used internally.
*/
Expand Down Expand Up @@ -108,6 +113,7 @@ export class Cell extends React.Component<ICellProps, {}> {
tabIndex,
onKeyDown,
onKeyUp,
onKeyPress,
style,
intent,
interactive,
Expand Down Expand Up @@ -163,7 +169,12 @@ export class Cell extends React.Component<ICellProps, {}> {
const content = <div className={textClasses}>{modifiedChildren}</div>;

return (
<div className={classes} title={tooltip} ref={cellRef} {...{ style, tabIndex, onKeyDown, onKeyUp }}>
<div
className={classes}
title={tooltip}
ref={cellRef}
{...{ style, tabIndex, onKeyDown, onKeyUp, onKeyPress }}
>
<LoadableContent loading={loading} variableLength={true}>
{content}
</LoadableContent>
Expand Down
18 changes: 16 additions & 2 deletions packages/table/src/cell/editableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class EditableCell extends React.Component<IEditableCellProps, IEditableC
onConfirm={this.handleConfirm}
onEdit={this.handleEdit}
placeholder=""
selectAllOnFocus={true}
selectAllOnFocus={false}
value={dirtyValue}
/>
);
Expand All @@ -134,7 +134,13 @@ export class EditableCell extends React.Component<IEditableCellProps, IEditableC
}

return (
<Cell {...spreadableProps} truncated={false} interactive={interactive} cellRef={this.refHandlers.cell}>
<Cell
{...spreadableProps}
truncated={false}
interactive={interactive}
cellRef={this.refHandlers.cell}
onKeyPress={this.handleKeyPress}
>
<Draggable
onActivate={this.handleCellActivate}
onDoubleClick={this.handleCellDoubleClick}
Expand Down Expand Up @@ -168,6 +174,14 @@ export class EditableCell extends React.Component<IEditableCellProps, IEditableC
}
}

private handleKeyPress = () => {
if (this.state.isEditing || !this.props.isFocused) {
return;
}
// setting dirty value to empty string because apparently the text field will pick up the key and write it in there
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 123 characters long. Curious why prettier didn't catch this. Can we line-wrap comments to 80 for readability?

Also, can you elaborate a little more on this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it wasn't caught. Basically, apparently, you don't need to insert the character yourself. When switched over to an empty text input (or non-empty one, technically), the bubbling up to the top of the browser will apparently get it to write a character. This is... incredibly weird, but, clearly what is happening, and I'm much happier to let chrome type the character than hope that we got the right one out of the keyboard event.

this.setState({ isEditing: true, dirtyValue: "", savedValue: this.state.savedValue });
};

private handleEdit = () => {
this.setState({ isEditing: true, dirtyValue: this.state.savedValue });
};
Expand Down