122 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-12-07 13:28:52 +02:00
import React from 'react';
2019-12-07 21:43:08 +02:00
import clsx from 'clsx';
import { omit } from 'app/functions';
2016-08-07 22:18:11 +03:00
import styles from './panel.scss';
import icons from './icons.scss';
2020-05-24 02:08:24 +03:00
export function Panel(props: { title?: string; icon?: string; children: React.ReactNode }) {
const { title: titleText, icon: iconType } = props;
let icon: React.ReactElement | undefined;
let title: React.ReactElement | undefined;
if (iconType) {
icon = (
<button className={styles.headerControl}>
<span className={icons[iconType]} />
</button>
);
}
2020-05-24 02:08:24 +03:00
if (titleText) {
title = (
<PanelHeader>
{icon}
{titleText}
</PanelHeader>
);
}
return (
<div className={styles.panel}>
{title}
2020-05-24 02:08:24 +03:00
{props.children}
</div>
);
}
2019-12-07 13:28:52 +02:00
export function PanelHeader(props: { children: React.ReactNode }) {
2020-05-24 02:08:24 +03:00
return (
<div className={styles.header} {...props} data-testid="auth-header">
{props.children}
</div>
);
}
2019-12-07 13:28:52 +02:00
export function PanelBody(props: { children: React.ReactNode }) {
2020-05-24 02:08:24 +03:00
return (
<div className={styles.body} {...props} data-testid="auth-body">
{props.children}
</div>
);
}
2019-12-07 13:28:52 +02:00
export function PanelFooter(props: { children: React.ReactNode }) {
2020-05-24 02:08:24 +03:00
return (
<div className={styles.footer} {...props} data-testid="auth-controls">
{props.children}
</div>
);
}
2019-12-07 13:28:52 +02:00
export class PanelBodyHeader extends React.Component<
2020-05-24 02:08:24 +03:00
{
type?: 'default' | 'error';
onClose?: () => void;
children: React.ReactNode;
},
{
isClosed: boolean;
}
> {
2020-05-24 02:08:24 +03:00
state: {
isClosed: boolean;
} = {
isClosed: false,
};
2020-05-24 02:08:24 +03:00
render() {
const { type = 'default', children } = this.props;
2020-05-24 02:08:24 +03:00
let close;
2020-05-24 02:08:24 +03:00
if (type === 'error') {
close = <span className={styles.close} onClick={this.onClose} />;
}
2020-05-24 02:08:24 +03:00
const className = clsx(styles[`${type}BodyHeader`], {
[styles.isClosed]: this.state.isClosed,
});
2020-05-24 02:08:24 +03:00
const extraProps = omit(this.props, ['type', 'onClose']);
2017-08-22 21:39:08 +03:00
2020-05-24 02:08:24 +03:00
return (
<div className={className} {...extraProps}>
{close}
{children}
</div>
);
}
2020-05-24 02:08:24 +03:00
onClose = (event: React.MouseEvent<HTMLElement>) => {
event.preventDefault();
2020-05-24 02:08:24 +03:00
const { onClose } = this.props;
2019-12-07 13:28:52 +02:00
2020-05-24 02:08:24 +03:00
this.setState({ isClosed: true });
2020-05-24 02:08:24 +03:00
if (onClose) {
onClose();
}
};
}
export function PanelIcon({ icon }: { icon: string }) {
2020-05-24 02:08:24 +03:00
return (
<div className={styles.panelIcon}>
<span className={icons[icon]} />
</div>
);
2017-08-22 21:39:08 +03:00
}