@@ -7,15 +7,20 @@ import { ListProps } from "../List/List";
7
7
import { Label } from "../Typography/Label/Label" ;
8
8
import { selector } from "./DateField.css" ;
9
9
10
- function getYears ( activeDate : Date ) : Date [ ] {
10
+ function getYears ( activeDate : Date , minDate ?: Date , maxDate ?: Date ) : Date [ ] {
11
11
const firstYear = new Date ( ) . getFullYear ( ) - 100 ;
12
12
return Array ( 200 )
13
13
. fill ( 0 )
14
14
. map ( ( _ , diff ) => {
15
15
const yearDate = new Date ( activeDate ) ;
16
16
yearDate . setFullYear ( firstYear + diff ) ;
17
17
return yearDate ;
18
- } ) ;
18
+ } )
19
+ . filter (
20
+ ( year ) =>
21
+ ( minDate === undefined || year . getFullYear ( ) >= minDate . getFullYear ( ) ) &&
22
+ ( maxDate === undefined || year . getFullYear ( ) <= maxDate . getFullYear ( ) )
23
+ ) ;
19
24
}
20
25
21
26
function getMonths ( activeDate : Date ) : Date [ ] {
@@ -28,31 +33,43 @@ function getMonths(activeDate: Date): Date[] {
28
33
} ) ;
29
34
}
30
35
31
- export function Selector ( props : {
32
- datePart : "month" | "year" ;
36
+ type Props = {
33
37
activeMonth : MonthType ;
34
38
onSelect : ( date : Date ) => void ;
35
- } ) {
39
+ } & (
40
+ | {
41
+ datePart : "year" ;
42
+ maxDate ?: Date ;
43
+ minDate ?: Date ;
44
+ }
45
+ | {
46
+ datePart : "month" ;
47
+ maxDate ?: never ;
48
+ minDate ?: never ;
49
+ }
50
+ ) ;
51
+
52
+ export function Selector ( { datePart, activeMonth, maxDate, minDate, onSelect } : Props ) {
36
53
const config = useBentoConfig ( ) . dateField ;
37
54
const formatter = useDateFormatter (
38
- props . datePart === "month" ? { month : "long" } : { year : "numeric" }
55
+ datePart === "month" ? { month : "long" } : { year : "numeric" }
39
56
) ;
40
57
41
58
const values =
42
- props . datePart === "month"
43
- ? getMonths ( props . activeMonth . date )
44
- : getYears ( props . activeMonth . date ) ;
59
+ datePart === "month"
60
+ ? getMonths ( activeMonth . date )
61
+ : getYears ( activeMonth . date , minDate , maxDate ) ;
45
62
46
63
const options : ListProps [ "items" ] = useMemo (
47
64
( ) =>
48
65
values . map ( ( value ) => {
49
66
return {
50
67
label : formatter . format ( value ) ,
51
- onPress : ( ) => props . onSelect ( value ) ,
52
- isSelected : value . getTime ( ) === props . activeMonth . date . getTime ( ) ,
68
+ onPress : ( ) => onSelect ( value ) ,
69
+ isSelected : value . getTime ( ) === activeMonth . date . getTime ( ) ,
53
70
} ;
54
71
} ) ,
55
- [ values , props . activeMonth ]
72
+ [ values , activeMonth , onSelect , formatter ]
56
73
) ;
57
74
58
75
return (
@@ -63,7 +80,7 @@ export function Selector(props: {
63
80
< Columns space = { 8 } align = "center" alignY = "center" >
64
81
< Column width = "content" >
65
82
< Label size = { config . monthYearLabelSize } color = "secondary" uppercase >
66
- { formatter . format ( props . activeMonth . date ) }
83
+ { formatter . format ( activeMonth . date ) }
67
84
</ Label >
68
85
</ Column >
69
86
< Column width = "content" >
0 commit comments