@@ -5,7 +5,11 @@ import {
55 getVerticalMonthsOffset ,
66 montHeaderHeight ,
77} from './Month'
8- import { beginOffset , estimatedMonthHeight , totalMonths } from './dateUtils'
8+ import {
9+ getBeginOffset ,
10+ estimatedMonthHeight ,
11+ getTotalMonths ,
12+ } from './dateUtils'
913import { useLatest } from '../shared/utils'
1014import {
1115 RenderProps ,
@@ -115,7 +119,9 @@ function Swiper({
115119 )
116120}
117121
118- const visibleArray = ( i : number ) => [ i - 2 , i - 1 , i , i + 1 , i + 2 ]
122+ const visibleArray = ( i : number ) => {
123+ return [ i - 2 , i - 1 , i , i + 1 , i + 2 ]
124+ }
119125
120126function VerticalScroller ( {
121127 width,
@@ -136,19 +142,22 @@ function VerticalScroller({
136142 startYear ?: number
137143 endYear ?: number
138144} ) {
145+ // Provide default values for startYear and endYear
146+ const effectiveStartYear = startYear || 1800
147+ const effectiveEndYear = endYear || 2200
139148 // Ensure initial index is within allowed range
140149 const constrainedInitialIndex = isIndexWithinRange (
141150 initialIndex ,
142- startYear ,
143- endYear
151+ effectiveStartYear ,
152+ effectiveEndYear
144153 )
145154 ? initialIndex
146155 : Math . max (
147- Math . min ( initialIndex , getMaxIndex ( endYear ) ) ,
148- getMinIndex ( startYear )
156+ Math . min ( initialIndex , getMaxIndex ( effectiveEndYear ) ) ,
157+ getMinIndex ( effectiveStartYear )
149158 )
150159
151- const [ visibleIndexes , setVisibleIndexes ] = useState < number [ ] > (
160+ const [ visibleIndexes , setVisibleIndexes ] = useState < number [ ] > ( ( ) =>
152161 visibleArray ( constrainedInitialIndex )
153162 )
154163
@@ -161,7 +170,12 @@ function VerticalScroller({
161170 return
162171 }
163172 const top =
164- getVerticalMonthsOffset ( idx . current , startWeekOnMonday ) - montHeaderHeight
173+ getVerticalMonthsOffset (
174+ idx . current ,
175+ startWeekOnMonday ,
176+ effectiveStartYear ,
177+ effectiveEndYear
178+ ) - montHeaderHeight
165179
166180 element . scrollTo ( {
167181 top,
@@ -177,11 +191,20 @@ function VerticalScroller({
177191 return
178192 }
179193
180- const offset = top - beginOffset
181- const index = getIndexFromVerticalOffset ( offset , startWeekOnMonday )
194+ const dynamicBeginOffset = getBeginOffset (
195+ effectiveStartYear ,
196+ effectiveEndYear
197+ )
198+ const offset = top - dynamicBeginOffset
199+ const index = getIndexFromVerticalOffset (
200+ offset ,
201+ startWeekOnMonday ,
202+ effectiveStartYear ,
203+ effectiveEndYear
204+ )
182205
183206 // Check if the new index is within allowed range
184- if ( ! isIndexWithinRange ( index , startYear , endYear ) ) {
207+ if ( ! isIndexWithinRange ( index , effectiveStartYear , effectiveEndYear ) ) {
185208 return
186209 }
187210
@@ -190,7 +213,12 @@ function VerticalScroller({
190213 setVisibleIndexesThrottled ( visibleArray ( index ) )
191214 }
192215 } ,
193- [ setVisibleIndexesThrottled , startWeekOnMonday , startYear , endYear ]
216+ [
217+ setVisibleIndexesThrottled ,
218+ startWeekOnMonday ,
219+ effectiveStartYear ,
220+ effectiveEndYear ,
221+ ]
194222 )
195223
196224 return (
@@ -207,37 +235,53 @@ function VerticalScroller({
207235 < div
208236 // eslint-disable-next-line react-native/no-inline-styles
209237 style = { {
210- height : estimatedHeight * totalMonths ,
238+ height :
239+ estimatedHeight *
240+ getTotalMonths ( effectiveStartYear , effectiveEndYear ) ,
211241 position : 'relative' ,
212242 } }
213243 >
214- { [ 0 , 1 , 2 , 3 , 4 ] . map ( ( vi ) => (
215- < div
216- key = { vi }
217- // eslint-disable-next-line react-native/no-inline-styles
218- style = { {
219- willChange : 'transform' ,
220- transform : `translateY(${ getVerticalMonthsOffset (
221- visibleIndexes [ vi ] ,
222- startWeekOnMonday
223- ) } px)`,
224- left : 0 ,
225- right : 0 ,
226- position : 'absolute' ,
227- height : getMonthHeight (
228- 'vertical' ,
229- visibleIndexes [ vi ] ,
230- startWeekOnMonday
231- ) ,
232- } }
233- >
234- { renderItem ( {
235- index : visibleIndexes [ vi ] ,
236- onPrev : empty ,
237- onNext : empty ,
238- } ) }
239- </ div >
240- ) ) }
244+ { [ 0 , 1 , 2 , 3 , 4 ]
245+ . map ( ( vi ) => {
246+ const monthIndex = visibleIndexes [ vi ]
247+
248+ if ( monthIndex === undefined ) {
249+ return null
250+ }
251+
252+ return (
253+ < div
254+ key = { vi }
255+ // eslint-disable-next-line react-native/no-inline-styles
256+ style = { {
257+ willChange : 'transform' ,
258+ transform : `translateY(${ getVerticalMonthsOffset (
259+ monthIndex ,
260+ startWeekOnMonday ,
261+ effectiveStartYear ,
262+ effectiveEndYear
263+ ) } px)`,
264+ left : 0 ,
265+ right : 0 ,
266+ position : 'absolute' ,
267+ height : getMonthHeight (
268+ 'vertical' ,
269+ monthIndex ,
270+ startWeekOnMonday ,
271+ effectiveStartYear ,
272+ effectiveEndYear
273+ ) ,
274+ } }
275+ >
276+ { renderItem ( {
277+ index : monthIndex ,
278+ onPrev : empty ,
279+ onNext : empty ,
280+ } ) }
281+ </ div >
282+ )
283+ } )
284+ . filter ( ( item ) => item !== null ) }
241285 </ div >
242286 </ div >
243287 )
0 commit comments