@momo-kits/calendar 0.167.1-duo.1 → 0.167.1-duo.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MonthList.tsx +34 -1
- package/package.json +1 -1
package/MonthList.tsx
CHANGED
|
@@ -6,11 +6,17 @@ import React, {
|
|
|
6
6
|
useCallback,
|
|
7
7
|
useContext,
|
|
8
8
|
useEffect,
|
|
9
|
+
useLayoutEffect,
|
|
9
10
|
useMemo,
|
|
10
11
|
useRef,
|
|
11
12
|
useState,
|
|
12
13
|
} from 'react';
|
|
13
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
FlatList,
|
|
16
|
+
NativeScrollEvent,
|
|
17
|
+
NativeSyntheticEvent,
|
|
18
|
+
Platform,
|
|
19
|
+
} from 'react-native';
|
|
14
20
|
import { ContainerContext } from './index';
|
|
15
21
|
import LunarDateConverter from './LunarDateConverter';
|
|
16
22
|
import Month from './Month';
|
|
@@ -102,6 +108,32 @@ const MonthList = forwardRef<MonthListRef, MonthListProps>((props, ref) => {
|
|
|
102
108
|
[selectedDate, getIndexOfMonth],
|
|
103
109
|
);
|
|
104
110
|
|
|
111
|
+
// paging keeps the old pixel offset when the width changes (fold, unfold,
|
|
112
|
+
// rotation), so remember the offset and move back onto the same month
|
|
113
|
+
const scrollOffset = useRef(Math.max(currentScrollIndex, 0) * size.width);
|
|
114
|
+
const pageWidth = useRef(size.width);
|
|
115
|
+
|
|
116
|
+
useLayoutEffect(() => {
|
|
117
|
+
const previousWidth = pageWidth.current;
|
|
118
|
+
pageWidth.current = size.width;
|
|
119
|
+
if (previousWidth === size.width || previousWidth <= 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const page = Math.round(scrollOffset.current / previousWidth);
|
|
123
|
+
scrollOffset.current = page * size.width;
|
|
124
|
+
listRef.current?.scrollToOffset({
|
|
125
|
+
offset: scrollOffset.current,
|
|
126
|
+
animated: false,
|
|
127
|
+
});
|
|
128
|
+
}, [size.width]);
|
|
129
|
+
|
|
130
|
+
const onScroll = useCallback(
|
|
131
|
+
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
132
|
+
scrollOffset.current = e.nativeEvent.contentOffset.x;
|
|
133
|
+
},
|
|
134
|
+
[],
|
|
135
|
+
);
|
|
136
|
+
|
|
105
137
|
const scrollToMonth = useCallback(
|
|
106
138
|
(month: Moment) => {
|
|
107
139
|
const idx = getIndexOfMonth(month);
|
|
@@ -211,6 +243,7 @@ const MonthList = forwardRef<MonthListRef, MonthListProps>((props, ref) => {
|
|
|
211
243
|
showsHorizontalScrollIndicator={false}
|
|
212
244
|
keyExtractor={keyExtractor}
|
|
213
245
|
onScrollToIndexFailed={() => {}}
|
|
246
|
+
onScroll={onScroll}
|
|
214
247
|
scrollEnabled
|
|
215
248
|
initialNumToRender={3}
|
|
216
249
|
maxToRenderPerBatch={MAX_RENDER_PER_BATCH}
|