dce-reactkit 3.0.0-beta.1 → 3.0.0-beta.2

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.
@@ -0,0 +1,417 @@
1
+ /**
2
+ * A generic popup modal
3
+ * @author Gabe Abrams
4
+ */
5
+
6
+ // Import React
7
+ import React, { useState, useEffect } from 'react';
8
+
9
+ // Import other components
10
+ import BootstrapModal from 'react-bootstrap/Modal';
11
+ import waitMs from '../helpers/waitMs';
12
+
13
+ // Import types
14
+ import Variant from '../types/Variant';
15
+ import ModalButtonType from '../types/ModalButtonType';
16
+ import ModalSize from '../types/ModalSize';
17
+ import ModalType from '../types/ModalType';
18
+
19
+ /*------------------------------------------------------------------------*/
20
+ /* Style */
21
+ /*------------------------------------------------------------------------*/
22
+
23
+ const style = `
24
+ /* More opaque backdrop */
25
+ .Modal-backdrop {
26
+ background-color: black !important;
27
+ opacity: 0.8 !important;
28
+ z-index: 10000000 !important;
29
+ }
30
+
31
+ /* Customize the modal header and body */
32
+ .modal-header {
33
+ /* Dark theme */
34
+ background-color: #222 !important;
35
+ color: white !important;
36
+
37
+ /* Border */
38
+ border: 1px solid #545454 !important;
39
+ }
40
+ .modal-body {
41
+ /* Dark theme */
42
+ background-color: #222 !important;
43
+ color: white !important;
44
+
45
+ /* Border */
46
+ border-top: 0 !important;
47
+ border-left: 1px solid #545454 !important;
48
+ border-bottom: 1px solid #545454 !important;
49
+ border-right: 1px solid #545454 !important;
50
+ border-bottom-left-radius: 3px;
51
+ border-bottom-right-radius: 3px;
52
+ }
53
+
54
+ /* Create our own animation */
55
+ .modal-content {
56
+ animation-name: Modal-drop-in;
57
+ animation-duration: 0.4s;
58
+ animation-iteration-count: 1;
59
+ animation-fill-mode: forwards;
60
+ animation-timing-function: ease-out;
61
+ }
62
+ @keyframes Modal-drop-in {
63
+ 0% {
64
+ transform: scale(0.8);
65
+ opacity: 0.5;
66
+ }
67
+ 100% {
68
+ transform: scale(1);
69
+ opacity: 1;
70
+ }
71
+ }
72
+ `;
73
+
74
+ /*------------------------------------------------------------------------*/
75
+ /* Constants */
76
+ /*------------------------------------------------------------------------*/
77
+
78
+ // Constants
79
+ const MS_TO_ANIMATE = 400; // Time to animate in/out (defined by bootstrap)
80
+ const MS_ANIMATE_IN_DELAY = 10;
81
+ // Time to wait before animating in (must be >0 or animation won't trigger)
82
+
83
+ // Modal type to list of buttons
84
+ const modalTypeToModalButtonTypes: {
85
+ [k: string]: ModalButtonType[]
86
+ } = {
87
+ [ModalType.Okay]: [
88
+ ModalButtonType.Okay,
89
+ ],
90
+ [ModalType.OkayCancel]: [
91
+ ModalButtonType.Okay,
92
+ ModalButtonType.Cancel,
93
+ ],
94
+ [ModalType.YesNo]: [
95
+ ModalButtonType.Yes,
96
+ ModalButtonType.No,
97
+ ],
98
+ [ModalType.YesNoCancel]: [
99
+ ModalButtonType.Yes,
100
+ ModalButtonType.No,
101
+ ModalButtonType.Cancel,
102
+ ],
103
+ [ModalType.AbandonGoBack]: [
104
+ ModalButtonType.Abandon,
105
+ ModalButtonType.GoBack,
106
+ ],
107
+ [ModalType.ImSureCancel]: [
108
+ ModalButtonType.ImSure,
109
+ ModalButtonType.Cancel,
110
+ ],
111
+ [ModalType.DeleteCancel]: [
112
+ ModalButtonType.Delete,
113
+ ModalButtonType.Cancel,
114
+ ],
115
+ [ModalType.ConfirmCancel]: [
116
+ ModalButtonType.Confirm,
117
+ ModalButtonType.Cancel,
118
+ ],
119
+ };
120
+
121
+ // Button type styling and labels
122
+ const ModalButtonTypeToLabelAndVariant = {
123
+ [ModalButtonType.Okay]: {
124
+ label: 'Okay',
125
+ variant: Variant.Dark,
126
+ },
127
+ [ModalButtonType.Cancel]: {
128
+ label: 'Cancel',
129
+ variant: Variant.Secondary,
130
+ },
131
+ [ModalButtonType.Yes]: {
132
+ label: 'Yes',
133
+ variant: Variant.Dark,
134
+ },
135
+ [ModalButtonType.No]: {
136
+ label: 'No',
137
+ variant: Variant.Secondary,
138
+ },
139
+ [ModalButtonType.Abandon]: {
140
+ label: 'Abandon Changes',
141
+ variant: Variant.Warning,
142
+ },
143
+ [ModalButtonType.GoBack]: {
144
+ label: 'Go Back',
145
+ variant: Variant.Secondary,
146
+ },
147
+ [ModalButtonType.Continue]: {
148
+ label: 'Continue',
149
+ variant: Variant.Dark,
150
+ },
151
+ [ModalButtonType.ImSure]: {
152
+ label: 'I am sure',
153
+ variant: Variant.Warning,
154
+ },
155
+ [ModalButtonType.Delete]: {
156
+ label: 'Yes, Delete',
157
+ variant: Variant.Danger,
158
+ },
159
+ [ModalButtonType.Confirm]: {
160
+ label: 'Confirm',
161
+ variant: Variant.Dark,
162
+ },
163
+ };
164
+
165
+ /*------------------------------------------------------------------------*/
166
+ /* Types */
167
+ /*------------------------------------------------------------------------*/
168
+
169
+ type Props = {
170
+ // Type of the modal
171
+ type?: ModalType,
172
+ // Size of the modal
173
+ size?: ModalSize,
174
+ // Title of the modal (if excluded, no header)
175
+ title?: React.ReactNode,
176
+ // The body of the modal
177
+ children?: React.ReactNode,
178
+ // Handler to call when modal is closed (if excluded, not closable)
179
+ onClose?: (type: ModalButtonType) => void,
180
+ // If true, don't allow the user to click the backdrop to exit
181
+ dontAllowBackdropExit?: boolean,
182
+ // Custom label for "okay" button
183
+ okayLabel?: string,
184
+ // Custom variant for "okay" button
185
+ okayVariant?: Variant,
186
+ // Custom label for "cancel" button
187
+ cancelLabel?: string,
188
+ // Custom variant for "cancel" button
189
+ cancelVariant?: Variant,
190
+ // Custom label for "yes" button
191
+ yesLabel?: string,
192
+ // Custom variant for "yes" button
193
+ yesVariant?: Variant,
194
+ // Custom label for "no" button
195
+ noLabel?: string,
196
+ // Custom variant for "no" button
197
+ noVariant?: Variant,
198
+ // Custom label for "abandon" button
199
+ abandonLabel?: string,
200
+ // Custom variant for "abandon" button
201
+ abandonVariant?: Variant,
202
+ // Custom label for "goBack" button
203
+ goBackLabel?: string,
204
+ // Custom variant for "goBack" button
205
+ goBackVariant?: Variant,
206
+ // Custom label for "continue" button
207
+ continueLabel?: string,
208
+ // Custom variant for "continue" button
209
+ continueVariant?: Variant,
210
+ // Custom label for "imSure" button
211
+ imSureLabel?: string,
212
+ // Custom variant for "imSure" button
213
+ imSureVariant?: Variant,
214
+ // Custom label for "delete" button
215
+ deleteLabel?: string,
216
+ // Custom variant for "delete" button
217
+ deleteVariant?: Variant,
218
+ // Custom label for "confirm" button
219
+ confirmLabel?: string,
220
+ // Custom variant for "confirm" button
221
+ confirmVariant?: Variant,
222
+ // True if modal should be on top of other modals
223
+ onTopOfOtherModals?: boolean,
224
+ };
225
+
226
+ /*------------------------------------------------------------------------*/
227
+ /* Component */
228
+ /*------------------------------------------------------------------------*/
229
+
230
+ const Modal: React.FC<Props> = (props) => {
231
+ /*------------------------------------------------------------------------*/
232
+ /* Setup */
233
+ /*------------------------------------------------------------------------*/
234
+
235
+ /* -------------- Props ------------- */
236
+
237
+ const {
238
+ type = ModalType.NoButtons,
239
+ size = ModalSize.Large,
240
+ title,
241
+ children,
242
+ onClose,
243
+ dontAllowBackdropExit,
244
+ onTopOfOtherModals,
245
+ } = props;
246
+
247
+ /* -------------- State ------------- */
248
+
249
+ // If true, the modal is shown
250
+ const [visible, setVisible] = useState(false);
251
+
252
+ // True if currently animating in
253
+ const [animatingIn, setAnimatingIn] = useState(false);
254
+
255
+ /*------------------------------------------------------------------------*/
256
+ /* Lifecycle Functions */
257
+ /*------------------------------------------------------------------------*/
258
+
259
+ /**
260
+ * Mount
261
+ * @author Gabe Abrams
262
+ */
263
+ useEffect(
264
+ () => {
265
+ (async () => {
266
+ // Start the component animating in
267
+ await waitMs(MS_ANIMATE_IN_DELAY);
268
+ setAnimatingIn(true);
269
+
270
+ // Wait and then set visible to true
271
+ await waitMs(MS_TO_ANIMATE);
272
+ setVisible(true);
273
+ })();
274
+ },
275
+ [],
276
+ );
277
+
278
+ /*------------------------------------------------------------------------*/
279
+ /* Component Functions */
280
+ /*------------------------------------------------------------------------*/
281
+
282
+ /**
283
+ * Handles the closing of the modal
284
+ * @author Gabe Abrams
285
+ * @param ModalButtonType the button that was clicked when closing the
286
+ * modal
287
+ */
288
+ const handleClose = async (ModalButtonType: ModalButtonType) => {
289
+ // Don't close if no handler
290
+ if (!onClose) {
291
+ return;
292
+ }
293
+
294
+ // Don't close if animating in
295
+ if (animatingIn) {
296
+ return;
297
+ }
298
+
299
+ // Don't close if already closed
300
+ if (!visible) {
301
+ return;
302
+ }
303
+
304
+ // Update the state
305
+ setVisible(false);
306
+
307
+ // Call the handler after the modal has animated out
308
+ await waitMs(MS_TO_ANIMATE);
309
+ onClose(ModalButtonType);
310
+ };
311
+
312
+ /*------------------------------------------------------------------------*/
313
+ /* Render */
314
+ /*------------------------------------------------------------------------*/
315
+
316
+ /*----------------------------------------*/
317
+ /* Footer */
318
+ /*----------------------------------------*/
319
+
320
+ // Get list of buttons for this modal type
321
+ const ModalButtonTypes: ModalButtonType[] = modalTypeToModalButtonTypes[type] ?? [];
322
+
323
+ // Create buttons
324
+ const buttons = ModalButtonTypes.map((ModalButtonType: ModalButtonType, i) => {
325
+ // Get default style
326
+ let {
327
+ label,
328
+ variant,
329
+ } = ModalButtonTypeToLabelAndVariant[ModalButtonType];
330
+
331
+ // Override with customizations
332
+ const newLabel = props[`${ModalButtonType}Label`];
333
+ if (newLabel) {
334
+ label = newLabel;
335
+ }
336
+ const newVariant = props[`${ModalButtonType}Variant`];
337
+ if (newVariant) {
338
+ variant = newVariant;
339
+ }
340
+
341
+ // Check if this button is last
342
+ const last = (i === ModalButtonTypes.length - 1);
343
+
344
+ // Create the button
345
+ return (
346
+ <button
347
+ type="button"
348
+ className={`Modal-${ModalButtonType}-button btn btn-${variant} ${last ? '' : 'mr-1'}`}
349
+ onClick={() => {
350
+ handleClose(ModalButtonType);
351
+ }}
352
+ >
353
+ {label}
354
+ </button>
355
+ );
356
+ });
357
+
358
+ // Put all buttons in a footer
359
+ const footer = (
360
+ (buttons && buttons.length)
361
+ ? (
362
+ <div>
363
+ {buttons}
364
+ </div>
365
+ )
366
+ : undefined
367
+ );
368
+
369
+ // Render the modal
370
+ return (
371
+ <BootstrapModal
372
+ show={visible}
373
+ size={size !== ModalSize.Medium ? size : undefined}
374
+ onHide={() => {
375
+ if (!dontAllowBackdropExit) {
376
+ handleClose(ModalButtonType.Cancel);
377
+ }
378
+ }}
379
+ style={{
380
+ zIndex: (
381
+ onTopOfOtherModals
382
+ ? 6000000000
383
+ : 5000000000
384
+ ),
385
+ }}
386
+ backdropClassName="Modal-backdrop"
387
+ centered
388
+ >
389
+ <style>{style}</style>
390
+ {title && (
391
+ <BootstrapModal.Header
392
+ closeButton={!!onClose}
393
+ >
394
+ <BootstrapModal.Title>
395
+ {title}
396
+ </BootstrapModal.Title>
397
+ </BootstrapModal.Header>
398
+ )}
399
+ {children && (
400
+ <BootstrapModal.Body>
401
+ {children}
402
+ </BootstrapModal.Body>
403
+ )}
404
+ {footer && (
405
+ <BootstrapModal.Footer>
406
+ {footer}
407
+ </BootstrapModal.Footer>
408
+ )}
409
+ </BootstrapModal>
410
+ );
411
+ };
412
+
413
+ /*------------------------------------------------------------------------*/
414
+ /* Wrap Up */
415
+ /*------------------------------------------------------------------------*/
416
+
417
+ export default Modal;
@@ -0,0 +1,136 @@
1
+ /**
2
+ * A box with a tab on the top that holds buttons and other content
3
+ * @author Gabe Abrams
4
+ */
5
+
6
+ // Import React
7
+ import React from 'react';
8
+
9
+ /*------------------------------------------------------------------------*/
10
+ /* Types */
11
+ /*------------------------------------------------------------------------*/
12
+
13
+ type Props = {
14
+ // Title of the box
15
+ title: React.ReactNode,
16
+ // Children/contents inside the box
17
+ children: React.ReactNode,
18
+ };
19
+
20
+ /*------------------------------------------------------------------------*/
21
+ /* Style */
22
+ /*------------------------------------------------------------------------*/
23
+
24
+ const style = `
25
+ /* Tab Box */
26
+ .TabBox-box {
27
+ /* Light Border */
28
+ border: 2px solid #dedede;
29
+
30
+ /* Rounded Corners (except top-left) */
31
+ border-bottom-right-radius: 5px;
32
+ border-bottom-left-radius: 5px;
33
+ border-top-right-radius: 5px;
34
+
35
+ /* Very Light Gray Border */
36
+ background: #fdfdfd;
37
+
38
+ /* Align Contents on Left */
39
+ text-align: left;
40
+ }
41
+
42
+ /* Container for Title */
43
+ .TabBox-title-container {
44
+ /* Place on Left */
45
+ position: relative;
46
+ left: 0;
47
+ text-align: left;
48
+ }
49
+
50
+ /* Tab-style Title */
51
+ .TabBox-title {
52
+ /* Place so it Barely Overlaps the Box Border */
53
+ display: inline-block;
54
+ position: relative;
55
+ top: 2px; /* Gives Illusion that Border Doesn't Exist Below Tab */
56
+
57
+ /* Title-sized Font */
58
+ font-size: 25px;
59
+
60
+ /* Add Border on Top and Sides */
61
+ border-top: 2px solid #dedede;
62
+ border-left: 2px solid #dedede;
63
+ border-right: 2px solid #dedede;
64
+
65
+ /* Round the Top Corners */
66
+ border-top-left-radius: 5px;
67
+ border-top-right-radius: 5px;
68
+
69
+ /* Add Text Padding */
70
+ padding-left: 12px;
71
+ padding-right: 12px;
72
+
73
+ /* Match Background Color of Box */
74
+ background: #fdfdfd;
75
+ }
76
+
77
+ /* Make the TabBox's Children Appear Above Title if Overlap Occurs */
78
+ .TabBox-children {
79
+ position: relative;
80
+ z-index: 1;
81
+ }
82
+ `;
83
+
84
+ /*------------------------------------------------------------------------*/
85
+ /* Component */
86
+ /*------------------------------------------------------------------------*/
87
+
88
+ const TabBox: React.FC<Props> = (props) => {
89
+ /*------------------------------------------------------------------------*/
90
+ /* Setup */
91
+ /*------------------------------------------------------------------------*/
92
+
93
+ /* -------------- Props ------------- */
94
+
95
+ const {
96
+ title,
97
+ children,
98
+ } = props;
99
+
100
+ /*------------------------------------------------------------------------*/
101
+ /* Render */
102
+ /*------------------------------------------------------------------------*/
103
+
104
+ /*----------------------------------------*/
105
+ /* Main UI */
106
+ /*----------------------------------------*/
107
+
108
+ // Full UI
109
+ return (
110
+ <div>
111
+ {/* Style */}
112
+ <style>{style}</style>
113
+
114
+ {/* Title */}
115
+ <div className="TabBox-title-container">
116
+ <div className="TabBox-title">
117
+ {title}
118
+ </div>
119
+ </div>
120
+
121
+ {/* Contents */}
122
+ <div className="TabBox-box p-2">
123
+ <div className="TabBox-children">
124
+ {children}
125
+ </div>
126
+ </div>
127
+ </div>
128
+ );
129
+ };
130
+
131
+ /*------------------------------------------------------------------------*/
132
+ /* Wrap Up */
133
+ /*------------------------------------------------------------------------*/
134
+
135
+ // Export component
136
+ export default TabBox;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * An error with a code
3
+ * @author Gabe Abrams
4
+ */
5
+ class ErrorWithCode extends Error {
6
+ code: string;
7
+
8
+ constructor(message: string, code: string) {
9
+ super(message);
10
+ this.name = 'ErrorWithCode';
11
+ this.code = code;
12
+ }
13
+ }
14
+
15
+ export default ErrorWithCode;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Shorten text so it fits into a certain number of chars
3
+ * @author Gabe Abrams
4
+ * @param text the text to abbreviate
5
+ * @param maxChars the maximum number of chars to include
6
+ * @returns abbreviated text with length no greater than maxChars
7
+ * (including ellipses if applicable)
8
+ */
9
+ const abbreviate = (text: string, maxChars: number): string => {
10
+ // Check if already short enough
11
+ if (text.trim().length < maxChars) {
12
+ return text.trim();
13
+ }
14
+
15
+ // Abbreviate
16
+ const shortenedText = (
17
+ text
18
+ .trim()
19
+ .substring(0, maxChars - 3)
20
+ .trim()
21
+ );
22
+ return `${shortenedText}...`;
23
+ };
24
+
25
+ export default abbreviate;
@@ -0,0 +1,22 @@
1
+ import sum from './sum';
2
+
3
+ /**
4
+ * Get the average of a set of numbers
5
+ * @author Gabe Abrams
6
+ * @param nums the numbers to average
7
+ * @returns average value or 0 if no numbers
8
+ */
9
+ const avg = (nums: number[]) => {
10
+ // Handle empty array case
11
+ if (nums.length === 0) {
12
+ return 0;
13
+ }
14
+
15
+ // Get the total value
16
+ const total = sum(nums);
17
+
18
+ // Get average
19
+ return (total / nums.length);
20
+ };
21
+
22
+ export default avg;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Round a number (ceiling) to a certain number of decimals
3
+ * @author Gabe Abrams
4
+ * @param num the number to round
5
+ * @param numDecimals the number of decimals to round to
6
+ * @returns rounded number
7
+ */
8
+ const ceilToNumDecimals = (num: number, numDecimals: number): number => {
9
+ const rounder = 10 ** numDecimals;
10
+ return (Math.ceil(num * rounder) / rounder);
11
+ };
12
+
13
+ export default ceilToNumDecimals;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Round a number (floor) to a certain number of decimals
3
+ * @author Gabe Abrams
4
+ * @param num the number to round
5
+ * @param numDecimals the number of decimals to round to
6
+ * @returns rounded number
7
+ */
8
+ const floorToNumDecimals = (num: number, numDecimals: number): number => {
9
+ const rounder = 10 ** numDecimals;
10
+ return (Math.floor(num * rounder) / rounder);
11
+ };
12
+
13
+ export default floorToNumDecimals;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Force a number to stay within specific bounds
3
+ * @author Gabe Abrams
4
+ * @param num the number to move into the bounds
5
+ * @param min the minimum number in the bound
6
+ * @param max the maximum number in the bound
7
+ * @returns bounded number
8
+ */
9
+ const forceNumIntoBounds = (num: number, min: number, max: number): number => {
10
+ return Math.max(
11
+ min,
12
+ Math.min(
13
+ max,
14
+ num,
15
+ ),
16
+ );
17
+ };
18
+
19
+ export default forceNumIntoBounds;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Pad a number's decimal with zeros on the right
3
+ * (e.g. 5.2 becomes 5.20 with 2 digit padding)
4
+ * @author Gabe Abrams
5
+ * @param num the number to pad
6
+ * @param numDigits the minimum number of digits after the decimal
7
+ * @returns padded number
8
+ */
9
+ const padDecimalZeros = (num: number, numDigits: number): string => {
10
+ // Skip if nothing to do
11
+ if (numDigits < 1) {
12
+ return String(num);
13
+ }
14
+
15
+ // Convert to string
16
+ let out = String(num);
17
+
18
+ // Add a decimal point if there isn't one
19
+ if (!out.includes('.')) {
20
+ out += '.';
21
+ }
22
+
23
+ // Add zeros
24
+ while (out.split('.')[1].length < numDigits) {
25
+ out = `${out}0`;
26
+ }
27
+
28
+ // Return
29
+ return out;
30
+ };
31
+
32
+ export default padDecimalZeros;