2025-12-13 02:34:34 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
2025-12-03 21:56:50 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* SwitchProps - Propriétés du composant Switch
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @interface SwitchProps
|
|
|
|
|
* @extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'>
|
|
|
|
|
*/
|
2026-01-13 18:47:57 +00:00
|
|
|
export interface SwitchProps
|
|
|
|
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'> {
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* État checked du switch
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <Switch checked={isEnabled} onCheckedChange={setIsEnabled} />
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
checked?: boolean;
|
2026-01-13 18:47:57 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
/**
|
|
|
|
|
* Fonction appelée lorsque l'état checked change
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @param {boolean} checked - Nouvel état checked
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* <Switch onCheckedChange={(checked) => console.log('Switch:', checked)} />
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
onCheckedChange?: (checked: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Switch - Composant d'interrupteur avec design system Kodo
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* Composant d'interrupteur (toggle) pour activer/désactiver une option.
|
|
|
|
|
* Utilise le design system Kodo avec une animation de transition fluide.
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @example
|
|
|
|
|
* ```tsx
|
|
|
|
|
* // Switch simple
|
|
|
|
|
* <Switch />
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* // Switch contrôlé
|
2026-01-13 18:47:57 +00:00
|
|
|
* <Switch
|
|
|
|
|
* checked={isEnabled}
|
2026-01-07 09:31:02 +00:00
|
|
|
* onCheckedChange={setIsEnabled}
|
|
|
|
|
* />
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* // Switch désactivé
|
|
|
|
|
* <Switch disabled />
|
|
|
|
|
* ```
|
2026-01-13 18:47:57 +00:00
|
|
|
*
|
2026-01-07 09:31:02 +00:00
|
|
|
* @component
|
|
|
|
|
* @param {SwitchProps} props - Propriétés du composant
|
|
|
|
|
* @returns {JSX.Element} Élément label contenant un switch stylisé
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(
|
|
|
|
|
({ className, checked, onCheckedChange, disabled, ...props }, ref) => {
|
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
if (onCheckedChange) {
|
|
|
|
|
onCheckedChange(e.target.checked);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-13 18:47:57 +00:00
|
|
|
<label
|
|
|
|
|
className={cn(
|
2026-02-08 21:56:36 +00:00
|
|
|
'peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors',
|
|
|
|
|
'focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 focus-within:ring-offset-background',
|
2026-01-13 18:47:57 +00:00
|
|
|
'disabled:cursor-not-allowed disabled:opacity-50',
|
2026-02-08 21:56:36 +00:00
|
|
|
checked ? 'bg-primary' : 'bg-muted',
|
2026-01-13 18:47:57 +00:00
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
2026-01-07 09:31:02 +00:00
|
|
|
<input
|
|
|
|
|
ref={ref}
|
|
|
|
|
type="checkbox"
|
|
|
|
|
className="sr-only"
|
|
|
|
|
checked={checked}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
<span
|
|
|
|
|
className={cn(
|
|
|
|
|
'pointer-events-none block h-5 w-5 rounded-full bg-white shadow-lg ring-0 transition-transform',
|
|
|
|
|
checked ? 'translate-x-5' : 'translate-x-0',
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
);
|
2026-01-13 18:47:57 +00:00
|
|
|
},
|
2026-01-07 09:31:02 +00:00
|
|
|
);
|
|
|
|
|
Switch.displayName = 'Switch';
|
2025-12-03 21:56:50 +00:00
|
|
|
|
2025-12-13 02:34:34 +00:00
|
|
|
export { Switch };
|