react-core-ts 1.5.8 → 1.6.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.
@@ -0,0 +1,622 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var jsx_runtime_1 = require("react/jsx-runtime");
54
+ var react_1 = require("react");
55
+ var customIcons_1 = __importDefault(require("./components/customIcons"));
56
+ var Spinner_1 = __importDefault(require("./components/loader/Spinner"));
57
+ var portal_1 = __importDefault(require("./components/portal"));
58
+ var debounce_1 = require("./utilities/debounce");
59
+ var getPosition_1 = require("./utilities/getPosition");
60
+ var icons_1 = require("./utilities/icons");
61
+ var ModernAutoCompleteDropdown = function (_a) {
62
+ var _b;
63
+ var label = _a.label, onChange = _a.onChange, getData = _a.getData, data = _a.data, errors = _a.errors, _c = _a.required, required = _c === void 0 ? false : _c, autoFocus = _a.autoFocus, name = _a.name, _d = _a.shortCode, shortCode = _d === void 0 ? "" : _d, fullWidth = _a.fullWidth, placeholder = _a.placeholder, id = _a.id, propsClassName = _a.className, _e = _a.type, type = _e === void 0 ? "custom_select" : _e, readOnly = _a.readOnly, _f = _a.disabled, disabled = _f === void 0 ? false : _f, value = _a.value, _g = _a.autoFilter, autoFilter = _g === void 0 ? false : _g, _h = _a.autoDropdown, autoDropdown = _h === void 0 ? true : _h, _j = _a.insideOpen, insideOpen = _j === void 0 ? false : _j, _k = _a.isClose, isClose = _k === void 0 ? true : _k, _l = _a.noLocalFilter, noLocalFilter = _l === void 0 ? false : _l, _m = _a.isStaticList, isStaticList = _m === void 0 ? false : _m, _o = _a.isCustomPlaceholder, isCustomPlaceholder = _o === void 0 ? false : _o, checkParams = _a.checkParams, width = _a.width, hasParentError = _a.hasParentError, fromPrefix = _a.fromPrefix, labelTitle = _a.labelTitle, _p = _a.isModern, isModern = _p === void 0 ? true : _p;
64
+ var _q = (0, react_1.useState)((_b = value === null || value === void 0 ? void 0 : value.name) !== null && _b !== void 0 ? _b : ""), inputValue = _q[0], setInputValue = _q[1];
65
+ var _r = (0, react_1.useState)(false), isHovered = _r[0], setIsHovered = _r[1];
66
+ var _s = (0, react_1.useState)(false), isLoading = _s[0], setIsLoading = _s[1];
67
+ var _t = (0, react_1.useState)(false), dropOpen = _t[0], setDropOpen = _t[1];
68
+ var _u = (0, react_1.useState)(disabled), isDisabled = _u[0], setIsDisabled = _u[1];
69
+ var _v = (0, react_1.useState)(false), showClose = _v[0], setShowClose = _v[1];
70
+ var _w = (0, react_1.useState)(false), showToolTip = _w[0], setShowTooltip = _w[1];
71
+ var _x = (0, react_1.useState)(false), tooltipIsHovered = _x[0], setTooltipIsHovered = _x[1];
72
+ var _y = (0, react_1.useState)([]), suggestions = _y[0], setSuggestions = _y[1];
73
+ var inputRef = (0, react_1.useRef)(null);
74
+ var adorementRef = (0, react_1.useRef)(null);
75
+ var dropdownref = (0, react_1.useRef)(null);
76
+ var dropBtnRef = (0, react_1.useRef)(null);
77
+ var timerRef = (0, react_1.useRef)(0); // To fix the no results found issue on second type
78
+ var _z = (0, react_1.useState)(0), selectedIndex = _z[0], setSelectedIndex = _z[1];
79
+ var itemRefs = (0, react_1.useRef)([]);
80
+ var scrollContainerRef = (0, react_1.useRef)(null);
81
+ var _0 = (0, react_1.useState)({
82
+ top: 0,
83
+ left: 0,
84
+ bottom: 0,
85
+ right: 0,
86
+ }), dropPosition = _0[0], setDropPosition = _0[1];
87
+ var checkIncludes = function (mainString, subString, param1, param2, param3, param4) {
88
+ var checkIncludesIfExists = function (mainString, substring) {
89
+ return substring &&
90
+ mainString &&
91
+ typeof mainString === "string" &&
92
+ typeof subString === "string"
93
+ ? mainString
94
+ .toString()
95
+ .toLowerCase()
96
+ .startsWith(substring.toString().toLowerCase())
97
+ : false;
98
+ };
99
+ var checkArrayContains = function (strArray, param) { var _a; return (_a = strArray === null || strArray === void 0 ? void 0 : strArray.includes(param)) !== null && _a !== void 0 ? _a : false; };
100
+ return (checkIncludesIfExists(mainString, subString) ||
101
+ (!!checkParams &&
102
+ ((checkArrayContains(checkParams, "param1") &&
103
+ checkIncludesIfExists(param1, subString)) ||
104
+ (checkArrayContains(checkParams, "param2") &&
105
+ checkIncludesIfExists(param2, subString)) ||
106
+ (checkArrayContains(checkParams, "param3") &&
107
+ checkIncludesIfExists(param3, subString)) ||
108
+ (checkArrayContains(checkParams, "param4") &&
109
+ checkIncludesIfExists(param4, subString)))));
110
+ };
111
+ var handleChange = function (event) {
112
+ var value = event.target.value;
113
+ timerRef.current = 0;
114
+ setInputValue(value);
115
+ handleValChange(value);
116
+ if (!value) {
117
+ setInputValue("");
118
+ onChange({ id: undefined, name: "", from: 2 });
119
+ }
120
+ };
121
+ var handleValChange = (0, react_1.useCallback)((0, debounce_1.debounce)(function (value) {
122
+ setDropOpen(true);
123
+ onChange({ id: undefined, name: "", from: 1 });
124
+ if (value.trim() === "" && type === "auto_complete") {
125
+ setSuggestions([]);
126
+ if (autoFilter) {
127
+ handleDropData("*");
128
+ }
129
+ else {
130
+ setDropOpen(false);
131
+ }
132
+ }
133
+ else {
134
+ handleDropData(value);
135
+ }
136
+ setTimeout(function () {
137
+ timerRef.current = 1;
138
+ }, 200);
139
+ }, isStaticList ? 0 : 1000), []);
140
+ var handleDropData = function (value) {
141
+ if (type === "auto_complete") {
142
+ if (isStaticList) {
143
+ loadStaticData();
144
+ var filteredData_1 = value && value !== "*"
145
+ ? data === null || data === void 0 ? void 0 : data.filter(function (item) {
146
+ return checkIncludes(item.name, value, item.param1, item.param2, item.param3);
147
+ })
148
+ : data;
149
+ setSuggestions(filteredData_1);
150
+ }
151
+ else {
152
+ handlePickSuggestions(value);
153
+ }
154
+ }
155
+ else if ((type === "custom_select" || type === "custom_search_select") &&
156
+ (!data || (data === null || data === void 0 ? void 0 : data.length) === 0)) {
157
+ handlePickSuggestions();
158
+ }
159
+ else if (type === "custom_search_select" && data && (data === null || data === void 0 ? void 0 : data.length) > 0) {
160
+ var filteredData_2 = value && value !== "*"
161
+ ? data.filter(function (item) {
162
+ return checkIncludes(item.name, value, item.param1, item.param2, item.param3);
163
+ })
164
+ : data;
165
+ setSuggestions(filteredData_2);
166
+ }
167
+ };
168
+ var handlePickSuggestions = function (value) { return __awaiter(void 0, void 0, void 0, function () {
169
+ var fetchedSuggestions, error_1;
170
+ return __generator(this, function (_a) {
171
+ switch (_a.label) {
172
+ case 0:
173
+ if (!getData) return [3 /*break*/, 4];
174
+ setIsLoading(true);
175
+ _a.label = 1;
176
+ case 1:
177
+ _a.trys.push([1, 3, , 4]);
178
+ return [4 /*yield*/, (getData === null || getData === void 0 ? void 0 : getData(value))];
179
+ case 2:
180
+ fetchedSuggestions = _a.sent();
181
+ setSuggestions(fetchedSuggestions);
182
+ setIsLoading(false);
183
+ return [3 /*break*/, 4];
184
+ case 3:
185
+ error_1 = _a.sent();
186
+ setIsLoading(false);
187
+ return [3 /*break*/, 4];
188
+ case 4: return [2 /*return*/];
189
+ }
190
+ });
191
+ }); };
192
+ var onLabelClick = function () {
193
+ var _a;
194
+ if (!isDisabled) {
195
+ (_a = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
196
+ if (autoFilter && inputValue === "") {
197
+ handleValChange("*");
198
+ }
199
+ else if ((type === "custom_select" || type === "custom_search_select") &&
200
+ data &&
201
+ (data === null || data === void 0 ? void 0 : data.length) > 0) {
202
+ handleDropData();
203
+ setDropOpen(true);
204
+ }
205
+ }
206
+ };
207
+ var handleClickOutside = function (event) {
208
+ if (dropdownref.current &&
209
+ !dropdownref.current.contains(event.target)) {
210
+ setDropOpen(false);
211
+ }
212
+ };
213
+ var checkIsEmptyField = function () {
214
+ var _a;
215
+ if (inputValue === undefined)
216
+ return true;
217
+ else
218
+ return ((_a = inputValue === null || inputValue === void 0 ? void 0 : inputValue.toString().length) !== null && _a !== void 0 ? _a : 0) <= 0 ? true : false;
219
+ };
220
+ var loadStaticData = function () { return __awaiter(void 0, void 0, void 0, function () {
221
+ var fetchedSuggestions, error_2;
222
+ return __generator(this, function (_a) {
223
+ switch (_a.label) {
224
+ case 0:
225
+ if (!!data) return [3 /*break*/, 4];
226
+ data = [];
227
+ if (!getData) return [3 /*break*/, 4];
228
+ _a.label = 1;
229
+ case 1:
230
+ _a.trys.push([1, 3, , 4]);
231
+ setIsLoading(true);
232
+ return [4 /*yield*/, (getData === null || getData === void 0 ? void 0 : getData("*"))];
233
+ case 2:
234
+ fetchedSuggestions = _a.sent();
235
+ setSuggestions(fetchedSuggestions);
236
+ setIsLoading(false);
237
+ return [3 /*break*/, 4];
238
+ case 3:
239
+ error_2 = _a.sent();
240
+ setIsLoading(false);
241
+ return [3 /*break*/, 4];
242
+ case 4: return [2 /*return*/];
243
+ }
244
+ });
245
+ }); };
246
+ (0, react_1.useEffect)(function () {
247
+ document.addEventListener("click", handleClickOutside);
248
+ return function () {
249
+ document.removeEventListener("click", handleClickOutside);
250
+ };
251
+ }, []);
252
+ (0, react_1.useEffect)(function () {
253
+ if (autoFocus) {
254
+ onLabelClick();
255
+ }
256
+ }, [autoFocus]);
257
+ var onInputFocus = function () {
258
+ if (!isDisabled) {
259
+ if (autoFilter && inputValue === "") {
260
+ handleValChange("*");
261
+ }
262
+ else if ((type === "custom_select" || type === "custom_search_select") &&
263
+ data &&
264
+ (data === null || data === void 0 ? void 0 : data.length) > 0) {
265
+ handleDropData();
266
+ //if (insideOpen) setDropOpen(true) // commeted this code bcoz added onclick function inside
267
+ }
268
+ }
269
+ };
270
+ (0, react_1.useEffect)(function () {
271
+ setIsDisabled(disabled);
272
+ }, [disabled]);
273
+ var handleSuggestionClick = function (suggestion, index) {
274
+ onChange(__assign(__assign(__assign({}, suggestion), { id: suggestion === null || suggestion === void 0 ? void 0 : suggestion.id, name: suggestion.name }), ((suggestion === null || suggestion === void 0 ? void 0 : suggestion.label) ? { label: suggestion === null || suggestion === void 0 ? void 0 : suggestion.label } : {})));
275
+ if (type !== "custom_select")
276
+ setInputValue(suggestion.name);
277
+ setDropOpen(false);
278
+ setSelectedIndex(index);
279
+ };
280
+ var handleOpen = function (e) {
281
+ if (!suggestions || (suggestions === null || suggestions === void 0 ? void 0 : suggestions.length) === 0)
282
+ handleDropData();
283
+ if (!isLoading) {
284
+ setDropOpen(!dropOpen);
285
+ if (type !== "custom_select")
286
+ setInputValue("");
287
+ }
288
+ };
289
+ var handleOpenDropdown = function (e) {
290
+ if (!suggestions || (suggestions === null || suggestions === void 0 ? void 0 : suggestions.length) === 0) {
291
+ if (autoDropdown && (inputValue === "" || inputValue.trim() === "")) {
292
+ loadStaticData();
293
+ }
294
+ else {
295
+ handleDropData();
296
+ }
297
+ }
298
+ if (!isLoading) {
299
+ setDropOpen(!dropOpen);
300
+ }
301
+ };
302
+ var handleScroll = function () {
303
+ setDropOpen(false);
304
+ };
305
+ (0, react_1.useEffect)(function () {
306
+ if (!insideOpen) {
307
+ setSelectedIndex(0);
308
+ window.addEventListener("scroll", handleScroll);
309
+ var mainElement = document.querySelector("main");
310
+ mainElement === null || mainElement === void 0 ? void 0 : mainElement.addEventListener("scroll", handleScroll);
311
+ var gridElements = document.querySelectorAll(".k-grid-content, .overflow-auto, .overflow-y-auto, .overflow-x-auto");
312
+ gridElements.forEach(function (gridElement) {
313
+ gridElement.addEventListener("scroll", handleScroll);
314
+ });
315
+ window.addEventListener("resize", getDropPosition);
316
+ getDropPosition();
317
+ return function () {
318
+ window.removeEventListener("scroll", handleScroll);
319
+ var mainElement = document.querySelector("main");
320
+ mainElement === null || mainElement === void 0 ? void 0 : mainElement.removeEventListener("scroll", handleScroll);
321
+ window.addEventListener("resize", getDropPosition);
322
+ var gridElements = document.querySelectorAll(".k-grid-content");
323
+ gridElements.forEach(function (gridElement) {
324
+ gridElement.removeEventListener("scroll", handleScroll);
325
+ });
326
+ };
327
+ }
328
+ }, [dropOpen]);
329
+ var handleClear = function () {
330
+ setDropOpen(false);
331
+ setInputValue("");
332
+ onChange({ id: undefined, name: "", from: 3 });
333
+ onLabelClick();
334
+ };
335
+ (0, react_1.useEffect)(function () {
336
+ var _a;
337
+ setInputValue((_a = value === null || value === void 0 ? void 0 : value.name) !== null && _a !== void 0 ? _a : "");
338
+ }, [value === null || value === void 0 ? void 0 : value.name]);
339
+ (0, react_1.useEffect)(function () {
340
+ setSuggestions(data);
341
+ }, [data]);
342
+ var getErrors = function (err) {
343
+ var _a, _b, _c, _d;
344
+ var errMsg = "";
345
+ if (err.message) {
346
+ errMsg = err === null || err === void 0 ? void 0 : err.message;
347
+ }
348
+ else if ((_a = err === null || err === void 0 ? void 0 : err.id) === null || _a === void 0 ? void 0 : _a.message) {
349
+ errMsg = (_b = err === null || err === void 0 ? void 0 : err.id) === null || _b === void 0 ? void 0 : _b.message;
350
+ }
351
+ else if ((_c = err === null || err === void 0 ? void 0 : err.name) === null || _c === void 0 ? void 0 : _c.message) {
352
+ errMsg = (_d = err === null || err === void 0 ? void 0 : err.name) === null || _d === void 0 ? void 0 : _d.message;
353
+ }
354
+ return errMsg;
355
+ };
356
+ var generateClassName = function (type) {
357
+ var className = propsClassName;
358
+ switch (type) {
359
+ case "input":
360
+ className += "block text-common text-input-text font-normal px-3.5 w-full text-sm text-gray-900 bg-transparent border appearance-none peer h-10 rounded-[4px] disabled:text-input-disabled bg-white disabled:bg-disabled ".concat(label && isModern
361
+ ? "placeholder-transparent"
362
+ : "focus:placeholder-grey-secondary placeholder-input-label", " focus:placeholder-grey-secondary");
363
+ if (errors && errors[name]) {
364
+ className +=
365
+ " border-[#FDA29B] focus:border-error-[#FDA29B] focus:ring-[#FDA29B] focus:ring-3 focus:outline-[#FDA29B] input-outline";
366
+ }
367
+ else {
368
+ className +=
369
+ " text-grey-dark border-input-light focus:border-blue-navy focus:outline-none focus:ring-0";
370
+ }
371
+ break;
372
+ case "label":
373
+ className += " flex modern-input-label-truncate peer-focus:modern-input-peer-focus-label-size \n ".concat(isDisabled
374
+ ? "cursor-pointer"
375
+ : "cursor-text peer-focus:cursor-pointer", " ").concat(isDisabled && !checkIsEmptyField()
376
+ ? "disabled-input-label-bg"
377
+ : !isDisabled || !checkIsEmptyField()
378
+ ? "active-input-label-bg"
379
+ : "", " absolute duration-300 transform -translate-y-4 top-2 z-1 origin-[0] px-0 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:top-1/2 peer-focus:top-2 peer-focus:-translate-y-4 start-[14px] rtl:peer-focus:translate-x-1/4 rtl:peer-focus:left-auto ").concat(isDisabled
380
+ ? "cursor-pointer"
381
+ : "cursor-text peer-focus:cursor-pointer", "\n ").concat(checkIsEmptyField()
382
+ ? "modern-input-label-size"
383
+ : "modern-input-peer-focus-label-size");
384
+ if (errors && errors[name]) {
385
+ className += " text-error-light ";
386
+ }
387
+ else {
388
+ className += " text-grey-dark peer-focus:text-blue-navy";
389
+ }
390
+ break;
391
+ case "message":
392
+ className = " text-error-icon ";
393
+ break;
394
+ case "adorement":
395
+ className += " absolute right-0 adorement gap-1 flex items-center ";
396
+ break;
397
+ default:
398
+ break;
399
+ }
400
+ return className;
401
+ };
402
+ var handleClose = function () {
403
+ setTimeout(function () {
404
+ setDropOpen(false);
405
+ }, 500);
406
+ };
407
+ var handleClearInputValue = function () {
408
+ // setInputValue(value?.name ?? '')
409
+ };
410
+ var getInnerWidth = function () {
411
+ var innerwidth = adorementRef.current
412
+ ? adorementRef.current.offsetWidth
413
+ : 0;
414
+ return innerwidth;
415
+ };
416
+ // const getPosition = () => {
417
+ // return 'bottom'
418
+ // }
419
+ var filteredData = inputValue !== "*" &&
420
+ inputValue !== "" &&
421
+ type !== "custom_select" &&
422
+ !noLocalFilter
423
+ ? suggestions === null || suggestions === void 0 ? void 0 : suggestions.filter(function (item) {
424
+ var _a, _b, _c, _d;
425
+ return checkIncludes(item.name, inputValue, (_a = item.param1) !== null && _a !== void 0 ? _a : "", (_b = item.param2) !== null && _b !== void 0 ? _b : "", (_c = item.param3) !== null && _c !== void 0 ? _c : "", (_d = item.param4) !== null && _d !== void 0 ? _d : "");
426
+ })
427
+ : suggestions;
428
+ var handleError = function (data) {
429
+ var _a;
430
+ if (getErrors(data[name]) === "required" ||
431
+ getErrors(data[name]) === "Required") {
432
+ return "".concat(label !== null && label !== void 0 ? label : labelTitle, " is ").concat(getErrors(data[name]));
433
+ }
434
+ else {
435
+ return (_a = getErrors(data[name])) !== null && _a !== void 0 ? _a : "";
436
+ }
437
+ };
438
+ var getDropPosition = function () {
439
+ if (!insideOpen && (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
440
+ var dropdownHeight = 200; // Assume the height of the dropdown is 200px, adjust as needed
441
+ var inputRect = inputRef.current.getBoundingClientRect();
442
+ var spaceBelow = window.innerHeight - (inputRect.bottom + window.scrollY);
443
+ var spaceAbove = inputRect.top + window.scrollY;
444
+ var top_1;
445
+ var bottom = void 0;
446
+ if (spaceBelow < dropdownHeight && spaceAbove > dropdownHeight) {
447
+ bottom = spaceBelow + 43;
448
+ }
449
+ else {
450
+ top_1 = inputRect.top + window.scrollY + inputRect.height + 2;
451
+ }
452
+ setDropPosition({
453
+ left: inputRect.left + window.scrollX,
454
+ top: top_1,
455
+ bottom: bottom,
456
+ width: inputRect.width,
457
+ });
458
+ }
459
+ return undefined;
460
+ };
461
+ var _1 = (0, react_1.useState)(false), showNoResults = _1[0], setShowNoResults = _1[1];
462
+ (0, react_1.useEffect)(function () {
463
+ if (inputValue &&
464
+ (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length) === 0 &&
465
+ !isLoading &&
466
+ timerRef.current === 1) {
467
+ var timer_1 = setTimeout(function () {
468
+ setShowNoResults(true);
469
+ }, 500); // Delay in milliseconds
470
+ return function () { return clearTimeout(timer_1); };
471
+ }
472
+ else {
473
+ setShowNoResults(false);
474
+ }
475
+ // Handle keyboard navigation
476
+ var handleKeyDown = function (e) {
477
+ var _a, _b;
478
+ if (!dropOpen)
479
+ return;
480
+ var atBottom = selectedIndex === filteredData.length - 1;
481
+ var atTop = selectedIndex === 0;
482
+ switch (e.key) {
483
+ case "ArrowDown":
484
+ e.preventDefault();
485
+ if (itemRefs.current && scrollContainerRef.current) {
486
+ if (!atBottom) {
487
+ scrollContainerRef.current.scrollTop +=
488
+ ((_a = itemRefs.current[selectedIndex]) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 50;
489
+ }
490
+ else {
491
+ scrollContainerRef.current.scrollTop = 0;
492
+ }
493
+ }
494
+ setSelectedIndex(function (prev) { return (prev + 1) % (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length); });
495
+ break;
496
+ case "ArrowUp":
497
+ e.preventDefault();
498
+ if (itemRefs.current && scrollContainerRef.current) {
499
+ if (!atTop) {
500
+ scrollContainerRef.current.scrollTop -=
501
+ ((_b = itemRefs.current[selectedIndex]) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 50;
502
+ }
503
+ else {
504
+ scrollContainerRef.current.scrollTop = 0;
505
+ }
506
+ }
507
+ setSelectedIndex(function (prev) { return (prev - 1 + (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length)) % (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length); });
508
+ break;
509
+ case "Enter":
510
+ e.preventDefault();
511
+ handleSuggestionClick(filteredData[selectedIndex], selectedIndex);
512
+ break;
513
+ case "Escape":
514
+ e.preventDefault();
515
+ setDropOpen(false);
516
+ break;
517
+ default:
518
+ break;
519
+ }
520
+ };
521
+ if ((filteredData === null || filteredData === void 0 ? void 0 : filteredData.length) > 0 && !isLoading) {
522
+ window.addEventListener("keydown", handleKeyDown);
523
+ return function () {
524
+ window.removeEventListener("keydown", handleKeyDown);
525
+ };
526
+ }
527
+ }, [
528
+ inputValue,
529
+ filteredData,
530
+ isLoading,
531
+ timerRef.current,
532
+ selectedIndex,
533
+ dropOpen,
534
+ ]);
535
+ var setDropDown = function () {
536
+ return (((filteredData === null || filteredData === void 0 ? void 0 : filteredData.length) > 0 || showNoResults) && ((0, jsx_runtime_1.jsx)("ul", __assign({ className: "autocomplete-suggections absolute h-auto max-h-40 overflow-auto w-full bg-white shadow-gray-300 shadow-md border border-grey-light py-1.5 z-50 mt-9", style: dropPosition, ref: scrollContainerRef }, { children: (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length) > 0 ? ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: filteredData.map(function (suggestion, index) { return ((0, jsx_runtime_1.jsxs)("li", __assign({ className: "".concat((value === null || value === void 0 ? void 0 : value.id) === (suggestion === null || suggestion === void 0 ? void 0 : suggestion.id)
537
+ ? "bg-blue-navy text-white"
538
+ : "".concat(index === selectedIndex ? "is-selected" : "", " hover:bg-table-hover"), " cursor-pointer p-1 text-xxs ps-3.5 pl-[10px] qbs-autocomplete-suggections-items"), "data-testid": suggestion.name, onClick: function () { return handleSuggestionClick(suggestion, index); }, tabIndex: index, ref: function (el) { return (itemRefs.current[index] = el); } }, { children: [(0, jsx_runtime_1.jsx)("span", { children: (suggestion === null || suggestion === void 0 ? void 0 : suggestion.label) ? suggestion === null || suggestion === void 0 ? void 0 : suggestion.label : suggestion.name }), shortCode && (suggestion === null || suggestion === void 0 ? void 0 : suggestion[shortCode]) && ((0, jsx_runtime_1.jsx)("span", __assign({ className: "short-code" }, { children: suggestion === null || suggestion === void 0 ? void 0 : suggestion[shortCode] })))] }), suggestion === null || suggestion === void 0 ? void 0 : suggestion.id)); }) })) : (showNoResults &&
539
+ !isLoading &&
540
+ timerRef.current === 1 && ((0, jsx_runtime_1.jsx)("li", __assign({ className: "$ cursor-pointer p-1 rounded-sm text-xxs", onClick: handleClose }, { children: "No Results Found" })))) }))));
541
+ };
542
+ var getBorderRight = function () {
543
+ if (fromPrefix) {
544
+ if ((hasParentError && Boolean(errors && errors[name])) ||
545
+ hasParentError) {
546
+ return 0;
547
+ }
548
+ else if (errors && errors[name]) {
549
+ return undefined;
550
+ }
551
+ }
552
+ return 0;
553
+ };
554
+ (0, react_1.useEffect)(function () {
555
+ (0, getPosition_1.applyPositionClass)(dropdownref, isHovered);
556
+ setDropOpen(false);
557
+ }, [isHovered]);
558
+ (0, react_1.useEffect)(function () {
559
+ var shouldShowClose = (value === null || value === void 0 ? void 0 : value.name) &&
560
+ ((value === null || value === void 0 ? void 0 : value.id) || String(value === null || value === void 0 ? void 0 : value.id) === "0") &&
561
+ !disabled &&
562
+ !readOnly &&
563
+ isClose;
564
+ if (showClose !== shouldShowClose) {
565
+ setShowClose(shouldShowClose);
566
+ }
567
+ }, [value, disabled, readOnly]);
568
+ (0, react_1.useEffect)(function () {
569
+ // Check if input content overflows
570
+ var checkOverflow = function () {
571
+ if (inputRef.current) {
572
+ var _a = inputRef.current, scrollWidth = _a.scrollWidth, clientWidth = _a.clientWidth, value_1 = _a.value;
573
+ // Check for overflow only when there's content
574
+ if (value_1.trim() !== "") {
575
+ setShowTooltip(scrollWidth > clientWidth + 2);
576
+ }
577
+ else {
578
+ setShowTooltip(false); // No tooltip for empty input
579
+ }
580
+ }
581
+ };
582
+ if (inputRef.current) {
583
+ inputRef.current.addEventListener("input", checkOverflow);
584
+ checkOverflow(); // Initial check
585
+ }
586
+ return function () {
587
+ if (inputRef.current) {
588
+ inputRef.current.removeEventListener("input", checkOverflow);
589
+ }
590
+ };
591
+ }, [inputValue]);
592
+ var handleMouseEnter = function () {
593
+ setTooltipIsHovered(true);
594
+ };
595
+ var handleMouseLeave = function () {
596
+ setTooltipIsHovered(false);
597
+ };
598
+ return ((0, jsx_runtime_1.jsxs)("div", __assign({ id: id ? "autocomplete-container-".concat(id) : "autocomplete-container-".concat(name), className: " flex-grow ".concat(fullWidth ? "w-full" : "w-auto"), ref: dropdownref }, { children: [label && !isModern && ((0, jsx_runtime_1.jsx)("div", __assign({ className: "mb-3" }, { children: (0, jsx_runtime_1.jsxs)("label", __assign({ className: "text-xs font-medium" }, { children: [label, required ? (0, jsx_runtime_1.jsx)("span", __assign({ className: "text-error" }, { children: " *" })) : (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {})] })) }))), (0, jsx_runtime_1.jsxs)("div", __assign({ className: "tooltip-container" }, { children: [isHovered && errors && errors[name] && ((0, jsx_runtime_1.jsxs)("span", __assign({ className: "tooltip" }, { children: [handleError(errors), " "] }))), tooltipIsHovered && showToolTip && !dropOpen && inputValue && ((0, jsx_runtime_1.jsx)("div", __assign({ className: "tooltip-info" }, { children: inputValue }))), (0, jsx_runtime_1.jsxs)("div", __assign({ className: "flex relative ".concat(fullWidth ? "w-full" : "w-auto"), style: { width: width } }, { children: [(0, jsx_runtime_1.jsxs)("div", __assign({ className: "relative w-full", onMouseEnter: function () { return handleMouseEnter(); }, onMouseLeave: function () { return handleMouseLeave(); } }, { children: [(0, jsx_runtime_1.jsx)("input", { type: "text", readOnly: readOnly !== null && readOnly !== void 0 ? readOnly : type === "custom_select", value: inputValue ? inputValue : "", onBlur: handleClearInputValue, autoComplete: "off", disabled: disabled, ref: inputRef, id: id ? "custom-autocomplete-".concat(id) : "custom-autocomplete-".concat(name), "data-testid": "custom-autocomplete", "aria-describedby": id, style: __assign({ paddingRight: getInnerWidth() }, (fromPrefix
599
+ ? {
600
+ borderRight: getBorderRight(),
601
+ borderTopRightRadius: 0,
602
+ borderBottomRightRadius: 0,
603
+ }
604
+ : {})), className: " ".concat(generateClassName("input")), onChange: handleChange, placeholder: type === "auto_complete"
605
+ ? placeholder && isCustomPlaceholder
606
+ ? placeholder
607
+ : "Type to search"
608
+ : placeholder !== null && placeholder !== void 0 ? placeholder : "--Select--", onFocus: onInputFocus, onClick: function (e) {
609
+ if (type === "custom_select") {
610
+ setDropOpen(!dropOpen);
611
+ handleOpen(e);
612
+ }
613
+ else {
614
+ if (dropOpen || filteredData.length > 0)
615
+ setDropOpen(!dropOpen);
616
+ }
617
+ } }), isModern && ((0, jsx_runtime_1.jsxs)("label", __assign({ htmlFor: id, onClick: function () { return onLabelClick(); }, className: generateClassName("label") }, { children: [label ? (0, jsx_runtime_1.jsx)("span", __assign({ className: "truncate" }, { children: label })) : "", required ? (0, jsx_runtime_1.jsx)("span", __assign({ className: "text-error" }, { children: " *" })) : (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {})] })))] })), (0, jsx_runtime_1.jsx)("div", __assign({ className: "flex items-center justify-center " }, { children: (0, jsx_runtime_1.jsxs)("div", __assign({ ref: adorementRef, className: "".concat(generateClassName("adorement"), " qbs-autocomplete-adorement mr-[1px] ").concat(isLoading ? "bg-white" : "") }, { children: [showClose && ((0, jsx_runtime_1.jsx)("button", __assign({ onClick: function () { return handleClear(); }, className: " text-table-bodyColor text-[#667085]", "aria-label": "close", type: "button", id: "autocomplete-close-icon" }, { children: (0, jsx_runtime_1.jsx)(customIcons_1.default, { name: "close", type: "large-m" }) }))), isLoading && (0, jsx_runtime_1.jsx)(Spinner_1.default, {}), (type !== "auto_complete" || autoDropdown) &&
618
+ !disabled &&
619
+ !readOnly && ((0, jsx_runtime_1.jsx)("button", __assign({ disabled: disabled !== null && disabled !== void 0 ? disabled : readOnly, onClick: function (e) { return handleOpenDropdown(e); }, onBlur: handleClose, className: " text-[#667085] focus-visible:outline-slate-100", "data-testid": "drop-arrow", type: "button", id: "autocomplete-drop-icon", ref: dropBtnRef }, { children: !dropOpen ? ((0, jsx_runtime_1.jsx)(icons_1.DropArrow, { uniqueDropArrowId: "drop-arrow-icon" })) : ((0, jsx_runtime_1.jsx)(icons_1.DropArrow, { className: "rotate-180", uniqueDropArrowId: "drop-arrow-icon" })) }))), errors && errors[name] && ((0, jsx_runtime_1.jsx)("div", __assign({ className: " text-error-label relative cursor-pointer ".concat(generateClassName("message")), onMouseEnter: function () { return setIsHovered(true); }, onMouseLeave: function () { return setIsHovered(false); } }, { children: (0, jsx_runtime_1.jsx)(customIcons_1.default, { name: "alert", type: "medium" }) })))] })) })), dropOpen && (!isLoading || (filteredData === null || filteredData === void 0 ? void 0 : filteredData.length) > 0) && ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: insideOpen ? setDropDown() : (0, jsx_runtime_1.jsx)(portal_1.default, { children: setDropDown() }) }))] }))] }))] })));
620
+ };
621
+ exports.default = ModernAutoCompleteDropdown;
622
+ //# sourceMappingURL=ReactAutoCompleteDropdown.js.map