feat(DateTimeInput): add calendar picker functionality to DateTimeInput component

- Introduced a ref to manage the input element for triggering the native calendar picker.
- Enhanced the Calendar icon with a click handler to open the date picker, improving user interaction.
- Updated styles for the Calendar icon to include hover effects for better visual feedback.
This commit is contained in:
Julien Froidefond
2025-10-10 08:24:33 +02:00
parent 52d8332f0c
commit e7cbd56e89

View File

@@ -1,6 +1,7 @@
'use client'; 'use client';
import { Calendar } from 'lucide-react'; import { Calendar } from 'lucide-react';
import { useRef } from 'react';
import { import {
formatDateForDateTimeInput, formatDateForDateTimeInput,
parseDateTimeInput, parseDateTimeInput,
@@ -23,6 +24,8 @@ export function DateTimeInput({
className = '', className = '',
onFocus, onFocus,
}: DateTimeInputProps) { }: DateTimeInputProps) {
const inputRef = useRef<HTMLInputElement>(null);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value const newValue = e.target.value
? parseDateTimeInput(e.target.value) ? parseDateTimeInput(e.target.value)
@@ -30,13 +33,22 @@ export function DateTimeInput({
onChange(newValue); onChange(newValue);
}; };
const handleCalendarClick = () => {
if (!disabled && inputRef.current) {
// Déclencher le calendrier natif du navigateur
inputRef.current.showPicker?.();
}
};
return ( return (
<div className={`relative flex items-center ${className}`}> <div className={`relative flex items-center ${className}`}>
<Calendar <Calendar
size={16} size={16}
className="absolute left-3 text-[var(--muted-foreground)] pointer-events-none z-10" className="absolute left-3 text-[var(--muted-foreground)] cursor-pointer z-10 hover:text-[var(--primary)] transition-colors"
onClick={handleCalendarClick}
/> />
<input <input
ref={inputRef}
type="datetime-local" type="datetime-local"
value={value ? formatDateForDateTimeInput(value) : ''} value={value ? formatDateForDateTimeInput(value) : ''}
onChange={handleChange} onChange={handleChange}