-
Notifications
You must be signed in to change notification settings - Fork 8
/
Days.js
151 lines (131 loc) · 4.35 KB
/
Days.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
/**
* Persian Calendar Picker Component
*
* Copyright 2016 Reza (github.com/rghorbani)
* Licensed under the terms of the MIT license. See LICENSE file in the project root for terms.
*/
'use strict';
const React = require('react');
const PropTypes = require('prop-types');
const moment = require('moment-jalaali');
const {
Text,
View,
} = require('react-native');
const styles = require('./style');
const {
MAX_ROWS,
MAX_COLUMNS,
} = require('./util');
const Day = require('./Day');
class Days extends React.Component {
static propTypes = {
maxDate: PropTypes.instanceOf(moment),
minDate: PropTypes.instanceOf(Date),
date: PropTypes.object.isRequired,
month: PropTypes.number.isRequired,
year: PropTypes.number.isRequired,
onDayChange: PropTypes.func.isRequired,
selectedDayColor: PropTypes.string,
selectedDayTextColor: PropTypes.string,
textStyle: Text.propTypes.style,
prevDays: PropTypes.string,
};
constructor(props) {
super(props);
this.state = {
selectedStates: []
};
this.updateSelectedStates = this.updateSelectedStates.bind(this);
this.onPressDay = this.onPressDay.bind(this);
this.getCalendarDays = this.getCalendarDays.bind(this);
}
componentDidMount() {
this.updateSelectedStates(this.props.date.jDate());
}
// Trigger date change if new props are provided.
// Typically, when selectedDate is changed programmatically.
//
componentWillReceiveProps(newProps) {
this.updateSelectedStates(newProps.date.jDate());
}
updateSelectedStates(day) {
let selectedStates = [],
daysInMonth = moment.jDaysInMonth(this.props.year, this.props.month);
for (let i = 1; i <= daysInMonth; i++) {
if (i === day) {
selectedStates.push(true);
} else {
selectedStates.push(false);
}
}
this.setState({
selectedStates: selectedStates
});
}
onPressDay(day) {
this.updateSelectedStates(day);
this.props.onDayChange({day: day});
}
// Not going to touch this one - I'd look at whether there is a more functional
// way you can do this using something like `range`, `map`, `partition` and such
// (see underscore.js), or just break it up into steps: first generate the array for
// data, then map that into the components
getCalendarDays() {
let columns,
matrix = [],
month = this.props.month,
year = this.props.year,
currentDay = 0,
thisMonthFirstDay = moment(year + '/' + (month + 1) + '/1', 'jYYYY/jM/jD'),
dayOfWeek = (thisMonthFirstDay.weekday() - 1) % 7,
slotsAccumulator = 0;
for (let i = 0; i < MAX_ROWS; i++ ) { // Week rows
columns = [];
for (let j = 0; j < MAX_COLUMNS; j++) { // Day columns
if (slotsAccumulator >= dayOfWeek) {
if (currentDay < moment.jDaysInMonth(year, month)) {
columns.push(<Day
key={j}
day={currentDay + 1}
selected={this.state.selectedStates[currentDay]}
date={moment(year + '/' + (month + 1) + '/' + (currentDay + 1), 'jYYYY/jM/jD')}
maxDate={this.props.maxDate}
minDate={this.props.minDate}
onDayChange={this.onPressDay}
screenWidth={this.props.screenWidth}
selectedDayColor={this.props.selectedDayColor}
selectedDayTextColor={this.props.selectedDayTextColor}
textStyle={this.props.textStyle}
prevDays={this.props.prevDays}
/>);
currentDay++;
} else if (currentDay >= moment.jDaysInMonth(year, month)) {
columns.push(<Day
key={j}
day={''}
screenWidth={this.props.screenWidth} />);
}
} else {
columns.push(<Day
key={j}
day={''}
screenWidth={this.props.screenWidth} />);
}
slotsAccumulator++;
}
matrix[i] = [];
columns.reverse();
matrix[i].push(<View key={i} style={styles.weekRow}>{columns}</View>);
}
return matrix;
}
render() {
return (
<View style={styles.daysWrapper}>
{this.getCalendarDays()}
</View>
);
}
}
module.exports = Days;