FormGroup: pass hasError to child component
This commit is contained in:
parent
5e531eda30
commit
d4ebfe474f
|
@ -15,12 +15,13 @@ const FormGroup: React.FC<IFormGroup> = (props) => {
|
||||||
const { children, errors = [], labelText, hintText } = props;
|
const { children, errors = [], labelText, hintText } = props;
|
||||||
const formFieldId: string = useMemo(() => `field-${uuidv4()}`, []);
|
const formFieldId: string = useMemo(() => `field-${uuidv4()}`, []);
|
||||||
const inputChildren = React.Children.toArray(children);
|
const inputChildren = React.Children.toArray(children);
|
||||||
|
const hasError = errors?.length > 0;
|
||||||
|
|
||||||
let firstChild;
|
let firstChild;
|
||||||
if (React.isValidElement(inputChildren[0])) {
|
if (React.isValidElement(inputChildren[0])) {
|
||||||
firstChild = React.cloneElement(
|
firstChild = React.cloneElement(
|
||||||
inputChildren[0],
|
inputChildren[0],
|
||||||
{ id: formFieldId },
|
{ id: formFieldId, hasError },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +41,7 @@ const FormGroup: React.FC<IFormGroup> = (props) => {
|
||||||
{firstChild}
|
{firstChild}
|
||||||
{inputChildren.filter((_, i) => i !== 0)}
|
{inputChildren.filter((_, i) => i !== 0)}
|
||||||
|
|
||||||
{errors?.length > 0 && (
|
{hasError && (
|
||||||
<p
|
<p
|
||||||
data-testid='form-group-error'
|
data-testid='form-group-error'
|
||||||
className='mt-0.5 text-xs text-danger-900 bg-danger-200 rounded-md inline-block px-2 py-1 relative form-error'
|
className='mt-0.5 text-xs text-danger-900 bg-danger-200 rounded-md inline-block px-2 py-1 relative form-error'
|
||||||
|
|
Loading…
Reference in New Issue