bigbuffet-rw/app/soapbox/components/ui/form-group/form-group.tsx

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-03-21 11:09:01 -07:00
import React, { useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';
interface IFormGroup {
/** Input label message. */
2022-05-05 15:45:32 -07:00
labelText?: React.ReactNode,
/** Input hint message. */
hintText?: React.ReactNode,
/** Input errors. */
2022-03-21 11:09:01 -07:00
errors?: string[]
}
2022-05-06 16:51:06 -07:00
/** Input container with label. Renders the child. */
2022-03-21 11:09:01 -07:00
const FormGroup: React.FC<IFormGroup> = (props) => {
const { children, errors = [], labelText, hintText } = props;
const formFieldId: string = useMemo(() => `field-${uuidv4()}`, []);
const inputChildren = React.Children.toArray(children);
let firstChild;
if (React.isValidElement(inputChildren[0])) {
firstChild = React.cloneElement(
inputChildren[0],
{ id: formFieldId },
);
}
return (
<div>
2022-05-05 15:45:32 -07:00
{labelText && (
<label
htmlFor={formFieldId}
data-testid='form-group-label'
className='block text-sm font-medium text-gray-700 dark:text-gray-400'
>
{labelText}
</label>
)}
2022-03-21 11:09:01 -07:00
<div className='mt-1 dark:text-white'>
2022-03-21 11:09:01 -07:00
{firstChild}
{inputChildren.filter((_, i) => i !== 0)}
{errors?.length > 0 && (
<p
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'
>
2022-03-21 11:09:01 -07:00
{errors.join(', ')}
</p>
)}
2022-05-05 15:45:32 -07:00
{hintText && (
<p data-testid='form-group-hint' className='mt-0.5 text-xs text-gray-400'>
2022-03-21 11:09:01 -07:00
{hintText}
</p>
2022-05-05 15:45:32 -07:00
)}
2022-03-21 11:09:01 -07:00
</div>
</div>
);
};
export default FormGroup;