dce-reactkit 3.9.3 → 3.9.4-beta-prompt.1

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/dist/cjs/index.js CHANGED
@@ -186,6 +186,7 @@ var ModalButtonType$1 = ModalButtonType;
186
186
  var ModalType;
187
187
  (function (ModalType) {
188
188
  ModalType["Okay"] = "okay";
189
+ ModalType["Cancel"] = "cancel";
189
190
  ModalType["OkayCancel"] = "okay-cancel";
190
191
  ModalType["YesNo"] = "yes-no";
191
192
  ModalType["YesNoCancel"] = "yes-no-cancel";
@@ -340,6 +341,9 @@ const modalTypeToModalButtonTypes = {
340
341
  [ModalType$1.Okay]: [
341
342
  ModalButtonType$1.Okay,
342
343
  ],
344
+ [ModalType$1.Cancel]: [
345
+ ModalButtonType$1.Cancel,
346
+ ],
343
347
  [ModalType$1.OkayCancel]: [
344
348
  ModalButtonType$1.Okay,
345
349
  ModalButtonType$1.Cancel,
@@ -1080,6 +1084,73 @@ const confirm = (title, text, opts) => __awaiter(void 0, void 0, void 0, functio
1080
1084
  });
1081
1085
  });
1082
1086
  /*----------------------------------------*/
1087
+ /* --------------- Prompt -------------- */
1088
+ /*----------------------------------------*/
1089
+ // Stored copies of setters
1090
+ let setPromptInfo;
1091
+ // Function to call when prompt is closed
1092
+ let onPromptClosed;
1093
+ /**
1094
+ * Show a prompt modal asking the user for input
1095
+ * @author Yuen Ler Chow
1096
+ * @param title the title text to display at the top of the prompt
1097
+ * @param text the text to display in the prompt
1098
+ * @param [opts={}] additional options for the prompt dialog
1099
+ * @param [opts.placeholder] the placeholder text for the input field
1100
+ * @param [opts.defaultText] the default text for the input field
1101
+ * @param [opts.confirmButtonText=Okay] the text of the confirm button
1102
+ * @param [opts.confirmButtonVariant=Variant.Dark] the variant of the confirm button
1103
+ * @param [opts.cancelButtonText=Cancel] the text of the cancel button
1104
+ * @param [opts.cancelButtonVariant=Variant.Secondary] the variant of the cancel button
1105
+ * @returns Promise that resolves with the input string or null if canceled
1106
+ */
1107
+ const prompt = (title, text, currentInputFieldText, opts) => __awaiter(void 0, void 0, void 0, function* () {
1108
+ var _a;
1109
+ // Wait for helper to exist
1110
+ yield waitForHelper(() => {
1111
+ return !!setPromptInfo;
1112
+ });
1113
+ // Fallback if prompt is not available
1114
+ if (!setPromptInfo) {
1115
+ const resultPassesValidation = false;
1116
+ while (!resultPassesValidation) {
1117
+ // eslint-disable-next-line no-alert
1118
+ const result = window.prompt(`${title}\n\n${text}`, (_a = opts === null || opts === void 0 ? void 0 : opts.defaultText) !== null && _a !== void 0 ? _a : '');
1119
+ if (result === null) {
1120
+ return null;
1121
+ }
1122
+ // Validate min num chars
1123
+ const minNumCharsValidationError = (((opts === null || opts === void 0 ? void 0 : opts.minNumChars) && result.length < opts.minNumChars)
1124
+ ? `Please enter at least ${opts.minNumChars} characters.`
1125
+ : undefined);
1126
+ // Run custom validation
1127
+ const customValidationError = ((opts === null || opts === void 0 ? void 0 : opts.findValidationError)
1128
+ && opts.findValidationError(result));
1129
+ // Show validation issue
1130
+ if (minNumCharsValidationError || customValidationError) {
1131
+ alert('Invalid Input', `${minNumCharsValidationError !== null && minNumCharsValidationError !== void 0 ? minNumCharsValidationError : ''} ${customValidationError !== null && customValidationError !== void 0 ? customValidationError : ''}`);
1132
+ }
1133
+ else {
1134
+ return result;
1135
+ }
1136
+ }
1137
+ }
1138
+ // Return promise that resolves with result of prompt
1139
+ return new Promise((resolve) => {
1140
+ // Setup handler
1141
+ onPromptClosed = (result) => {
1142
+ resolve(result);
1143
+ };
1144
+ // Show the prompt
1145
+ setPromptInfo({
1146
+ title,
1147
+ text,
1148
+ currentInputFieldText,
1149
+ opts: (opts !== null && opts !== void 0 ? opts : {}),
1150
+ });
1151
+ });
1152
+ });
1153
+ /*----------------------------------------*/
1083
1154
  /* ------------- Fatal Error ------------ */
1084
1155
  /*----------------------------------------*/
1085
1156
  // Stored copies of setters
@@ -1095,14 +1166,14 @@ const fatalErrorHandlers = [];
1095
1166
  * @param [errorTitle] title of the error box
1096
1167
  */
1097
1168
  const showFatalError = (error, errorTitle = 'An Error Occurred') => __awaiter(void 0, void 0, void 0, function* () {
1098
- var _a, _b;
1169
+ var _b, _c;
1099
1170
  // Determine message and code
1100
1171
  const message = (typeof error === 'string'
1101
1172
  ? error.trim()
1102
- : String((_a = error.message) !== null && _a !== void 0 ? _a : 'An unknown error occurred.'));
1173
+ : String((_b = error.message) !== null && _b !== void 0 ? _b : 'An unknown error occurred.'));
1103
1174
  const code = (typeof error === 'string'
1104
1175
  ? ReactKitErrorCode$1.NoCode
1105
- : String((_b = error.code) !== null && _b !== void 0 ? _b : ReactKitErrorCode$1.NoCode));
1176
+ : String((_c = error.code) !== null && _c !== void 0 ? _c : ReactKitErrorCode$1.NoCode));
1106
1177
  // Call all fatal error listeners
1107
1178
  try {
1108
1179
  fatalErrorHandlers.forEach((handler) => {
@@ -1229,6 +1300,9 @@ const AppWrapper = (props) => {
1229
1300
  // Confirm
1230
1301
  const [confirmInfo, setConfirmInfoInner,] = React.useState(undefined);
1231
1302
  setConfirmInfo = setConfirmInfoInner;
1303
+ // Prompt
1304
+ const [promptInfo, setPromptInfoInner] = React.useState(undefined);
1305
+ setPromptInfo = setPromptInfoInner;
1232
1306
  // Session expired
1233
1307
  const [sessionHasExpired, setSessionHasExpiredInner,] = React.useState(false);
1234
1308
  setSessionHasExpired = setSessionHasExpiredInner;
@@ -1259,6 +1333,37 @@ const AppWrapper = (props) => {
1259
1333
  }
1260
1334
  }, onTopOfOtherModals: true, dontAllowBackdropExit: true }, confirmInfo.text));
1261
1335
  }
