1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-26 15:52:20 +01:00

88 lines
2.5 KiB
TypeScript
Raw Normal View History

/**
* This component is a template for a modal dialog that wraps the application style
*/
2021-02-04 17:00:02 +01:00
import React, { ReactNode, SyntheticEvent } from 'react';
import Modal from 'react-modal';
import { useTranslation } from 'react-i18next';
import { Loader } from './loader';
import CustomAssetAPI from '../api/custom-asset';
2020-11-24 16:26:18 +01:00
import { CustomAssetName } from '../models/custom-asset';
2021-02-04 17:00:02 +01:00
import { FabButton } from './fab-button';
Modal.setAppElement('body');
2020-11-30 16:52:55 +01:00
export enum ModalSize {
small = 'sm',
medium = 'md',
large = 'lg'
}
interface FabModalProps {
title: string,
isOpen: boolean,
toggleModal: () => void,
2020-11-30 16:52:55 +01:00
confirmButton?: ReactNode,
closeButton?: boolean,
className?: string,
width?: ModalSize,
2021-02-04 17:00:02 +01:00
customFooter?: ReactNode,
2021-02-08 15:28:47 +01:00
onConfirm?: (event: SyntheticEvent) => void,
preventConfirm?: boolean
}
2020-11-24 16:26:18 +01:00
const blackLogoFile = CustomAssetAPI.get(CustomAssetName.LogoBlackFile);
2021-02-08 15:28:47 +01:00
export const FabModal: React.FC<FabModalProps> = ({ title, isOpen, toggleModal, children, confirmButton, className, width = 'sm', closeButton, customFooter, onConfirm, preventConfirm }) => {
const { t } = useTranslation('shared');
const blackLogo = blackLogoFile.read();
/**
* Check if the confirm button should be present
*/
const hasConfirmButton = (): boolean => {
return confirmButton !== undefined;
}
2020-11-30 16:52:55 +01:00
/**
* Should we display the close button?
*/
const hasCloseButton = (): boolean => {
return closeButton;
}
/**
* Check if there's a custom footer
*/
const hasCustomFooter = (): boolean => {
return customFooter !== undefined;
}
return (
2021-02-08 15:28:47 +01:00
<Modal isOpen={isOpen}
2020-11-30 16:52:55 +01:00
className={`fab-modal fab-modal-${width} ${className}`}
overlayClassName="fab-modal-overlay"
onRequestClose={toggleModal}>
<div className="fab-modal-header">
<Loader>
<img src={blackLogo.custom_asset_file_attributes.attachment_url}
alt={blackLogo.custom_asset_file_attributes.attachment}
className="modal-logo" />
</Loader>
<h1>{ title }</h1>
</div>
<div className="fab-modal-content">
{children}
</div>
<div className="fab-modal-footer">
<Loader>
2021-02-04 17:00:02 +01:00
{hasCloseButton() &&<FabButton className="modal-btn--close" onClick={toggleModal}>{t('app.shared.buttons.close')}</FabButton>}
2021-02-08 15:28:47 +01:00
{hasConfirmButton() && <FabButton className="modal-btn--confirm" disabled={preventConfirm} onClick={onConfirm}>{confirmButton}</FabButton>}
2020-11-30 16:52:55 +01:00
{hasCustomFooter() && customFooter}
</Loader>
</div>
</Modal>
);
}