forked from doublenot/react-native-minicalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniCalendar.js
214 lines (197 loc) · 6.46 KB
/
MiniCalendar.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
'use strict';
import React, { Component } from 'react'
import moment from 'moment'
import {
View,
Text,
TouchableOpacity,
StyleSheet,
} from 'react-native'
class MiniCalendar extends React.Component {
constructor(props) {
super(props)
this.state = {
selectedDate: null,
dayHeadings: (this.props.hasOwnProperty('dayHeadings') === true) ? this.props.dayHeadings : ['Su','Mo','Tu','We','Th','Fr','Sa'],
showDayHeading: (this.props.hasOwnProperty('showDayHeading') === true) ? this.props.showDayHeading : true,
enabledDaysOfTheWeek: (this.props.hasOwnProperty('enabledDaysOfTheWeek') === true) ? this.props.enabledDaysOfTheWeek : ['Su','Mo','Tu','We','Th','Fr','Sa'],
selectedDate: (this.props.hasOwnProperty('selectedDate') === true) ? this.props.selectedDate : moment().format('YYYY-MM-DD'),
startOfWeek: (this.props.isoWeek === true) ? 'isoWeek' : 'week',
disablePreviousDays: (this.props.hasOwnProperty('disablePreviousDays') === true) ? this.props.disablePreviousDays : true,
disableToday: (this.props.hasOwnProperty('disableToday') === true) ? this.props.disableToday : false,
}
}
onDateSelect(date) {
this.setState({
selectedDate: date,
}, () => {
this.props.onDateSelect(date)
})
}
renderWeek(weekIdx, actualStartDate) {
const daysOfWeek = this.state.dayHeadings.map((dow, dowIdx) => {
let dowStyle = styles.dayOfWeek
let dowActiveStyle = styles.dayOfWeekActive
let inactiveStyle = {}
let activeDayStyle = {}
let dowSelectedDateStyle = {}
let selectedDayStyle = {}
let disabledDate = false
const fullDate = actualStartDate.format('YYYY-MM-DD')
const displayDate = actualStartDate.format('D')
let comparePreviousDiff = 86400000
if(this.state.disableToday === true){
comparePreviousDiff = 0
}
if(
this.state.enabledDaysOfTheWeek.indexOf(dow) === -1
|| (this.state.disablePreviousDays === true && moment().diff(moment(fullDate)) > comparePreviousDiff)
){
disabledDate = true
dowActiveStyle = {}
if(this.props.disabledDayStyle){
inactiveStyle = Object.assign({}, inactiveStyle, this.props.disabledDayStyle)
}
} else if(this.props.activeDayStyle){
activeDayStyle = Object.assign({}, activeDayStyle, this.props.activeDayStyle)
}
if(fullDate === this.state.selectedDate){
dowSelectedDateStyle = styles.dayOfWeekSelected
if(this.props.selectedDayStyle){
selectedDayStyle = Object.assign({}, selectedDayStyle, this.props.selectedDayStyle)
}
}
const dowElement = (
<TouchableOpacity
key={`${weekIdx}-${dowIdx}`}
underlayColor='transparent'
onPress={() => {
if(disabledDate === false){
this.onDateSelect(fullDate)
}
}}
style={styles.dowStyleContainer}
>
<Text
style={[
dowStyle,
this.props.dayStyle || {},
inactiveStyle,
dowActiveStyle,
activeDayStyle,
dowSelectedDateStyle,
selectedDayStyle
]}
>
{displayDate}
</Text>
</TouchableOpacity>
)
actualStartDate.add(1, 'days')
return dowElement;
})
return (
<View key={weekIdx} style={[styles.dayOfWeekContainer, this.props.weekContainerStyle || {}]}>
{daysOfWeek}
</View>
)
}
render() {
const {numberOfDaysToShow, numberOfPreviousDaysToShow, startDate} = this.props
const {dayHeadings, startOfWeek} = this.state
const totalNumberOfDaysToShow = (numberOfPreviousDaysToShow || 0 + numberOfDaysToShow || 0)
const actualStartDate = moment(this.props.startDate).startOf(this.state.startOfWeek)
let miniCalendarHeadings = null
if(this.state.showDayHeading === true){
const headingsList = this.state.dayHeadings.map((dowHeading, dowIdx) => {
return (
<View key={`heading-${dowIdx}`} style={styles.dowStyleContainer}>
<Text style={[styles.dayOfWeekHeading, this.props.headingStyle || {}]}>{dowHeading}</Text>
</View>
)
})
miniCalendarHeadings = (
<View key={'headings'} style={[styles.dayOfWeekHeadingsContainer, this.props.headingContainerStyle || {}]}>
{headingsList}
</View>
)
}
let miniCalendar = []
for(let i = 0, j = Math.ceil(totalNumberOfDaysToShow/dayHeadings.length); i<j; i++){
miniCalendar.push(this.renderWeek(i, actualStartDate))
}
return (
<View style={[styles.container, this.props.containerStyle || {}]}>
{miniCalendarHeadings}
{miniCalendar}
</View>
)
}
}
MiniCalendar.propTypes = {
showDayHeading: React.PropTypes.bool,
dayHeadings: React.PropTypes.array,
enabledDaysOfTheWeek: React.PropTypes.array,
startDate: React.PropTypes.string.isRequired,
numberOfDaysToShow: React.PropTypes.number.isRequired,
numberOfPreviousDaysToShow: React.PropTypes.number,
selectedDate: React.PropTypes.string,
onDateSelect: React.PropTypes.func.isRequired,
isoWeek: React.PropTypes.bool,
disablePreviousDays: React.PropTypes.bool,
disableToday: React.PropTypes.bool,
containerStyle: React.PropTypes.object,
headingContainerStyle: React.PropTypes.object,
headingStyle: React.PropTypes.object,
weekContainerStyle: React.PropTypes.object,
dayStyle: React.PropTypes.object,
activeDayStyle: React.PropTypes.object,
disabledDayStyle: React.PropTypes.object,
selectedDayStyle: React.PropTypes.object,
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
dayOfWeekHeadingsContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'stretch',
justifyContent: 'center',
},
dayOfWeekContainer: {
flex: 2,
flexDirection: 'row',
alignItems: 'stretch',
justifyContent: 'center',
},
dayOfWeekHeading: {
flex: 1,
textAlign: 'center',
margin: 1,
color: '#eee',
backgroundColor: '#777',
alignSelf: 'stretch',
},
dayOfWeek: {
flex: 1,
textAlign: 'right',
margin: 1,
color: '#aaa',
backgroundColor: '#ddd',
alignSelf: 'stretch',
},
dayOfWeekActive: {
color: '#222',
backgroundColor: 'lightblue',
},
dayOfWeekSelected: {
backgroundColor: 'yellow'
},
dowStyleContainer: {
flex: 1,
alignItems: 'center',
flexDirection: 'row',
},
})
export default MiniCalendar