react native 自己动手写日历区间选择控件

首先还是看效果图:

image.png

代码复制直接可用



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

const width = Dimensions.get('window').width
const now = new Date()
export default class App extends Component {

  constructor(props) {
    super(props)
    this.state = {
      nowDate: new Date(),
      showBtn: true,
      startDate: new Date(),
      count: 0,
      endDate: new Date() + 48 * 60 * 60 * 1000
    }
  }

  _onPressButton(date) {
    let { count } = this.state
    let temp = ++count
    if (this.props.range) { // 区间选择
      this.setState({
        count: temp
      })
      if (temp % 2 === 1) { // 第一个日期
        this.setState({
          startDate: date,
          endDate: date,
          showBtn: false
        })
      } else { // 第二个日期
        if (+new Date(this.state.startDate) >= +new Date(date)) { // 第一个日期大于等于第二个日期
          this.setState({
            startDate: date,
            endDate: date,
            count: ++temp,
            showBtn: false
          })
          return
        }
        this.setState({
          endDate: date,
          showBtn: true
        })
      }
      return
    }
    // 单日点击
    this.setState({
      nowDate: date
    }, () => {
      this.callBack()
    })
  }

  callBack() {
    // 回调事件
  }

  render() {
    let { nowDate, showBtn, startDate, endDate } = this.state
    let content = []
    for (let i = 0; i < this.props.monthNum; i++) { // 月数循环
      let date = new Date(now.getFullYear(), now.getMonth() + i, 1);
      let week = date.getDay(); // 当月从星期几开始
      let days = new Date(now.getFullYear(), now.getMonth() + i + 1, 0).getDate();  // 当月的天数
      let dayList = [];
      for (let y = 0; y < week; y++) { // 前面补空占位
        dayList.push(
          
            
          
        )
      }

      for (let x = 1; x <= days; x++) { // 循环天数
        let toDay = new Date(now.getFullYear(), now.getMonth() + i, x); // 今天
        let g = this.props.goliday[moment(toDay).format('YYYY-MM-DD')];
        if (!g) { // 节假日
          g = x;
        }
        let node = {g}
        if (i == 0 && x < now.getDate()) { // 今天以前的日期置灰 ,无事件
          if ((week + x) % 7 == 1 || (week + x) % 7 == 0) { // 周末 蓝色
            node = {g}
          } else {
            node = {g}
          }
        } else if (i == 0 && x == now.getDate()) { // 今天
          node = 今天
        } else if ((week + x) % 7 == 1 || (week + x) % 7 == 0) { // 周末 蓝色
          node = {g}
        }
        if (!this.props.range) { // 单日
          if (moment(toDay).format('YYYY-MM-DD') === moment(nowDate).format('YYYY-MM-DD')) { // 选中日期
            node = {g}
          }
        } else { // 区间

          if (+new Date(toDay) > +new Date(startDate) && +new Date(toDay) < +new Date(endDate)) {
            node = {g}
          }
          if (moment(toDay).format('YYYY-MM-DD') === moment(endDate).format('YYYY-MM-DD')) { // 选中日期
            node = {g}离店
          }
          if (moment(toDay).format('YYYY-MM-DD') === moment(startDate).format('YYYY-MM-DD')) { // 选中日期
            node = {g}入住
          }
        }
        dayList.push(node)
      }
      content.push( // 添加节点
        
          {moment(date).format('YYYY年MM月')}
          
            {dayList}
          
        
      )
    }

    return (
      
        
          
          
          
          
          
          
          
        
        
          {content}
        
        {
          showBtn ?
            确定 : null
        }
      
    )
  }
}

const styles = StyleSheet.create({
  container: {
    paddingTop: 20,
    flex: 1,
    backgroundColor: '#fff'
  },
  weekContainer: {
    width: width,
    height: 35,
    backgroundColor: '#105eae',
    flexDirection: 'row',
    alignItems: 'center'
  },
  weekText: {
    width: width / 7,
    lineHeight: 35,
    textAlign: 'center',
    color: '#fff'
  },
  content: {
    flexDirection: 'row',
    alignItems: 'center',
    flexWrap: 'wrap'
  },
  day: {
    width: width / 7 - 0.1,
  },
  dayText: {
    textAlign: 'center',
    fontSize: 16,
    paddingVertical: ((width / 7) - 20) * 0.5
  },
  startdayText: {
    textAlign: 'center',
    fontSize: 14
  },
  curr: {
    backgroundColor: '#105eae',
  },
  range: {
    backgroundColor: '#6CAFF5'
  },
  head: {
    alignItems: 'center',
    margin: 10
  },
  start: {
    color: '#fff',
    textAlign: 'center',
    fontSize: 14
  },
  btn: {
    width: width,
    height: 45,
    backgroundColor: '#105eae',
    alignItems: 'center',
    alignSelf: 'center',
    justifyContent: 'center'
  }
})

App.defaultProps = {
  monthNum: 6, // 显示的月数
  range: true,
  goliday: { // 节假日在这里添加
    '2018-04-05': '清明',
    '2018-05-01': '劳动',
    '2018-06-18': '端午',
    '2018-09-24': '中秋',
    '2018-10-01': '国庆'
  }
}

你可能感兴趣的:(react native 自己动手写日历区间选择控件)