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

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,420 @@
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 waitMs from '../helpers/waitMs';
11
+
12
+ // Import types
13
+ import Variant from '../types/Variant';
14
+ import ModalButtonType from '../types/ModalButtonType';
15
+ import ModalSize from '../types/ModalSize';
16
+ import ModalType from '../types/ModalType';
17
+
18
+ /*------------------------------------------------------------------------*/
19
+ /* Style */
20
+ /*------------------------------------------------------------------------*/
21
+
22
+ const style = `
23
+ /* More opaque backdrop */
24
+ .Modal-backdrop {
25
+ background-color: black !important;
26
+ opacity: 0.8 !important;
27
+ z-index: 10000000 !important;
28
+ }
29
+
30
+ /* Customize the modal header and body */
31
+ .modal-header {
32
+ /* Dark theme */
33
+ background-color: #222 !important;
34
+ color: white !important;
35
+
36
+ /* Border */
37
+ border: 1px solid #545454 !important;
38
+ }
39
+ .modal-body {
40
+ /* Dark theme */
41
+ background-color: #222 !important;
42
+ color: white !important;
43
+
44
+ /* Border */
45
+ border-top: 0 !important;
46
+ border-left: 1px solid #545454 !important;
47
+ border-bottom: 1px solid #545454 !important;
48
+ border-right: 1px solid #545454 !important;
49
+ border-bottom-left-radius: 3px;
50
+ border-bottom-right-radius: 3px;
51
+ }
52
+
53
+ /* Create our own animation */
54
+ .modal-content {
55
+ animation-name: Modal-drop-in;
56
+ animation-duration: 0.4s;
57
+ animation-iteration-count: 1;
58
+ animation-fill-mode: forwards;
59
+ animation-timing-function: ease-out;
60
+ }
61
+ @keyframes Modal-drop-in {
62
+ 0% {
63
+ transform: scale(0.8);
64
+ opacity: 0.5;
65
+ }
66
+ 100% {
67
+ transform: scale(1);
68
+ opacity: 1;
69
+ }
70
+ }
71
+ `;
72
+
73
+ /*------------------------------------------------------------------------*/
74
+ /* Constants */
75
+ /*------------------------------------------------------------------------*/
76
+
77
+ // Constants
78
+ const MS_TO_ANIMATE = 400; // Time to animate in/out (defined by bootstrap)
79
+ const MS_ANIMATE_IN_DELAY = 10;
80
+ // Time to wait before animating in (must be >0 or animation won't trigger)
81
+
82
+ // Modal type to list of buttons
83
+ const modalTypeToModalButtonTypes: {
84
+ [k: string]: ModalButtonType[]
85
+ } = {
86
+ [ModalType.Okay]: [
87
+ ModalButtonType.Okay,
88
+ ],
89
+ [ModalType.OkayCancel]: [
90
+ ModalButtonType.Okay,
91
+ ModalButtonType.Cancel,
92
+ ],
93
+ [ModalType.YesNo]: [
94
+ ModalButtonType.Yes,
95
+ ModalButtonType.No,
96
+ ],
97
+ [ModalType.YesNoCancel]: [
98
+ ModalButtonType.Yes,
99
+ ModalButtonType.No,
100
+ ModalButtonType.Cancel,
101
+ ],
102
+ [ModalType.AbandonGoBack]: [
103
+ ModalButtonType.Abandon,
104
+ ModalButtonType.GoBack,
105
+ ],
106
+ [ModalType.ImSureCancel]: [
107
+ ModalButtonType.ImSure,
108
+ ModalButtonType.Cancel,
109
+ ],
110
+ [ModalType.DeleteCancel]: [
111
+ ModalButtonType.Delete,
112
+ ModalButtonType.Cancel,
113
+ ],
114
+ [ModalType.ConfirmCancel]: [
115
+ ModalButtonType.Confirm,
116
+ ModalButtonType.Cancel,
117
+ ],
118
+ };
119
+
120
+ // Button type styling and labels
121
+ const ModalButtonTypeToLabelAndVariant = {
122
+ [ModalButtonType.Okay]: {
123
+ label: 'Okay',
124
+ variant: Variant.Dark,
125
+ },
126
+ [ModalButtonType.Cancel]: {
127
+ label: 'Cancel',
128
+ variant: Variant.Secondary,
129
+ },
130
+ [ModalButtonType.Yes]: {
131
+ label: 'Yes',
132
+ variant: Variant.Dark,
133
+ },
134
+ [ModalButtonType.No]: {
135
+ label: 'No',
136
+ variant: Variant.Secondary,
137
+ },
138
+ [ModalButtonType.Abandon]: {
139
+ label: 'Abandon Changes',
140
+ variant: Variant.Warning,
141
+ },
142
+ [ModalButtonType.GoBack]: {
143
+ label: 'Go Back',
144
+ variant: Variant.Secondary,
145
+ },
146
+ [ModalButtonType.Continue]: {
147
+ label: 'Continue',
148
+ variant: Variant.Dark,
149
+ },
150
+ [ModalButtonType.ImSure]: {
151
+ label: 'I am sure',
152
+ variant: Variant.Warning,
153
+ },
154
+ [ModalButtonType.Delete]: {
155
+ label: 'Yes, Delete',
156
+ variant: Variant.Danger,
157
+ },
158
+ [ModalButtonType.Confirm]: {
159
+ label: 'Confirm',
160
+ variant: Variant.Dark,
161
+ },
162
+ };
163
+
164
+ /*------------------------------------------------------------------------*/
165
+ /* Types */
166
+ /*------------------------------------------------------------------------*/
167
+
168
+ type Props = {
169
+ // Type of the modal
170
+ type?: ModalType,
171
+ // Size of the modal
172
+ size?: ModalSize,
173
+ // Title of the modal (if excluded, no header)
174
+ title?: React.ReactNode,
175
+ // The body of the modal
176
+ children?: React.ReactNode,
177
+ // Handler to call when modal is closed (if excluded, not closable)
178
+ onClose?: (type: ModalButtonType) => void,
179
+ // If true, don't allow the user to click the backdrop to exit
180
+ dontAllowBackdropExit?: boolean,
181
+ // Custom label for "okay" button
182
+ okayLabel?: string,
183
+ // Custom variant for "okay" button
184
+ okayVariant?: Variant,
185
+ // Custom label for "cancel" button
186
+ cancelLabel?: string,
187
+ // Custom variant for "cancel" button
188
+ cancelVariant?: Variant,
189
+ // Custom label for "yes" button
190
+ yesLabel?: string,
191
+ // Custom variant for "yes" button
192
+ yesVariant?: Variant,
193
+ // Custom label for "no" button
194
+ noLabel?: string,
195
+ // Custom variant for "no" button
196
+ noVariant?: Variant,
197
+ // Custom label for "abandon" button
198
+ abandonLabel?: string,
199
+ // Custom variant for "abandon" button
200
+ abandonVariant?: Variant,
201
+ // Custom label for "goBack" button
202
+ goBackLabel?: string,
203
+ // Custom variant for "goBack" button
204
+ goBackVariant?: Variant,
205
+ // Custom label for "continue" button
206
+ continueLabel?: string,
207
+ // Custom variant for "continue" button
208
+ continueVariant?: Variant,
209
+ // Custom label for "imSure" button
210
+ imSureLabel?: string,
211
+ // Custom variant for "imSure" button
212
+ imSureVariant?: Variant,
213
+ // Custom label for "delete" button
214
+ deleteLabel?: string,
215
+ // Custom variant for "delete" button
216
+ deleteVariant?: Variant,
217
+ // Custom label for "confirm" button
218
+ confirmLabel?: string,
219
+ // Custom variant for "confirm" button
220
+ confirmVariant?: Variant,
221
+ // True if modal should be on top of other modals
222
+ onTopOfOtherModals?: boolean,
223
+ };
224
+
225
+ /*------------------------------------------------------------------------*/
226
+ /* Component */
227
+ /*------------------------------------------------------------------------*/
228
+
229
+ const Modal: React.FC<Props> = (props) => {
230
+ /*------------------------------------------------------------------------*/
231
+ /* Setup */
232
+ /*------------------------------------------------------------------------*/
233
+
234
+ /* -------------- Props ------------- */
235
+
236
+ const {
237
+ type = ModalType.NoButtons,
238
+ size = ModalSize.Large,
239
+ title,
240
+ children,
241
+ onClose,
242
+ dontAllowBackdropExit,
243
+ onTopOfOtherModals,
244
+ } = props;
245
+
246
+ /* -------------- State ------------- */
247
+
248
+ // If true, the modal is shown
249
+ const [visible, setVisible] = useState(false);
250
+
251
+ // True if currently animating in
252
+ const [animatingIn, setAnimatingIn] = useState(false);
253
+
254
+ /*------------------------------------------------------------------------*/
255
+ /* Lifecycle Functions */
256
+ /*------------------------------------------------------------------------*/
257
+
258
+ /**
259
+ * Mount
260
+ * @author Gabe Abrams
261
+ */
262
+ useEffect(
263
+ () => {
264
+ (async () => {
265
+ // Start the component animating in
266
+ await waitMs(MS_ANIMATE_IN_DELAY);
267
+ setAnimatingIn(true);
268
+
269
+ // Wait and then set visible to true
270
+ await waitMs(MS_TO_ANIMATE);
271
+ setVisible(true);
272
+ })();
273
+ },
274
+ [],
275
+ );
276
+
277
+ /*------------------------------------------------------------------------*/
278
+ /* Component Functions */
279
+ /*------------------------------------------------------------------------*/
280
+
281
+ /**
282
+ * Handles the closing of the modal
283
+ * @author Gabe Abrams
284
+ * @param ModalButtonType the button that was clicked when closing the
285
+ * modal
286
+ */
287
+ const handleClose = async (ModalButtonType: ModalButtonType) => {
288
+ // Don't close if no handler
289
+ if (!onClose) {
290
+ return;
291
+ }
292
+
293
+ // Don't close if animating in
294
+ if (animatingIn) {
295
+ return;
296
+ }
297
+
298
+ // Don't close if already closed
299
+ if (!visible) {
300
+ return;
301
+ }
302
+
303
+ // Update the state
304
+ setVisible(false);
305
+
306
+ // Call the handler after the modal has animated out
307
+ await waitMs(MS_TO_ANIMATE);
308
+ onClose(ModalButtonType);
309
+ };
310
+
311
+ /*------------------------------------------------------------------------*/
312
+ /* Render */
313
+ /*------------------------------------------------------------------------*/
314
+
315
+ /*----------------------------------------*/
316
+ /* Footer */
317
+ /*----------------------------------------*/
318
+
319
+ // Get list of buttons for this modal type
320
+ const ModalButtonTypes: ModalButtonType[] = modalTypeToModalButtonTypes[type] ?? [];
321
+
322
+ // Create buttons
323
+ const buttons = ModalButtonTypes.map((ModalButtonType: ModalButtonType, i) => {
324
+ // Get default style
325
+ let {
326
+ label,
327
+ variant,
328
+ } = ModalButtonTypeToLabelAndVariant[ModalButtonType];
329
+
330
+ // Override with customizations
331
+ const newLabel = props[`${ModalButtonType}Label`];
332
+ if (newLabel) {
333
+ label = newLabel;
334
+ }
335
+ const newVariant = props[`${ModalButtonType}Variant`];
336
+ if (newVariant) {
337
+ variant = newVariant;
338
+ }
339
+
340
+ // Check if this button is last
341
+ const last = (i === ModalButtonTypes.length - 1);
342
+
343
+ // Create the button
344
+ return (
345
+ <button
346
+ type="button"
347
+ className={`Modal-${ModalButtonType}-button btn btn-${variant} ${last ? '' : 'mr-1'}`}
348
+ onClick={() => {
349
+ handleClose(ModalButtonType);
350
+ }}
351
+ >
352
+ {label}
353
+ </button>
354
+ );
355
+ });
356
+
357
+ // Put all buttons in a footer
358
+ const footer = (
359
+ (buttons && buttons.length)
360
+ ? (
361
+ <div>
362
+ {buttons}
363
+ </div>
364
+ )
365
+ : undefined
366
+ );
367
+
368
+ // Render the modal
369
+ return (
370
+ <div
371
+ className={`modal fade modal-${size}`}
372
+ tabIndex={-1}
373
+ style={{
374
+ zIndex: (
375
+ onTopOfOtherModals
376
+ ? 6000000000
377
+ : 5000000000
378
+ ),
379
+ }}
380
+ >
381
+ <div className="modal-dialog">
382
+ <div className="modal-content">
383
+ <div className="modal-header">
384
+ <h5 className="modal-title">
385
+ {title}
386
+ </h5>
387
+
388
+ {onClose && (
389
+ <button
390
+ type="button"
391
+ className="btn-close"
392
+ data-bs-dismiss="modal"
393
+ aria-label="Close"
394
+ onClick={() => {
395
+ handleClose(ModalButtonType.Cancel);
396
+ }}
397
+ />
398
+ )}
399
+ </div>
400
+ {children && (
401
+ <div className="modal-body">
402
+ {children}
403
+ </div>
404
+ )}
405
+ {footer && (
406
+ <div className="modal-footer">
407
+ {footer}
408
+ </div>
409
+ )}
410
+ </div>
411
+ </div>
412
+ </div>
413
+ );
414
+ };
415
+
416
+ /*------------------------------------------------------------------------*/
417
+ /* Wrap Up */
418
+ /*------------------------------------------------------------------------*/
419
+
420
+ 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;