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(react): do not submit request when close button is focused #9259

Merged
merged 6 commits into from
Jul 29, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions packages/react/src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export default class Modal extends Component {
};

button = React.createRef();
secondaryButton = React.createRef();
outerModal = React.createRef();
innerModal = React.createRef();
startTrap = React.createRef();
Expand All @@ -234,13 +235,26 @@ export default class Modal extends Component {
modalLabelId = `${prefix}--modal-header__label--${this.modalInstanceId}`;
modalHeadingId = `${prefix}--modal-header__heading--${this.modalInstanceId}`;
modalBodyId = `${prefix}--modal-body--${this.modalInstanceId}`;
modalCloseButtonClass = `${prefix}--modal-close`;

isCloseButton = (element) => {
return (
(!this.props.onSecondarySubmit &&
element === this.secondaryButton.current) ||
element.classList.contains(this.modalCloseButtonClass)
);
};

handleKeyDown = (evt) => {
if (this.props.open) {
if (evt.which === 27) {
this.props.onRequestClose(evt);
}
if (evt.which === 13 && this.props.shouldSubmitOnEnter) {
if (
evt.which === 13 &&
this.props.shouldSubmitOnEnter &&
!this.isCloseButton(evt.target)
) {
this.props.onRequestSubmit(evt);
}
}
Expand Down Expand Up @@ -397,15 +411,15 @@ export default class Modal extends Component {

const modalButton = (
<button
className={`${prefix}--modal-close`}
className={this.modalCloseButtonClass}
type="button"
onClick={onRequestClose}
title={iconDescription}
aria-label={iconDescription}
ref={this.button}>
<Close20
aria-label={iconDescription}
className={`${prefix}--modal-close__icon`}
className={`${this.modalCloseButtonClass}__icon`}
/>
</button>
);
Expand Down Expand Up @@ -449,7 +463,10 @@ export default class Modal extends Component {
}
if (secondaryButtonText) {
return (
<Button kind="secondary" onClick={onSecondaryButtonClick}>
<Button
kind="secondary"
onClick={onSecondaryButtonClick}
ref={this.secondaryButton}>
{secondaryButtonText}
</Button>
);
Expand Down