ReactNative学习笔记--基于Modal的多步弹窗的封装

本文介绍了一个React Native中的网络请求弹窗组件封装方法,该组件能够处理带有输入框的弹窗,并在用户确认后发起网络请求,在请求过程中显示加载指示器,请求完成后展示结果。
效果

弹窗的封装

此组件只针对有输入框,然后点击确定进行网络请求,伴随菊花转,请求返回的时候在现实请求结果的,如上图显示
和直接显示菊花转,然后显示网络请求结果的。很简单直接上代码。



 
[javascript] view plain copy
  1. import React from 'react';  
  2. import {  
  3.     View,  
  4.     Text,  
  5.     ActivityIndicator,  
  6.     TextInput,  
  7.     TouchableOpacity,  
  8.     Modal,  
  9. } from 'react-native';  
  10.   
  11. import Const from '../../util/const';  
  12. import System_styles from '../../util/system_styles';  
  13.   
  14. import {  
  15.     Button,  
  16. } from '../../common';  
  17.   
  18. export default class AlertView_NetRequest extends React.Component {  
  19.   
  20.     /** 
  21.      *  组件介绍:本组件只针对直接有网络请求过程和结果显示的情况,和有输入再进行 
  22.      *  网络请求过程 然后显示结果的情况 
  23.      *   组件显示:菊花转----- 请求结果 & 输入框 确定 ------ 菊花转-----请求结果 
  24.      * */  
  25.     static defaultProps = {  
  26.         firstItem:{  
  27.             title:'',  
  28.             subTitle:'',  
  29.             cancleBtn:undefined,  
  30.             confirmBtn:'确定',  
  31.             unit:'',////输入框的单位  
  32.             placeHolder:'',///占位符  
  33.         },  
  34.         secondItem:{  
  35.             title:'',  
  36.             subTitle:'',  
  37.             cancleBtn:undefined,  
  38.             confirmBtn:'查看详情',  
  39.         },  
  40.         firstConfirmBtnClicked:undefined,  
  41.         secondConfirmBtnClicked:undefined,  
  42.         top:140,  
  43.         type:0,///默认直接请求  
  44.     };  
  45.   
  46.     constructor(){  
  47.         super();  
  48.         this.state = {  
  49.             text:'',  
  50.             visible:false,  
  51.             step:0,///请求步骤  
  52.         };  
  53.     }  
  54.   
  55.     ///显示  
  56.     show = ()=>{  
  57.         this.setState({  
  58.             step:0,  
  59.             visible:true  
  60.         })  
  61.     };  
  62.   
  63.     ///隐藏  
  64.     _hide = ()=>{  
  65.         this.setState({  
  66.             visible:false,  
  67.         })  
  68.     };  
  69.   
  70.     showNetResult = ()=>{  
  71.         const {step} = this.state;  
  72.         this.setState({  
  73.             step:step+1,  
  74.         })  
  75.   
  76.     };  
  77.   
  78.     ///确定按钮点击  
  79.     _confirmBtnClicked = ()=>{  
  80.         const {firstItem,secondItem,type,firstConfirmBtnClicked,secondConfirmBtnClicked} = this.props;  
  81.         const {step}= this.state;  
  82.   
  83.         var btnClicked = type == 0 ? secondConfirmBtnClicked : (step == 0 ? firstConfirmBtnClicked : secondConfirmBtnClicked);  
  84.   
  85.         btnClicked&&btnClicked(this.state.text);  
  86.         if(type == 0){  
  87.             this._hide();  
  88.         }else {  
  89.             if(step == 0){  
  90.                 this.setState({  
  91.                     step:step+1,  
  92.                 })  
  93.             }else {  
  94.                 this._hide();  
  95.             }  
  96.         }  
  97.     }  
  98.   
  99.     render() {  
  100.   
  101.         const {firstItem,secondItem,type} = this.props;  
  102.   
  103.         const {visible,step}= this.state;  
  104.   
  105.         var item = type == 0 ? secondItem : (step == 0 ? firstItem : secondItem);  
  106.   
  107.         let cancleBtnView = item.cancleBtn == undefined ? null : (  
  108.             <Button style={{flex:1}}  
  109.                     title={item.cancleBtn}  
  110.                     onPress={this._hide}  
  111.             >  
  112.             </Button>  
  113.         );  
  114.   
  115.         let bottomBtns = (  
  116.             <View style={{height:53,flexDirection:'row'}}>  
  117.                 {cancleBtnView}  
  118.                 <View style={{height:53,backgroundColor:System_styles.hei_12,width:1}}/>  
  119.                 <Button style={{flex:1}}  
  120.                         title={item.confirmBtn}  
  121.                         onPress={this._confirmBtnClicked}  
  122.                 >  
  123.                 </Button>  
  124.             </View>  
  125.         );  
  126.   
  127.         let textInput = type == 0 ? null :( step == 0 ? (  
  128.             <View style={{height:64,paddingLeft:16,paddingRight:16,flexDirection:'row'}}>  
  129.                 <View style={{flexDirection:'row',height:40,borderRadius:3,backgroundColor:System_styles.hei_12,paddingLeft:10,paddingRight:7,flex:1,alignItems:'center'}}>  
  130.                     <TextInput  style={{height:40,flex:7}}  
  131.                                 placeholder = {item.placeHolder}  
  132.                                 onChangeText={(text) => this.setState({text})}  
  133.                                 keyboardType='numeric'  
  134.                     >  
  135.                     </TextInput>  
  136.                     <Text style={[{flex:1},System_styles.getChanggui(15,System_styles.hei_32)]}>  
  137.                         {item.unit}  
  138.                     </Text>  
  139.                 </View>  
  140.             </View>  
  141.         ):null);  
  142.   
  143.         let normalContent = (type == 0&&step == 0)||(type == 1 && step == 1) ? (  
  144.             <View  
  145.                 style={[{justifyContent:'center',  
  146.                 alignItems:'center',borderRadius:6  
  147.                 ,backgroundColor:System_styles.white,width:Const.screen_width/3.0,height:Const.screen_width/3.0},this.props.style]}>  
  148.                 <ActivityIndicator  
  149.                     size = 'large'  
  150.                     color={System_styles.hei_56}  
  151.                 />  
  152.             </View>  
  153.         ):(  
  154.             <View  
  155.                 style={[{justifyContent:'center',  
  156.                 alignItems:'center',borderRadius:6  
  157.                 ,backgroundColor:System_styles.white,},this.props.style]}>  
  158.   
  159.                 <View style={[{borderTopLeftRadius:3,borderTopRightRadius:3,alignItems:'center',paddingBottom:26,paddingTop:23}]}>  
  160.                     <Text style={System_styles.getChanggui(17,System_styles.hei_84)}>  
  161.                         {item.title}  
  162.                     </Text>  
  163.                     <Text style={System_styles.getChanggui(13,System_styles.hei_56)}>  
  164.                         {item.subTitle}  
  165.                     </Text>  
  166.                 </View>  
  167.                 {textInput}  
  168.                 <View style={{height:1,backgroundColor:System_styles.hei_12,width:Const.screen_width-64}}/>  
  169.                 {bottomBtns}  
  170.             </View>  
  171.         );  
  172.   
  173.         return (  
  174.             <Modal  
  175.                 animationType={'fade'}  
  176.                 transparent={true}  
  177.                 visible={visible}  
  178.             >  
  179.                 <View  
  180.                     style={{flex:1,backgroundColor:'rgba(0,0,0,0.5)',alignItems:'center',paddingHorizontal:32}}  
  181.                 >  
  182.                     {normalContent}  
  183.                 </View>  
  184.             </Modal>  
  185.   
  186.         )  
  187.     }  
  188. }  
