react-native DatePicker日期選擇組件的實(shí)現(xiàn)代碼
本教程的實(shí)現(xiàn)效果如下:

為了實(shí)現(xiàn)其淡入/淡出的覆蓋效果, 還有取消按鈕, 在此用了一個(gè)三方的組件, 大家可以先安裝一下:
三方組件的地址:https://github.com/eyaleizenberg/react-native-custom-action-sheet (可以看看,也可以直接按我的步驟走)
1. 在terminal的該工程目錄下運(yùn)行: npm install react-native-custom-action-sheet --save
2. 然后運(yùn)行: npm start
3. 具體實(shí)現(xiàn)代碼如下:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
DatePickerIOS
} from 'react-native';
//這是一個(gè)三方組件 github地址:https://github.com/eyaleizenberg/react-native-custom-action-sheet
var CustomActionSheet = require('react-native-custom-action-sheet');
class Demo extends Component {
state = {
datePickerModalVisible: false, //選擇器顯隱標(biāo)記
chooseDate: new Date() //選擇的日期
};
_showDatePicker () { //切換顯隱標(biāo)記
this.setState({datePickerModalVisible: !this.state.datePickerModalVisible});
};
_onDateChange (date) { //改變?nèi)掌趕tate
alert(date); //彈出提示框: 顯示你選擇日期
this.setState({
chooseDate: date
});
};
render() {
let datePickerModal = ( //日期選擇器組件 (根據(jù)標(biāo)記賦值為 選擇器 或 空)
this.state.datePickerModalVisible ?
<CustomActionSheet
modalVisible={this.state.datePickerModalVisible} //顯隱標(biāo)記
onCancel={()=>this._showDatePicker()}> //點(diǎn)擊取消按鈕 觸發(fā)事件
<View style={styles.datePickerContainer}>
<DatePickerIOS
mode={"datetime"} //選擇器模式: 'date'(日期), 'time'(時(shí)間), 'datetime'(日期和時(shí)間)
minimumDate={new Date()} //最小時(shí)間 (這里設(shè)置的是當(dāng)前的時(shí)間)
minuteInterval={30} //最小時(shí)間間隔 (這里設(shè)置的是30分鐘)
date={this.state.chooseDate} //默認(rèn)的時(shí)間
onDateChange={this._onDateChange.bind(this)} //日期被修改時(shí)回調(diào)此函數(shù)
/>
</View>
</CustomActionSheet> : null
);
return (
<View style={styles.container}>
<TouchableHighlight
style={{backgroundColor:'cyan', padding:5}}
onPress={()=>this._showDatePicker()} //按鈕: 點(diǎn)擊觸發(fā)方法
underlayColor='gray'
>
<Text >show DatePick</Text>
</TouchableHighlight>
{datePickerModal} //日期選擇組件
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
datePickerContainer: {
flex: 1,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
marginBottom: 10,
},
});
AppRegistry.registerComponent('Demo', () => Demo);
寫好了,在terminal中運(yùn)行:react-native run-ios 就能看到效果了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react實(shí)現(xiàn)可播放的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)可播放的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
React中useState原理的代碼簡(jiǎn)單實(shí)現(xiàn)
要實(shí)現(xiàn)useState的背后原理,則需要深入了解狀態(tài)是如何在函數(shù)組件的渲染周期中保持和更新的,本文將通過一段代碼簡(jiǎn)單闡述useState鉤子函數(shù)的實(shí)現(xiàn)思路,希望對(duì)大家有所幫助2023-12-12
React路由渲染方式與withRouter高階組件及自定義導(dǎo)航組件應(yīng)用詳細(xì)介紹
這篇文章主要介紹了React路由三種渲染方式、withRouter高階組件、自定義導(dǎo)航組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09
基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼
本篇文章主要介紹了基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
ReactQuery系列之?dāng)?shù)據(jù)轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了ReactQuery系列之?dāng)?shù)據(jù)轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
使用react+redux實(shí)現(xiàn)計(jì)數(shù)器功能及遇到問題
使用redux管理數(shù)據(jù),由于Store獨(dú)立于組件,使得數(shù)據(jù)管理獨(dú)立于組件,解決了組件之間傳遞數(shù)據(jù)困難的問題,非常好用,今天重點(diǎn)給大家介紹使用react+redux實(shí)現(xiàn)計(jì)數(shù)器功能及遇到問題,感興趣的朋友參考下吧2021-06-06
React Draggable插件如何實(shí)現(xiàn)拖拽功能
這篇文章主要介紹了React Draggable插件如何實(shí)現(xiàn)拖拽功能問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
使用React?MUI庫(kù)實(shí)現(xiàn)用戶列表分頁(yè)功能
MUI是一款基于React的UI組件庫(kù),可以方便地構(gòu)建美觀的用戶界面,使用MUI的DataTable組件和分頁(yè)器組件可以輕松實(shí)現(xiàn)用戶列表分頁(yè)功能,這篇文章使用MUI庫(kù)實(shí)現(xiàn)了用戶列表分頁(yè)功能,感興趣的同學(xué)可以參考下文2023-05-05

