Skip to content

Commit

Permalink
removed dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliodiez committed Aug 2, 2024
1 parent 30c15b0 commit 1662f6f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/common/components/inline-edit/inline-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const EditableComponent: React.FC<Props> = props => {
const [editText, setEditText] = useState(text);

const inputRef = useRef<HTMLInputElement>(null);
const hasTextSelected = useRef(false);

// handle click outside of the input when editing
useEffect(() => {
Expand All @@ -35,25 +34,20 @@ export const EditableComponent: React.FC<Props> = props => {
!inputRef.current.contains(event.target as Node)
) {
setIsEditing(false);
onTextSubmit(editText);
hasTextSelected.current = false;
onTextSubmit(inputRef.current?.value || '');
}
};

const handleKeyDown = (event: KeyboardEvent) => {
if (isEditing && event.key === 'Enter') {
setIsEditing(false);
onTextSubmit(editText);
hasTextSelected.current = false;
onTextSubmit(inputRef.current?.value || '');
}
};

if (isEditing) {
if (!hasTextSelected.current) {
inputRef.current?.focus();
inputRef.current?.select();
hasTextSelected.current = true;
}
inputRef.current?.focus();
inputRef.current?.select();
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);
} else {
Expand All @@ -65,7 +59,7 @@ export const EditableComponent: React.FC<Props> = props => {
document.removeEventListener('mousedown', handleClickOutside);
document.addEventListener('keydown', handleKeyDown);
};
}, [isEditing, editText]);
}, [isEditing]);

const handleDoubleClick = () => {
if (isEditable) {
Expand Down

0 comments on commit 1662f6f

Please sign in to comment.