import React, { Component } from 'react' import { View, Text, StyleSheet, TouchableOpacity, Image, ScrollView, TouchableHighlight, Keyboard } from 'react-native'; import BottomSheet from 'reanimated-bottom-sheet' import Animated, { exp } from 'react-native-reanimated'; import { px2dp, height, size, onePx, colors, toast, isIos, isEmpty } from '../../utils/base' import { layout } from '../../utils/com'; import RNDatePicker from 'react-native-date-picker'; //@ts-ignore import { Picker } from 'react-native-wheel-pick'; interface State { date: any yearMap: Array monthMap: Array }; interface Props { onDateChange?: Function date?: any mode?: 'time' | 'datetime' | 'date' | 'YY' | 'MM' | 'YY-MM' title?: string }; export default class JDatePicker extends Component { constructor(props: Props) { super(props); this.renderInner = this.renderInner.bind(this) }; public readonly state: Readonly = { date: new Date(), yearMap: this.getYear(), monthMap: this.getMonth(), } public tempDate = this.props.date; public tempYear = new Date().getFullYear().toString(); public tempMonth = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1).toString() : (new Date().getMonth() + 1).toString(); public sheetContainerRef = null; public sheetRef = null; public fall = new Animated.Value(1); public delayHideTimer = null; componentWillUnmount() { this.delayHideTimer && clearTimeout(this.delayHideTimer) }; public getYear() { return ['2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030'] }; public getMonth() { return ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'] }; public show = () => { //打开下拉框 Keyboard.dismiss(); this.delayHideTimer && clearTimeout(this.delayHideTimer); this.sheetContainerRef.setNativeProps({ style: { display: 'flex', zIndex: 999 } }); this.sheetRef.snapTo(1); this.sheetRef.snapTo(1); }; public _onDateChange = (e) => { console.log(e); this.tempDate = e; }; public onYearChange = (e) => { console.log(e); this.tempYear = e; }; public onMonthChange = (e) => { this.tempMonth = e; }; public onDone() { const { mode, onDateChange } = this.props; if (mode === 'YY-MM') { if (isEmpty(this.tempYear) || isEmpty(this.tempMonth)) { return toast("请选择日期"); }; onDateChange && onDateChange({ year: this.tempYear, month: this.tempMonth }); this.handleHide(); } else { if (isEmpty(this.tempDate)) { return toast("请选择日期"); }; if (this.tempDate === this.props.date) { this.handleHide(); return false; } onDateChange && onDateChange(this.tempDate); this.handleHide(); }; }; renderInner() { const { mode = 'date', title = '日期选择' } = this.props; const { date, yearMap, monthMap } = this.state; return ( 取消 {title} 确定 { mode === 'MM' || mode === 'YY-MM' ? { isIos() ? null : <> } : } ) }; public _onCloseEnd = () => { this.sheetContainerRef.setNativeProps({ style: { display: 'none', zIndex: -1 } }); }; public handleHide = () => { this.sheetRef.snapTo(0); this.sheetRef.snapTo(0); this.delayHideTimer = setTimeout(() => { this._onCloseEnd(); }, 250) }; render() { return ( this.sheetContainerRef = c} style={styles.sheetContainer}> this.handleHide()} style={styles.touchOverlayContent}> { this.sheetRef = c }} borderRadius={px2dp(12)} onCloseEnd={() => this._onCloseEnd()} /> ) } }; const styles = StyleSheet.create({ sheetContainer: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, height: '100%', width: '100%', display: 'none', zIndex: -1, overflow: 'hidden' }, overlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, width: '100%', height: '100%', backgroundColor: 'rgba(0,0,0,0.7)', }, touchOverlayContent: { width: '100%', height: '100%', }, headerTouchArea: { height: px2dp(52), paddingHorizontal: px2dp(12), alignItems: 'center', justifyContent: 'center', }, headerRow: { width: '100%', flexDirection: 'row', height: px2dp(52), justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: px2dp(12), borderBottomColor: '#eee', borderBottomWidth: onePx }, headerCancelBtn: { width: px2dp(64), height: px2dp(28), justifyContent: 'center', alignItems: 'center', borderRadius: px2dp(4), // borderColor: '#eee', // borderWidth: onePx }, headerBtn: { width: px2dp(64), height: px2dp(28), backgroundColor: colors.themeColor, justifyContent: 'center', alignItems: 'center', borderRadius: px2dp(4), }, datePickerContainer: { position: 'relative', width: '100%', height: px2dp(280), flexDirection: 'row', justifyContent: 'center', alignItems: 'flex-start', }, datePcikerContent: { width: px2dp(350), height: px2dp(280), // backgroundColor: 'red' }, pickerLine: { width: px2dp(120), flex: 1, backgroundColor: '#fff', // borderStartColor: 'red' }, topCheckedLine: { position: 'absolute', left: 0, top: px2dp(86), width: px2dp(375), height: 1, zIndex: 999, borderBottomColor: '#e9e9e9', borderBottomWidth: onePx }, bottomCheckedLine: { position: 'absolute', left: 0, top: px2dp(128), width: px2dp(375), height: 1, zIndex: 999, borderBottomColor: '#e9e9e9', borderBottomWidth: onePx } });