博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ReactNative学习笔记--基于Modal的多步弹窗的封装
阅读量:4086 次
发布时间:2019-05-25

本文共 5202 字,大约阅读时间需要 17 分钟。

效果

弹窗的封装

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

和直接显示菊花转,然后显示网络请求结果的。很简单直接上代码。

import React from 'react';import {    View,    Text,    ActivityIndicator,    TextInput,    TouchableOpacity,    Modal,} from 'react-native';import Const from '../../util/const';import System_styles from '../../util/system_styles';import {    Button,} from '../../common';export default class AlertView_NetRequest extends React.Component {    /**     *  组件介绍:本组件只针对直接有网络请求过程和结果显示的情况,和有输入再进行     *  网络请求过程 然后显示结果的情况     *   组件显示:菊花转----- 请求结果 & 输入框 确定 ------ 菊花转-----请求结果     * */    static defaultProps = {        firstItem:{            title:'',            subTitle:'',            cancleBtn:undefined,            confirmBtn:'确定',            unit:'',输入框的单位            placeHolder:'',///占位符        },        secondItem:{            title:'',            subTitle:'',            cancleBtn:undefined,            confirmBtn:'查看详情',        },        firstConfirmBtnClicked:undefined,        secondConfirmBtnClicked:undefined,        top:140,        type:0,///默认直接请求    };    constructor(){        super();        this.state = {            text:'',            visible:false,            step:0,///请求步骤        };    }    ///显示    show = ()=>{        this.setState({            step:0,            visible:true        })    };    ///隐藏    _hide = ()=>{        this.setState({            visible:false,        })    };    showNetResult = ()=>{        const {step} = this.state;        this.setState({            step:step+1,        })    };    ///确定按钮点击    _confirmBtnClicked = ()=>{        const {firstItem,secondItem,type,firstConfirmBtnClicked,secondConfirmBtnClicked} = this.props;        const {step}= this.state;        var btnClicked = type == 0 ? secondConfirmBtnClicked : (step == 0 ? firstConfirmBtnClicked : secondConfirmBtnClicked);        btnClicked&&btnClicked(this.state.text);        if(type == 0){            this._hide();        }else {            if(step == 0){                this.setState({                    step:step+1,                })            }else {                this._hide();            }        }    }    render() {        const {firstItem,secondItem,type} = this.props;        const {visible,step}= this.state;        var item = type == 0 ? secondItem : (step == 0 ? firstItem : secondItem);        let cancleBtnView = item.cancleBtn == undefined ? null : (                    );        let bottomBtns = (            
{cancleBtnView}
); let textInput = type == 0 ? null :( step == 0 ? (
this.setState({text})} keyboardType='numeric' >
{item.unit}
):null); let normalContent = (type == 0&&step == 0)||(type == 1 && step == 1) ? (
):(
{item.title}
{item.subTitle}
{textInput}
{bottomBtns}
); return (
{normalContent}
) }}

使用方法:

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

render() {        var sub = '最低起购金额'+this.max+'万';       var firstItem ={            title:'申请额度',            subTitle:sub,            cancleBtn:undefined,            confirmBtn:'确定',            unit:'万',输入框的单位            placeHolder:'请输入申请金额',///占位符        };       var secondItem={            title:'您的额度申请已提交',            subTitle:'在额度申请查询中查看审核结果',            cancleBtn:'取消',            confirmBtn:'额度申请查询',       };        return(            
{this._renderHeader()} {this._renderSalesInfoView()} {this._renderContents()}
{this.netRequestAlert=ref}} style={
{marginTop:Const.screen_height/3.0}} firstItem = {firstItem} secondItem = {secondItem} firstConfirmBtnClicked = {this._alertConfirmBtnClicked} secondConfirmBtnClicked = {this._alertConfirmBtnClicked} type = {1} >
) }
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

转载地址:http://oueni.baihongyu.com/

你可能感兴趣的文章
【Unity】SQLite在Unity中的使用-思维导图
查看>>
【Unity】使用类对象存储Json数据——星座运势案例
查看>>
【Unity】DoTween插件的使用
查看>>
【Unity】封装SQLite管理类
查看>>
【Unity】面试题整理
查看>>
【C#】如何实现一个迭代器
查看>>
【Unity】Destroy和DestroyImmediate的区别
查看>>
【Lua】Mac系统下配置SublimeText的Lua编译环境
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
【Unity】微信登录后将头像存为bytes,将bytes读取成sprite图片
查看>>
【Unity】使用GPS定位经纬度
查看>>
【UGUI/NGUI】一键换Text/Label字体
查看>>
【C#】身份证本地验证
查看>>
【Unity】坑爹的Bug
查看>>
【算法】求数组中某两个数的和为目标值
查看>>
【Shader】ShaderToy通用模板
查看>>
【Shader】使用Unity创建水流岩浆等流动效果
查看>>
【Shader】游戏屏幕黑白特效
查看>>
【OpenGL】OpenGL渲染流程详解
查看>>
【Shader】扭曲能量罩特效
查看>>