使用方法:

导入后直接放到render()里,AlertView_NetRequest,注意使用位置,一定是最上层,同时设置标记 ref={ref=>{this.netRequestAlert=ref}},便于显示组件,开始请求,结果返回的显示

 
[javascript] view plain copy
  1. render() {  
  2.        var sub = '最低起购金额'+this.max+'万';  
  3.       var firstItem ={  
  4.            title:'申请额度',  
  5.            subTitle:sub,  
  6.            cancleBtn:undefined,  
  7.            confirmBtn:'确定',  
  8.            unit:'万',////输入框的单位  
  9.            placeHolder:'请输入申请金额',///占位符  
  10.        };  
  11.       var secondItem={  
  12.            title:'您的额度申请已提交',  
  13.            subTitle:'在额度申请查询中查看审核结果',  
  14.            cancleBtn:'取消',  
  15.            confirmBtn:'额度申请查询',  
  16.       };  
  17.   
  18.        return(  
  19.            <View  
  20.                style={{flex:1,backgroundColor:'white'}}  
  21.            >  
  22.                <View style={{flex:1,}}>  
  23.                    {this._renderHeader()}  
  24.                    {this._renderSalesInfoView()}  
  25.                    {this._renderContents()}  
  26.                </View>  
  27.                <HorizontalButtons  
  28.                    style={{borderTopColor:System_styles.hei_12,borderTopWidth:0.5,paddingHorizontal:8}}  
  29.                    buttons = {items}  
  30.                    callBack={this._bottomBtnClicked}  
  31.                />  
  32.                <AlertView_NetRequest  
  33.                    ref={ref=>{this.netRequestAlert=ref}}  
  34.                    style={{marginTop:Const.screen_height/3.0}}  
  35.                    firstItem = {firstItem}  
  36.                    secondItem = {secondItem}  
  37.                    firstConfirmBtnClicked = {this._alertConfirmBtnClicked}  
  38.                    secondConfirmBtnClicked = {this._alertConfirmBtnClicked}  
  39.                    type = {1}  
  40.                >  
  41.                </AlertView_NetRequest>  
  42.            </View>  
  43.        )  
  44.    }  
1.开始显示
     this.netRequestAlert.show();
2.点击弹出框上的确定按钮,开始网络请求
 _alertConfirmBtnClicked = (text)=>{
     dispatch(ProductListsActions.requestApplyTrustCount(proItem.getIn(['productId']),Const.userInfo.id,text,this._requestCallBak));
    };
3.请求结果返回的时候调用显示结果
            this.netRequestAlert.showNetResult();

注意:上面的组件研究明白了,就能封装简单的弹出窗,前一部分是全部代码,里面有个组件Button记得用自己项目里的按钮组件替换掉。喜欢的请点

链接:http://www.imooc.com/article/16793
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值