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);
寫(xiě)好了,在terminal中運(yùn)行:react-native run-ios 就能看到效果了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
D3.js(v3)+react 實(shí)現(xiàn)帶坐標(biāo)與比例尺的柱形圖 (V3版本)
這篇文章主要介紹了D3.js(v3)+react 制作 一個(gè)帶坐標(biāo)與比例尺的柱形圖 (V3版本) ,本文通過(guò)實(shí)例代碼文字相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-05-05
React組件中使用JSON數(shù)據(jù)文件的方法詳解
要在 React 組件中使用 JSON 數(shù)據(jù),有多種方法,這篇文章主要為大家詳細(xì)介紹了五個(gè)常見(jiàn)的方法,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2024-01-01
通過(guò)React-Native實(shí)現(xiàn)自定義橫向滑動(dòng)進(jìn)度條的 ScrollView組件
開(kāi)發(fā)一個(gè)首頁(yè)擺放菜單入口的ScrollView可滑動(dòng)組件,允許自定義橫向滑動(dòng)進(jìn)度條,且內(nèi)部渲染的菜單內(nèi)容支持自定義展示的行數(shù)和列數(shù),在內(nèi)容超出屏幕后,渲染順序?yàn)榭v向由上至下依次排列,對(duì)React Native橫向滑動(dòng)進(jìn)度條相關(guān)知識(shí)感興趣的朋友一起看看吧2024-02-02
React如何實(shí)現(xiàn)像Vue一樣將css和js寫(xiě)在同一文件
這篇文章主要介紹了React如何實(shí)現(xiàn)像Vue一樣將css和js寫(xiě)在同一文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
React中實(shí)現(xiàn)編輯框自動(dòng)獲取焦點(diǎn)與失焦更新功能
在React應(yīng)用中,編輯框的焦點(diǎn)控制和數(shù)據(jù)回填是一個(gè)常見(jiàn)需求,本文將介紹如何使用useRef和useEffect鉤子,在組件中實(shí)現(xiàn)輸入框自動(dòng)獲取焦點(diǎn)及失焦后更新數(shù)據(jù)的功能,文中通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2024-01-01
Can't?perform?a?React?state?update?on?an?unmoun
這篇文章主要為大家介紹了Can't?perform?a?React?state?update?on?an?unmounted?component報(bào)錯(cuò)解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

