import React, {Children, cloneElement, isValidElement} from "react";
import FileUploadProps from "./FileUpload.types";
import FormGroup from "../../layout/FormGroup";
import Label from "../Label";
import ErrorMessage from "../ErrorMessage";
import Hint from "../Hint";
export const FileUpload = ({
name,
id,
value,
describedBy,
errorMessage,
children,
classes,
formGroupClasses,
...attributes
}: FileUploadProps) => {
let describedByValue = '';
let errorMessageComponent;
if (errorMessage) {
const errorId = id ? `${id}-error` : '';
describedByValue += ` ${errorId}`;
errorMessageComponent = {errorMessage};
}
const arrayChildren = Children.toArray(children);
{ Children.map(arrayChildren, (child:any, _index) => {
if (isValidElement(child) && (child.type === Hint)) {
describedByValue += ` ${id}-hint`;
}
})}
return (
{ Children.map(arrayChildren, (child:any, _index) => {
if (isValidElement(child) && ((child.type === Label) || (child.type === Hint))) {
return (
<>
{
cloneElement(child as React.ReactElement, {})
}
>
);
}
})}
{ errorMessageComponent }
);
};
export default FileUpload;