fix: override native browser date input format to explicitly show DD/MM/YY in custom period selector
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState, useRef } from 'react';
|
||||||
import { Calendar, RefreshCw, ChevronDown } from 'lucide-react';
|
import { Calendar, RefreshCw, ChevronDown } from 'lucide-react';
|
||||||
import type { DateRange } from '../types';
|
import type { DateRange } from '../types';
|
||||||
|
|
||||||
@@ -33,6 +33,8 @@ const REFRESH_OPTIONS = [
|
|||||||
|
|
||||||
const DateRangePicker: React.FC<DateRangePickerProps> = ({ dateRange, onChange, refreshInterval, setRefreshInterval, onManualRefresh }) => {
|
const DateRangePicker: React.FC<DateRangePickerProps> = ({ dateRange, onChange, refreshInterval, setRefreshInterval, onManualRefresh }) => {
|
||||||
const [isPresetOpen, setIsPresetOpen] = useState(false);
|
const [isPresetOpen, setIsPresetOpen] = useState(false);
|
||||||
|
const startRef = useRef<HTMLInputElement>(null);
|
||||||
|
const endRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const formatShortDate = (date: Date) => {
|
const formatShortDate = (date: Date) => {
|
||||||
return date.toLocaleDateString('pt-BR', { day: '2-digit', month: '2-digit', year: '2-digit' });
|
return date.toLocaleDateString('pt-BR', { day: '2-digit', month: '2-digit', year: '2-digit' });
|
||||||
@@ -66,6 +68,20 @@ const DateRangePicker: React.FC<DateRangePickerProps> = ({ dateRange, onChange,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const openPicker = (ref: React.RefObject<HTMLInputElement | null>) => {
|
||||||
|
if (ref.current) {
|
||||||
|
try {
|
||||||
|
if ('showPicker' in HTMLInputElement.prototype) {
|
||||||
|
ref.current.showPicker();
|
||||||
|
} else {
|
||||||
|
ref.current.focus();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
ref.current.focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-wrap items-center gap-3">
|
<div className="flex flex-wrap items-center gap-3">
|
||||||
{/* Date Presets Dropdown */}
|
{/* Date Presets Dropdown */}
|
||||||
@@ -88,21 +104,35 @@ const DateRangePicker: React.FC<DateRangePickerProps> = ({ dateRange, onChange,
|
|||||||
<div className="flex flex-col gap-1.5">
|
<div className="flex flex-col gap-1.5">
|
||||||
<div className="flex items-center justify-between gap-2">
|
<div className="flex items-center justify-between gap-2">
|
||||||
<span className="text-xs font-medium text-dark-text w-6">De:</span>
|
<span className="text-xs font-medium text-dark-text w-6">De:</span>
|
||||||
<input
|
<div
|
||||||
type="date"
|
className="relative bg-dark-input border border-dark-border text-dark-text text-xs rounded-lg px-2 py-1 focus-within:border-brand-primary w-full cursor-pointer overflow-hidden flex items-center h-6"
|
||||||
value={formatDateForInput(dateRange.start)}
|
onClick={() => openPicker(startRef)}
|
||||||
onChange={handleStartChange}
|
>
|
||||||
className="bg-dark-input border border-dark-border text-dark-text text-xs rounded-lg px-2 py-1 focus:outline-none focus:border-brand-primary w-full cursor-pointer"
|
<span className="w-full text-center">{formatShortDate(dateRange.start)}</span>
|
||||||
/>
|
<input
|
||||||
|
ref={startRef}
|
||||||
|
type="date"
|
||||||
|
value={formatDateForInput(dateRange.start)}
|
||||||
|
onChange={handleStartChange}
|
||||||
|
className="absolute inset-0 opacity-0 cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center justify-between gap-2">
|
<div className="flex items-center justify-between gap-2">
|
||||||
<span className="text-xs font-medium text-dark-text w-6">Até:</span>
|
<span className="text-xs font-medium text-dark-text w-6">Até:</span>
|
||||||
<input
|
<div
|
||||||
type="date"
|
className="relative bg-dark-input border border-dark-border text-dark-text text-xs rounded-lg px-2 py-1 focus-within:border-brand-primary w-full cursor-pointer overflow-hidden flex items-center h-6"
|
||||||
value={formatDateForInput(dateRange.end)}
|
onClick={() => openPicker(endRef)}
|
||||||
onChange={handleEndChange}
|
>
|
||||||
className="bg-dark-input border border-dark-border text-dark-text text-xs rounded-lg px-2 py-1 focus:outline-none focus:border-brand-primary w-full cursor-pointer"
|
<span className="w-full text-center">{formatShortDate(dateRange.end)}</span>
|
||||||
/>
|
<input
|
||||||
|
ref={endRef}
|
||||||
|
type="date"
|
||||||
|
value={formatDateForInput(dateRange.end)}
|
||||||
|
onChange={handleEndChange}
|
||||||
|
className="absolute inset-0 opacity-0 cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user