2025-12-03 21:56:50 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* FormFieldProps - Propriétés du composant FormField
|
|
|
|
|
*
|
|
|
|
|
* @interface FormFieldProps
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
interface FormFieldProps {
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* Label du champ de formulaire
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <FormField label="Email">
|
|
|
|
|
* <Input type="email" />
|
|
|
|
|
* </FormField>
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
label: string;
|
2026-01-07 09:31:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Message d'erreur à afficher sous le champ
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <FormField label="Email" error={errors.email}>
|
|
|
|
|
* <Input type="email" />
|
|
|
|
|
* </FormField>
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
error?: string;
|
2026-01-07 09:31:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Si `true`, affiche un indicateur requis (*)
|
|
|
|
|
*
|
|
|
|
|
* @default false
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
required?: boolean;
|
2026-01-07 09:31:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Champ de formulaire enfant (Input, Textarea, Select, etc.)
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
children: React.ReactNode;
|
2026-01-07 09:31:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Classes CSS personnalisées
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
className?: string;
|
2026-01-07 09:31:02 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Texte d'aide à afficher sous le champ (si pas d'erreur)
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <FormField
|
|
|
|
|
* label="Mot de passe"
|
|
|
|
|
* helpText="Minimum 8 caractères"
|
|
|
|
|
* >
|
|
|
|
|
* <Input type="password" />
|
|
|
|
|
* </FormField>
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
2025-12-03 21:56:50 +00:00
|
|
|
helpText?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* FormField - Composant de champ de formulaire avec label et validation
|
|
|
|
|
*
|
|
|
|
|
* Composant wrapper pour les champs de formulaire avec support pour :
|
|
|
|
|
* - Label avec indicateur requis
|
|
|
|
|
* - Message d'erreur
|
|
|
|
|
* - Texte d'aide
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* // Champ simple
|
|
|
|
|
* <FormField label="Nom">
|
|
|
|
|
* <Input type="text" />
|
|
|
|
|
* </FormField>
|
|
|
|
|
*
|
|
|
|
|
* // Champ requis avec erreur
|
|
|
|
|
* <FormField
|
|
|
|
|
* label="Email"
|
|
|
|
|
* required
|
|
|
|
|
* error={errors.email}
|
|
|
|
|
* >
|
|
|
|
|
* <Input type="email" />
|
|
|
|
|
* </FormField>
|
|
|
|
|
*
|
|
|
|
|
* // Champ avec texte d'aide
|
|
|
|
|
* <FormField
|
|
|
|
|
* label="Description"
|
|
|
|
|
* helpText="Maximum 500 caractères"
|
|
|
|
|
* >
|
|
|
|
|
* <Textarea />
|
|
|
|
|
* </FormField>
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* @component
|
|
|
|
|
* @param {FormFieldProps} props - Propriétés du composant
|
|
|
|
|
* @returns {JSX.Element} Élément div contenant le label, le champ et les messages
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-03 21:56:50 +00:00
|
|
|
export const FormField: React.FC<FormFieldProps> = ({
|
|
|
|
|
label,
|
|
|
|
|
error,
|
|
|
|
|
required = false,
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
helpText,
|
|
|
|
|
}) => {
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn('space-y-2', className)}>
|
|
|
|
|
<label className="text-sm font-medium text-gray-700 dark:text-gray-300">
|
|
|
|
|
{label}
|
|
|
|
|
{required && <span className="text-red-500 ml-1">*</span>}
|
|
|
|
|
</label>
|
|
|
|
|
{children}
|
|
|
|
|
{helpText && !error && (
|
|
|
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">{helpText}</p>
|
|
|
|
|
)}
|
|
|
|
|
{error && (
|
|
|
|
|
<p className="text-xs text-red-500 dark:text-red-400">{error}</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
|
|
|
error?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
export const Input: React.FC<InputProps> = ({
|
|
|
|
|
error = false,
|
|
|
|
|
className,
|
|
|
|
|
...props
|
2025-12-03 21:56:50 +00:00
|
|
|
}) => {
|
|
|
|
|
return (
|
|
|
|
|
<input
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full px-3 py-2 border rounded-md shadow-sm transition-colors',
|
|
|
|
|
'focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent',
|
|
|
|
|
'dark:bg-gray-800 dark:border-gray-600 dark:text-white',
|
|
|
|
|
error
|
|
|
|
|
? 'border-red-500 focus:ring-red-500'
|
|
|
|
|
: 'border-gray-300 dark:border-gray-600',
|
2025-12-13 02:34:34 +00:00
|
|
|
className,
|
2025-12-03 21:56:50 +00:00
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
interface TextareaProps
|
|
|
|
|
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
2025-12-03 21:56:50 +00:00
|
|
|
error?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
export const Textarea: React.FC<TextareaProps> = ({
|
|
|
|
|
error = false,
|
|
|
|
|
className,
|
|
|
|
|
...props
|
2025-12-03 21:56:50 +00:00
|
|
|
}) => {
|
|
|
|
|
return (
|
|
|
|
|
<textarea
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full px-3 py-2 border rounded-md shadow-sm transition-colors resize-none',
|
|
|
|
|
'focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent',
|
|
|
|
|
'dark:bg-gray-800 dark:border-gray-600 dark:text-white',
|
|
|
|
|
error
|
|
|
|
|
? 'border-red-500 focus:ring-red-500'
|
|
|
|
|
: 'border-gray-300 dark:border-gray-600',
|
2025-12-13 02:34:34 +00:00
|
|
|
className,
|
2025-12-03 21:56:50 +00:00
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
|
|
|
|
|
error?: boolean;
|
|
|
|
|
options: Array<{ value: string; label: string }>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
export const Select: React.FC<SelectProps> = ({
|
|
|
|
|
error = false,
|
|
|
|
|
className,
|
2025-12-03 21:56:50 +00:00
|
|
|
options,
|
2025-12-13 02:34:34 +00:00
|
|
|
...props
|
2025-12-03 21:56:50 +00:00
|
|
|
}) => {
|
|
|
|
|
return (
|
|
|
|
|
<select
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full px-3 py-2 border rounded-md shadow-sm transition-colors',
|
|
|
|
|
'focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent',
|
|
|
|
|
'dark:bg-gray-800 dark:border-gray-600 dark:text-white',
|
|
|
|
|
error
|
|
|
|
|
? 'border-red-500 focus:ring-red-500'
|
|
|
|
|
: 'border-gray-300 dark:border-gray-600',
|
2025-12-13 02:34:34 +00:00
|
|
|
className,
|
2025-12-03 21:56:50 +00:00
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{options.map((option) => (
|
|
|
|
|
<option key={option.value} value={option.value}>
|
|
|
|
|
{option.label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
);
|
|
|
|
|
};
|