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

效果如图所示:

实现方法:

一、组件封装

  1. CustomAlertDialog.js
  1. import React, {Component} from 'react';
  2. import {Dimensions, Modal, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
  3.  
  4. const {width} = Dimensions.get('window');
  5. export default class CustomAlertDialog extends Component {
  6.  
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. isVisible: this.props.show,
  11. };
  12. this.entityList = this.props.entityList;
  13. }
  14.  
  15. componentWillReceiveProps(nextProps) {
  16. this.setState({isVisible: nextProps.show});
  17. }
  18.  
  19. closeModal() {
  20. this.setState({
  21. isVisible: false
  22. });
  23. this.props.closeModal(false);
  24. }
  25.  
  26. renderItem(item, i) {
  27. return (
  28. <TouchableOpacity key={i} onPress={this.choose.bind(this, i)} style={styles.item}>
  29. <Text style={styles.itemText}>{item}</Text>
  30. </TouchableOpacity>
  31. );
  32. }
  33.  
  34. choose(i) {
  35. if (this.state.isVisible) {
  36. this.props.callback(i);
  37. this.closeModal();
  38. }
  39. }
  40.  
  41. renderDialog() {
  42. return (
  43. <View style={styles.modalStyle}>
  44. <View style={styles.optArea}>
  45. {
  46. this.entityList.map((item, i) => this.renderItem(item, i))
  47. }
  48. </View>
  49. </View>
  50. )
  51. }
  52.  
  53. render() {
  54. return (
  55. <View style={{flex: }}>
  56. <Modal
  57. transparent={true}
  58. visible={this.state.isVisible}
  59. animationType={'fade'}
  60. onRequestClose={() => this.closeModal()}>
  61. <TouchableOpacity style={styles.container} activeOpacity={}
  62. onPress={() => this.closeModal()}>
  63. {this.renderDialog()}
  64. </TouchableOpacity>
  65. </Modal>
  66. </View>
  67. );
  68. }
  69. }
  70.  
  71. const styles = StyleSheet.create({
  72. container: {
  73. flex: ,
  74. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  75. },
  76. modalStyle: {
  77. position: "absolute",
  78. left: ,
  79. bottom: ,
  80. width: width,
  81. flex: ,
  82. flexDirection: "column",
  83. backgroundColor: '#ffffff',
  84. },
  85. optArea: {
  86. flex: ,
  87. flexDirection: 'column',
  88. marginTop: ,
  89. marginBottom: ,
  90. },
  91. item: {
  92. width: width,
  93. height: ,
  94. paddingLeft: ,
  95. paddingRight: ,
  96. alignItems: 'center',
  97. },
  98. itemText: {
  99. fontSize: ,
  100. },
  101. cancel: {
  102. width: width,
  103. height: ,
  104. marginTop: ,
  105. alignItems: 'center',
  106. backgroundColor: '#ffffff'
  107. },
  108. });

使用

TestCustomAlert.js

  1. import React, {Component} from "react";
  2. import {StyleSheet, Text, TouchableHighlight, View,} from 'react-native';
  3. import CustomAlertDialog from "./CustomAlertDialog";
  4.  
  5. const typeArr = ["不限", "男", "女"];
  6.  
  7. export default class TestCustomAlert extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. typeName: '性别',
  12. type: ,
  13. showTypePop: false,
  14. }
  15. }
  16.  
  17. _openTypeDialog() {
  18. this.setState({showTypePop: !this.state.showTypePop})
  19. }
  20.  
  21. render() {
  22.  
  23. return (
  24. <View style={{flex: }}>
  25.  
  26. <TouchableHighlight onPress={() => this._openTypeDialog()} style={styles.button}
  27. underlayColor="#a5a5a5">
  28. <Text>{this.state.typeName}-{this.state.type}</Text>
  29. </TouchableHighlight>
  30.  
  31. <CustomAlertDialog entityList={typeArr} callback={(i) => {
  32. this.setState({
  33. type: i,
  34. typeName: typeArr[i],
  35. })
  36. }} show={this.state.showTypePop} closeModal={(show) => {
  37. this.setState({
  38. showTypePop: show
  39. })
  40. }}/>
  41. </View>
  42. );
  43.  
  44. }
  45. }
  46.  
  47. const styles = StyleSheet.create({
  48. button: {
  49. margin: ,
  50. backgroundColor: 'white',
  51. padding: ,
  52. borderBottomWidth: StyleSheet.hairlineWidth,
  53. borderBottomColor: '#cdcdcd'
  54. },
  55. });

本博客地址: 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. git 用 diff 来检查改动

    用 diff 来检查改动 项目的开发是由无数个微小的改动组成的.了解项目开发过程的关键就是要搞清楚每一个改动.当然你可以使用 “git status” 命令或更简单的 “git log” 命令来打印出 ...

  2. Blend Brush介绍

    原文:Blend Brush介绍 这篇文章会介绍 Blend怎么设置Brush 全局画刷 1)blend面板的介绍 这5个rectangle分别对应 blend中的 5个设置 1 设置无颜色 2 设置 ...

  3. MySQL job/定时任务/event 学习

    参考文章: https://blog.csdn.net/qq_21108311/article/details/82589850 https://blog.csdn.net/qq_27238185/a ...

  4. .net mvc 几种跨域获取数据方案

    方案一: 在web.conflg配置文件system.webServer节点中添加以下节点配置 <!--允许 "所有网站" 跨域访问写法:--><httpProt ...

  5. PyCharm使用分享

    常用快捷键 PyCharm的快捷键可以通过Setting->keymap查看和设置,如果不知道具体在哪个位置,可以在搜索框中搜索 如果不习惯PyCharm默认的快捷键,也不想去设置,比如习惯了使 ...

  6. php中,5行代码实现无限级分类

    <?php /** * 此方法由@Tonton 提供 * http://my.oschina.net/u/918697 * @date 2012-12-12 */function genTree ...

  7. Flask简介及使用

    目录 Flask简介 wsgiref wsgiref简单应用 两个依赖 werkzeug Jinja2 简单使用 安装 flask快速使用 Django与Flask返回值的对比 Flask简介 ​ F ...

  8. OKHttp和NumberProgressbar组建强大的Android版本更新功能

    你们看过韩国电影<奇怪的她>不?女主角是不是超级漂亮的.......好啦,扯正事吧,先看看女神照片. 公司新项目用到了OKHttp网络框架,在下载文件这块都蒙圈啦,再查查资料就一个Reso ...

  9. Git的下载安装

    下载地址:https://git-scm.com/download/win 命令: git add ...  ---将资源放到缓存区域 git commit  -m "提交说明"  ...

  10. day 69作业

    """ 1.按照上方 知识点总结 模块,总结今天所学知识点: 2.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/t ...