基于react-native-date-picker的日期选择组件

基于react-native-date-picker的日期选择组件

效果示例图

在这里插入图片描述

安装依赖

https://www.npmjs.com/package/react-native-date-picker?activeTab=readme
npm install react-native-date-picker --save

封装组件PickerDateComponent

import React, {forwardRef, useImperativeHandle, useState} from 'react';
import {
  Modal,
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  Platform,
} from 'react-native';
import {pxToPd} from '../../common/js/device';
//下拉组件
import DatePicker from 'react-native-date-picker';

const PickerDateComponent = forwardRef((props, ref) => {
  const [visible, setVisible] = useState(false);
  const [options, setOptions] = useState({
    date: '', //回显日期
    mode: 'date', //date:年月日;datetime:年月日时分;time:时分
  });
  const [tempValue, setTempValue] = useState('');
  const hide = value => {
    setVisible(false);
    options.success(value);
  };
  const show = params => {
    const date = params.date? params.date: getCurrentTime(params.mode ? params.mode : 'date');
    setOptions({
      mode: params.mode ? params.mode : 'date', //类型
      date: date, //当前日期
      success: params.success,
    });
    setTempValue(date);
    setVisible(true);
  };
  //点击取消
  const cancelHandle = () => {
    hide(options.date);
  };
  //点击确认
  const confirmHandle = () => {
    hide(tempValue);
  };
  //选择日期
  const dateChangeHandle = value => {
    const beijingTime = convertToBeijingTime(options.mode, value);
    setTempValue(beijingTime);
  };

  const convertToBeijingTime = (mode, date) => {
    const beijingDateTime = new Date(date.getTime());
    return isoToCustomFormat(mode, beijingDateTime.toISOString()); // 转换为ISO字符串表示
  };
  const isoToCustomFormat = (mode, isoString) => {
    const date = new Date(isoString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    if (mode === 'datetime') {
      const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:00`;
      return formattedTime;
    } else if (mode === 'time') {
      const formattedTime = `${hours}:${minutes}`;
      return formattedTime;
    } else {
      const formattedTime = `${year}-${month}-${day}`;
      return formattedTime;
    }
  };

  //获取当前时间
  const getCurrentTime = mode => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    if (mode === 'datetime') {
      const currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      return currentTime;
    } else if (mode === 'time') {
      const currentTime = ` ${hours}:${minutes}`;
      return currentTime;
    } else {
      const currentTime = `${year}-${month}-${day}`;
      return currentTime;
    }
  };

  useImperativeHandle(ref, () => ({
    show,
  }));
  return (
    <>
      <Modal visible={visible} animationType="fade" transparent>
        <View style={styles.pickerWrap}>
          <View style={styles.pickerBlock}>
            <View style={{height: pxToPd(24)}}></View>
            <View style={styles.pickerContent}>
              {/* 头部 */}
              <View style={styles.pickerHeader}>
                <View style={styles.cancelBtn}>
                  <TouchableOpacity onPress={cancelHandle}>
                    <Text style={styles.cancelText}>取消</Text>
                  </TouchableOpacity>
                </View>
                <View style={styles.timeValue}>
                  <Text style={styles.date}>
                    {tempValue ? tempValue : getCurrentTime(options.mode)}
                  </Text>
                </View>
                <View style={styles.confirmBtn}>
                  <TouchableOpacity onPress={confirmHandle}>
                    <Text style={styles.confirmText}>确认</Text>
                  </TouchableOpacity>
                </View>
              </View>
              {/* 内容 */}
              <View style={styles.pickerBody}>
                {tempValue ? (
                  <DatePicker
                    style={styles.pickerStyle}
                    date={new Date(tempValue)}
                    mode={options.mode}
                    onDateChange={text => dateChangeHandle(text)}
                  />
                ) : (
                  <DatePicker
                    style={styles.pickerStyle}
                    date={new Date(getCurrentTime(options.mode))}
                    mode={options.mode}
                    onDateChange={text => dateChangeHandle(text)}
                  />
                )}
              </View>
            </View>
            <View style={{height: pxToPd(24)}}></View>
          </View>
        </View>
      </Modal>
    </>
  );
});
const styles = StyleSheet.create({
  pickerWrap: {
    flex: 1,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    position: 'relative',
  },
  pickerBlock: {
    borderColor: '#dcdcdc',
    borderWidth: pxToPd(1),
    borderStyle: 'solid',
    borderBottomWidth: 0,
    borderLeftWidth: 0,
    borderRightWidth: 0,
    width: '100%',
    height: pxToPd(540),
    position: 'absolute',
    left: 0,
    right: 0,
    bottom: 0,
    backgroundColor: '#fff',
  },
  pickerContent: {
    width: '91.47%',
    marginLeft: '4.265%',
  },
  pickerHeader: {
    width: '100%',
    height: pxToPd(70),
    flexDirection: 'row',
    justifyContent: 'space-between',
    position: 'relative',
  },
  timeValue: {
    width: pxToPd(300),
    position: 'absolute',
    top: pxToPd(20),
    left: '28.13%',
    alignItems: 'center',
  },
  date: {
    color: '#333',
    fontSize: pxToPd(28),
    fontWeight: '400',
  },
  cancelBtn: {
    borderRadius: pxToPd(35),
    width: pxToPd(150),
    height: pxToPd(70),
    backgroundColor: '#f5f5f5',
    overflow: 'hidden',
  },
  cancelText: {
    color: '#666',
    fontSize: pxToPd(34),
    fontWeight: '400',
    width: pxToPd(150),
    height: pxToPd(70),
    lineHeight: pxToPd(70),
    textAlign: 'center',
  },
  confirmBtn: {
    borderRadius: pxToPd(35),
    width: pxToPd(150),
    height: pxToPd(70),
    backgroundColor: '#FF5B23',
    overflow: 'hidden',
  },
  confirmText: {
    color: '#fff',
    fontSize: pxToPd(34),
    fontWeight: '400',
    width: pxToPd(150),
    height: pxToPd(70),
    lineHeight: pxToPd(70),
    textAlign: 'center',
  },
  pickerBody: {
    width: '100%',
    marginTop: pxToPd(12),
  },
  pickerStyle: {
    width: pxToPd(686),
    backgroundColor: 'white',
    borderRadius: 10,
  },
});
export default PickerDateComponent;

使用组件

//日期组件
import PickerDateComponent from '../../componets/PickerDateComponent';
const [queryParams, setQueryParams] = useState({
    brithday: '1994-04-01', //生日
  });
//生日
  const brithdayRef = useRef(null);
  const brithdayHandle = () => {
    console.log('[生日]');
    brithdayRef.current.show({
      mode: 'date', //类型
      date: queryParams.brithday, //当前日期
      success: value => {
        console.log('[生日]', value);
        setQueryParams({
          ...queryParams,
          brithday: value,
        });
      },
    });
  };
<PickerDateComponent ref={brithdayRef} />

device.js

import {Dimensions, StatusBar, Platform} from 'react-native';
//RN中的尺寸单位为dp,设计稿的单位为px

// 获取屏幕尺寸
const windowDimensions = Dimensions.get('window');

//设备宽度,单位pd
const deviceWidthDp = windowDimensions.width;

//设备高度
const windowHeight = windowDimensions.height;

// 获取状态栏高度
const statusBarCurrentHeight =
  Platform.OS === 'android' ? StatusBar.currentHeight : 20;

//设计稿宽度(这里为750px),单位px
const uiWidthPx = 750;

//px转pd(设计稿中的px转RN中的dp)
//计算公式:设计稿元素宽度(px)/设计稿总宽度(px)=元素的宽度(dp)/屏幕的总宽度(dp)
export const pxToPd = uiElePx => {
  return (uiElePx * deviceWidthDp) / uiWidthPx;
};

//状态栏高度
export const statusBarHeight = () => {
  return statusBarCurrentHeight;
};

// 计算应用程序窗口的高度
// 获取导航栏高度(如果有的话) navigationBarHeight = 0; // 设置默认值
export const appWindowHeight = (navigationBarHeight = 0) => {
  return windowHeight - navigationBarHeight;
};

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是react-native-common-date-picker的介绍: react-native-common-date-picker是一个React Native日期选择组件,支持年月日格式的日期选择。该组件适用于iOS和Android平台,并且可以自定义样式和参数。 使用react-native-common-date-picker,你可以轻松地在你的React Native应用程序中添加日期选择器功能。该组件提供了多种可选参数,例如日期格式、最小日期、最大日期、默认日期等等,以便你根据自己的需求进行自定义。 以下是一个使用react-native-common-date-picker的例子: ```javascript import React, { useState } from 'react'; import { View, Text, TouchableOpacity } from 'react-native'; import DatePicker from 'react-native-common-date-picker'; const App = () => { const [date, setDate] = useState(new Date()); const handleDateChange = (newDate) => { setDate(newDate); }; return ( <View> <TouchableOpacity onPress={() => this.datePicker.onPressDate()}> <Text>{date.toLocaleDateString()}</Text> </TouchableOpacity> <DatePicker ref={(picker) => { this.datePicker = picker; }} style={{ width: 0, height: 0 }} mode="date" date={date} onDateChange={handleDateChange} /> </View> ); }; export default App; ``` 在上面的例子中,我们首先导入了react-native-common-date-picker组件,并在组件中使用useState钩子来管理日期状态。然后,我们在TouchableOpacity组件中渲染了当前日期,并在用户点击该组件时打开了日期选择器。最后,我们将DatePicker组件添加到了视图中,并将其隐藏在了一个看不见的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值