-
Notifications
You must be signed in to change notification settings - Fork 3k
/
item.tsx
106 lines (95 loc) Β· 2.93 KB
/
item.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import XDate from 'xdate';
import React, {useRef, useMemo, useCallback} from 'react';
import {Text} from 'react-native';
import {Theme} from '../types';
import {toMarkingFormat} from '../interface';
import {extractCalendarProps} from '../componentUpdater';
import styleConstructor from './style';
import Calendar, {CalendarProps} from '../calendar';
export type CalendarListItemProps = CalendarProps & {
item: any;
calendarWidth?: number;
calendarHeight?: number;
horizontal?: boolean;
theme?: Theme;
scrollToMonth?: (date: XDate) => void;
visible?: boolean;
};
const CalendarListItem = React.memo((props: CalendarListItemProps) => {
const {
item,
theme,
scrollToMonth,
horizontal,
calendarHeight,
calendarWidth,
style: propsStyle,
headerStyle,
onPressArrowLeft,
onPressArrowRight,
visible
} = props;
const style = useRef(styleConstructor(theme));
const calendarProps = extractCalendarProps(props);
const dateString = toMarkingFormat(item);
const calendarStyle = useMemo(() => {
return [
{
width: calendarWidth,
minHeight: calendarHeight
},
style.current.calendar,
propsStyle
];
}, [calendarWidth, calendarHeight, propsStyle]);
const textStyle = useMemo(() => {
return [calendarStyle, style.current.placeholderText];
}, [calendarStyle]);
const _onPressArrowLeft = useCallback((method: () => void, month?: XDate) => {
const monthClone = month?.clone();
if (monthClone) {
if (onPressArrowLeft) {
onPressArrowLeft(method, monthClone);
} else if (scrollToMonth) {
const currentMonth = monthClone.getMonth();
monthClone.addMonths(-1);
// Make sure we actually get the previous month, not just 30 days before currentMonth.
while (monthClone.getMonth() === currentMonth) {
monthClone.setDate(monthClone.getDate() - 1);
}
scrollToMonth(monthClone);
}
}
}, [onPressArrowLeft, scrollToMonth]);
const _onPressArrowRight = useCallback((method: () => void, month?: XDate) => {
const monthClone = month?.clone();
if (monthClone) {
if (onPressArrowRight) {
onPressArrowRight(method, monthClone);
} else if (scrollToMonth) {
monthClone.addMonths(1);
scrollToMonth(monthClone);
}
}
}, [onPressArrowRight, scrollToMonth]);
if (!visible) {
return (
<Text style={textStyle}>{dateString}</Text>
);
}
return (
<Calendar
hideArrows={true}
hideExtraDays={true}
{...calendarProps}
current={dateString}
style={calendarStyle}
headerStyle={horizontal ? headerStyle : undefined}
disableMonthChange
onPressArrowLeft={horizontal ? _onPressArrowLeft : onPressArrowLeft}
onPressArrowRight={horizontal ? _onPressArrowRight : onPressArrowRight}
/>
);
});
export default CalendarListItem;
CalendarListItem.displayName = 'CalendarListItem';