-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.js
175 lines (158 loc) · 8.59 KB
/
index.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
import _ from 'underscore';
import React from 'react';
import {View, TouchableOpacity, Pressable} from 'react-native';
import moment from 'moment';
import Text from '../Text';
import ArrowIcon from './ArrowIcon';
import styles from '../../styles/styles';
import {propTypes as calendarPickerPropType, defaultProps as defaultCalendarPickerPropType} from './calendarPickerPropTypes';
import generateMonthMatrix from './generateMonthMatrix';
import withLocalize from '../withLocalize';
import Navigation from '../../libs/Navigation/Navigation';
import ROUTES from '../../ROUTES';
import CONST from '../../CONST';
import getButtonState from '../../libs/getButtonState';
import * as StyleUtils from '../../styles/StyleUtils';
class CalendarPicker extends React.PureComponent {
constructor(props) {
super(props);
this.monthNames = moment.localeData(props.preferredLocale).months();
this.daysOfWeek = moment.localeData(props.preferredLocale).weekdays();
let currentDateView = props.value;
if (props.selectedYear) {
currentDateView = moment(currentDateView).set('year', props.selectedYear).toDate();
}
if (props.selectedMonth != null) {
currentDateView = moment(currentDateView).set('month', props.selectedMonth).toDate();
}
if (props.maxDate < currentDateView) {
currentDateView = props.maxDate;
} else if (props.minDate > currentDateView) {
currentDateView = props.minDate;
}
this.state = {
currentDateView,
};
this.moveToPrevMonth = this.moveToPrevMonth.bind(this);
this.moveToNextMonth = this.moveToNextMonth.bind(this);
this.onYearPickerPressed = this.onYearPickerPressed.bind(this);
this.onDayPressed = this.onDayPressed.bind(this);
}
componentDidMount() {
if (this.props.minDate <= this.props.maxDate) {
return;
}
throw new Error('Minimum date cannot be greater than the maximum date.');
}
componentDidUpdate(prevProps) {
// Check if selectedYear has changed
if (this.props.selectedYear === prevProps.selectedYear) {
return;
}
// If the selectedYear prop has changed, update the currentDateView state with the new year value
this.setState(prev => ({currentDateView: moment(prev.currentDateView).set('year', this.props.selectedYear).toDate()}));
}
/**
* Handles the user pressing the year picker button.
* Opens the year selection screen with the minimum and maximum year range
* based on the props, the current year based on the state, and the active route.
*/
onYearPickerPressed() {
const minYear = moment(this.props.minDate).year();
const maxYear = moment(this.props.maxDate).year();
const currentYear = this.state.currentDateView.getFullYear();
Navigation.navigate(ROUTES.getYearSelectionRoute(minYear, maxYear, currentYear, Navigation.getActiveRoute()));
this.props.onYearPickerOpen(this.state.currentDateView);
}
/**
* Calls the onSelected function with the selected date.
* @param {Number} day - The day of the month that was selected.
*/
onDayPressed(day) {
const selectedDate = new Date(this.state.currentDateView.getFullYear(), this.state.currentDateView.getMonth(), day);
this.props.onSelected(selectedDate);
}
moveToPrevMonth() {
this.setState(prev => ({currentDateView: moment(prev.currentDateView).subtract(1, 'M').toDate()}));
}
moveToNextMonth() {
this.setState(prev => ({currentDateView: moment(prev.currentDateView).add(1, 'M').toDate()}));
}
render() {
const currentMonthView = this.state.currentDateView.getMonth();
const currentYearView = this.state.currentDateView.getFullYear();
const calendarDaysMatrix = generateMonthMatrix(currentYearView, currentMonthView);
const hasAvailableDatesNextMonth = moment(this.props.maxDate).endOf('month').startOf('day') > moment(this.state.currentDateView).add(1, 'M');
const hasAvailableDatesPrevMonth = moment(this.props.minDate).startOf('day') < moment(this.state.currentDateView).subtract(1, 'M').endOf('month');
return (
<View>
<View style={[styles.calendarHeader, styles.flexRow, styles.justifyContentBetween, styles.alignItemsCenter, styles.ph4, styles.pr1]}>
<TouchableOpacity
onPress={this.onYearPickerPressed}
style={[styles.alignItemsCenter, styles.flexRow, styles.flex1, styles.justifyContentStart]}
>
<Text style={styles.sidebarLinkTextBold} testID="currentYearText" accessibilityLabel={this.props.translate('common.currentYear')}>{currentYearView}</Text>
<ArrowIcon />
</TouchableOpacity>
<View style={[styles.alignItemsCenter, styles.flexRow, styles.flex1, styles.justifyContentEnd]}>
<Text
style={styles.sidebarLinkTextBold}
testID="currentMonthText"
accessibilityLabel={this.props.translate('common.currentMonth')}
>
{this.monthNames[currentMonthView]}
</Text>
<TouchableOpacity testID="prev-month-arrow" disabled={!hasAvailableDatesPrevMonth} onPress={this.moveToPrevMonth}>
<ArrowIcon disabled={!hasAvailableDatesPrevMonth} direction={CONST.DIRECTION.LEFT} />
</TouchableOpacity>
<TouchableOpacity testID="next-month-arrow" disabled={!hasAvailableDatesNextMonth} onPress={this.moveToNextMonth}>
<ArrowIcon disabled={!hasAvailableDatesNextMonth} />
</TouchableOpacity>
</View>
</View>
<View style={styles.flexRow}>
{_.map(this.daysOfWeek, (dayOfWeek => (
<View key={dayOfWeek} style={[styles.calendarDayRoot, styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}>
<Text style={styles.sidebarLinkTextBold}>{dayOfWeek[0]}</Text>
</View>
)))}
</View>
{_.map(calendarDaysMatrix, week => (
<View key={`week-${week}`} style={styles.flexRow}>
{_.map(week, (day, index) => {
const currentDate = moment([currentYearView, currentMonthView, day]);
const isBeforeMinDate = currentDate < moment(this.props.minDate).startOf('day');
const isAfterMaxDate = currentDate > moment(this.props.maxDate).startOf('day');
const isDisabled = !day || isBeforeMinDate || isAfterMaxDate;
const isSelected = moment(this.props.value).isSame(moment([currentYearView, currentMonthView, day]), 'day');
return (
<Pressable
key={`${index}_day-${day}`}
disabled={isDisabled}
onPress={() => this.onDayPressed(day)}
style={styles.calendarDayRoot}
accessibilityLabel={day ? day.toString() : undefined}
>
{({hovered, pressed}) => (
<View
style={[
styles.calendarDayContainer,
isSelected ? styles.calendarDayContainerSelected : {},
StyleUtils.getButtonBackgroundColorStyle(getButtonState(hovered, pressed)),
]}
>
<Text style={isDisabled ? styles.calendarButtonDisabled : styles.dayText}>{day}</Text>
</View>
)}
</Pressable>
);
})}
</View>
))}
</View>
);
}
}
CalendarPicker.propTypes = calendarPickerPropType;
CalendarPicker.defaultProps = defaultCalendarPickerPropType;
export default withLocalize(CalendarPicker);