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

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