Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

3.0 KB
Raw Blame
import { cloneElement, isValidElement, type ReactElement, type ReactNode, useId } from 'react';

import { cn } from '../../lib/cn';

export interface FieldProps {
  htmlFor?: string;
  label?: string;
  description?: string;
  control: ReactNode;
  stacked?: boolean;
  disabled?: boolean;
  /**
   * Validation message, rendered in coral below the control. The control gets
   * `aria-describedby` (merged with any it already has) and `aria-invalid`
   * when `control` is a single React element. If it is a fragment or a
   * component that drops those props, wire it yourself: the message id is
   * `${htmlFor}-error` when `htmlFor` is set, otherwise a generated id.
   */
  error?: string;
  className?: string;
  'data-testid'?: string;
}

/**
 * A labelled control row — label + optional description on one side, the
 * control on the other, or stacked. Generalized out of
 * `settings/controls/SettingsRow`, which now re-exports this.
 *
 * `htmlFor` produces a real `<label>`; without it the label is a `<span>`, so a
 * caller that forgets the association does not get a label pointing nowhere.
 */
const Field = ({
  htmlFor,
  label,
  description,
  control,
  stacked = false,
  disabled = false,
  error,
  className,
  'data-testid': testId,
}: FieldProps) => {
  const generatedId = useId();
  const errorId = error ? (htmlFor ? `${htmlFor}-error` : `${generatedId}-error`) : undefined;

  let controlEl = control;
  if (errorId && isValidElement(control)) {
    const el = control as ReactElement<{ 'aria-describedby'?: string }>;
    const existing = el.props['aria-describedby'];
    controlEl = cloneElement(el, {
      'aria-describedby': existing ? `${existing} ${errorId}` : errorId,
      'aria-invalid': true,
    } as Record<string, unknown>);
  }

  const labelEl =
    label && htmlFor ? (
      <label htmlFor={htmlFor} className="text-sm font-medium text-content">
        {label}
      </label>
    ) : label ? (
      <span className="text-sm font-medium text-content">{label}</span>
    ) : null;

  return (
    <div
      data-slot="field"
      data-testid={testId}
      className={cn(
        stacked
          ? 'flex flex-col gap-2 px-4 py-3'
          : 'flex items-center justify-between gap-4 px-4 py-3',
        // Only an errored row wraps, so the error drops below the control row
        // while error-free rows keep their exact geometry.
        error && !stacked && 'flex-wrap',
        disabled && 'pointer-events-none opacity-50',
        className
      )}>
      {(labelEl || description) && (
        <div className={stacked ? undefined : 'min-w-0 flex-1'}>
          {labelEl}
          {description && (
            <p className="mt-0.5 text-xs leading-relaxed text-content-muted">{description}</p>
          )}
        </div>
      )}
      <div className={stacked ? 'w-full' : 'shrink-0'}>{controlEl}</div>
      {error && (
        <p id={errorId} data-slot="field-error" className="w-full text-xs text-coral-600">
          {error}
        </p>
      )}
    </div>
  );
};

export default Field;