Skip to content

Commit 2c9c286

Browse files
fix: convert modalCloseButton to Typescript
Co-authored-by: GitHub Copilot <noreply@github.com>
1 parent d12c43e commit 2c9c286

3 files changed

Lines changed: 28 additions & 27 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
import React, { useContext } from 'react';
2-
import PropTypes from 'prop-types';
1+
import {
2+
useContext, ReactNode, ElementType, forwardRef, createElement,
3+
} from 'react';
34
import classNames from 'classnames';
45
import ModalContext from './ModalContext';
56
import Button from '../Button';
67

7-
const ModalCloseButton = React.forwardRef(({ as, children, ...props }, ref) => {
8+
export interface ModalCloseButtonProps {
9+
/** Specifies the base element */
10+
as?: ElementType;
11+
/** Specifies the content of the button */
12+
children?: ReactNode;
13+
/** Specifies class name to append to the base element */
14+
className?: string;
15+
/** Specifies the callback function when the close button is clicked */
16+
onClick?: () => void;
17+
[key: string]: any; // For spreading other props
18+
}
19+
20+
const ModalCloseButton = forwardRef(({
21+
as = Button,
22+
children = null,
23+
className,
24+
onClick,
25+
...props
26+
}: ModalCloseButtonProps, ref: React.Ref<HTMLButtonElement>) => {
827
const { onClose } = useContext(ModalContext);
928
const type = as;
1029
const componentProps = {
1130
...props,
12-
className: classNames('pgn__modal-close-button', props.className),
31+
className: classNames('pgn__modal-close-button', className),
1332
onClick: () => {
1433
onClose();
15-
if (props.onClick) {
16-
props.onClick();
34+
if (onClick) {
35+
onClick();
1736
}
1837
},
1938
ref,
2039
};
2140

2241
// Use the non-jsx syntax to create this element so we can more
2342
// finely control the component type (defaulted to Button via defaultProps)
24-
return React.createElement(type, componentProps, children);
43+
return createElement(type, componentProps, children);
2544
});
2645

27-
ModalCloseButton.propTypes = {
28-
/** Specifies the base element */
29-
as: PropTypes.elementType,
30-
/** Specifies the content of the button */
31-
children: PropTypes.node,
32-
/** Specifies class name to append to the base element */
33-
className: PropTypes.string,
34-
/** Specifies the callback function when the close button is clicked */
35-
onClick: PropTypes.func,
36-
};
37-
38-
ModalCloseButton.defaultProps = {
39-
as: Button,
40-
onClick: undefined,
41-
className: undefined,
42-
children: null,
43-
};
46+
ModalCloseButton.displayName = 'ModalCloseButton';
4447

4548
export default ModalCloseButton;

src/Modal/ModalDialog.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import classNames from 'classnames';
33
import { useMediaQuery } from 'react-responsive';
44
import { useIntl } from 'react-intl';
55
import ModalLayer from './ModalLayer';
6-
// @ts-ignore for now - this needs to be converted to TypeScript
76
import ModalCloseButton from './ModalCloseButton';
87
import ModalDialogHeader from './ModalDialogHeader';
98
// @ts-ignore for now - this needs to be converted to TypeScript

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export {
3838
export { default as Hyperlink } from './Hyperlink';
3939
export { default as Icon } from './Icon';
4040
export { default as IconButton, IconButtonWithTooltip } from './IconButton';
41+
export { default as ModalCloseButton } from './Modal/ModalCloseButton';
4142
export { default as ModalContext } from './Modal/ModalContext';
4243
export { default as ModalDialog } from './Modal/ModalDialog';
4344
export { default as ModalLayer } from './Modal/ModalLayer';
@@ -109,8 +110,6 @@ export { default as MenuItem } from './Menu/MenuItem';
109110
// @ts-ignore: has yet to be converted to TypeScript
110111
export { default as SelectMenu, SELECT_MENU_DEFAULT_MESSAGE } from './Menu/SelectMenu';
111112
// @ts-ignore: has yet to be converted to TypeScript
112-
export { default as ModalCloseButton } from './Modal/ModalCloseButton';
113-
// @ts-ignore: has yet to be converted to TypeScript
114113
export { default as FullscreenModal, FULLSCREEN_MODAL_CLOSE_LABEL } from './Modal/FullscreenModal';
115114
// @ts-ignore: has yet to be converted to TypeScript
116115
export { default as MarketingModal } from './Modal/MarketingModal';

0 commit comments

Comments
 (0)