All files / src/components/ui/FormField index.tsx

100% Statements 154/154
90.9% Branches 10/11
100% Functions 2/2
100% Lines 154/154

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 1551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 57x 57x 57x 57x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 6x 6x 6x 3x 6x 6x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 400x 400x 400x 400x 400x 400x 400x 400x 400x 400x 400x 400x 400x 400x  
'use client';
 
import { forwardRef } from 'react';
import { Input } from '../Input';
import { InputInputProps, TextFieldProps } from '@/types/ui';
import { cn } from '@/lib/core';
 
/**
 * FormField - Wrapper around Input with consistent layout
 *
 * Simplifies form creation by providing a standard field layout.
 *
 * @example
 * ```tsx
 * <FormField
 *   name="email"
 *   label="Email Address"
 *   type="email"
 *   required
 *   placeholder="you@example.com"
 * />
 * ```
 */
export const FormField = forwardRef<HTMLInputElement, InputInputProps>(
  (props, ref) => {
    return (
      <div className="mb-4">
        <Input ref={ref as React.Ref<HTMLInputElement>} {...props} />
      </div>
    );
  }
);
 
FormField.displayName = 'FormField';
 
/**
 * TextField - Email/text input field
 */
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
  ({ type = 'text', ...props }, ref) => {
    return <FormField ref={ref} type={type} {...props} />;
  }
);
 
TextField.displayName = 'TextField';
 
/**
 * EmailField - Email input field
 */
export const EmailField = forwardRef<HTMLInputElement, Omit<InputInputProps, 'type' | 'as'>>(
  (props, ref) => {
    return <FormField ref={ref} type="email" {...props} />;
  }
);
 
EmailField.displayName = 'EmailField';
 
/**
 * PasswordField - Password input field
 */
export const PasswordField = forwardRef<HTMLInputElement, Omit<InputInputProps, 'type' | 'as'>>(
  (props, ref) => {
    return <FormField ref={ref} type="password" {...props} />;
  }
);
 
PasswordField.displayName = 'PasswordField';
 
/**
 * SearchField - Search input field
 */
export const SearchField = forwardRef<HTMLInputElement, Omit<InputInputProps, 'type' | 'as'>>(
  (props, ref) => {
    return <FormField ref={ref} type="search" {...props} />;
  }
);
 
SearchField.displayName = 'SearchField';
 
/**
 * NumberField - Number input field
 */
export interface NumberFieldProps extends Omit<InputInputProps, 'type' | 'as'> {
  min?: number;
  max?: number;
  step?: number;
}
 
export const NumberField = forwardRef<HTMLInputElement, NumberFieldProps>(
  (props, ref) => {
    return <FormField ref={ref} type="number" {...props} />;
  }
);
 
NumberField.displayName = 'NumberField';
 
/**
 * FormGroup - Group multiple form fields
 */
export interface FormGroupProps {
  /** Group label */
  label?: string;
  /** Children form fields */
  children: React.ReactNode;
  /** Optional description */
  description?: string;
  /** Additional CSS classes */
  className?: string;
}
 
export function FormGroup({ label, description, children, className }: FormGroupProps) {
  return (
    <div className={cn('mb-6', className)}>
      {label && (
        <div className="mb-4">
          <h3 className="text-lg font-medium text-dark">{label}</h3>
          {description && (
            <p className="text-sm text-gray-600 mt-1">{description}</p>
          )}
        </div>
      )}
      <div className="space-y-4">
        {children}
      </div>
    </div>
  );
}
 
/**
 * FormRow - Horizontal row of form fields
 */
export interface FormRowProps {
  /** Children form fields */
  children: React.ReactNode;
  /** Gap between fields */
  gap?: number;
  /** Additional CSS classes */
  className?: string;
}
 
export function FormRow({ children, gap = 4, className }: FormRowProps) {
  const gapMap: Record<number, string> = {
    2: 'gap-2',
    3: 'gap-3',
    4: 'gap-4',
    6: 'gap-6',
    8: 'gap-8'};
 
  return (
    <div className={cn('grid grid-cols-1 md:grid-cols-2', gapMap[gap] || 'gap-4', className)}>
      {children}
    </div>
  );
}