accounts-frontend/src/components/ui/Panel.js

124 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-08-23 00:09:08 +05:30
// @flow
2019-06-09 13:59:54 +05:30
import type { Node } from 'react';
2017-08-23 00:09:08 +05:30
import React, { Component } from 'react';
import classNames from 'classnames';
2016-08-08 00:48:11 +05:30
import { omit } from 'functions';
import styles from './panel.scss';
import icons from './icons.scss';
2017-08-23 00:09:08 +05:30
export function Panel(props: {
title?: string,
icon?: string,
children: Node,
2017-08-23 00:09:08 +05:30
}) {
let { title, icon } = props;
if (icon) {
icon = (
<button className={styles.headerControl}>
<span className={icons[icon]} />
</button>
);
}
if (title) {
title = (
<PanelHeader>
{icon}
{title}
</PanelHeader>
);
}
return (
<div className={styles.panel}>
{title}
{props.children}
</div>
);
}
export function PanelHeader(props: { children: * }) {
return (
<div className={styles.header} {...props}>
{props.children}
</div>
);
}
export function PanelBody(props: { children: * }) {
return (
<div className={styles.body} {...props}>
{props.children}
</div>
);
}
export function PanelFooter(props: { children: * }) {
return (
<div className={styles.footer} {...props}>
{props.children}
</div>
);
}
export class PanelBodyHeader extends Component<
{
type: 'default' | 'error',
2017-08-23 02:01:41 +05:30
onClose: Function,
children: *,
},
{
isClosed: boolean,
},
> {
state: {
isClosed: boolean,
} = {
isClosed: false,
};
render() {
const { type = 'default', children } = this.props;
let close;
if (type === 'error') {
close = <span className={styles.close} onClick={this.onClose} />;
}
const className = classNames(styles[`${type}BodyHeader`], {
[styles.isClosed]: this.state.isClosed,
});
const extraProps = omit(this.props, ['type', 'onClose']);
2017-08-23 00:09:08 +05:30
return (
<div className={className} {...extraProps}>
{close}
{children}
</div>
2017-08-23 00:09:08 +05:30
);
}
onClose = (event: MouseEvent) => {
event.preventDefault();
this.setState({ isClosed: true });
this.props.onClose();
};
}
export function PanelIcon({ icon }: { icon: string }) {
return (
<div className={styles.panelIcon}>
<span className={icons[icon]} />
</div>
);
2017-08-23 00:09:08 +05:30
}