react native06:日歷組件

做react native 項目的時候需要用到日歷組件 橄杨,但是在網(wǎng)上找的都不是自己想要的樣式和功能 鲁冯,所以自己動手寫了一個 符合自己項目的 ,
calendar.js 代碼如下

/**
 * 日歷組件
 */

import React, {Component} from 'react'
import {
  StyleSheet,
  View,
  Text,
  TouchableOpacity,
  Dimensions,
  FlatList
} from 'react-native'

import moment from 'moment'
import lodash from 'lodash'
import PropTypes from 'prop-types'

const width = Dimensions.get('window').width
const height = Dimensions.get('window').height

class Calendar extends Component {
  constructor(props) {
    super(props)
    this.state = {
      dataSource: [],
      myMonth: moment(new Date()).format('YYYY年MM月')
    }
  }
  componentDidMount() {
    this.monthDay(moment(new Date()).format('YYYY-MM-DD'))
  }
  monthDay = date => {
    var daysArr = []
    var currentWeekday = moment(date)
      .date(1)
      .weekday() // 獲取當月1日為星期幾
    var currentMonthDays = moment(date).daysInMonth() // 獲取當月天數(shù)
    if (currentWeekday == 0) {
      //如果是0的話就是周日
      currentWeekday = 7
    }
    for (var i = 1; i < currentWeekday; i++) {
      daysArr.push({id: ''})
    }
    var YYMM = moment(date, 'YYYY-MM-DD').format('YYYYMM')
    var nowDate = moment(new Date()).format('YYYYMMDD')
    for (var i = 1; i <= currentMonthDays; i++) {
      var myDate = moment(YYMM + i, 'YYYYMMD').format('YYYYMMDD')
      if (moment(new Date()).date() === moment(myDate, 'YYYYMMDD').date()) {
        daysArr.push({id: i, isSelected: true})
      } else {
        daysArr.push({id: i})
      }
    }
    this.setState({
      dataSource: daysArr
    })
  }
  left = () => {
    this.setState({
      myMonth: moment(this.state.myMonth, 'YYYY年MM月')
        .subtract('month', 1)
        .format('YYYY年MM月')
    })
    this.monthDay(
      moment(this.state.myMonth, 'YYYY年MM月')
        .subtract('month', 1)
        .format('YYYY-MM-DD')
    )
  }
  right = () => {
    this.setState({
      myMonth: moment(this.state.myMonth, 'YYYY年MM月')
        .add('month', 1)
        .format('YYYY年MM月')
    })
    this.monthDay(
      moment(this.state.myMonth, 'YYYY年MM月')
        .add('month', 1)
        .format('YYYY-MM-DD')
    )
  }
  myClickDate = index => {
    let {dataSource} = this.state
    dataSource.forEach(item => {
      item.isSelected = false
    })
    dataSource[index].isSelected = true
    this.setState({
      dataSource: lodash.cloneDeep(dataSource)
    })
    let selectDate = this.state.myMonth + dataSource[index].id + '日'
    this.props.callback(selectDate)
  }
  render() {
    return (
      <View
        style={{
          paddingHorizontal: width * 0.03,
          backgroundColor: this.props.bgColor ? this.props.bgColor : '#ff5754',
          paddingBottom: 10
        }}
      >
        <React.Fragment>
              <View
                style={{
                  flexDirection: 'row',
                  alignItems: 'center',
                  justifyContent: 'center',
                  paddingVertical: 10
                }}
              >
                <TouchableOpacity onPress={this.left} style={styles.leftIcon}>
                  <Text
                    style={{
                      color: this.props.dayColor
                        ? this.props.dayColor
                        : '#fff',
                      fontSize: 16
                    }}
                  >
                    上一月{' '}
                  </Text>
                </TouchableOpacity>
                <View
                  style={{
                    width: width * 0.4,
                    height: 50,
                    alignItems: 'center',
                    justifyContent: 'center'
                  }}
                >
                  <Text
                    style={{
                      fontSize: 16,
                      color: this.props.dayColor
                        ? this.props.dayColor
                        : '#fff'
                    }}
                  >
                    {this.state.myMonth}
                  </Text>
                </View>
                <TouchableOpacity
                  onPress={this.right}
                  style={styles.leftIcon}
                >
                  <Text
                    style={{
                      color: this.props.dayColor
                        ? this.props.dayColor
                        : '#fff',
                      fontSize: 16
                    }}
                  >
                    下一月
                  </Text>
                </TouchableOpacity>
              </View>
              <View style={{flexDirection: 'row', paddingBottom: 10}}>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  一
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  二
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  三
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  四
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  五
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  六
                </Text>
                <Text
                  style={[styles.line, {color: this.props.headTextColor}]}
                >
                  日
                </Text>
              </View>
            </React.Fragment>
            <View style={{flexDirection:'row',flexWrap:'wrap'}}>
              {this.state.dataSource.map((item,index)=>{
                return(
                  <View key={index} style={styles.row}>
                    {item.isSelected ? (
                      <TouchableOpacity
                        onPress={() => this.myClickDate(index)}
                        style={[
                          styles.littleRow,
                          {backgroundColor: this.props.activeBgColor,borderRadius:this.props.borderRadius?this.props.borderRadius:width * 0.2}
                        ]}
                      >
                        <Text style={{color: this.props.activeTextColor}}>
                          {item.id}
                        </Text>
                      </TouchableOpacity>
                    ) : (
                      <TouchableOpacity
                        onPress={() => this.myClickDate(index)}
                        style={[styles.littleRow,{borderRadius:this.props.borderRadius?this.props.borderRadius:width * 0.2}]}
                      >
                        <Text style={{color: this.props.textColor}}>{item.id}</Text>
                      </TouchableOpacity>
                    )}
                  </View>
                )
              })
            }
          </View>
      </View>
    )
  }
}
const styles = StyleSheet.create({
  line: {
    flex: 1,
    textAlign: 'center',
    color: '#ff8c87'
  },
  row: {
    width: (width * 0.936) / 7,
    height: (width * 0.936) / 7,
    alignItems: 'center',
    justifyContent: 'center'
  },
  littleRow: {
    width: width * 0.1,
    height: width * 0.1,
    alignItems: 'center',
    justifyContent: 'center',
    // borderRadius: width * 0.2
  },
  leftIcon: {
    height: 50,
    width: width * 0.3,
    alignItems: 'center',
    justifyContent: 'center'
  }
})
Calendar.defaultProps = {
  bgColor: '#ff6060',
  headTextColor: '#ff8c87',
  textColor: '#fff',
  activeBgColor: '#fff',
  activeTextColor: '#ff6060'
}
Calendar.PropTypes = {
  bgColor: PropTypes.string.isRequired,
  headTextColor: PropTypes.string.isRequired,
  textColor: PropTypes.string.isRequired,
  activeBgColor: PropTypes.string.isRequired,
  callback: PropTypes.func.isRequired
}

