|
1 | | -import { FC } from 'react' |
| 1 | +import { FC, ReactNode } from 'react' |
2 | 2 | import Modal, { ModalProps } from 'react-responsive-modal' |
3 | 3 | import classNames from 'classnames' |
4 | 4 |
|
| 5 | +import { LoadingSpinner } from '../../loading-spinner' |
5 | 6 | import { IconOutline } from '../../svgs' |
6 | 7 |
|
| 8 | +import { ModalContentResponse, useFetchModalContent } from './use-fetch-modal-content' |
7 | 9 | import styles from './BaseModal.module.scss' |
8 | 10 |
|
9 | 11 | export interface BaseModalProps extends ModalProps { |
| 12 | + contentClassName?: string |
| 13 | + contentUrl?: string |
10 | 14 | size?: 'lg' | 'md' |
11 | 15 | title: string |
12 | 16 | } |
13 | 17 |
|
14 | | -const BaseModal: FC<BaseModalProps> = ({ |
15 | | - children, |
16 | | - title, |
17 | | - ...props |
18 | | -}: BaseModalProps) => ( |
19 | | - <Modal |
20 | | - {...props} |
21 | | - classNames={{ modal: `modal-${props.size || 'md'}` }} |
22 | | - closeIcon={<IconOutline.XIcon width={28} height={28} />} |
23 | | - > |
24 | | - <div className={styles['modal-header']}> |
25 | | - <h3>{title}</h3> |
26 | | - </div> |
27 | | - |
28 | | - <hr className={styles.spacer} /> |
29 | | - |
30 | | - <div className={classNames(styles['modal-body'], 'modal-body')}> |
31 | | - {children} |
32 | | - </div> |
33 | | - </Modal> |
34 | | -) |
| 18 | +const BaseModal: FC<BaseModalProps> = (props: BaseModalProps) => { |
| 19 | + |
| 20 | + const { content }: ModalContentResponse = useFetchModalContent(props.contentUrl, props.open) |
| 21 | + |
| 22 | + const renterContent: () => ReactNode = () => { |
| 23 | + if (props.children || !props.contentUrl) { |
| 24 | + return undefined |
| 25 | + } |
| 26 | + |
| 27 | + if (!content) { |
| 28 | + return <LoadingSpinner /> |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <div |
| 33 | + className={props.contentClassName} |
| 34 | + dangerouslySetInnerHTML={{ __html: content }} |
| 35 | + /> |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <Modal |
| 41 | + {...props} |
| 42 | + classNames={{ modal: `modal-${props.size || 'md'}` }} |
| 43 | + closeIcon={<IconOutline.XIcon width={28} height={28} />} |
| 44 | + > |
| 45 | + <div className={styles['modal-header']}> |
| 46 | + <h3>{props.title}</h3> |
| 47 | + </div> |
| 48 | + |
| 49 | + <hr className={styles.spacer} /> |
| 50 | + |
| 51 | + <div className={classNames(styles['modal-body'], 'modal-body')}> |
| 52 | + {renterContent()} |
| 53 | + {props.children} |
| 54 | + </div> |
| 55 | + </Modal> |
| 56 | + ) |
| 57 | +} |
35 | 58 |
|
36 | 59 | export default BaseModal |
0 commit comments