-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiauto.tsx
36 lines (31 loc) · 1.09 KB
/
Apiauto.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
export const DynamicControl = ({
inputType,
fieldName,
defaultValue,
options = [],
config = {}
}: DynamicFieldData) => {
const { register, setValue } = useFormContext();
const [nextFieldOptions, setNextFieldOptions] = useState([]);
const watchedValue = useWatch({
name: config.watchField, // Watch the specified field
defaultValue: defaultValue // Provide a default value
});
useEffect(() => {
if (config.loadOptionsFromAPI && watchedValue) {
const apiUrl = apiUrls[config.loadOptionsFromAPI]; // Fetch URL based on key
// Make the API call using the watchedValue
// Update the options for the next field based on the API response
}
}, [watchedValue, config.loadOptionsFromAPI]);
const handleSelectChange = (event) => {
const selectedValue = event.target.value;
setValue(fieldName, selectedValue);
if (config.setNextField) {
const nextFieldName = config.setNextField.fieldName;
const nextFieldValue = config.setNextField.fieldValue;
setValue(nextFieldName, nextFieldValue);
}
};
// Rest of the code remains the same
};