-
Notifications
You must be signed in to change notification settings - Fork 8
/
HeaderControls.js
166 lines (148 loc) · 4.62 KB
/
HeaderControls.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
/**
* 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 {
Text,
TouchableOpacity,
View,
} = require('react-native');
const styles = require('./style');
const {
MONTHS,
} = require('./util');
class HeaderControls extends React.Component {
static propTypes = {
month: PropTypes.number.isRequired,
year: PropTypes.number,
getNextYear: PropTypes.func.isRequired,
getPrevYear: PropTypes.func.isRequired,
onMonthChange: PropTypes.func.isRequired,
textStyle: Text.propTypes.style
};
constructor(props) {
super(props);
this.state = {
selectedMonth: this.props.month
};
this.getNext = this.getNext.bind(this);
this.getPrevious = this.getPrevious.bind(this);
this.previousMonthDisabled = this.previousMonthDisabled.bind(this);
this.nextMonthDisabled = this.nextMonthDisabled.bind(this);
}
// Trigger date change if new props are provided.
// Typically, when selectedDate is changed programmatically.
//
componentWillReceiveProps(newProps) {
this.setState({
selectedMonth: newProps.month
});
}
// Logic seems a bit awkawardly split up between here and the CalendarPicker
// component, eg: getNextYear is actually modifying the state of the parent,
// could just let header controls hold all of the logic and have CalendarPicker
// `onChange` callback fire and update itself on each change
getNext() {
let next = this.state.selectedMonth + 1;
if (next > 11) {
this.setState( { selectedMonth: 0 },
// Run this function as a callback to ensure state is set first
() => {
this.props.getNextYear();
this.props.onMonthChange(this.state.selectedMonth);
}
);
} else {
this.setState({ selectedMonth: next },
() => {
this.props.onMonthChange(this.state.selectedMonth);
}
);
}
}
getPrevious() {
let prev = this.state.selectedMonth - 1;
if (prev < 0) {
this.setState({ selectedMonth: 11},
// Run this function as a callback to ensure state is set first
() => {
this.props.getPrevYear();
this.props.onMonthChange(this.state.selectedMonth);
}
);
} else {
this.setState({ selectedMonth: prev },
() => {
this.props.onMonthChange(this.state.selectedMonth);
}
);
}
}
previousMonthDisabled() {
if (this.state.selectedMonth === 1 && this.props.minDate) {
return true;
}
// return ( this.props.minDate &&
// ( this.props.year < this.props.minDate.getFullYear() ||
// ( this.props.year == this.props.minDate.getFullYear() && this.state.selectedMonth <= this.props.minDate.getMonth() )
// )
// );
}
nextMonthDisabled() {
return ( this.props.maxDate &&
( this.props.year > this.props.maxDate.getFullYear() ||
( this.props.year == this.props.maxDate.getFullYear() && this.state.selectedMonth >= this.props.maxDate.getMonth() )
)
);
}
render() {
let textStyle = this.props.textStyle;
let previous;
if ( this.previousMonthDisabled() ) {
previous = (
<Text style={[styles.prev, textStyle, styles.disabledTextColor]}>{this.props.previousTitle || 'ماه قبل'}</Text>
);
}
else {
previous = (
<TouchableOpacity onPress={this.getPrevious}>
<Text style={[styles.prev, textStyle]}>{this.props.previousTitle || 'ماه قبل'}</Text>
</TouchableOpacity>
);
}
let next;
if ( this.nextMonthDisabled() ) {
next = (
<Text style={[styles.next, textStyle, styles.disabledTextColor]}>{this.props.nextTitle || 'ماه بعد'}</Text>
);
}
else {
next = (
<TouchableOpacity onPress={this.getNext}>
<Text style={[styles.next, textStyle]}>{this.props.nextTitle || 'ماه بعد'}</Text>
</TouchableOpacity>
);
}
return (
<View style={styles.headerWrapper}>
<View style={styles.monthSelector}>
{next}
</View>
<View>
<Text style={[styles.monthLabel, textStyle]}>
{ (this.props.months || MONTHS)[this.state.selectedMonth] } { this.props.year.toString().PersianNumber() }
</Text>
</View>
<View style={styles.monthSelector}>
{previous}
</View>
</View>
);
}
}
module.exports = HeaderControls;