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

127 lines
3.1 KiB
TypeScript
Raw Normal View History

2019-12-18 01:50:19 +01:00
import React from 'react';
2020-01-22 04:09:34 +01:00
import classNames from 'classnames';
2019-12-18 01:50:19 +01:00
import { SessionIconButton, SessionIconSize, SessionIconType } from './icon/';
2020-01-22 04:28:29 +01:00
import { SessionButtonColor, SessionButtonType } from './SessionButton';
2019-12-18 01:50:19 +01:00
interface Props {
title: string;
onClose: any;
onOk: any;
2019-12-30 07:38:28 +01:00
showExitIcon?: boolean;
showHeader?: boolean;
2020-01-22 04:09:34 +01:00
headerReverse?: boolean;
2020-01-22 04:28:29 +01:00
//Maximum of two icons or buttons in header
headerIconButtons?: Array<{
2020-01-22 04:09:34 +01:00
iconType: SessionIconType;
iconRotation: number;
onClick?: any;
}>;
headerButtons?: Array<{
buttonType: SessionButtonType;
buttonColor: SessionButtonColor;
text: string;
onClick?: any;
}>;
2019-12-18 01:50:19 +01:00
}
interface State {
isVisible: boolean;
}
2020-01-03 01:48:55 +01:00
export class SessionModal extends React.PureComponent<Props, State> {
2019-12-30 07:38:28 +01:00
public static defaultProps = {
showExitIcon: true,
showHeader: true,
2020-01-22 04:09:34 +01:00
headerReverse: false,
2019-12-30 07:38:28 +01:00
};
2020-01-03 01:48:55 +01:00
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() {
2020-01-22 04:28:29 +01:00
const {
title,
headerIconButtons,
showExitIcon,
showHeader,
headerReverse,
} = this.props;
const { isVisible } = this.state;
return isVisible ? (
2019-12-30 07:38:28 +01:00
<div className={'session-modal'}>
2020-01-03 01:48:55 +01:00
{showHeader ? (
2019-12-30 07:38:28 +01:00
<>
2020-01-22 04:28:29 +01:00
<div
className={classNames(
'session-modal__header',
headerReverse && 'reverse'
)}
>
2019-12-30 07:38:28 +01:00
<div className="session-modal__header__close">
{showExitIcon ? (
<SessionIconButton
iconType={SessionIconType.Exit}
iconSize={SessionIconSize.Small}
onClick={this.close}
/>
2020-01-03 01:48:55 +01:00
) : null}
2019-12-30 07:38:28 +01:00
</div>
<div className="session-modal__header__title">{title}</div>
<div className="session-modal__header__icons">
{headerIconButtons
? headerIconButtons.map((iconItem: any) => {
return (
<SessionIconButton
2020-01-22 04:09:34 +01:00
key={iconItem.iconType}
iconType={iconItem.iconType}
iconSize={SessionIconSize.Large}
iconRotation={iconItem.iconRotation}
onClick={iconItem.onClick}
2019-12-30 07:38:28 +01:00
/>
);
})
: null}
</div>
</div>
</>
2020-01-03 01:48:55 +01:00
) : 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);
if (this.props.onClose) {
this.props.onClose();
}
}
2019-12-22 23:44:18 +01:00
public onKeyUp(event: any) {
switch (event.key) {
case 'Esc':
case 'Escape':
this.close();
break;
default:
}
2019-12-18 01:50:19 +01:00
}
}