@momo-kits/calendar 0.169.2 → 0.169.4-beta.1
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/ContainerContext.ts +12 -1
- package/MonthList.tsx +11 -9
- package/index.tsx +15 -2
- package/package.json +1 -1
- package/styles.ts +2 -0
package/ContainerContext.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
1
|
import { createContext } from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/** Style width of the probe the calendar measures to learn the rendered size scale. */
|
|
4
|
+
export const SCALE_PROBE_SIZE = 100;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Calendar container size: `width`/`height` are style sizes for layout, `pageWidth` is
|
|
8
|
+
* the rendered width that horizontal paging and scroll offsets are measured in.
|
|
9
|
+
*/
|
|
10
|
+
export const ContainerContext = createContext({
|
|
11
|
+
width: 0,
|
|
12
|
+
height: 0,
|
|
13
|
+
pageWidth: 0,
|
|
14
|
+
});
|
package/MonthList.tsx
CHANGED
|
@@ -111,22 +111,24 @@ const MonthList = forwardRef<MonthListRef, MonthListProps>((props, ref) => {
|
|
|
111
111
|
|
|
112
112
|
// paging keeps the old pixel offset when the width changes (fold, unfold,
|
|
113
113
|
// rotation), so remember the offset and move back onto the same month
|
|
114
|
-
const scrollOffset = useRef(
|
|
115
|
-
|
|
114
|
+
const scrollOffset = useRef(
|
|
115
|
+
Math.max(currentScrollIndex, 0) * size.pageWidth,
|
|
116
|
+
);
|
|
117
|
+
const pageWidth = useRef(size.pageWidth);
|
|
116
118
|
|
|
117
119
|
useLayoutEffect(() => {
|
|
118
120
|
const previousWidth = pageWidth.current;
|
|
119
|
-
pageWidth.current = size.
|
|
120
|
-
if (previousWidth === size.
|
|
121
|
+
pageWidth.current = size.pageWidth;
|
|
122
|
+
if (previousWidth === size.pageWidth || previousWidth <= 0) {
|
|
121
123
|
return;
|
|
122
124
|
}
|
|
123
125
|
const page = Math.round(scrollOffset.current / previousWidth);
|
|
124
|
-
scrollOffset.current = page * size.
|
|
126
|
+
scrollOffset.current = page * size.pageWidth;
|
|
125
127
|
listRef.current?.scrollToOffset({
|
|
126
128
|
offset: scrollOffset.current,
|
|
127
129
|
animated: false,
|
|
128
130
|
});
|
|
129
|
-
}, [size.
|
|
131
|
+
}, [size.pageWidth]);
|
|
130
132
|
|
|
131
133
|
const onScroll = useCallback(
|
|
132
134
|
(e: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
@@ -188,11 +190,11 @@ const MonthList = forwardRef<MonthListRef, MonthListProps>((props, ref) => {
|
|
|
188
190
|
|
|
189
191
|
const getItemLayout = useCallback(
|
|
190
192
|
(_: any, index: number) => ({
|
|
191
|
-
length: size.
|
|
192
|
-
offset: size.
|
|
193
|
+
length: size.pageWidth,
|
|
194
|
+
offset: size.pageWidth * index,
|
|
193
195
|
index,
|
|
194
196
|
}),
|
|
195
|
-
[size.
|
|
197
|
+
[size.pageWidth],
|
|
196
198
|
);
|
|
197
199
|
|
|
198
200
|
const renderItem = useCallback(
|
package/index.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
import CalendarPro from './CalendarPro';
|
|
12
12
|
import { HeaderPanel, SwitchReturnSelection } from './CalendarHeader';
|
|
13
13
|
import { CalendarProps, CalendarProRef, TabHeaderRef } from './types';
|
|
14
|
-
import { ContainerContext } from './ContainerContext';
|
|
14
|
+
import { ContainerContext, SCALE_PROBE_SIZE } from './ContainerContext';
|
|
15
15
|
import styles from './styles';
|
|
16
16
|
|
|
17
17
|
const DOUBLE = 'doubleDate';
|
|
@@ -51,6 +51,8 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
51
51
|
const [containerWidth, setContainerWidth] = useState<number>(
|
|
52
52
|
Dimensions.get('window').width,
|
|
53
53
|
);
|
|
54
|
+
// rendered size per style unit, read from a probe of known style width
|
|
55
|
+
const [layoutScale, setLayoutScale] = useState(1);
|
|
54
56
|
const [isDoubleDateMode, setIsDoubleDateMode] = useState<boolean>(
|
|
55
57
|
mode === DOUBLE,
|
|
56
58
|
);
|
|
@@ -104,6 +106,11 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
104
106
|
const onLayout = (e: LayoutChangeEvent) =>
|
|
105
107
|
setContainerWidth(e.nativeEvent.layout.width);
|
|
106
108
|
|
|
109
|
+
const handleProbeLayout = (e: LayoutChangeEvent) => {
|
|
110
|
+
const ratio = e.nativeEvent.layout.width / SCALE_PROBE_SIZE;
|
|
111
|
+
if (ratio > 0) setLayoutScale(ratio);
|
|
112
|
+
};
|
|
113
|
+
|
|
107
114
|
const onChangeTab = (tabId: number) => {
|
|
108
115
|
onChangeTabProp?.(tabId);
|
|
109
116
|
tabSelected.current = tabId;
|
|
@@ -252,11 +259,17 @@ const Calendar: React.FC<CalendarProps> = props => {
|
|
|
252
259
|
return (
|
|
253
260
|
<ContainerContext.Provider
|
|
254
261
|
value={{
|
|
255
|
-
width: containerWidth,
|
|
262
|
+
width: containerWidth / layoutScale,
|
|
256
263
|
height: priceList ? scaledHeight48 : scaledHeight34,
|
|
264
|
+
pageWidth: containerWidth,
|
|
257
265
|
}}
|
|
258
266
|
>
|
|
259
267
|
<View onLayout={onLayout} style={[style, styles.scrollView]}>
|
|
268
|
+
<View
|
|
269
|
+
pointerEvents="none"
|
|
270
|
+
style={styles.scaleProbe}
|
|
271
|
+
onLayout={handleProbeLayout}
|
|
272
|
+
/>
|
|
260
273
|
<View
|
|
261
274
|
style={[
|
|
262
275
|
{
|
package/package.json
CHANGED
package/styles.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {StyleSheet} from 'react-native';
|
|
2
2
|
import {Colors, Radius, Spacing} from '@momo-kits/foundation';
|
|
3
|
+
import {SCALE_PROBE_SIZE} from './ContainerContext';
|
|
3
4
|
|
|
4
5
|
export default StyleSheet.create({
|
|
5
6
|
//calendar
|
|
@@ -100,6 +101,7 @@ export default StyleSheet.create({
|
|
|
100
101
|
borderTopRightRadius: Radius.XS,
|
|
101
102
|
borderBottomRightRadius: Radius.XS,
|
|
102
103
|
},
|
|
104
|
+
scaleProbe: {position: 'absolute', width: SCALE_PROBE_SIZE, height: 0},
|
|
103
105
|
invisible: {
|
|
104
106
|
height: 0,
|
|
105
107
|
width: 0,
|