Skip to content

Commit

Permalink
Merge pull request #104 from Lemoncode/feature/#95-inline-edition-ent…
Browse files Browse the repository at this point in the history
…er-key

Add keydown listener for Enter key to submit text
  • Loading branch information
brauliodiez authored Aug 1, 2024
2 parents 4ed0846 + 899da6e commit ef21da7
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/common/components/inline-edit/inline-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ export const EditableComponent: React.FC<Props> = props => {
}
};

const handleKeyDown = (event: KeyboardEvent) => {
if (isEditing && event.key === 'Enter') {
setIsEditing(false);
onTextSubmit(editText);
}
};

if (isEditing) {
inputRef.current?.focus();
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);
} else {
document.removeEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);
}

return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);
};
}, [isEditing, editText]);

Expand Down

0 comments on commit ef21da7

Please sign in to comment.