-
Notifications
You must be signed in to change notification settings - Fork 8
/
PersianCalendarPicker.js
146 lines (130 loc) · 4.07 KB
/
PersianCalendarPicker.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
/**
* 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 HeaderControls = require('./HeaderControls');
const WeekDaysLabels = require('./WeekDaysLabels');
const Days = require('./Days');
class PersianCalendarPicker extends React.Component {
static propTypes = {
maxDate: PropTypes.instanceOf(Date),
minDate: PropTypes.instanceOf(Date),
selectedDate: PropTypes.instanceOf(Date).isRequired,
onDateChange: PropTypes.func,
screenWidth: PropTypes.number,
weekdays: PropTypes.array,
months: PropTypes.array,
previousTitle: PropTypes.string,
nextTitle: PropTypes.string,
selectedDayColor: PropTypes.string,
selectedDayTextColor: PropTypes.string,
scaleFactor: PropTypes.number,
textStyle: Text.propTypes.style,
prevDays: PropTypes.string,
};
constructor(props) {
super(props);
// if (this.props.scaleFactor !== undefined) {
// styles = StyleSheet.create(makeStyles(this.props.scaleFactor));
// }
let date = moment(this.props.selectedDate);
this.state = {
date: date,
day: date.jDate(),
month: date.jMonth(),
year: date.jYear(),
selectedDay: []
};
this.onDayChange = this.onDayChange.bind(this);
this.onMonthChange = this.onMonthChange.bind(this);
this.getNextYear = this.getNextYear.bind(this);
this.getPrevYear = this.getPrevYear.bind(this);
this.onDateChange = this.onDateChange.bind(this);
}
// Trigger date change if new props are provided.
// Typically, when selectedDate is changed programmatically.
//
componentWillReceiveProps(newProps) {
let date = moment(newProps.selectedDate);
this.setState({
date: date,
day: date.jDate(),
month: date.jMonth(),
year: date.jYear(),
});
}
onDayChange(day) {
this.setState({day: day.day}, () => { this.onDateChange(); });
}
onMonthChange(month) {
this.setState({month: month}, () => { this.onDateChange(); });
}
getNextYear(){
this.setState({year: this.state.year + 1}, () => { this.onDateChange(); });
}
getPrevYear() {
this.setState({year: this.state.year - 1}, () => { this.onDateChange(); });
}
onDateChange() {
let {
day,
month,
year,
} = this.state;
let date = moment(year + '/' + (month + 1) + '/' + day, 'jYYYY/jM/jD');
let date2 = new Date(date.year(), date.month(), date.date());
console.log(this.props);
console.log(this.state);
this.setState({date: date});
this.props.onDateChange(date2);
}
render() {
return (
<View style={styles.calendar}>
<HeaderControls
maxDate={this.props.maxDate}
minDate={this.props.minDate}
year={this.state.year}
month={this.state.month}
onMonthChange={this.onMonthChange}
getNextYear={this.getNextYear}
getPrevYear={this.getPrevYear}
months={this.props.months}
previousTitle={this.props.previousTitle}
nextTitle={this.props.nextTitle}
textStyle={this.props.textStyle}
/>
<WeekDaysLabels
screenWidth={this.props.screenWidth}
weekdays={this.props.weekdays}
textStyle={this.props.textStyle}
/>
<Days
maxDate={this.props.maxDate}
minDate={this.props.minDate}
month={this.state.month}
year={this.state.year}
date={this.state.date}
onDayChange={this.onDayChange}
screenWidth={this.props.screenWidth}
selectedDayTextColor={this.props.selectedDayTextColor}
selectedDayColor={this.props.selectedDayColor}
textStyle={this.props.textStyle}
prevDays={this.props.prevDays}
/>
</View>
);
}
}
module.exports = PersianCalendarPicker;