component — conform

Conform Textarea

The quebi Textarea wired to Conform. Binds a multi-line string field's name, required, default, and validity from field metadata and renders inline errors.

  • form
  • conform
  • textarea
  • multiline
  • validation

01 example

Bound to a Conform form

Submit a short bio to see the length error wired from field metadata.

Shown on your public profile.

source

conform-textarea.tsx

Copy this into your project. Resolve its dependencies from the registryDependencies in the component's API entry.

"use client"

import type { FieldMetadata } from "@conform-to/react"
import { composeRenderProps, TextField, type TextFieldProps } from "react-aria-components"
import { cn } from "@/lib/utils"
import { Description, FieldError, fieldStyles, Label } from "@/components/field"
import { Textarea } from "@/components/textarea"

export interface ConformTextareaProps
  extends Omit<
    TextFieldProps,
    "name" | "form" | "type" | "value" | "defaultValue" | "isRequired" | "isInvalid" | "children"
  > {
  /** A multi-line text field bound to a string form value. */
  field: FieldMetadata<string>
  label?: string
  placeholder?: string
  description?: string
  rows?: number
}

/**
 * ConformTextarea — Textarea wired to Conform.
 *
 * Binds a string Conform field to the quebi Textarea: derives name, id, form,
 * required, default, and validity from the field metadata and renders an inline
 * label, optional description, and field errors.
 *
 * `conform-field` cannot cover this case — its `type` union is text/email/
 * password, which is a single-line input by definition.
 */
export function ConformTextarea({
  field,
  label,
  placeholder,
  description,
  rows,
  className,
  ...props
}: ConformTextareaProps) {
  const hasErrors = !field.valid && !!field.errors
  const isRequired = field.required ?? false

  return (
    <TextField
      {...props}
      id={field.id}
      name={field.name}
      form={field.formId}
      defaultValue={(field.initialValue as string) ?? ""}
      isRequired={isRequired}
      isInvalid={hasErrors}
      className={composeRenderProps(className, (resolved) => cn(fieldStyles, resolved))}
    >
      {label && (
        <Label className={cn(hasErrors && "text-quebi-danger")}>
          {label}
          {isRequired && <span className="ml-1 text-quebi-fg">*</span>}
        </Label>
      )}
      <Textarea placeholder={placeholder} rows={rows} />
      {/* No ids and no aria-describedby here: this is a react-aria field, so it
          generates the description and error ids and already points the control
          at them. Setting id={field.errorId} would not break that — on mount
          react-aria re-points the control at whatever id the element actually
          carries — it would just duplicate wiring that is already correct.
          Outside a react-aria field the ids are yours: see ConformSwitch. */}
      {description && <Description>{description}</Description>}
      {hasErrors && <FieldError>{field.errors?.join(", ")}</FieldError>}
    </TextField>
  )
}