forked from yubo725/rn-office
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckinRecord.android.js
228 lines (216 loc) · 5.62 KB
/
CheckinRecord.android.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
'use strict';
import React, {
Component,
Text,
Image,
View,
Dimensions,
PixelRatio,
ToastAndroid,
TouchableHighlight,
TouchableOpacity
} from 'react-native';
var screenWidth = Dimensions.get('window').width;
var dateUtils = new DateUtils();
var CheckinRecord = React.createClass({
getInitialState: function() {
return {
days: dateUtils.getDays(new Date()),
date: new Date(),
dateStr: dateUtils.getDateStr(new Date())
};
},
render: function() {
var cellRow = [];
var cells = [];
for(var i = 1; i <= 35; i++) {
if(i % 7 == 0) {//添加一行
cellRow.push(<Cell num={this.state.days[i - 1]} key={'cell' + i}/>);
cells.push(<View key={'cellrow' + i} style={{flexDirection: 'row'}}>{cellRow}</View>);
cellRow = [];
}else{//添加小格子
cellRow.push(<Cell num={this.state.days[i - 1]} key={'cell' + i}/>);
}
}
return (
<View style={styles.container}>
<View style={{height: 35, alignItems: 'center', justifyContent: 'center'}}>
<Text style={{fontSize: 16, color: 'green'}}>{this.state.dateStr}</Text>
</View>
<View style={{borderWidth: 1, borderColor: '#6699ff'}}>
<CalenderHeader />
<View>{cells}</View>
</View>
<Buttons showPreMonth={this.showPreMonth} showNextMonth={this.showNextMonth}/>
</View>
);
},
showPreMonth: function() {
var date = this.state.date;
date.setMonth(date.getMonth() - 1);
var days = dateUtils.getDays(date);
this.setState({days: days, dateStr: dateUtils.getDateStr(date)});
},
showNextMonth: function() {
var date = this.state.date;
date.setMonth(date.getMonth() + 1);
var days = dateUtils.getDays(date);
this.setState({days: days, dateStr: dateUtils.getDateStr(date)});
}
});
var Buttons = React.createClass({
render: function() {
return (
<View style={{flexDirection: 'row'}}>
<TouchableOpacity onPress={this.props.showPreMonth} activityOpacity={0.9} style={{flex: 1, marginRight: 10}}>
<View style={styles.btnContainer}>
<Text style={styles.btnStyle}>上个月</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={this.props.showNextMonth} activityOpacity={0.9} style={{flex: 1, marginLeft: 10}}>
<View style={styles.btnContainer}>
<Text style={styles.btnStyle}>下个月</Text>
</View>
</TouchableOpacity>
</View>
);
},
});
var CalenderHeader = React.createClass({
render: function() {
var headerCells = [];
var names = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
for(var i = 0; i < 7; i++) {
headerCells.push(<HeaderCell key={i} name={names[i]}/>);
}
return (
<View style={styles.headerContainer}>{headerCells}</View>
);
}
});
var HeaderCell = React.createClass({
render: function() {
return (
<View style={styles.headerCell}>
<Text style={styles.headerCellText}>{this.props.name}</Text>
</View>
);
}
});
var Cell = React.createClass({
render: function() {
return (
<TouchableHighlight underlayColor='#ccccff' onPress={this.handleCellClick}>
<View style={styles.cell}>
<Text>{this.props.num}</Text>
<Text style={styles.cellHintText}>打卡8次</Text>
</View>
</TouchableHighlight>
);
},
handleCellClick: function() {
}
});
function DateUtils(){
this.dateNow = new Date();
this.getDateNow = getDateNow;
this.getMonthDays = getMonthDays;
this.getDays = getDays;
this.getIndexOfMonthFirstDay = getIndexOfMonthFirstDay;
this.getDateStr = getDateStr;
function getDateNow() {
return {
year: this.dateNow.getFullYear(),
month: this.dateNow.getMonth() + 1,
day: this.dateNow.getDate()
};
}
function getMonthDays(year, month) {//获取某个月的天数,month取值1-12
var date = new Date(year, month, 0);
return date.getDate();
}
function getDays(date) {//获取某个月的天数数组
var days = [];
var year = date.getFullYear();
var month = date.getMonth() + 1;
var index = getIndexOfMonthFirstDay(year, month);
var daysOfMonth = dateUtils.getMonthDays(year, month);
var startIndex = index - 1;
var endIndex = startIndex + daysOfMonth - 1;
for(var i = 0, count = 1; i < 35; i++) {
if(i >= startIndex && i <= endIndex) {
days[i] = '' + (count++);
}else{
days[i] = ' ';
}
}
return days;
}
function getIndexOfMonthFirstDay(year, month) {//获取当月第一天是星期几(0~6, 0代表星期天,返回值1-7对应星期一到星期天),month取值1-12
var date = new Date();
date.setFullYear(year);
date.setMonth(month - 1);
date.setDate(1);
var index = date.getDay();
if(index == 0) index = 7;
return index;
}
function getDateStr(date) {//时间格式化
return date.getFullYear() + '年' + (date.getMonth() + 1) + '月';
}
}
function showMsg(msg) {
ToastAndroid.show(msg, ToastAndroid.LONG);
}
var borderWidth = 1 / PixelRatio.get();
const styles = {
container: {
flex: 1,
padding: 10,
},
headerContainer: {
flexDirection: 'row',
height: 40,
},
headerCell: {
width: (screenWidth - 22) / 7,
height: 40,
backgroundColor: '#d8d8d8',
justifyContent: 'center',
alignItems: 'center',
borderWidth: borderWidth,
borderColor: '#6699ff',
},
headerCellText: {
color: 'red',
fontSize: 16,
},
cell: {
width: (screenWidth - 22) / 7,
height: 60,
alignItems: 'center',
justifyContent: 'center',
borderWidth: borderWidth,
borderColor: '#6699ff',
},
cellHintText: {
fontSize: 12,
color: 'red',
marginTop: 5,
borderRadius: 10,
borderColor: 'white',
backgroundColor: '#cccc99'
},
btnContainer: {
borderRadius: 5,
height: 45,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#6699ff',
marginTop: 10,
},
btnStyle: {
color: 'white',
},
};
module.exports = CheckinRecord;