Skip to content

Commit

Permalink
feat(usebruno#1222): trigger modal's handleConfirm on ENTER key down (u…
Browse files Browse the repository at this point in the history
…sebruno#1223)

* feat(usebruno#1222): trigger modal's handleConfirm on ENTER key down

* Update index.js

---------

Co-authored-by: Anoop M D <[email protected]>
  • Loading branch information
juprem and helloanoop authored Sep 15, 2024
1 parent 1950181 commit 4419634
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/bruno-app/src/components/Modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { useEffect, useState, useRef } from 'react';
import StyledWrapper from './StyledWrapper';
import useFocusTrap from 'hooks/useFocusTrap';

const ESC_KEY_CODE = 27;
const ENTER_KEY_CODE = 13;

const ModalHeader = ({ title, handleCancel, customHeader, hideClose }) => (
<div className="bruno-modal-header">
{customHeader ? customHeader : <>{title ? <div className="bruno-modal-header-title">{title}</div> : null}</>}
Expand Down Expand Up @@ -72,10 +75,17 @@ const Modal = ({
}) => {
const modalRef = useRef(null);
const [isClosing, setIsClosing] = useState(false);
const escFunction = (event) => {
const escKeyCode = 27;
if (event.keyCode === escKeyCode) {
closeModal({ type: 'esc' });

const handleKeydown = ({ keyCode }) => {
switch (keyCode) {
case ESC_KEY_CODE: {
return closeModal({ type: 'esc' });
}
case ENTER_KEY_CODE: {
if(handleConfirm) {
return handleConfirm();
}
}
}
};

Expand All @@ -88,9 +98,9 @@ const Modal = ({

useEffect(() => {
if (disableEscapeKey) return;
document.addEventListener('keydown', escFunction, false);
document.addEventListener('keydown', handleKeydown, false);
return () => {
document.removeEventListener('keydown', escFunction, false);
document.removeEventListener('keydown', handleKeydown);
};
}, [disableEscapeKey, document]);

Expand Down

0 comments on commit 4419634

Please sign in to comment.