export default Calendar

使用方法:

<Calendars 
        // bgColor='#b50' //背景顏色
          // headTextColor='#b30' //頭部文字顏色
          // textColor='#f00' //日期文字顏色
          // activeBgColor='#fb0' //選中背景色
          // activeTextColor='#fff' //選中文字顏色
          // borderRadius={50} //圓角 
          callback={(data)=>{console.log(data)}} //回調(diào)方法
        />

更新了ios上面第一次日期顯示不全的問題
具體效果看下圖


QQ20180825-000523.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拖刃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贪绘,老刑警劉巖兑牡,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異税灌,居然都是意外死亡均函,警方通過查閱死者的電腦和手機亿虽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苞也,“玉大人洛勉,你說我怎么就攤上這事∪绯伲” “怎么了收毫?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長氓涣。 經(jīng)常有香客問我牛哺,道長,這世上最難降的妖魔是什么劳吠? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任引润,我火速辦了婚禮,結(jié)果婚禮上痒玩,老公的妹妹穿的比我還像新娘淳附。我一直安慰自己,他們只是感情好蠢古,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布奴曙。 她就那樣靜靜地躺著,像睡著了一般草讶。 火紅的嫁衣襯著肌膚如雪洽糟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天堕战,我揣著相機與錄音坤溃,去河邊找鬼。 笑死嘱丢,一個胖子當著我的面吹牛薪介,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播越驻,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼汁政,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缀旁?” 一聲冷哼從身側(cè)響起记劈,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诵棵,沒想到半個月后抠蚣,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡履澳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年嘶窄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片距贷。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡柄冲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出忠蝗,到底是詐尸還是另有隱情现横,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布阁最,位于F島的核電站戒祠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏速种。R本人自食惡果不足惜姜盈,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望配阵。 院中可真熱鬧馏颂,春花似錦、人聲如沸棋傍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瘫拣。三九已至亿絮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間麸拄,已是汗流浹背派昧。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留感帅,地道東北人斗锭。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像失球,于是被迫代替她去往敵國和親岖是。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,185評論 25 707
  • 1实苞、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,982評論 3 119
  • 01 說到兼職黔牵,就忍不住要吐槽聪轿。 大一剛開學(xué)的時候,下決心一定要找份兼職猾浦,這樣我就可以任性地買東西陆错,跟朋友到處游山...
    小仙哥閱讀 398評論 1 0
  • 其實我最怕的一個詞就是“得過且過”音瓷。 在一個人沒有追求的時候对嚼,就是混混沌沌的過日子,時間少了绳慎,遺憾多了纵竖。 這是我最...
    見夏知曉閱讀 287評論 0 0
  • 無論苦難和成功都是對我們的考驗,遭遇失敗和苦難的時候杏愤,不應(yīng)牢騷滿腹靡砌,不要怨天尤人,而是要忍受考驗珊楼,堅持努力通殃,...
    有一天_ceb9閱讀 231評論 0 0