forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateRangeChips.tsx
More file actions
79 lines (72 loc) · 2.16 KB
/
Copy pathDateRangeChips.tsx
File metadata and controls
79 lines (72 loc) · 2.16 KB
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
import { useEffect, useId } from 'react';
import { DatePicker } from './DatePicker';
import './DateRangeChips.css';
export const DATE_PRESET_OPTIONS = [
{ value: 'today', label: 'Today' },
{ value: '7d', label: '7d' },
{ value: '30d', label: '30d' },
{ value: '90d', label: '90d' },
{ value: 'custom', label: 'Custom' },
] as const;
export type DatePreset = (typeof DATE_PRESET_OPTIONS)[number]['value'];
interface DateRangeChipsProps {
selectedPreset: DatePreset;
customStartDate: string;
customEndDate: string;
onPresetChange: (preset: DatePreset) => void;
onCustomStartDateChange: (value: string) => void;
onCustomEndDateChange: (value: string) => void;
}
export function DateRangeChips({
selectedPreset,
customStartDate,
customEndDate,
onPresetChange,
onCustomStartDateChange,
onCustomEndDateChange,
}: DateRangeChipsProps) {
const labelId = useId();
useEffect(() => {
if (selectedPreset === 'custom') {
document.getElementById('custom-start-date')?.focus();
}
}, [selectedPreset]);
return (
<div className="date-range-chips">
<span className="th-filter-label" id={labelId}>
Date Range
</span>
<div className="th-chip-group" role="group" aria-labelledby={labelId}>
{DATE_PRESET_OPTIONS.map((option) => (
<button
key={option.value}
type="button"
className="th-filter-chip"
aria-pressed={selectedPreset === option.value}
onClick={() => onPresetChange(option.value)}
>
{option.label}
</button>
))}
</div>
{selectedPreset === 'custom' && (
<div className="date-range-custom-fields">
<DatePicker
id="custom-start-date"
label="Start date"
value={customStartDate}
onChange={onCustomStartDateChange}
max={customEndDate || undefined}
/>
<DatePicker
id="custom-end-date"
label="End date"
value={customEndDate}
onChange={onCustomEndDateChange}
min={customStartDate || undefined}
/>
</div>
)}
</div>
);
}