|
1 | | -import React, { useContext } from 'react'; |
2 | | -import PropTypes from 'prop-types'; |
| 1 | +import { |
| 2 | + useContext, ReactNode, ElementType, forwardRef, createElement, |
| 3 | +} from 'react'; |
3 | 4 | import classNames from 'classnames'; |
4 | 5 | import ModalContext from './ModalContext'; |
5 | 6 | import Button from '../Button'; |
6 | 7 |
|
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>) => { |
8 | 27 | const { onClose } = useContext(ModalContext); |
9 | 28 | const type = as; |
10 | 29 | const componentProps = { |
11 | 30 | ...props, |
12 | | - className: classNames('pgn__modal-close-button', props.className), |
| 31 | + className: classNames('pgn__modal-close-button', className), |
13 | 32 | onClick: () => { |
14 | 33 | onClose(); |
15 | | - if (props.onClick) { |
16 | | - props.onClick(); |
| 34 | + if (onClick) { |
| 35 | + onClick(); |
17 | 36 | } |
18 | 37 | }, |
19 | 38 | ref, |
20 | 39 | }; |
21 | 40 |
|
22 | 41 | // Use the non-jsx syntax to create this element so we can more |
23 | 42 | // finely control the component type (defaulted to Button via defaultProps) |
24 | | - return React.createElement(type, componentProps, children); |
| 43 | + return createElement(type, componentProps, children); |
25 | 44 | }); |
26 | 45 |
|
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'; |
44 | 47 |
|
45 | 48 | export default ModalCloseButton; |
0 commit comments