Skip to content

Commit

Permalink
fix(react): do not submit request when close button is focused (#9259)
Browse files Browse the repository at this point in the history
  • Loading branch information
atikenny authored Jul 29, 2021
1 parent 36a6fdc commit cb71475
Showing 1 changed file with 21 additions and 4 deletions.
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

0 comments on commit cb71475

Please sign in to comment.