1336
+ /* ------------- Prompt ------------ */
1337
+ if (promptInfo) {
1338
+ // Run min char validation
1339
+ const minNumCharsValidationError = ((promptInfo.opts.minNumChars
1340
+ && promptInfo.currentInputFieldText.length < promptInfo.opts.minNumChars)
1341
+ ? `Please enter at least ${promptInfo.opts.minNumChars} characters.`
1342
+ : undefined);
1343
+ // Run custom validation
1344
+ const customValidationError = (promptInfo.opts.findValidationError
1345
+ && promptInfo.opts.findValidationError(promptInfo.currentInputFieldText));
1346
+ modal = (React__default["default"].createElement(Modal, { key: `prompt-${promptInfo.title}-${promptInfo.text}`, title: promptInfo.title,
1347
+ // Don't show ok button if there is a validation error
1348
+ type: ((customValidationError || minNumCharsValidationError)
1349
+ ? ModalType$1.Cancel
1350
+ : ModalType$1.OkayCancel), okayLabel: promptInfo.opts.confirmButtonText, okayVariant: promptInfo.opts.confirmButtonVariant, cancelLabel: promptInfo.opts.cancelButtonText, cancelVariant: promptInfo.opts.cancelButtonVariant, onClose: (buttonType) => {
1351
+ const result = (buttonType === ModalButtonType$1.Okay
1352
+ ? promptInfo.currentInputFieldText
1353
+ : null);
1354
+ setPromptInfo(undefined);
1355
+ if (onPromptClosed) {
1356
+ onPromptClosed(result);
1357
+ }
1358
+ }, onTopOfOtherModals: true, dontAllowBackdropExit: true },
1359
+ React__default["default"].createElement("div", { className: "d-flex flex-column align-items-center" },
1360
+ React__default["default"].createElement("p", null, promptInfo.text),
1361
+ React__default["default"].createElement("input", { type: "text", placeholder: promptInfo.opts.placeholder, value: promptInfo.currentInputFieldText, onChange: (e) => {
1362
+ return setPromptInfo(Object.assign(Object.assign({}, promptInfo), { currentInputFieldText: e.target.value }));
1363
+ } }),
1364
+ minNumCharsValidationError && (React__default["default"].createElement("div", { className: "text-danger bg-danger bg-opacity-25 p-2 m-1 rounded" }, minNumCharsValidationError)),
1365
+ customValidationError && (React__default["default"].createElement("div", { className: "text-danger bg-danger bg-opacity-25 p-2 m-1 rounded" }, customValidationError)))));
1366
+ }
1262
1367
  /* ------ Custom Modal Portals ------ */
1263
1368
  // Custom modal portals
1264
1369
  const customModalPortals = [];
@@ -16321,6 +16426,7 @@ exports.padDecimalZeros = padDecimalZeros;
16321
16426
  exports.padZerosLeft = padZerosLeft;
16322
16427
  exports.parallelLimit = parallelLimit;
16323
16428
  exports.prefixWithAOrAn = prefixWithAOrAn;
16429
+ exports.prompt = prompt;
16324
16430
  exports.roundToNumDecimals = roundToNumDecimals;
16325
16431
  exports.setClientEventMetadataPopulator = setClientEventMetadataPopulator;
16326
16432
  exports.showFatalError = showFatalError;