@onehat/ui 0.3.294 → 0.3.295
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Components/Hoc/withModal.js +177 -0
package/package.json
CHANGED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect, } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Box,
|
|
4
|
+
Button,
|
|
5
|
+
Column,
|
|
6
|
+
Icon,
|
|
7
|
+
Modal,
|
|
8
|
+
Row,
|
|
9
|
+
Text,
|
|
10
|
+
} from 'native-base';
|
|
11
|
+
import Panel from '../Panel/Panel.js';
|
|
12
|
+
import TriangleExclamation from '../Icons/TriangleExclamation.js';
|
|
13
|
+
import useAdjustedWindowSize from '../../Hooks/useAdjustedWindowSize.js';
|
|
14
|
+
import testProps from '../../Functions/testProps.js';
|
|
15
|
+
import _ from 'lodash';
|
|
16
|
+
|
|
17
|
+
export default function withModal(WrappedComponent) {
|
|
18
|
+
return (props) => {
|
|
19
|
+
|
|
20
|
+
if (props.disableWithModal || props.showModal) {
|
|
21
|
+
return <WrappedComponent {...props} />;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const
|
|
25
|
+
[title, setTitle] = useState(''),
|
|
26
|
+
[message, setMessage] = useState(''),
|
|
27
|
+
[canClose, setCanClose] = useState(true),
|
|
28
|
+
[includeCancel, setIncludeCancel] = useState(false),
|
|
29
|
+
[isModalShown, setIsModalShown] = useState(false),
|
|
30
|
+
[onOk, setOnOk] = useState(),
|
|
31
|
+
[onYes, setOnYes] = useState(),
|
|
32
|
+
[onNo, setOnNo] = useState(),
|
|
33
|
+
[customButtons, setCustomButtons] = useState(),
|
|
34
|
+
[color, setColor] = useState('#000'),
|
|
35
|
+
autoFocusRef = useRef(null),
|
|
36
|
+
cancelRef = useRef(null),
|
|
37
|
+
[width, height] = useAdjustedWindowSize(400, 250),
|
|
38
|
+
onCancel = () => {
|
|
39
|
+
setIsModalShown(false);
|
|
40
|
+
},
|
|
41
|
+
showModal = (args) => {
|
|
42
|
+
const {
|
|
43
|
+
title = '',
|
|
44
|
+
message = '',
|
|
45
|
+
canClose = true,
|
|
46
|
+
includeCancel = false,
|
|
47
|
+
onOk,
|
|
48
|
+
onYes,
|
|
49
|
+
onNo,
|
|
50
|
+
customButtons,
|
|
51
|
+
color,
|
|
52
|
+
} = args;
|
|
53
|
+
|
|
54
|
+
if (title) {
|
|
55
|
+
setTitle(title);
|
|
56
|
+
}
|
|
57
|
+
if (!message) {
|
|
58
|
+
throw new Error('Message is required for showModal');
|
|
59
|
+
}
|
|
60
|
+
setMessage(message);
|
|
61
|
+
setCanClose(canClose);
|
|
62
|
+
setIncludeCancel(includeCancel);
|
|
63
|
+
if (onOk) {
|
|
64
|
+
setOnOk(() => onOk);
|
|
65
|
+
}
|
|
66
|
+
if (onYes) {
|
|
67
|
+
setOnYes(() => onYes);
|
|
68
|
+
}
|
|
69
|
+
if (onNo) {
|
|
70
|
+
setOnNo(() => onNo);
|
|
71
|
+
}
|
|
72
|
+
if (customButtons) {
|
|
73
|
+
setCustomButtons(customButtons);
|
|
74
|
+
}
|
|
75
|
+
if (color) {
|
|
76
|
+
setColor(color);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
setIsModalShown(true);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let buttons = [];
|
|
83
|
+
if (isModalShown) {
|
|
84
|
+
if (includeCancel) {
|
|
85
|
+
buttons.push(<Button
|
|
86
|
+
{...testProps('cancelBtn')}
|
|
87
|
+
key="cancelBtn"
|
|
88
|
+
onPress={onCancel}
|
|
89
|
+
color="#fff"
|
|
90
|
+
colorScheme="coolGray"
|
|
91
|
+
variant="ghost" // or unstyled
|
|
92
|
+
ref={cancelRef}
|
|
93
|
+
>Cancel</Button>);
|
|
94
|
+
}
|
|
95
|
+
if (onOk) {
|
|
96
|
+
buttons.push(<Button
|
|
97
|
+
{...testProps('okBtn')}
|
|
98
|
+
key="okBtn"
|
|
99
|
+
ref={autoFocusRef}
|
|
100
|
+
onPress={onOk}
|
|
101
|
+
color="#fff"
|
|
102
|
+
>OK</Button>);
|
|
103
|
+
}
|
|
104
|
+
if (onNo) {
|
|
105
|
+
buttons.push(<Button
|
|
106
|
+
{...testProps('noBtn')}
|
|
107
|
+
key="noBtn"
|
|
108
|
+
onPress={onNo}
|
|
109
|
+
color="trueGray.800"
|
|
110
|
+
variant="ghost"
|
|
111
|
+
// colorScheme="neutral"
|
|
112
|
+
mr={2}
|
|
113
|
+
>No</Button>);
|
|
114
|
+
}
|
|
115
|
+
if (onYes) {
|
|
116
|
+
buttons.push(<Button
|
|
117
|
+
{...testProps('yesBtn')}
|
|
118
|
+
key="yesBtn"
|
|
119
|
+
ref={autoFocusRef}
|
|
120
|
+
onPress={onYes}
|
|
121
|
+
color="#fff"
|
|
122
|
+
// colorScheme="danger"
|
|
123
|
+
>Yes</Button>);
|
|
124
|
+
}
|
|
125
|
+
if (customButtons) {
|
|
126
|
+
_.each(customButtons, (button) => {
|
|
127
|
+
buttons.push(button);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return <>
|
|
133
|
+
<WrappedComponent
|
|
134
|
+
{...props}
|
|
135
|
+
disableWithModal={false}
|
|
136
|
+
showModal={showModal}
|
|
137
|
+
hideModal={onCancel}
|
|
138
|
+
/>
|
|
139
|
+
{isModalShown &&
|
|
140
|
+
<Modal
|
|
141
|
+
isOpen={true}
|
|
142
|
+
onClose={onCancel}
|
|
143
|
+
>
|
|
144
|
+
<Panel
|
|
145
|
+
{...props}
|
|
146
|
+
reference="modal"
|
|
147
|
+
isCollapsible={false}
|
|
148
|
+
bg="#fff"
|
|
149
|
+
w={width}
|
|
150
|
+
h={height}
|
|
151
|
+
flex={null}
|
|
152
|
+
>
|
|
153
|
+
|
|
154
|
+
{title && <Modal.Header>{title}</Modal.Header>}
|
|
155
|
+
<Modal.Body
|
|
156
|
+
borderTopWidth={0}
|
|
157
|
+
bg="#fff"
|
|
158
|
+
p={3}
|
|
159
|
+
justifyContent="center"
|
|
160
|
+
alignItems="center"
|
|
161
|
+
borderRadius={5}
|
|
162
|
+
flexDirection="row"
|
|
163
|
+
>
|
|
164
|
+
<Box w="50px" mx={2}>
|
|
165
|
+
<Icon as={TriangleExclamation} color={color} size="10" />
|
|
166
|
+
</Box>
|
|
167
|
+
<Text flex={1} color={color} fontSize="18px">{message}</Text>
|
|
168
|
+
</Modal.Body>
|
|
169
|
+
<Modal.Footer py={2} pr={4} justifyContent="flex-end">
|
|
170
|
+
{buttons}
|
|
171
|
+
</Modal.Footer>
|
|
172
|
+
</Panel>
|
|
173
|
+
</Modal>}
|
|
174
|
+
|
|
175
|
+
</>;
|
|
176
|
+
};
|
|
177
|
+
}
|