Skip to content

Commit

Permalink
fix(Modal): fix modal not closing when keyboard=false (#6515)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang committed Dec 7, 2022
1 parent 847f04a commit 92703d5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/Modal.tsx
Expand Up @@ -391,13 +391,16 @@ const Modal: BsPrefixRefForwardingComponent<'div', ModalProps> =
};

const handleEscapeKeyDown = (e) => {
if (!keyboard && backdrop === 'static') {
// Call preventDefault to stop modal from closing in restart ui,
// then play our animation.
if (keyboard) {
onEscapeKeyDown?.(e);
} else {
// Call preventDefault to stop modal from closing in @restart/ui.
e.preventDefault();
handleStaticModalAnimation();
} else if (keyboard && onEscapeKeyDown) {
onEscapeKeyDown(e);

if (backdrop === 'static') {
// Play static modal animation.
handleStaticModalAnimation();
}
}
};

Expand Down
34 changes: 33 additions & 1 deletion test/ModalSpec.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
import sinon from 'sinon';
import { expect } from 'chai';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import ModalManager from '@restart/ui/ModalManager';
import Modal, { ModalProps } from '../src/Modal';

Expand Down Expand Up @@ -450,6 +450,38 @@ describe('<Modal>', () => {
onEscapeKeyDownSpy.should.not.have.been.called;
});

it('Should not hide modal when keyboard is false', async () => {
function ModalTest() {
const [show, setShow] = React.useState(false);

return (
<>
<Modal show={show} onHide={() => setShow(false)} keyboard={false}>
<strong>Message</strong>
</Modal>
<button type="button" onClick={() => setShow((s) => !s)}>
Button
</button>
</>
);
}

render(<ModalTest />);

// Show the modal.
fireEvent.click(screen.getByRole('button'));

// Escape key.
fireEvent.keyDown(screen.getByRole('dialog'), {
keyCode: 27,
});

// Wait a bit before checking if modal is still there.
await new Promise((r) => setTimeout(r, 500));

expect(screen.queryByRole('dialog')).to.exist;
});

it('Should use custom props manager if specified', (done) => {
class MyModalManager extends ModalManager {
// @ts-ignore
Expand Down

0 comments on commit 92703d5

Please sign in to comment.