Skip to content

Commit 488aac2

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

2 files changed

Lines changed: 28 additions & 26 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/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { default as Bubble } from './Bubble';
1212
export { default as Button, ButtonGroup, ButtonToolbar } from './Button';
1313
export { default as Chip, CHIP_PGN_CLASS } from './Chip';
1414
export { default as ChipCarousel } from './ChipCarousel';
15+
export { default as CloseButton } from './CloseButton';
1516
export { default as Container, type ContainerSize } from './Container';
1617
export {
1718
default as Form,
@@ -76,8 +77,6 @@ export {
7677
// @ts-ignore: has yet to be converted to TypeScript
7778
} from './Carousel';
7879
// @ts-ignore: has yet to be converted to TypeScript
79-
export { default as CloseButton } from './CloseButton';
80-
// @ts-ignore: has yet to be converted to TypeScript
8180
export { default as Layout, Col, Row } from './Layout';
8281
// @ts-ignore: has yet to be converted to TypeScript
8382
export { default as Collapse } from './Collapse';

0 commit comments

Comments
 (0)