-
Notifications
You must be signed in to change notification settings - Fork 33
/
calendar.js
205 lines (181 loc) · 5.32 KB
/
calendar.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
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
PixelRatio,
ScrollView,
TouchableHighlight,
} = React;
var Calendar = React.createClass({
getInitialState: function(){
//开始时间
var startTime = this.props.startTime || new Date();
var holiday = this.props.holiday || {};
var check = this.props.check || {};
var headerStyle = this.props.headerStyle || {};
//显示月份的个数
var num = this.props.num || 3;
return {
startTime: startTime,
num: num,
holiday: holiday,
check: check,
headerStyle: headerStyle
};
},
render: function() {
var date = this.state.startTime;
var num = this.state.num;
var holiday = this.state.holiday;
var check = this.state.check;
var headerStyle = this.state.headerStyle;
var items = [];
var dateNow = new Date();
for(var n = 0; n < num; n++){
/*循环完成一个月*/
var rows = [];
var newDate = new Date(date.getFullYear(), date.getMonth() + 1 + n, 0); //天数
var week = new Date(date.getFullYear(), date.getMonth() + n, 1).getDay(); //月份开始的星期
if(week === 0){
week = 7;
}
var counts = newDate.getDate();
var rowCounts = Math.ceil((counts + week - 1) / 7); //本月行数
for(var i = 0; i < rowCounts; i++){
var days = [];
for(var j = (i * 7) + 1; j < ((i+1) * 7) + 1; j++){
//根据每个月开始的[星期]往后推
var dayNum = j - week + 1;
if(dayNum > 0 && j < counts + week){
//如果当前日期小于今天,则变灰
var dateObj = new Date(date.getFullYear(), date.getMonth() + n, dayNum);
var dateStr = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dayNum;
var grayStyle = {};
var bk = {};
if(dateNow >= new Date(date.getFullYear(), date.getMonth() + n, dayNum + 1)){
grayStyle = {
color:'#ccc'
};
}
if(holiday[dateStr]){
dayNum = holiday[dateStr];
}
if(check[dateStr]){
bk = {
backgroundColor: '#1EB7FF',
width:46,
height:35,
alignItems: 'center',
justifyContent: 'center'
};
grayStyle = {
color:'#fff'
};
}
days.push(
<TouchableHighlight style={[styles.flex_1]} underlayColor="#fff" onPress={this.props.touchEvent?this.props.touchEvent.bind(this, dateStr):null}>
<View style={bk}>
<Text style={grayStyle}>{dayNum}</Text>
</View>
</TouchableHighlight>
);
}else{
days.push(
<View style={[styles.flex_1]}>
<Text></Text>
</View>
);
}
}
rows.push(
<View style={styles.row}>{days}</View>
);
}
items.push(
<View style={[styles.cm_bottom]}>
<View style={styles.month}>
<Text style={styles.month_text}>{newDate.getFullYear()}年{newDate.getMonth() + 1}月</Text>
</View>
{rows}
</View>
);
}
return (
<View style={styles.calendar_container}>
<View style={[styles.row, styles.row_header, this.props.headerStyle]}>
<View style={[styles.flex_1]}>
<Text style={this.props.headerStyle}>一</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={this.props.headerStyle}>二</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={this.props.headerStyle}>三</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={this.props.headerStyle}>四</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={this.props.headerStyle}>五</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={[styles.week_highlight, this.props.headerStyle]}>六</Text>
</View>
<View style={[styles.flex_1]}>
<Text style={[styles.week_highlight, this.props.headerStyle]}>日</Text>
</View>
</View>
<ScrollView style={{flex:1,}}>
{items}
</ScrollView>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:'blue'
},
flex_1:{
flex:1,
alignItems:'center',
justifyContent:'center',
},
calendar_container:{
backgroundColor:'#fff',
flex:1,
borderTopWidth:1/PixelRatio.get(),
borderBottomWidth:1/PixelRatio.get(),
borderColor:'#ccc'
},
row_header:{
backgroundColor:'#F5F5F5',
borderBottomWidth:1/PixelRatio.get(),
borderBottomColor:'#ccc',
},
row:{
flexDirection:'row',
height:35,
},
month:{
alignItems:'center',
justifyContent:'center',
height:40,
},
month_text:{
fontSize:18,
fontWeight:'400',
},
week_highlight:{
color:'#23B8FC'
},
cm_bottom:{
borderBottomWidth:1/PixelRatio.get(),
borderBottomColor:'#ccc',
}
});
module.exports = Calendar;