-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
302 lines (289 loc) · 11.6 KB
/
index.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import React, {Component} from "react";
import {
View,
Text,
FlatList,
Platform,
Dimensions,
TouchableOpacity,
StyleSheet,
} from "react-native";
import PropTypes from "prop-types";
import XDate from "xdate";
import {parseDate} from "./utils/interface";
import {CalendarItem} from "./src/CalendarItem";
import {CalendarHeader} from "./src/CalendarHeader";
import {MonthChooser} from "./src/MonthChooser";
import {nameOfMonthArr} from "./utils/daysCountOfSpecificMonthYear";
const {width} = Dimensions.get("window");
const VIEWABILITY_CONFIG = {
// minimumViewTime: 300,
viewAreaCoveragePercentThreshold: 30,
// waitForInteraction: true,
};
export default class Calendar extends Component {
static propTypes = {
// Max amount of months allowed to scroll to the past. Default = 50
pastScrollRange: PropTypes.number,
// Max amount of months allowed to scroll to the future. Default = 50
futureScrollRange: PropTypes.number,
// Enable or disable scrolling of calendar list
scrollEnabled: PropTypes.bool,
// Enable or disable vertical scroll indicator. Default = false
showScrollIndicator: PropTypes.bool,
// When true, the calendar list scrolls to top when the status bar is tapped. Default = true
scrollsToTop: PropTypes.bool,
// Enable or disable paging on scroll
pagingEnabled: PropTypes.bool,
// Whether the scroll is horizontal
// horizontal: PropTypes.bool,
// Used when calendar scroll is horizontal, default is device width, pagination should be disabled
calendarWidth: PropTypes.number,
// Dynamic calendar height
// calendarHeight: PropTypes.number,
// Style for the List item (the calendar)
// calendarStyle: PropTypes.oneOfType([
// PropTypes.object,
// PropTypes.number,
// PropTypes.array,
// ]),
// // Whether to use static header that will not scroll with the list (horizontal only)
// staticHeader: PropTypes.bool,
};
static defaultProps = {
// horizontal: false,
calendarWidth: width,
// calendarHeight: 360,
pastScrollRange: 50,
futureScrollRange: 50,
showScrollIndicator: false,
scrollEnabled: true,
scrollsToTop: false,
removeClippedSubviews: Platform.OS === "android" ? false : true,
pagingEnabled: true,
showMonthChooser: false,
showTodayButton: true,
};
_onViewableItemsChange = async props => {
const {viewableItems} = props;
function rowIsCloseToViewable(index, distance) {
//以viewableItems的数量作为遍历的计数
for (let i = 0; i < viewableItems.length; i++) {
if (Math.abs(index - parseInt(viewableItems[i]?.index)) <= distance) {
//只要与任一viewableItems位置接近,就说明即将渲染,直接返回true
return true;
}
}
//与所有的viewableItems 都不接近, 则返回false
return false;
}
const dataArrCopy = this.state.dataArr;
for (let i = 0; i < dataArrCopy.length; i++) {
if (rowIsCloseToViewable(i, 1)) {
// console.warn("openDate", this.state.openDate);
dataArrCopy[i] = this.state.openDate
.clone()
.addMonths(i - this.props.pastScrollRange, true);
} else {
if (dataArrCopy[i] instanceof XDate) {
dataArrCopy[i] = dataArrCopy[i].clone().toString("MMM yyyy");
} else {
dataArrCopy[i] = dataArrCopy[i];
}
}
}
await this.setState({
dataArr: dataArrCopy,
currentDate: this.state.textsArr[(viewableItems[0]?.index)],
currentDateText: this.state.textsArr[(viewableItems[0]?.index)]?.toString(
"MMM yyyy"
),
});
};
constructor(props) {
super(props);
const date = parseDate(this.props.currentDate) || XDate();
const dataArr = [];
const textsArr = [];
for (
let i = 0;
i < this.props.pastScrollRange + this.props.futureScrollRange + 1;
++i
) {
const dateItem = date
.clone()
.addMonths(i - this.props.pastScrollRange, true);
const dateItemText = dateItem.toString("MMM yyyy");
textsArr.push(dateItemText);
if (
(i >= this.props.pastScrollRange - 1 &&
i <= this.props.pastScrollRange + 1) ||
(!this.props.pastScrollRange && i <= this.props.pastScrollRange + 2)
) {
dataArr.push(dateItem);
} else {
dataArr.push(dateItemText);
}
}
this.state = {
openDate: this.props.currentDate || XDate(),
currentDate: parseDate(this.props.currentDate),
dataArr,
textsArr,
currentDateText: "Jun 2019",
};
if (this.props.showMonthChooser) {
this.state.choosableMonthBegins = textsArr[0];
this.state.choosableMonthEnds =
textsArr[this.props.pastScrollRange + this.props.futureScrollRange];
}
}
_renderItem = ({item, index}) => {
// console.warn(Object.keys(this.props.markedDates));
let regText = "";
if (item instanceof XDate) {
regText = `${item.getFullYear()}-${
item.getMonth() > 8 ? item.getMonth() + 1 : `0${item.getMonth() + 1}`
}`;
} else {
regText = `${item?.split(" ")[1]}-${
nameOfMonthArr.indexOf(item?.split(" ")[0]) + 1 > 9
? nameOfMonthArr.indexOf(item?.split(" ")[0]) + 1
: `0${nameOfMonthArr.indexOf(item?.split(" ")[0]) + 1}`
}`;
}
const regExp = new RegExp(regText);
// const markedDatesArr = [];
const markedDatesObj = {};
for (const key in this.props.markedDates) {
if (regExp.test(key)) {
markedDatesObj[
parseInt(key?.split("-")[2], 10)
] = this.props.markedDates[key];
}
}
return (
<CalendarItem
key={
item instanceof Object
? item.getFullYear() + item.getMonth() + index
: item + index
}
style={{flex: 1, width: this.props.calendarWidth}}
item={item}
index={index}
onDayPress={this.props.onDayPress}
calendarWidth={this.props.calendarWidth}
markedDatesObj={markedDatesObj}
/>
);
};
getItemLayout = (data, index) => ({
length: this.props.calendarWidth,
offset: this.props.calendarWidth * index,
index,
});
handleClickToday = () => {
this.flatList.scrollToOffset({
offset: this.props.pastScrollRange * this.props.calendarWidth,
animated: false,
});
};
scrollToMonth = ({month, year}) => {
const foundIndex = this.state.textsArr.indexOf(`${month} ${year}`);
this.flatList.scrollToOffset({
offset: foundIndex * this.props.calendarWidth,
animated: true,
});
};
render() {
return (
<View
style={{height: 530, width: this.props.calendarWidth, marginTop: 50}}>
{this.props.showMonthChooser ? (
<MonthChooser
calendarWidth={this.props.calendarWidth}
choosableMonthArr={this.state.choosableMonthArr}
flatListRef={this.flatList}
scrollToMonth={this.scrollToMonth}
choosableMonthBegins={this.state.choosableMonthBegins}
choosableMonthEnds={this.state.choosableMonthEnds}
currentDateText={this.state.currentDateText}
dropdownStyle={this.props.dropdownStyle}
dropdownListTextStyle={this.props.dropdownListTextStyle}
dropdownSelectionStyle={this.props.dropdownSelectionStyle}
disabledDropdownListStyle={this.props.disabledDropdownListStyle}
dropdownMenuStyle={this.props.dropdownMenuStyle}
disabledDropdownMenuTextStyle={this.props.disabledDropdownMenuTextStyle}
dropdownMenuTextStyle={this.props.dropdownMenuTextStyle}
dropdownTextHighlightStyle={this.props.dropdownTextHighlightStyle}
/>
) : (
<View style={{height: 50}}>
<Text style={styles.dateTextStyle}>
{this.state.currentDateText}
</Text>
</View>
)}
<CalendarHeader
calendarWidth={this.props.calendarWidth}
dateText={this.state.currentDateText}
dayTextArr={this.props.dayTextArr}
showCalendarTextHeader={this.props.showCalendarTextHeader}
/>
<FlatList
ref={flatList => (this.flatList = flatList)}
viewabilityConfig={VIEWABILITY_CONFIG}
onViewableItemsChanged={this._onViewableItemsChange}
initialListSize={
this.props.pastScrollRange + this.props.futureScrollRange + 1
}
horizontal={true}
data={this.state.dataArr}
renderItem={this._renderItem}
scrollEnabled={this.props.scrollEnabled}
pagingEnabled={this.props.pagingEnabled}
initialScrollIndex={this.props.pastScrollRange}
getItemLayout={this.getItemLayout}
keyExtractor={(item, index) =>
String(
item instanceof Object
? item.getFullYear() + item.getMonth() + index
: item + index
)
}
overScrollMode={"never"}
alwaysBounceHorizontal={false}
directionalLockEnabled={true}
showsVerticalScrollIndicator={this.props.showScrollIndicator}
showsHorizontalScrollIndicator={this.props.showScrollIndicator}
scrollsToTop={this.props.scrollsToTop}
extraData={Object.keys(this.props.markedDates).length}
/>
{this.props.showTodayButton && (
<TouchableOpacity
style={styles.todayButtonStyle}
onPress={this.handleClickToday}>
<Text style={styles.todayButtonTextStyle}>Today</Text>
</TouchableOpacity>
)}
</View>
);
}
}
const styles = StyleSheet.create({
todayButtonStyle: {
alignItems: "center",
justifyContent: "center",
backgroundColor: "transparent",
borderRadius: 25,
borderWidth: 1,
borderColor: "#eee",
height: 35,
margin: 10,
},
todayButtonTextStyle: {
fontSize: 20,
color: "#eee",
},
});