Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Modal): fix modal not closing when keyboard=false #6515

Merged
merged 1 commit into from Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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