This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaseModal.tsx
83 lines (69 loc) · 2.34 KB
/
BaseModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as React from 'react';
import {connect} from 'react-redux';
import {Dispatch} from 'redux';
import IAction from '../../stores/IAction';
import IStore from '../../stores/IStore';
import ModalAction from '../../stores/modal/ModalAction';
import KeyboardKeyEnum from '../../constants/KeyboardKeyEnum';
import * as classNames from 'classnames';
interface IProps {
isRequired?: boolean;
}
interface IState {}
interface IStateToProps {}
interface IDispatchToProps {
dispatch: (action: IAction<any>) => void;
}
const mapStateToProps = (state: IStore) => ({});
const mapDispatchToProps = (dispatch: Dispatch<IAction<any>>): IDispatchToProps => ({
dispatch,
});
type PropsUnion = IStateToProps & IDispatchToProps & IProps;
class BaseModal extends React.Component<PropsUnion, IState> {
public static defaultProps: Partial<PropsUnion> = {
isRequired: false,
};
public componentDidMount(): void {
if (!this.props.isRequired) {
global.window.addEventListener('keydown', this._onKeyDownModal);
}
}
public componentWillUnmount(): void {
if (!this.props.isRequired) {
global.window.removeEventListener('keydown', this._onKeyDownModal);
}
}
public render(): JSX.Element {
return (
<div
className="modal"
role="alert"
aria-live="polite"
>
<div
className={this._buildModalOverlayClassNames()}
onClick={this._onClickOverlay}
/>
{this.props.children}
</div>
);
}
private _onClickOverlay = (event: React.MouseEvent<HTMLElement>): void => {
if (!this.props.isRequired) {
this.props.dispatch(ModalAction.closeModal());
}
}
private _onKeyDownModal = (event: KeyboardEvent): void => {
if (event.key === KeyboardKeyEnum.ESCAPE) {
event.preventDefault();
this.props.dispatch(ModalAction.closeModal());
}
}
private _buildModalOverlayClassNames = (): string => {
return classNames({
'modal-overlay': true,
'modal-overlay_required': this.props.isRequired,
});
}
}
export default connect<IStateToProps, IDispatchToProps, IProps>(mapStateToProps, mapDispatchToProps)(BaseModal);