import React, { Children, cloneElement, isValidElement } from "react";
import Label from '../Label';
import Hint from '../Hint';
import RadioItemProps from "./RadioItem.types";
export const RadioItem = ({
id,
name,
children,
key,
divider,
value,
checked,
conditional,
classes,
...attributes }: RadioItemProps) => {
if (!id && name) {
id = name;
} else if (!name && id) {
name = id;
}
const conditionalId = `conditional-${id}`
const arrayChildren: any = Children.toArray(children);
return (
{ !divider && (
<>
{ Children.map(arrayChildren, (child:any, _index) => {
if (isValidElement(child) && child.type === Label) {
return (
<>
{
cloneElement(child as React.ReactElement
, {
classes: 'govuk-radios__label',
htmlFor: id
})
}
>
);
} else if (isValidElement(child) && child.type === Hint) {
return (
<>
{
cloneElement(child as React.ReactElement, {
classes: 'govuk-radios__hint'
})
}
>
);
}
})}
{ conditional && (
{ conditional }
)}
>
)}
{ divider && (
<>
{ children }
>
)}
);
};
export default RadioItem;