diff --git a/packages/react/src/components/ModalWrapper/ModalWrapper.js b/packages/react/src/components/ModalWrapper/ModalWrapper.tsx similarity index 65% rename from packages/react/src/components/ModalWrapper/ModalWrapper.js rename to packages/react/src/components/ModalWrapper/ModalWrapper.tsx index 2d144e2b1df9..7e1b73e9b2e2 100644 --- a/packages/react/src/components/ModalWrapper/ModalWrapper.js +++ b/packages/react/src/components/ModalWrapper/ModalWrapper.tsx @@ -6,16 +6,50 @@ */ import PropTypes from 'prop-types'; -import React from 'react'; +import React, { ReactNode } from 'react'; import Modal from '../Modal'; import Button from '../Button'; -import { ButtonKinds } from '../../prop-types/types'; import { warning } from '../../internal/warning'; +import { ButtonKinds } from '../Button'; import { noopFn } from '../../internal/noopFn'; +import { keys, match } from '../../internal/keyboard'; + +interface ModalWrapperProps { + buttonTriggerClassName?: string; + buttonTriggerText?: ReactNode; + children?: ReactNode; + disabled?: boolean; + handleOpen?: React.MouseEventHandler; + handleSubmit?: React.ReactEventHandler; + id?: string; + modalBeforeContent?: boolean; + modalHeading?: string; + modalLabel?: string; + modalText?: string; + onKeyDown?: React.KeyboardEventHandler; + passiveModal?: boolean; + preventCloseOnClickOutside?: boolean; + primaryButtonText?: string; + renderTriggerButtonIcon: React.ElementType; + secondaryButtonText?: string; + selectorPrimaryFocus?: string; + shouldCloseAfterSubmit?: boolean; + status?: string; + triggerButtonIconDescription?: string; + triggerButtonKind: (typeof ButtonKinds)[number]; + withHeader?: boolean; +} + +interface ModelWrapperState { + isOpen: boolean; +} let didWarnAboutDeprecation = false; -export default class ModalWrapper extends React.Component { +export default class ModalWrapper extends React.Component< + ModalWrapperProps, + ModelWrapperState +> { if(__DEV__) { warning( didWarnAboutDeprecation, @@ -53,8 +87,8 @@ export default class ModalWrapper extends React.Component { withHeader: PropTypes.bool, }; - triggerButton = React.createRef(); - modal = React.createRef(); + triggerButton = React.createRef(); + modal = React.createRef(); state = { isOpen: false, @@ -66,31 +100,31 @@ export default class ModalWrapper extends React.Component { }); }; - handleClose = (evt) => { - const innerModal = this.modal.current.querySelector('div'); + handleClose = (evt: React.KeyboardEvent) => { + const innerModal = this.modal.current?.querySelector('div'); if ( this.modal.current && evt && - !innerModal.contains(evt.target) && + !innerModal?.contains(evt.target as Node) && this.props.preventCloseOnClickOutside ) { return; } else { this.setState({ isOpen: false }, () => - this.triggerButton.current.focus() + this.triggerButton.current?.focus() ); } }; - handleOnRequestSubmit = () => { + handleOnRequestSubmit = (evt: React.KeyboardEvent) => { const { handleSubmit, shouldCloseAfterSubmit } = this.props; if (handleSubmit && shouldCloseAfterSubmit) { - handleSubmit(); - this.handleClose(); + handleSubmit(evt); + this.handleClose(evt); } - handleSubmit(); + handleSubmit?.(evt); }; render() { @@ -105,10 +139,10 @@ export default class ModalWrapper extends React.Component { triggerButtonIconDescription = 'Provide icon description if icon is used', triggerButtonKind = 'primary', disabled = false, - handleSubmit, // eslint-disable-line no-unused-vars - shouldCloseAfterSubmit = true, // eslint-disable-line no-unused-vars + handleSubmit, // eslint-disable-line @typescript-eslint/no-unused-vars + shouldCloseAfterSubmit = true, // eslint-disable-line @typescript-eslint/no-unused-vars selectorPrimaryFocus = '[data-modal-primary-focus]', - preventCloseOnClickOutside = false, // eslint-disable-line no-unused-vars + preventCloseOnClickOutside = false, // eslint-disable-line @typescript-eslint/no-unused-vars ...other } = this.props; @@ -124,8 +158,8 @@ export default class ModalWrapper extends React.Component {
{ - if (evt.which === 27) { - this.handleClose(); + if (match(evt, keys.Escape)) { + this.handleClose(evt); onKeyDown(evt); } }}> diff --git a/packages/react/src/components/ModalWrapper/index.js b/packages/react/src/components/ModalWrapper/index.ts similarity index 100% rename from packages/react/src/components/ModalWrapper/index.js rename to packages/react/src/components/ModalWrapper/index.ts