@@ -4,6 +4,7 @@ import React, {
4
4
useImperativeHandle ,
5
5
useMemo ,
6
6
useRef ,
7
+ useEffect ,
7
8
} from 'react'
8
9
import type { ReactNode } from 'react'
9
10
import { NativeProps , withNativeProps } from '../../utils/native-props'
@@ -29,7 +30,7 @@ const classPrefix = 'adm-calendar-picker-view'
29
30
export type CalendarPickerViewRef = {
30
31
jumpTo : ( page : Page | ( ( page : Page ) => Page ) ) => void
31
32
jumpToToday : ( ) => void
32
- scrollTo : ( date : Date ) => void
33
+ scrollTo : ( page : Page ) => void
33
34
getDateRange : ( ) => DateRange
34
35
}
35
36
@@ -108,6 +109,21 @@ export const CalendarPickerView = forwardRef<
108
109
const [ current , setCurrent ] = useState ( ( ) =>
109
110
dayjs ( dateRange ? dateRange [ 0 ] : today ) . date ( 1 )
110
111
)
112
+ useEffect ( ( ) => {
113
+ if ( dateRange ) {
114
+ const curr = dayjs ( dateRange [ 0 ] )
115
+ scrollTo ( { year : curr . year ( ) , month : curr . month ( ) + 1 } )
116
+ }
117
+ } , [ dateRange ] )
118
+
119
+ const scrollTo = ( page : Page ) => {
120
+ const cell = rootRef . current ?. querySelector (
121
+ `[data-date="${ convertPageToDayjs ( page ) . format ( 'YYYY-MM' ) } "`
122
+ )
123
+ if ( cell ) {
124
+ cell . scrollIntoView ( )
125
+ }
126
+ }
111
127
112
128
useImperativeHandle ( ref , ( ) => ( {
113
129
jumpTo : pageOrPageGenerator => {
@@ -121,19 +137,15 @@ export const CalendarPickerView = forwardRef<
121
137
page = pageOrPageGenerator
122
138
}
123
139
setCurrent ( convertPageToDayjs ( page ) )
140
+ scrollTo ( page )
124
141
} ,
125
142
jumpToToday : ( ) => {
126
- setCurrent ( dayjs ( ) . date ( 1 ) )
143
+ const curr = dayjs ( ) . date ( 1 )
144
+ setCurrent ( curr )
145
+ scrollTo ( { year : curr . year ( ) , month : curr . month ( ) + 1 } )
127
146
} ,
128
147
getDateRange : ( ) => dateRange ,
129
- scrollTo : ( date : Date ) => {
130
- const cell = rootRef . current ?. querySelector (
131
- `[data-date="${ dayjs ( date ) . format ( 'YYYY-MM' ) } "`
132
- )
133
- if ( cell ) {
134
- cell . scrollIntoView ( )
135
- }
136
- } ,
148
+ scrollTo : scrollTo ,
137
149
} ) )
138
150
139
151
const header = (
0 commit comments