调研rn能用的日历/日期选择器

①rn中根本不能用antd-mobile,亲测不能用!不知道https://www.jianshu.com/p/29e5f259cc9d 写这篇文章的人用了什么特异功能
②antd-mobile-rn 里没有日历,只有日期选择器,但是只能在页面中使用,不能再组件中使用,在组件中显示不全,可能因为是从底部弹出来吧
③react-native-calendar 日历样式非常不错,但是在安卓中出现选不中日期的问题!
④最终选择react-native-picker ,他其实就是个滚动效果组件的封装,日期,区域都可以滚动,值都是自己传的,以下介绍用法

git地址

npm install react-native-picker --save
react-native link  react-native-picker


import React,{
      Component } from 'react';
import {
     
  View,
  Modal,
  Text,
  StyleSheet,
  WebView,
  TouchableOpacity,
  Image
} from 'react-native';
import {
      DatePicker, List, Provider,WingBlank } from '@ant-design/react-native';
import {
      connect } from 'react-redux';
import {
     scrnRatio, } from '../../utils/screenAdapter';
import moment from 'moment';
import Picker from 'react-native-picker';
class Calendars extends React.Component {
     
  static navigationOptions = {
     
    header: null,
  };
  constructor(props){
     
    super(props);
  }
  componentDidMount(){
     
    //每次进入日历都是显示今天而不是redux里的值
      this.props.dispatch({
     
          type:'updateStates',
          payload:{
     
              date:moment().format("YYYY-MM-DD")
          }
      })
  }

  //组装日期数据
  _createDateData(){
     
    let date = [];
    var currDate = new Date()
    var year = currDate.getFullYear()
    var month = currDate.getMonth()+1
    for(let i=1970;i<=year;i++){
     
        let month = [];
        for(let j = 1;j<13;j++){
     
            let day = [];
            if(j === 2){
     
                for(let k=1;k<29;k++){
     
                    day.push(k+'日');
                }
                //Leap day for years that are divisible by 4, such as 2000, 2004
                if(i%4 === 0){
     
                    day.push(29+'日');
                }
            }
            else if(j in {
     1:1, 3:1, 5:1, 7:1, 8:1, 10:1, 12:1}){
     
                for(let k=1;k<32;k++){
     
                    day.push(k+'日');
                }
            }
            else{
     
                for(let k=1;k<31;k++){
     
                    day.push(k+'日');
                }
            }
            let _month = {
     };
            _month[j+'月'] = day;
            month.push(_month);
        }
        let _date = {
     };
        _date[i+'年'] = month;
        date.push(_date);
    }
    return date;
  }

  _showDatePicker() {
     
    const {
      dispatch} = this.props
    var year = ''
    var month = ''
    var day = ''
    var dateStr = moment().format("YYYY-MM-DD")
    //console.log('dateStr',dateStr)
    year = dateStr.substring(0,4)
    month = parseInt(dateStr.substring(5,7))
    day = parseInt(dateStr.substring(8,10))

    Picker.init({
     
      pickerTitleText:'时间选择',
      pickerCancelBtnText:'取消',
      pickerConfirmBtnText:'确定',
      
      pickerData: this._createDateData(),
      selectedValue:[year+'年',month+'月',day+'日'],

      onPickerConfirm: (pickedValue, pickedIndex) => {
     
        var year = pickedValue[0].substring(0,pickedValue[0].length-1)
        var month = pickedValue[1].substring(0,pickedValue[1].length-1)
        month = month.padStart(2,'0')
        var day = pickedValue[2].substring(0,pickedValue[2].length-1)
        day = day.padStart(2,'0')
        let str = year+'-'+month+'-'+day
        // this.setState({
     
        // currentDate:str,
        // })
        dispatch({
     
            type:'updateStates',
            payload:{
     
                date:str
            }
        })
        
      },
    });
    Picker.show();
  }
  render() {
     
    console.disableYellowBox = true
    const {
     date} = this.props;
    // console.log(date,'date')
    return (
        <View style={
     {
     height:30*scrnRatio,width:120*scrnRatio,}}>
          <TouchableOpacity style={
     {
     flex:1,height:30*scrnRatio,flexDirection:'row',alignItems:'center',backgroundColor:'#fff',justifyContent:'center'}} onPress={
     ()=>this._showDatePicker()}>
            <Text style={
     styles.txt}>{
     date} </Text>../assets/expand.png')}></Image>
          </TouchableOpacity>
        </View>
        
    );
  }
}

const styles = StyleSheet.create({
     
  big:{
     
    backgroundColor:'#fff',
    flex:1, 
    flexDirection: 'row',justifyContent:'space-between',paddingTop:10*scrnRatio
  },
  txt:{
     
    color:'#333333',
    fontSize:14
  },
});

//当前组件中可使用的store里状态变量
function mapStateToProps({
     param}) {
     
    const {
      date } = param;
    return {
     
      date
    }
}
export default connect(mapStateToProps)(Calendars);

你可能感兴趣的:(知识分享,学习笔记,react,native)