React Native 自定义 底部选择框 实现

效果如图所示:

实现方法:

一、组件封装

CustomAlertDialog.js
import React, {Component} from 'react';
import {Dimensions, Modal, StyleSheet, Text, TouchableOpacity, View} from 'react-native'; const {width} = Dimensions.get('window');
export default class CustomAlertDialog extends Component { constructor(props) {
super(props);
this.state = {
isVisible: this.props.show,
};
this.entityList = this.props.entityList;
} componentWillReceiveProps(nextProps) {
this.setState({isVisible: nextProps.show});
} closeModal() {
this.setState({
isVisible: false
});
this.props.closeModal(false);
} renderItem(item, i) {
return (
<TouchableOpacity key={i} onPress={this.choose.bind(this, i)} style={styles.item}>
<Text style={styles.itemText}>{item}</Text>
</TouchableOpacity>
);
} choose(i) {
if (this.state.isVisible) {
this.props.callback(i);
this.closeModal();
}
} renderDialog() {
return (
<View style={styles.modalStyle}>
<View style={styles.optArea}>
{
this.entityList.map((item, i) => this.renderItem(item, i))
}
</View>
</View>
)
} render() {
return (
<View style={{flex: }}>
<Modal
transparent={true}
visible={this.state.isVisible}
animationType={'fade'}
onRequestClose={() => this.closeModal()}>
<TouchableOpacity style={styles.container} activeOpacity={}
onPress={() => this.closeModal()}>
{this.renderDialog()}
</TouchableOpacity>
</Modal>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: ,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
modalStyle: {
position: "absolute",
left: ,
bottom: ,
width: width,
flex: ,
flexDirection: "column",
backgroundColor: '#ffffff',
},
optArea: {
flex: ,
flexDirection: 'column',
marginTop: ,
marginBottom: ,
},
item: {
width: width,
height: ,
paddingLeft: ,
paddingRight: ,
alignItems: 'center',
},
itemText: {
fontSize: ,
},
cancel: {
width: width,
height: ,
marginTop: ,
alignItems: 'center',
backgroundColor: '#ffffff'
},
});

使用

TestCustomAlert.js

import React, {Component} from "react";
import {StyleSheet, Text, TouchableHighlight, View,} from 'react-native';
import CustomAlertDialog from "./CustomAlertDialog"; const typeArr = ["不限", "男", "女"]; export default class TestCustomAlert extends Component {
constructor(props) {
super(props);
this.state = {
typeName: '性别',
type: ,
showTypePop: false,
}
} _openTypeDialog() {
this.setState({showTypePop: !this.state.showTypePop})
} render() { return (
<View style={{flex: }}> <TouchableHighlight onPress={() => this._openTypeDialog()} style={styles.button}
underlayColor="#a5a5a5">
<Text>{this.state.typeName}-{this.state.type}</Text>
</TouchableHighlight> <CustomAlertDialog entityList={typeArr} callback={(i) => {
this.setState({
type: i,
typeName: typeArr[i],
})
}} show={this.state.showTypePop} closeModal={(show) => {
this.setState({
showTypePop: show
})
}}/>
</View>
); }
} const styles = StyleSheet.create({
button: {
margin: ,
backgroundColor: 'white',
padding: ,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#cdcdcd'
},
});

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/11038382.html

转载请著名出处!谢谢~~

[RN] React Native 自定义 底部 弹出 选择框 实现的更多相关文章

  1. [deviceone开发]-底部弹出选择

    一.简介 个人上传的第一个示例源码,两天空闲时间写的,一点简单组件,写的挺乱还没啥注释,仅供新手学习. 底部弹出选择,可滑动选择选项,如果停留在选项中间,可自动校正位置,加了一点简单的动画效果,需要的 ...

  2. 【ABAP系列】SAP ALV 导出报表数据 始终使用选定的格式”,一旦勾上,就再也不会弹出选择框了。

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ALV 导出报表数据 始 ...

