|
| 1 | +import React, { PropTypes, Component } from 'react'; |
| 2 | +import { |
| 3 | + View, |
| 4 | + Text, |
| 5 | +} from 'react-native'; |
| 6 | +import { Utils } from './Utils'; |
| 7 | +import Controls from './Controls'; |
| 8 | + |
| 9 | +export default class HeaderControls extends Component { |
| 10 | + constructor(props) { |
| 11 | + super(props); |
| 12 | + this.state = { |
| 13 | + currentMonth: null, |
| 14 | + currentYear: null, |
| 15 | + } |
| 16 | + this.handleOnPressPrevious = this.handleOnPressPrevious.bind(this); |
| 17 | + this.handleOnPressNext = this.handleOnPressNext.bind(this); |
| 18 | + } |
| 19 | + |
| 20 | + componentWillMount() { |
| 21 | + const { initialDate } = this.props; |
| 22 | + const date = initialDate ? initialDate : new Date(); |
| 23 | + |
| 24 | + this.setState({ |
| 25 | + currentMonth: parseInt(date.getMonth()), |
| 26 | + currentYear: parseInt(date.getFullYear()), |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + handleOnPressPrevious() { |
| 31 | + const { currentMonth, currentYear } = this.state; |
| 32 | + const previousMonth = currentMonth - 1; |
| 33 | + // if previousMonth is negative it means the current month is January, |
| 34 | + // so we have to go to previous year, and set the current month to December |
| 35 | + if (previousMonth < 0) { |
| 36 | + this.setState({ |
| 37 | + currentMonth: parseInt(11), // setting month to December |
| 38 | + currentYear: parseInt(currentYear) - 1, // decrement year |
| 39 | + }); |
| 40 | + } else { |
| 41 | + this.setState({ |
| 42 | + currentMonth: parseInt(previousMonth), |
| 43 | + currentYear: parseInt(currentYear), |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + handleOnPressNext() { |
| 49 | + const { currentMonth, currentYear } = this.state; |
| 50 | + const nextMonth = currentMonth + 1; |
| 51 | + // if nextMonth is greater than 11 it means the current month is December, |
| 52 | + // so we have to go to next year, and set the current month to January |
| 53 | + if (nextMonth > 11) { |
| 54 | + this.setState({ |
| 55 | + currentMonth: parseInt(0), // setting month to December |
| 56 | + currentYear: parseInt(currentYear) + 1, // decrement year |
| 57 | + }); |
| 58 | + } else { |
| 59 | + this.setState({ |
| 60 | + currentMonth: parseInt(nextMonth), |
| 61 | + currentYear: parseInt(currentYear), |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + render() { |
| 67 | + const { styles, initialDate } = this.props; |
| 68 | + const { currentMonth, currentYear } = this.state; |
| 69 | + const MONTHS = Utils.MONTHS; // English Month Array |
| 70 | + const date = initialDate ? initialDate : new Date(); |
| 71 | + // getMonth() call below will return the month number, we will use it as the |
| 72 | + // index for month array in english |
| 73 | + const month = MONTHS[currentMonth]; |
| 74 | + const year = currentYear; |
| 75 | + |
| 76 | + return ( |
| 77 | + <View style={styles.headerWrapper}> |
| 78 | + <View style={styles.monthSelector}> |
| 79 | + <Controls |
| 80 | + label="Previous" |
| 81 | + onPressControl={this.handleOnPressPrevious} |
| 82 | + styles={styles.prev} |
| 83 | + /> |
| 84 | + </View> |
| 85 | + <View> |
| 86 | + <Text style={[styles.monthLabel]}> |
| 87 | + { month } { year } |
| 88 | + </Text> |
| 89 | + </View> |
| 90 | + <View style={styles.monthSelector}> |
| 91 | + <Controls |
| 92 | + label="Next" |
| 93 | + onPressControl={this.handleOnPressNext} |
| 94 | + styles={styles.next} |
| 95 | + /> |
| 96 | + </View> |
| 97 | + |
| 98 | + </View> |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +HeaderControls.propTypes = { |
| 104 | + initialDate: PropTypes.instanceOf(Date), |
| 105 | +}; |
0 commit comments