Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 84 additions & 40 deletions src/Date/Swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
getVerticalMonthsOffset,
montHeaderHeight,
} from './Month'
import { beginOffset, estimatedMonthHeight, totalMonths } from './dateUtils'
import {
getBeginOffset,
estimatedMonthHeight,
getTotalMonths,
} from './dateUtils'
import { useLatest } from '../shared/utils'
import {
RenderProps,
Expand Down Expand Up @@ -115,7 +119,9 @@ function Swiper({
)
}

const visibleArray = (i: number) => [i - 2, i - 1, i, i + 1, i + 2]
const visibleArray = (i: number) => {
return [i - 2, i - 1, i, i + 1, i + 2]
}

function VerticalScroller({
width,
Expand All @@ -136,19 +142,22 @@ function VerticalScroller({
startYear?: number
endYear?: number
}) {
// Provide default values for startYear and endYear
const effectiveStartYear = startYear || 1800
const effectiveEndYear = endYear || 2200
// Ensure initial index is within allowed range
const constrainedInitialIndex = isIndexWithinRange(
initialIndex,
startYear,
endYear
effectiveStartYear,
effectiveEndYear
)
? initialIndex
: Math.max(
Math.min(initialIndex, getMaxIndex(endYear)),
getMinIndex(startYear)
Math.min(initialIndex, getMaxIndex(effectiveEndYear)),
getMinIndex(effectiveStartYear)
)

const [visibleIndexes, setVisibleIndexes] = useState<number[]>(
const [visibleIndexes, setVisibleIndexes] = useState<number[]>(() =>
visibleArray(constrainedInitialIndex)
)

Expand All @@ -161,7 +170,12 @@ function VerticalScroller({
return
}
const top =
getVerticalMonthsOffset(idx.current, startWeekOnMonday) - montHeaderHeight
getVerticalMonthsOffset(
idx.current,
startWeekOnMonday,
effectiveStartYear,
effectiveEndYear
) - montHeaderHeight

element.scrollTo({
top,
Expand All @@ -177,11 +191,20 @@ function VerticalScroller({
return
}

const offset = top - beginOffset
const index = getIndexFromVerticalOffset(offset, startWeekOnMonday)
const dynamicBeginOffset = getBeginOffset(
effectiveStartYear,
effectiveEndYear
)
const offset = top - dynamicBeginOffset
const index = getIndexFromVerticalOffset(
offset,
startWeekOnMonday,
effectiveStartYear,
effectiveEndYear
)

// Check if the new index is within allowed range
if (!isIndexWithinRange(index, startYear, endYear)) {
if (!isIndexWithinRange(index, effectiveStartYear, effectiveEndYear)) {
return
}

Expand All @@ -190,7 +213,12 @@ function VerticalScroller({
setVisibleIndexesThrottled(visibleArray(index))
}
},
[setVisibleIndexesThrottled, startWeekOnMonday, startYear, endYear]
[
setVisibleIndexesThrottled,
startWeekOnMonday,
effectiveStartYear,
effectiveEndYear,
]
)

return (
Expand All @@ -207,37 +235,53 @@ function VerticalScroller({
<div
// eslint-disable-next-line react-native/no-inline-styles
style={{
height: estimatedHeight * totalMonths,
height:
estimatedHeight *
getTotalMonths(effectiveStartYear, effectiveEndYear),
position: 'relative',
}}
>
{[0, 1, 2, 3, 4].map((vi) => (
<div
key={vi}
// eslint-disable-next-line react-native/no-inline-styles
style={{
willChange: 'transform',
transform: `translateY(${getVerticalMonthsOffset(
visibleIndexes[vi],
startWeekOnMonday
)}px)`,
left: 0,
right: 0,
position: 'absolute',
height: getMonthHeight(
'vertical',
visibleIndexes[vi],
startWeekOnMonday
),
}}
>
{renderItem({
index: visibleIndexes[vi],
onPrev: empty,
onNext: empty,
})}
</div>
))}
{[0, 1, 2, 3, 4]
.map((vi) => {
const monthIndex = visibleIndexes[vi]

if (monthIndex === undefined) {
return null
}

return (
<div
key={vi}
// eslint-disable-next-line react-native/no-inline-styles
style={{
willChange: 'transform',
transform: `translateY(${getVerticalMonthsOffset(
monthIndex,
startWeekOnMonday,
effectiveStartYear,
effectiveEndYear
)}px)`,
left: 0,
right: 0,
position: 'absolute',
height: getMonthHeight(
'vertical',
monthIndex,
startWeekOnMonday,
effectiveStartYear,
effectiveEndYear
),
}}
>
{renderItem({
index: monthIndex,
onPrev: empty,
onNext: empty,
})}
</div>
)
})
.filter((item) => item !== null)}
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ exports[`renders CalendarEdit 1`] = `
}
testID="text-input-flat"
underlineColorAndroid="transparent"
value="06/09/2025"
value="06/29/2025"
withModal={false}
/>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ exports[`renders DatePickerInput 1`] = `
}
testID="text-input-flat"
underlineColorAndroid="transparent"
value="06/09/2025"
value="06/29/2025"
/>
</View>
<View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ exports[`renders DatePickerInputWithoutModal 1`] = `
}
testID="text-input-flat"
underlineColorAndroid="transparent"
value="06/09/2025"
value="06/29/2025"
/>
</View>
</View>
Expand Down