  3. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

  4. 微信小程序之 动画 —— 自定义底部弹出层

    wxml: <view class='buy' bindtap='showBuyModal'>立即购买</view> <!-- 点击立即购买 弹出购买遮罩层 --> ...

  5. Jquery实现弹出选择框选择后返回,支持多级分类

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. appium+python自动化98-非select弹出选择框定位解决

    前言 遇到问题:document.getElementsByClassName(...)[0] is undefined 选择框如果是select标签的,可以直接用select专用的方法去定位点击操作 ...

  7. 框架-弹出选择框(Jquery传递Json数组)

    给一个button按钮,执行方法 Json传值$("body").on("click", "#btnsure", function() {  ...

  8. 微信小程序之自定义底部弹出框动画

    最近做小程序时,会经常用到各种弹框.直接做显示和隐藏虽然也能达到效果,但是体验性太差,也比较简单粗暴.想要美美地玩,添加点动画还是非常有必要的.下面做一个底部上滑的弹框. wxml <view ...

  9. Android仿IOS底部弹出选择菜单ActionSheet

    使用Dialog的实现方式,解决原ActionSheet使用Fragment实现而出现的部分手机取消按钮被遮盖的问题 java部分代码: import android.app.Dialog; impo ...

随机推荐

  1. bizcharts 图表内容居中

    当图表内的数据只有一组时,会紧靠在y轴上,如下图: 想要图表的内容居中,解决方法分两种情况. 第一种:如果x轴是日期,则代码设置如下,图表的内容就居中了 const cols = { x: { ali ...

  2. mybatis + mysql 批量插入、删除、更新

    mybatis + mysql 批量插入.删除.更新 Student 表结构 批量插入 public int insertBatchStudent(List<Student> studen ...

  3. HUT 排序训练赛 F - 水果

    Problem's Link Mean: 略. analyse: 使用结构体排序. 首先,定义一个结构体,用来存放输入的数据,然后就是输入,注意:这儿有一个小细节,输入数字,然后紧跟着输入字符串,这时 ...

  4. MongoDB学习笔记(五)

    MongoDB 查看执行计划 MongoDB 中的 explain() 函数可以帮助我们查看查询相关的信息,这有助于我们快速查找到搜索瓶颈进而解决它,本文我们就来看看 explain() 的一些用法及 ...

  5. 缓存雪崩、穿透如何解决,如何确保Redis只缓存热点数据?

    缓存雪崩如何解决? 缓存穿透如何解决? 如何确保Redis缓存的都是热点数据? 如何更新缓存数据? 如何处理请求倾斜? 实际业务场景下,如何选择缓存数据结构 缓存雪崩 缓存雪崩简单说就是所有请求都从缓 ...

  6. 2019-11-29-WPF-绑定命令在-MVVM-的-CanExecute-和-Execute-在按钮点击都没触发可能的原因...

    原文:2019-11-29-WPF-绑定命令在-MVVM-的-CanExecute-和-Execute-在按钮点击都没触发可能的原因... title author date CreateTime c ...

  7. HTML+CSS学习笔记整理

    一.标签语义化(重点): 1.可以方便代码的阅读和维护 2.同时让网络爬虫更好的解析从而更好的分析其内容 3.更好的优化引擎 如何做到标签语义化:个人理解是,首先,网页的HTML主要作用在网页的结构上 ...

  8. C# ??(两个问号)的表达式使用详解

    今天有人问我C#中两个问号是什么意思,怎么使用,于是乎有了这篇随笔 有时候我们需要判断某个对象是否为null,一般的做法是 if(x=null){....} 若想让自己的代码更简洁,可以这样写: st ...

  9. .Net Core 给WebApi接口返回值添加全局的日期格式化

    public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(option ...

  10. C# Socket服务器及多客户端连接示例

    服务端代码[控制台示例] static List<Socket> Sockets = new List<Socket>(); static void Main(string[] ...