-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInformationDialog.js
More file actions
132 lines (117 loc) · 3.61 KB
/
InformationDialog.js
File metadata and controls
132 lines (117 loc) · 3.61 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { spacing } from '@ui5/webcomponents-react-base';
import { Icon } from '@ui5/webcomponents-react/lib/Icon';
import { Button } from '@ui5/webcomponents-react/lib/Button';
import { ButtonDesign } from '@ui5/webcomponents-react/lib/ButtonDesign';
import { Dialog } from '@ui5/webcomponents-react/lib/Dialog';
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox';
import { FlexBoxDirection } from '@ui5/webcomponents-react/lib/FlexBoxDirection';
import { FlexBoxAlignItems } from '@ui5/webcomponents-react/lib/FlexBoxAlignItems';
import { FlexBoxJustifyContent } from '@ui5/webcomponents-react/lib/FlexBoxJustifyContent';
import { Text } from '@ui5/webcomponents-react/lib/Text';
const KEYBOARD_KEYS = {
ESCAPE: 27,
};
const style = {
warning: {
width: '1.5rem',
height: '1.5rem',
color: '#feb60a',
},
error: {
width: '1.5rem',
height: '1.5rem',
color: '#ff5254',
},
information: {
width: '1.5rem',
height: '1.5rem',
color: 'black',
},
text: {
lineHeight: '20px',
},
};
const _getHeaderIcon = (type) => {
switch (type) {
case Type.Warning:
return _getHeaderWarningIcon();
case Type.Error:
return _getHeaderErrorIcon();
default:
return _getHeaderInfoIcon();
}
};
const _getHeaderWarningIcon = () => {
return <Icon name="message-warning" style={style.warning} />;
};
const _getHeaderErrorIcon = () => {
return <Icon name="message-error" style={style.error} />;
};
const _getHeaderInfoIcon = () => {
return <Icon name="message-information" style={style.information} />;
};
const _handleAvoidEscapeClosing = (avoidEscapeClose) => {
document.addEventListener(
'keydown',
(e) => {
if (e.keyCode === KEYBOARD_KEYS.ESCAPE && avoidEscapeClose) {
e.stopPropagation();
}
},
true,
);
};
const InformationDialog = ({ dialogRef, avoidEscapeClose, headerText, innerText, closeButtonText, children, onClose, type }) => {
const { t } = useTranslation();
useEffect(() => {
_handleAvoidEscapeClosing(avoidEscapeClose);
});
const _onClose = () => {
onClose && onClose();
if (dialogRef.current) {
dialogRef.current.close();
}
};
const _getFooter = () => {
return (
<FlexBox alignItems={FlexBoxAlignItems.Center} direction={FlexBoxDirection.RowReverse} style={spacing.sapUiTinyMargin}>
<Button design={ButtonDesign.Transparent} onClick={_onClose}>
{closeButtonText ? closeButtonText : t('app.generics.close')}
</Button>
</FlexBox>
);
};
const _getHeader = () => {
return (
<FlexBox alignItems={FlexBoxAlignItems.Center} justifyContent={FlexBoxJustifyContent.Center} style={spacing.sapUiContentPadding}>
{_getHeaderIcon(type)}
<Text tooltip={headerText} wrapping style={{ ...spacing.sapUiTinyMarginBegin, ...style.text }}>
{headerText}
</Text>
</FlexBox>
);
};
return (
<Dialog ref={dialogRef} slot="header" header={_getHeader()} footer={_getFooter()} onAfterClose={_onClose} data-testid="information-dialog">
<div style={{ ...spacing.sapUiContentPadding }}>
<FlexBox direction={FlexBoxDirection.Column}>
{innerText ? (
<Text tooltip={innerText} wrapping style={{ ...spacing.sapUiTinyMarginBegin, ...style.text }}>
{innerText}
</Text>
) : (
children
)}
</FlexBox>
</div>
</Dialog>
);
};
export default InformationDialog;
export const Type = {
Warning: 'WARNING',
Error: 'ERROR',
Info: 'INFO',
};