session-desktop/ts/components/session/SessionModal.tsx

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-12-18 01:50:19 +01:00
import React from 'react';
import { SessionIconButton, SessionIconSize, SessionIconType } from './icon/';
interface Props {
title: string;
onClose: any;
onOk: any;
2019-12-30 07:38:28 +01:00
showExitIcon?: boolean;
showHeader?: boolean;
//Maximum of two icons in header
headerIconButtons?: Array<{
type: SessionIconType;
onClick?: any;
}>;
2019-12-18 01:50:19 +01:00
}
interface State {
isVisible: boolean;
}
2019-12-30 07:38:28 +01:00
export class SessionModal extends React.PureComponent<Props, State> {
public static defaultProps = {
showExitIcon: true,
showHeader: true,
};
2019-12-18 01:50:19 +01:00
constructor(props: any) {
super(props);
this.state = {
isVisible: true,
};
this.close = this.close.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
window.addEventListener('keyup', this.onKeyUp);
2019-12-18 01:50:19 +01:00
}
public render() {
2019-12-30 07:38:28 +01:00
const { title, headerIconButtons, showExitIcon, showHeader } = this.props;
const { isVisible } = this.state;
return isVisible ? (
2019-12-30 07:38:28 +01:00
<div className={'session-modal'}>
{ showHeader ? (
<>
<div className="session-modal__header">
<div className="session-modal__header__close">
{showExitIcon ? (
<SessionIconButton
iconType={SessionIconType.Exit}
iconSize={SessionIconSize.Small}
onClick={this.close}
/>
) : null }
</div>
<div className="session-modal__header__title">{title}</div>
<div className="session-modal__header__icons">
{headerIconButtons
? headerIconButtons.map((iconItem: any) => {
return (
<SessionIconButton
key={iconItem.type}
iconType={iconItem.type}
iconSize={SessionIconSize.Medium}
/>
);
})
: null}
</div>
</div>
</>
) : null }
2019-12-22 23:44:18 +01:00
<div className="session-modal__body">{this.props.children}</div>
</div>
) : null;
}
public close() {
this.setState({
isVisible: false,
});
window.removeEventListener('keyup', this.onKeyUp);
this.props.onClose();
}
2019-12-22 23:44:18 +01:00
public onKeyUp(event: any) {
switch (event.key) {
case 'Enter':
this.props.onOk();
break;
case 'Esc':
case 'Escape':
this.close();
break;
default:
}
2019-12-18 01:50:19 +01:00
}
}