-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathMonthList.js
More file actions
140 lines (139 loc) · 4.03 KB
/
MonthList.js
File metadata and controls
140 lines (139 loc) · 4.03 KB
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
/**
* Created by TinySymphony on 2017-05-11.
*/
import React, {PropTypes, Component} from 'react';
import {
View,
Text,
ListView,
Dimensions
} 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.ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => {
return r2.shouldUpdate;
}
});
this.monthList = [];
this.state = {
dataSource: this.ds.cloneWithRows(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._scrollToSelecetdMonth = this._scrollToSelecetdMonth.bind(this);
}
componentWillReceiveProps (nextProps) {
let 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.state.dataSource.cloneWithRows(this._getMonthList(nextProps))
});
}
}
_renderMonth (month) {
return (
<Month
month={month.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;
let next = this._checkRange(date, startDate, endDate);
let prev = this._checkRange(date, this.props.startDate, this.props.endDate);
if (prev || next) return true;
return false;
}
_getMonthList (props) {
let minDate = (props || this.props).minDate.clone().date(1);
let maxDate = (props || this.props).maxDate.clone();
let monthList = [];
if (!maxDate || !minDate) return monthList;
while (maxDate > minDate || (
maxDate.year() === minDate.year() &&
maxDate.month() === minDate.month()
)) {
let month = {
date: minDate.clone()
};
month.shouldUpdate = this._shouldUpdate(month, props);
monthList.push(month);
minDate.add(1, 'month');
}
return monthList;
}
_getWeekNums(start, end) {
let clonedMoment = Moment(start), date, day, num, y, m, 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;
}
_scrollToSelecetdMonth () {
const {
startDate,
minDate
} = this.props;
let monthOffset = 12 * (startDate.year() - minDate.year()) +
startDate.month() - minDate.month();
let weekOffset = this._getWeekNums(minDate, startDate);
setTimeout(() => {
this.list && this.list.scrollTo({
x: 0,
y: monthOffset * (24 + 25) + (monthOffset ? weekOffset * Math.ceil(width / 7 + 10) : 0),
animated: true
});
}, 400);
}
componentDidMount () {
this.props.startDate && this._scrollToSelecetdMonth();
}
render () {
return (
<ListView
ref={(list) => {this.list = list;}}
style={styles.scrollArea}
dataSource={this.state.dataSource}
renderRow={this._renderMonth}
pageSize={2}
initialListSize={2}
showsVerticalScrollIndicator={false}
/>
);
}
}