From 4419634db755c028c9638f45b517b02faebbd81b Mon Sep 17 00:00:00 2001 From: juprem <134919753+juprem@users.noreply.github.com> Date: Sun, 15 Sep 2024 22:05:44 +0200 Subject: [PATCH] feat(#1222): trigger modal's handleConfirm on ENTER key down (#1223) * feat(#1222): trigger modal's handleConfirm on ENTER key down * Update index.js --------- Co-authored-by: Anoop M D --- .../bruno-app/src/components/Modal/index.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/bruno-app/src/components/Modal/index.js b/packages/bruno-app/src/components/Modal/index.js index 0ec1aca08..413856eaf 100644 --- a/packages/bruno-app/src/components/Modal/index.js +++ b/packages/bruno-app/src/components/Modal/index.js @@ -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 }) => (
{customHeader ? customHeader : <>{title ?
{title}
: null}} @@ -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(); + } + } } }; @@ -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]);