import { useLingui } from '@lingui/react'; import type { DocumentMeta, Signature, TemplateMeta } from '@prisma/client'; import { FieldType } from '@prisma/client'; import { ChevronDown } from 'lucide-react'; import { DEFAULT_DOCUMENT_DATE_FORMAT, convertToLocalSystemFormat, } from '@documenso/lib/constants/date-formats'; import type { TFieldMetaSchema } from '@documenso/lib/types/field-meta'; import { fromCheckboxValue } from '@documenso/lib/universal/field-checkbox'; import { cn } from '../../lib/utils'; import { Checkbox } from '../checkbox'; import { Label } from '../label'; import { RadioGroup, RadioGroupItem } from '../radio-group'; import { FRIENDLY_FIELD_TYPE } from './types'; type FieldIconProps = { /** * Loose field type since this is used for partial fields. */ field: { inserted?: boolean; customText?: string; type: FieldType; fieldMeta?: TFieldMetaSchema | null; signature?: Signature | null; }; documentMeta?: Pick; }; /** * Renders the content inside field containers prior to sealing. */ export const FieldContent = ({ field, documentMeta }: FieldIconProps) => { const { _ } = useLingui(); const { type, fieldMeta } = field; // Render checkbox layout for checkbox fields, even if no values exist yet if (field.type === FieldType.CHECKBOX && field.fieldMeta?.type === 'checkbox') { let checkedValues: string[] = []; try { checkedValues = fromCheckboxValue(field.customText ?? ''); } catch (err) { // Do nothing. console.error(err); } // If no values exist yet, show a placeholder checkbox if (!field.fieldMeta.values || field.fieldMeta.values.length === 0) { return (
); } return (
{field.fieldMeta.values.map((item, index) => (
{item.value && ( )}
))}
); } // Only render radio if values exist, otherwise render the empty radio field content. if ( field.type === FieldType.RADIO && field.fieldMeta?.type === 'radio' && field.fieldMeta.values && field.fieldMeta.values.length > 0 ) { return (
{field.fieldMeta.values.map((item, index) => (
{item.value && ( )}
))}
); } if ( field.type === FieldType.DROPDOWN && field.fieldMeta?.type === 'dropdown' && !field.inserted ) { return (

Select

); } if ( field.type === FieldType.SIGNATURE && field.signature?.signatureImageAsBase64 && field.inserted ) { return ( Signature ); } const labelToDisplay = fieldMeta?.label || _(FRIENDLY_FIELD_TYPE[type]) || ''; let textToDisplay: string | undefined; const isSignatureField = field.type === FieldType.SIGNATURE || field.type === FieldType.FREE_SIGNATURE; if (field.type === FieldType.TEXT && field.fieldMeta?.type === 'text' && field.fieldMeta?.text) { textToDisplay = field.fieldMeta.text; } if (field.inserted) { if (field.customText) { textToDisplay = field.customText; } if (field.type === FieldType.DATE) { textToDisplay = convertToLocalSystemFormat( field.customText ?? '', documentMeta?.dateFormat ?? DEFAULT_DOCUMENT_DATE_FORMAT, ); } if (isSignatureField && field.signature?.typedSignature) { textToDisplay = field.signature.typedSignature; } } const textAlign = fieldMeta && 'textAlign' in fieldMeta ? fieldMeta.textAlign : 'left'; return (

{textToDisplay || labelToDisplay}

); };