/* eslint-disable complexity */ import type { ReactNode, HTMLAttributes } from 'react' import React, { forwardRef, Children } from 'react' import type { BaseProps } from '@toptal/picasso-shared' import { Container } from '@toptal/picasso-container' import { twJoin, twMerge } from '@toptal/picasso-tailwind-merge' import { useFieldsLayoutContext } from '@toptal/picasso-form-layout' import { FormHint } from '../FormHint' import { FormError } from '../FormError' import { createLabelWidthStyles, horizontalLayoutClasses } from './styles' import { FormWarning } from '../FormWarning' export interface Props extends BaseProps, HTMLAttributes { /** The text of the hint */ hint?: string | ReactNode /** The text of the error */ error?: string | ReactNode /** The text of the warning */ warning?: string | ReactNode /** The content of the Form.Field */ children: ReactNode /** Field requirements for this specific field */ fieldRequirements?: ReactNode /** instance of FormAutoSaveIndicator component */ autoSaveIndicator?: ReactNode /** whether multiline counter is visible */ hasMultilineCounter?: boolean } type FormFieldAdornmentsProps = Pick< Props, 'autoSaveIndicator' | 'hasMultilineCounter' | 'children' > const FormFieldAdornments = ({ autoSaveIndicator, children, hasMultilineCounter, }: FormFieldAdornmentsProps) => { const { layout } = useFieldsLayoutContext() const isHorizontal = layout === 'horizontal' if (Children.toArray(children).length === 0) { return null } if (!autoSaveIndicator) { return (
{children}
) } return ( {children} {autoSaveIndicator} ) } export const FormField = forwardRef(function FormField( props, ref ) { const { autoSaveIndicator, className, style, hint, children, error, warning, fieldRequirements, hasMultilineCounter, ...rest } = props const { layout, labelWidth } = useFieldsLayoutContext() const isHorizontal = layout === 'horizontal' const labelWidthStyles = isHorizontal ? createLabelWidthStyles(labelWidth) : {} return (
{children} {error && {error}} {warning && ( {warning} )} {hint && ( {hint} )} {fieldRequirements}
) }) FormField.displayName = 'FormField' export default FormField