component — conform

Conform Search Field

The quebi SearchField wired to Conform. Binds a string field's name, required, default, and validity from field metadata — for a query that is part of a submitted form.

  • form
  • conform
  • search
  • validation

01 example

Bound to a Conform form

Submit a one-letter query to see the validation error wired from field metadata.

Part of a submitted form — a filter box would be component state instead.

source

conform-search-field.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 } from "react-aria-components"
import { cn } from "@/lib/utils"
import { Description, FieldError, fieldStyles, Label } from "@/components/field"
import type { InputSize } from "@/components/input"
import { SearchField, type SearchFieldProps, SearchInput } from "@/components/search-field"

export interface ConformSearchFieldProps
  extends Omit<
    SearchFieldProps,
    "name" | "form" | "value" | "defaultValue" | "isRequired" | "isInvalid" | "children"
  > {
  /** A search field bound to a string form value. */
  field: FieldMetadata<string>
  label?: string
  placeholder?: string
  description?: string
  /** Control height. Matches `Input`'s scale and `Button`'s `xs` / `sm`. */
  size?: InputSize
}

/**
 * ConformSearchField — SearchField wired to Conform.
 *
 * Binds a string Conform field to the quebi SearchField: derives name, id,
 * form, required, default, and validity from the field metadata and renders
 * inline errors.
 *
 * Only reach for this when the query is part of a submitted form — a filter box
 * that drives a client-side query has no Conform field behind it and belongs in
 * component state (see the rule's documented exception).
 */
export function ConformSearchField({
  field,
  label,
  placeholder,
  description,
  size,
  className,
  ...props
}: ConformSearchFieldProps) {
  const hasErrors = !field.valid && !!field.errors
  const isRequired = field.required ?? false

  return (
    <SearchField
      {...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>
      )}
      <SearchInput placeholder={placeholder} size={size} />
      {/* 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>}
    </SearchField>
  )
}