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

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