forked from ahmad2smile/react-native-calendar-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonthList.js
149 lines (148 loc) · 3.72 KB
/
MonthList.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
/**
* Created by TinySymphony on 2017-05-11.
*/
import React, { Component } from "react"
import { FlatList, Dimensions,View } from "react-native"
import Moment from "moment"
import styles from "./CalendarStyle"
import Month from "./Month"
const { width } = Dimensions.get("window")
export default class MonthList extends Component {
constructor (props) {
super(props)
this.monthList = []
this.state = {
dataSource: this._getMonthList()
}
this._renderMonth = this._renderMonth.bind(this)
this._shouldUpdate = this._shouldUpdate.bind(this)
this._checkRange = this._checkRange.bind(this)
this._getWeekNums = this._getWeekNums.bind(this)
this._scrollToSelectedMonth = this._scrollToSelectedMonth.bind(this)
}
componentWillReceiveProps (nextProps) {
const isDateUpdated = ["startDate", "endDate", "minDate", "maxDate"].reduce(
(prev, next) => {
if (prev || nextProps[next] !== this.props[next]) {
return true
}
return prev
},
false
)
if (isDateUpdated) {
this.setState({
dataSource: this._getMonthList(nextProps)
})
}
}
_renderMonth (month) {
return <Month month={month.item.date || {}} {...this.props} />
}
_checkRange (date, start, end) {
if (!date || !start) {
return false
}
if (!end) {
return date.year() === start.year() && date.month() === start.month()
}
if (
date.year() < start.year() ||
(date.year() === start.year() && date.month() < start.month())
) {
return false
}
if (
date.year() > end.year() ||
(date.year() === end.year() && date.month() > end.month())
) {
return false
}
return true
}
_shouldUpdate (month, props) {
if (!props) {
return false
}
const { startDate, endDate } = props
const { date } = month
const next = this._checkRange(date, startDate, endDate)
const prev = this._checkRange(date, this.props.startDate, this.props.endDate)
if (prev || next) {
return true
}
return false
}
_getMonthList (props) {
const minDate = (props || this.props).minDate.clone().date(1)
const maxDate = (props || this.props).maxDate.clone()
const monthList = []
if (!maxDate || !minDate) {
return monthList
}
while (
maxDate > minDate || // eslint-disable-line no-unmodified-loop-condition
(maxDate.year() === minDate.year() && maxDate.month() === minDate.month())
) {
const month = {
date: minDate.clone()
}
month.shouldUpdate = this._shouldUpdate(month, props)
monthList.push(month)
minDate.add(1, "month")
}
return monthList
}
_getWeekNums (start, end) {
const clonedMoment = Moment(start)
let date
let day
let num
let y
let m
let total = 0
while (!clonedMoment.isSame(end, "months")) {
y = clonedMoment.year()
m = clonedMoment.month()
date = new Date(y, m, 1)
day = date.getDay()
num = new Date(y, m + 1, 0).getDate()
total += Math.ceil((num + day) / 7)
clonedMoment.add(1, "months")
}
return total
}
_scrollToSelectedMonth () {
const { startDate, minDate } = this.props
const monthOffset =
12 * (startDate.year() - minDate.year()) + startDate.month() - minDate.month()
const weekOffset = this._getWeekNums(minDate, startDate)
setTimeout(() => {
this.list &&
this.list.scrollToOffset({
offset:
monthOffset * (24 + 25) +
(monthOffset ? weekOffset * Math.ceil(width / 7 + 10) : 0),
animated: true
})
}, 400)
}
componentDidMount () {
this.props.startDate && this._scrollToSelectedMonth()
}
render () {
return (
<FlatList
ref={list => {
this.list = list
}}
style={styles.scrollArea}
data={this.state.dataSource}
renderItem={this._renderMonth}
initialNumToRender={2}
keyExtractor={(item, index) => index.toString()}
showsVerticalScrollIndicator={false}
/>
)
}
}