/
Complete.tsx
532 lines (494 loc) · 15.8 KB
/
Complete.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import * as React from "react";
import { useState, useEffect, useMemo, useRef, useLayoutEffect } from "react";
import Popover from "@mui/material/Popover";
import IconButton from "@mui/material/IconButton";
import MatTextField from "@mui/material/TextField";
import InputAdornment from "@mui/material/InputAdornment";
import CircularProgress from "@mui/material/CircularProgress";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemText from "@mui/material/ListItemText";
import ListItemButton from "@mui/material/ListItemButton";
import ClearIcon from "@mui/icons-material/Clear";
import VirtualView from "../../../../VirtualView";
import useMediaContext from "../../../../../hooks/useMediaContext";
import useItemModal from "../../../../../hooks/useItemModal";
import { useOnePayload } from "../../../context/PayloadProvider";
import { useOneState } from "../../../context/StateProvider";
import { useOneProps } from "../../../context/PropsProvider";
import { useOneMenu } from "../../../context/MenuProvider";
import useActualCallback from "../../../../../hooks/useActualCallback";
import useElementSize from "../../../../../hooks/useElementSize";
import useActualValue from "../../../../../hooks/useActualValue";
import useDebounce from "../../../hooks/useDebounce";
import { ICompleteSlot } from "../../../slots/CompleteSlot";
import queued from "../../../../../utils/hof/queued";
import formatText from "../../../../../utils/formatText";
import FieldType from "../../../../../model/FieldType";
const FETCH_DEBOUNCE = 500;
const ITEMS_LIMIT = 100;
const ITEM_HEIGHT = 36;
const NEVER_POS = Symbol("never-pos");
/**
* Retrieves the current caret position in the given HTML input element or textarea.
*
* @param element - The HTML input element or textarea.
* @returns - The current caret position.
*/
const getCaretPos = (element: HTMLInputElement | HTMLTextAreaElement) => {
return element.selectionStart || element.value.length;
};
/**
* Represents a complete field component with various options and functionality.
*
* @typedef Complete
* @property invalid - Indicates if the field value is invalid.
* @property incorrect - Indicates if the field value is incorrect.
* @property value - The current value of the field.
* @property disabled - Indicates if the field is disabled.
* @property readonly - Indicates if the field is readonly.
* @property inputType - The type of input element to render (default is "text").
* @property inputMode - The input mode of the field (default is "text").
* @property inputPattern - The pattern attribute of the input element.
* @property labelShrink - Indicates if the label should shrink when the field value is not empty.
* @property description - The description text to display below the field.
* @property outlined - Indicates if the input element should be outlined.
* @property keepRaw - Indicates if the raw value should be kept in the field.
* @property title - The title attribute of the input element.
* @property placeholder - The placeholder text to display in the input element.
* @property inputAutocomplete - The autocomplete attribute of the input element (default is "off").
* @property dirty - Indicates if the field value has been modified.
* @property loading - Indicates if the field data is currently being loaded.
* @property tip - A function that returns an array of data to display as the tip.
* @property tipSelect - Indicates if the tip options should be selectable.
* @property autoFocus - Indicates if the input element should be focused on mount.
* @property onChange - A callback function that is called when the field value changes.
* @property inputFormatterSymbol - The symbol used in the input formatter (default is "0").
* @property inputFormatterAllowed - An array of allowed characters in the input formatter.
* @property inputFormatterReplace - A regular expression used to replace characters in the input formatter.
* @property inputFormatterTemplate - The template used in the input formatter.
* @property inputFormatter - The function used to format the input value.
* @property withContextMenu - Indicates if the context menu should be enabled for the field.
*/
export const Complete = ({
invalid,
incorrect,
value,
disabled,
readonly,
inputType = "text",
inputMode = "text",
inputPattern = undefined,
labelShrink,
description = "",
outlined = false,
keepRaw = false,
title = "",
placeholder = "",
inputAutocomplete: autoComplete = "off",
dirty,
loading: upperLoading,
tip = () => ["unset"],
tipSelect,
autoFocus,
onChange,
inputFormatterSymbol: symbol = "0",
inputFormatterAllowed: allowed,
inputFormatterReplace: replace,
inputFormatterTemplate: template = "",
inputFormatter = (raw) =>
formatText(raw, template, {
symbol,
allowed,
replace,
}),
withContextMenu,
}: ICompleteSlot) => {
const { isMobile } = useMediaContext();
const payload = useOnePayload();
const { object, changeObject: handleChangeObj } = useOneState<object>();
const { requestSubject } = useOneMenu();
const {
fallback = (e: Error) => {
throw e;
},
} = useOneProps();
const { elementRef: anchorElRef, size } = useElementSize<HTMLDivElement>();
const inputElementRef = useRef<HTMLInputElement>(null);
const [anchorEl, setAnchorEl] = React.useState<HTMLDivElement | null>(null);
/**
* Handles click events.
*
* @param event - The click event object.
* @returns
*/
const handleClick = ({
clientX,
clientY,
target,
}: React.MouseEvent<HTMLDivElement>) => {
if (isMobile) {
pickModal();
return;
}
const pointTarget = document.elementFromPoint(clientX, clientY);
if (pointTarget) {
setAnchorEl(pointTarget as HTMLDivElement);
return;
}
setAnchorEl(target as unknown as HTMLDivElement);
};
/**
* Closes the anchor element by setting its value to null.
*
* @function
*/
const handleClose = () => {
setAnchorEl(null);
};
const [selectedIdx, setSelectedIdx] = useState(-1);
const [currentLoading, setCurrentLoading] = useState(false);
const [items, setItems] = useState<string[]>([]);
const loading = upperLoading || currentLoading;
const value$ = useActualValue(value);
const [valueD] = useDebounce(value, FETCH_DEBOUNCE);
const tip$ = useActualValue(tip);
const object$ = useActualValue(object);
const onChange$ = useActualCallback(onChange);
const pickModal = useItemModal({
data: object,
payload,
itemList: undefined,
tr: undefined,
tip,
keepRaw: false,
onValueChange: onChange,
placeholder,
title,
type: FieldType.Complete,
value,
});
/**
* Manages the caret (cursor) position in an input element.
*/
const caretManager = useMemo(() => {
let lastPos: symbol | number = NEVER_POS;
const getAdjust = (pos: number) => {
let adjust = 0;
for (let i = Math.max(pos - 1, 0); i < template.length; i++) {
const char = template[i];
if (char === symbol) {
break;
}
adjust += 1;
}
return adjust;
};
return {
/**
* Renders the position of caret
*/
render: () => {
if (inputType !== "text") {
return;
}
const { current: input } = inputElementRef;
if (typeof lastPos === "number") {
input?.setSelectionRange(lastPos, lastPos);
lastPos = NEVER_POS;
}
},
/**
* Retrieves the position of the caret in the current input element.
*/
pos: () => {
const { current: input } = inputElementRef;
if (input) {
lastPos = getCaretPos(input);
lastPos += getAdjust(lastPos);
}
return lastPos;
},
};
}, []);
useLayoutEffect(() => {
if (!template) {
return;
}
const { current: input } = inputElementRef;
const handler = () => caretManager.pos();
input && input.addEventListener("keyup", handler);
input && input.addEventListener("click", handler);
return () => {
input && input.removeEventListener("keyup", handler);
input && input.removeEventListener("click", handler);
};
}, [inputElementRef.current]);
useLayoutEffect(() => {
if (template) {
caretManager.render();
}
}, [value]);
/**
* A memoized function that handles a change event.
*
* @param text - The text to be processed.
* @returns
*/
const handleChange = useMemo(
() =>
queued(async (text: string) => {
setSelectedIdx(-1);
await onChange$(text);
}),
[]
);
/**
* Memoized function for handling requests.
*
* @returns
*/
const handleRequest = useMemo(
() =>
queued(async () => {
setCurrentLoading(true);
try {
let items =
typeof tip$.current === "function"
? await tip$.current(
value$.current || "",
object$.current,
payload
)
: tip$.current;
if (Array.isArray(items)) {
if (!keepRaw && value$.current) {
const search = String(value$.current || "").toLowerCase();
const searchQuery = search.split(" ");
items = items.filter((item) => {
const itemValue = String(item).toLowerCase().split(" ");
let isOk = true;
searchQuery.forEach((search) => {
isOk =
isOk &&
itemValue.some((item: string) => {
return item.includes(search);
});
});
return isOk;
});
}
items = items.slice(0, ITEMS_LIMIT);
setSelectedIdx(-1);
setItems([...new Set(items)]);
} else {
throw new Error("CompleteField tip invalid result");
}
} catch (error: any) {
fallback(error);
} finally {
setCurrentLoading(false);
}
}),
[]
);
useEffect(() => {
if (!open) {
return;
}
handleRequest();
}, [valueD, open]);
/**
* Handles the blur event.
*
* This function closes the handle, resets the selected index, and clears the selection range of the input element.
*/
const handleBlur = () => {
handleClose();
setSelectedIdx(-1);
inputElementRef.current?.setSelectionRange(null, null);
};
/**
* Handles key down events for a specific key.
*
* @param key - The key value from the event.
* @param blur - A function to blur the input element.
* @returns - Returns true if the key event was handled, otherwise false.
*/
const handleKeyDown = (key: string, blur: () => void) => {
if (key === "Escape") {
handleClose();
setSelectedIdx(-1);
blur();
return true;
}
if (key === "ArrowDown") {
setSelectedIdx((idx) => Math.min(Math.max(idx + 1, 0), items.length - 1));
return true;
}
if (key === "ArrowUp") {
setSelectedIdx((idx) => Math.min(Math.max(idx - 1, 0), items.length - 1));
return true;
}
if (key === "Enter") {
const item = items.find((_, idx) => idx === selectedIdx);
if (item) {
handleChange(item);
inputElementRef.current?.setSelectionRange(item.length, item.length);
handleClose();
setSelectedIdx(-1);
return true;
}
}
return false;
};
useEffect(
() =>
withContextMenu &&
requestSubject.subscribe(() => {
handleBlur();
}),
[]
);
return (
<>
<div ref={anchorElRef}>
<MatTextField
sx={{
...(!outlined && {
position: "relative",
mt: 1,
"& .MuiFormHelperText-root": {
position: "absolute",
top: "100%",
},
}),
}}
fullWidth
inputRef={inputElementRef}
variant={outlined ? "outlined" : "standard"}
value={String(value || "")}
helperText={(dirty && (invalid || incorrect)) || description}
error={dirty && (invalid !== null || incorrect !== null)}
InputProps={{
onKeyDown: (e) => {
if (handleKeyDown(e.key, () => e.currentTarget.blur())) {
e.preventDefault();
}
},
autoComplete,
readOnly: readonly,
inputMode,
autoFocus,
endAdornment: (
<InputAdornment sx={{ position: "relative" }} position="end">
<IconButton
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
if (!loading && !open && !!value) {
handleChange("");
inputElementRef.current?.setSelectionRange(null, null);
}
}}
disabled={disabled}
edge="end"
>
{loading && <CircularProgress color="inherit" size={20} />}
{!loading && !open && !!value && <ClearIcon />}
</IconButton>
</InputAdornment>
),
}}
inputProps={{
pattern: inputPattern,
}}
InputLabelProps={
labelShrink
? {
shrink: labelShrink,
}
: undefined
}
type={inputType}
focused={autoFocus}
autoComplete={autoComplete}
placeholder={placeholder}
onChange={({ target }) => {
let result = target.value;
if (template) {
result = "";
for (let i = 0; i < target.value.length; i++) {
result += target.value[i];
result = inputFormatter(result);
}
caretManager.pos();
}
onChange(result);
}}
onClick={handleClick}
label={title}
disabled={disabled}
/>
</div>
<Popover
open={!!anchorEl}
anchorEl={anchorEl}
onClose={handleBlur}
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
disableAutoFocus
disableEnforceFocus
disableRestoreFocus
>
{!!anchorEl && (
<VirtualView
component={List}
sx={{
width: size.width,
height: items.length
? Math.min(items.length * ITEM_HEIGHT, 250)
: ITEM_HEIGHT,
mb: 1,
maxHeight: "45vh",
}}
minRowHeight={ITEM_HEIGHT}
>
{!items.length && (
<ListItem disableGutters dense>
<ListItemButton disableRipple disableTouchRipple>
<ListItemText primary={loading ? "Loading" : "No tips"} />
</ListItemButton>
</ListItem>
)}
{items.map((value, idx) => (
<ListItem disableGutters dense key={`${value}-${idx}`}>
<ListItemButton
selected={idx === selectedIdx}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleChange(value);
tipSelect &&
tipSelect(
value,
object$.current,
payload,
handleChangeObj
);
handleClose();
}}
>
<ListItemText primary={value} />
</ListItemButton>
</ListItem>
))}
</VirtualView>
)}
</Popover>
</>
);
};
export default Complete;