@festo-ui/react 10.1.0 → 10.1.1-dev.918

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.
@@ -1,85 +0,0 @@
1
- import { useState } from "react";
2
- import { useControlled } from "../../utils/useControlled.js";
3
- function useSearchInput(inputRef, cappedSuggestions, value, defaultValue = '', onChange, onSearch, onKeyboardNavigate) {
4
- const [innerValue, setValue] = useControlled({
5
- controlled: value,
6
- default: defaultValue
7
- });
8
- const [hideSuggestionList, setHideSuggestionList] = useState(false);
9
- const [selectedSuggestionIndex, setSelectedSuggestionIndex] = useState(-1);
10
- function blurInput() {
11
- if (inputRef.current) inputRef.current.blur();
12
- }
13
- function reset() {
14
- setSelectedSuggestionIndex(-1);
15
- setHideSuggestionList(true);
16
- blurInput();
17
- }
18
- function handleSearch() {
19
- reset();
20
- if (void 0 !== innerValue) onSearch?.(innerValue);
21
- }
22
- function updateValue(newValue) {
23
- setValue(newValue);
24
- onChange?.(newValue);
25
- onSearch?.(newValue);
26
- }
27
- function handleClearQuery() {
28
- reset();
29
- updateValue('');
30
- }
31
- function handleArrowKey(newSuggestionIndex) {
32
- if (!cappedSuggestions || !cappedSuggestions.length) return;
33
- const selectedSuggestion = cappedSuggestions[newSuggestionIndex];
34
- setSelectedSuggestionIndex(newSuggestionIndex);
35
- setValue(selectedSuggestion.value);
36
- onKeyboardNavigate?.(selectedSuggestion.value);
37
- }
38
- function handleUpArrowKey() {
39
- handleArrowKey(selectedSuggestionIndex > 0 ? selectedSuggestionIndex - 1 : cappedSuggestions.length - 1);
40
- }
41
- function handleDownArrowKey() {
42
- handleArrowKey(selectedSuggestionIndex >= cappedSuggestions.length - 1 ? 0 : selectedSuggestionIndex + 1);
43
- }
44
- function handleKeyDown(event) {
45
- switch(event.key){
46
- case 'Enter':
47
- handleSearch();
48
- break;
49
- case 'Escape':
50
- handleClearQuery();
51
- break;
52
- case 'ArrowUp':
53
- handleUpArrowKey();
54
- break;
55
- case 'ArrowDown':
56
- handleDownArrowKey();
57
- break;
58
- default:
59
- break;
60
- }
61
- }
62
- const handleFocus = ()=>setHideSuggestionList(false);
63
- const handleOutsideClick = ()=>setHideSuggestionList(true);
64
- function handleSuggestionClick(suggestion) {
65
- reset();
66
- updateValue(suggestion.value);
67
- }
68
- function handleInput(event) {
69
- setSelectedSuggestionIndex(-1);
70
- setValue(event.target.value);
71
- onChange?.(event.target.value);
72
- }
73
- return {
74
- innerValue,
75
- hideSuggestionList,
76
- selectedSuggestionIndex,
77
- handleClearQuery,
78
- handleFocus,
79
- handleInput,
80
- handleSuggestionClick,
81
- handleOutsideClick,
82
- handleKeyDown
83
- };
84
- }
85
- export { useSearchInput };