Guest User

CalendarYearView.js (screen navigating to)

a guest
Aug 17th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { View, Text, ScrollView } from "react-native";
  3. import style from "./style";
  4. import CalenderAppHeader from "./../../components/CalenderAppHeader";
  5. import YearButton from "./YearButton";
  6. import MonthCalendar from "./MonthCalendar";
  7.  
  8. class CalendarYearView extends Component {
  9.  
  10.     static SCREEN_KEY = "CalendarYearView";
  11.  
  12.     constructor (props) {
  13.         super(props);
  14.         this.state = {
  15.             year: new Date().getFullYear(),
  16.         };
  17.         this.monthArray = this.getArrayOfMonths();
  18.     }
  19.  
  20.     /**
  21.      * This method is used to create a grid of arrays representing months in a year.
  22.      * @return [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]].
  23.      */
  24.     getArrayOfMonths = () => {
  25.         return Array.from(
  26.             {length: 4},
  27.             (v, i) => {
  28.                 return Array.from(
  29.                     {length: 3},
  30.                     (v, j) => this.zeroPad (i*3 + j+1));
  31.             });
  32.     };
  33.  
  34.     /**
  35.      * This method is used to prefill zero if number is less than base
  36.      * zeroPad(3, 10) = 03
  37.      * zeroPad(12, 10) = 12
  38.      * @param {Number} nr - any Number
  39.      * @param {Number} base - 10, 100, 1000
  40.      */
  41.     zeroPad(nr,base = 10){
  42.         var  len = (String(base).length - String(nr).length)+1;
  43.         return len > 0? new Array(len).join('0')+nr : nr;
  44.     }
  45.  
  46.     onYearPress = (year) => {
  47.         this.setState(() => {
  48.             return { year };
  49.         });
  50.     };
  51.  
  52.     render () {
  53.         return (
  54.             <View style={style.container}>
  55.                 <CalenderAppHeader onAddButton={() => {}} />
  56.                 <View style={style.content}>
  57.                     <YearButton onPress={this.onYearPress} year={this.state.year} />
  58.                     <ScrollView style={style.scroll}>
  59.                         {this.monthArray.map(this.renderMonthsGrid)}
  60.                     </ScrollView>
  61.                 </View>
  62.             </View>
  63.         );
  64.     }
  65.    
  66.     renderMonthsGrid = (monthNumbers, index) => {
  67.         return (
  68.             <View key={index} style={style.threeMon}>
  69.                 {monthNumbers.map(monthNum => {
  70.                     return (<MonthCalendar
  71.                                 events={{
  72.                                     "2018-11-09": [{ dotColor: 'green'},
  73.                                     {dotColor: 'blue'}],
  74.                                     "2018-03-11": {}
  75.                                 }}
  76.                                 month={monthNum} year={this.state.year} key={monthNum} />);
  77.                 })}
  78.             </View>
  79.         );
  80.     };
  81. }
  82.  
  83. export default CalendarYearView;
Add Comment
Please, Sign In to add comment