import React, { Children, cloneElement, isValidElement } from "react"; import RadiosProps from "./Radios.types"; import FormGroup from "../../layout/FormGroup"; import ErrorMessage from "../ErrorMessage"; import Fieldset from "../../layout/Fieldset"; import Hint from "../Hint"; import Legend from "../../layout/Legend"; import RadioItem from "../RadioItem"; export const Radios = ({ idPrefix, name, children, classes, value, errorMessage, ...attributes }: RadiosProps) => { if (!idPrefix && name) { idPrefix = name; } else if (!name && idPrefix) { name = idPrefix; } let describedByValue = ""; let errorMessageComponent; if (errorMessage) { const errorId = idPrefix ? `${idPrefix}-error` : ""; describedByValue += ` ${errorId}`; errorMessageComponent = {errorMessage}; } const arrayChildren: any = Children.toArray(children); const radioItemsArray: any = arrayChildren.filter((child: any) => child.type === RadioItem); { Children.map(arrayChildren, (child, _index) => { if (isValidElement(child) && child.type === Hint) { describedByValue += ` ${idPrefix}-hint`; } }); } return (
{Children.map(arrayChildren, (child: any, _index) => { if (isValidElement(child) && (child.type === Legend || child.type === Hint)) { return <>{cloneElement(child as React.ReactElement, {})}; } })} {errorMessageComponent}
{Children.map(radioItemsArray, (child: any, index) => { if (isValidElement(child) && child.type === RadioItem) { const radioItemId = child?.props?.id ? child?.props?.id : idPrefix; return ( <> {cloneElement(child as React.ReactElement, { id: index > 0 ? `${radioItemId}-${index}` : radioItemId, checked: child.props.value === value, name: name, })} ); } })}
); }; export default Radios;