accounts-frontend/packages/app/components/ui/Panel.tsx

92 lines
2.4 KiB
TypeScript
Raw Normal View History

import React, { FC, PropsWithChildren, useState, useCallback } from 'react';
2019-12-08 01:13:08 +05:30
import clsx from 'clsx';
2016-08-08 00:48:11 +05:30
import styles from './panel.scss';
import icons from './icons.scss';
interface PanelProps extends PropsWithChildren<any> {
title?: string;
icon?: string;
}
2020-05-24 04:38:24 +05:30
export const Panel: FC<PanelProps> = ({ title, icon, children }) => {
2020-05-24 04:38:24 +05:30
return (
<div className={styles.panel}>
{title && (
<PanelHeader>
{icon && (
<button className={styles.headerControl}>
<span className={icons[icon]} />
</button>
)}
{title}
</PanelHeader>
)}
{children}
2020-05-24 04:38:24 +05:30
</div>
);
};
export const PanelHeader: FC<PropsWithChildren<any>> = ({ children }) => {
2020-05-24 04:38:24 +05:30
return (
<div className={styles.header} data-testid="auth-header">
{children}
2020-05-24 04:38:24 +05:30
</div>
);
};
export const PanelBody: FC<PropsWithChildren<any>> = ({ children }) => {
2020-05-24 04:38:24 +05:30
return (
<div className={styles.body} data-testid="auth-body">
{children}
2020-05-24 04:38:24 +05:30
</div>
);
};
export const PanelFooter: FC<PropsWithChildren<any>> = ({ children }) => {
2020-05-24 04:38:24 +05:30
return (
<div className={styles.footer} data-testid="auth-controls">
{children}
2020-05-24 04:38:24 +05:30
</div>
);
};
interface PanelBodyHeaderProps extends PropsWithChildren<any> {
type?: 'default' | 'error';
onClose?: () => void;
}
export const PanelBodyHeader: FC<PanelBodyHeaderProps> = ({ type = 'default', onClose, children }) => {
const [isClosed, setIsClosed] = useState<boolean>(false);
const handleCloseClick = useCallback(() => {
setIsClosed(true);
onClose?.();
}, [onClose]);
2019-12-07 16:58:52 +05:30
return (
<div
className={clsx({
[styles.defaultBodyHeader]: type === 'default',
[styles.errorBodyHeader]: type === 'error',
[styles.isClosed]: isClosed,
})}
>
{type === 'error' && <span className={styles.close} onClick={handleCloseClick} />}
{children}
</div>
);
};
interface PanelIconProps {
icon: string;
}
export const PanelIcon: FC<PanelIconProps> = ({ icon }) => {
2020-05-24 04:38:24 +05:30
return (
<div className={styles.panelIcon}>
<span className={icons[icon]} />
</div>
);
};