Skip to content

Commit

Permalink
fix(EditInPlace): removes focus when pressing esc or enter key (#5943)
Browse files Browse the repository at this point in the history
* fix(EditInPlace): removes focus when pressing esc or enter key

* fix: remove focus state on functions
  • Loading branch information
AlexanderMelox authored Aug 28, 2024
1 parent 7ab472a commit 5eff024
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/ibm-products/src/components/EditInPlace/EditInPlace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,11 @@ export let EditInPlace = forwardRef<HTMLDivElement, EditInplaceProps>(

const onSaveHandler = () => {
setInitialValue(value);
setFocused(false);
setDirtyInput(false);
onSave();
};

const onCancelHandler = () => {
setFocused(false);
setDirtyInput(false);
onCancel(initialValue);
};
Expand Down Expand Up @@ -248,16 +246,21 @@ export let EditInPlace = forwardRef<HTMLDivElement, EditInplaceProps>(
onCancelHandler();
};

const removeFocus = () => {
inputRef.current?.blur();
setFocused(false);
};

const onKeyHandler = (e) => {
// to prevent blur handler from being called twice add additional state to check if escape is being used
escaping.current = true;
switch (e.key) {
case 'Escape':
inputRef.current?.blur();
removeFocus();
escapeHandler();
break;
case 'Enter':
inputRef.current?.blur();
removeFocus();
returnHandler();
break;
default:
Expand Down

0 comments on commit 5eff024

Please sign in to comment.