-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathPathService.tsx
166 lines (147 loc) · 5.08 KB
/
PathService.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
// /src/components/PathService.tsx
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { SearchInput } from '@patternfly/react-core/dist/dynamic/components/SearchInput';
import { List } from '@patternfly/react-core/dist/dynamic/components/List';
import { ListItem } from '@patternfly/react-core/dist/dynamic/components/List';
import { Popper, PopperProps, ValidatedOptions } from '@patternfly/react-core/dist/dynamic/helpers';
import { FormHelperText } from '@patternfly/react-core/dist/dynamic/components/Form';
import { HelperText } from '@patternfly/react-core/dist/dynamic/components/HelperText';
import { HelperTextItem } from '@patternfly/react-core/dist/dynamic/components/HelperText';
import ExclamationCircleIcon from '@patternfly/react-icons/dist/dynamic/icons/exclamation-circle-icon';
interface PathServiceProps {
reset?: boolean;
rootPath: string;
path?: string;
handlePathChange: (value: string) => void;
}
const PathService: React.FC<PathServiceProps> = ({ reset, rootPath, path, handlePathChange }) => {
const [inputValue, setInputValue] = useState<string>('');
const [items, setItems] = useState<string[]>([]);
const [showDropdown, setShowDropdown] = useState<boolean>(false);
const inputRef = useRef<HTMLInputElement>(null);
const [validPath, setValidPath] = React.useState<ValidatedOptions>();
const validatePath = useCallback(() => {
if (inputValue.trim().length > 0) {
setValidPath(ValidatedOptions.success);
return;
}
setValidPath(ValidatedOptions.error);
}, [inputValue]);
const fetchData = useCallback(
async (subpath: string) => {
try {
const response = await fetch('/api/tree', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ root_path: rootPath, dir_name: subpath })
});
if (!response.ok) {
console.warn('Failed to get path service tree for subpath ( ' + subpath + ' ) from server.');
}
const result = await response.json();
// set items to be displayed in the dropdown
if (result.data === null || result.data.length === 0) {
setItems([]);
return;
}
setItems(result.data.map((item: string) => item.valueOf()));
} catch (error) {
console.warn('Error fetching path service data:', error);
setItems([]);
}
},
[rootPath]
);
useEffect(() => {
setInputValue('');
setShowDropdown(false);
}, [reset]);
useEffect(() => {
if (path) {
setInputValue(path);
setValidPath(ValidatedOptions.success);
}
const handleEsc = (event: { key: string }) => {
if (event.key === 'Escape') {
setShowDropdown(false);
}
};
window.addEventListener('keydown', handleEsc);
return () => {
window.removeEventListener('keydown', handleEsc);
};
}, [path]);
useEffect(() => {
// check if input value is empty or ends with a slash
if (inputValue.endsWith('/')) {
fetchData(inputValue);
setShowDropdown(true);
handlePathChange(inputValue);
} else {
setItems([]);
}
validatePath();
}, [inputValue, fetchData, handlePathChange, validatePath]);
const handleChange = (value: string) => {
setInputValue(value);
};
const handleFocus = (event: React.FocusEvent<HTMLDivElement, Element>) => {
setShowDropdown(true);
setInputValue((event.target as HTMLInputElement).value);
// check if input value is empty
if ((event.target as HTMLInputElement).value === '') {
fetchData('');
}
};
const handleSelect = (item: string) => {
setShowDropdown(false);
setInputValue(inputValue + item + '/');
};
const handleBlurEvent = () => {
setShowDropdown(false);
handlePathChange(inputValue);
validatePath();
};
const popperProps: PopperProps = {
triggerRef: inputRef,
popper: (
<List style={{ border: '1px solid #ccc', backgroundColor: 'white', maxHeight: '20%', overflow: 'auto' }}>
{items.map((item, index) => (
<ListItem key={index} onClick={() => handleSelect(item)} style={{ padding: '5px 10px', cursor: 'pointer' }}>
{item}
</ListItem>
))}
</List>
),
width: 'trigger',
preventOverflow: true,
isVisible: showDropdown,
onPopperClick: () => handleBlurEvent()
};
return (
<div>
<SearchInput
ref={inputRef}
placeholder="Type to find taxonomy path"
value={inputValue}
onChange={(_event, value) => handleChange(value)}
onFocus={(_event) => handleFocus(_event)}
onClear={() => setInputValue('')}
onBlur={() => handlePathChange(inputValue)}
/>
{validPath === 'error' && (
<FormHelperText>
<HelperText>
<HelperTextItem icon={<ExclamationCircleIcon />} variant={validPath}>
Required field and must be a valid file path.
</HelperTextItem>
</HelperText>
</FormHelperText>
)}
<Popper {...popperProps} />
</div>
);
};
export default PathService;