React 点击按钮显示div与隐藏div,并给div传children
最近做了一个react的点击按钮显示与隐藏div的一个小组件:


【筛选】组件FilterButton
import React,{Component} from 'react';
import {render} from 'react-dom';
export default class FilterButton extends Component{
constructor(props){
super(props);
this.state = {
clickProps:{
display: 'none', //控制display的值来隐藏与显示
name:'筛选'
}
}
}
//组件的props发生改变,在组件接收到一个新的prop时被执行。这个方法在初始化render时不会被调用。
componentWillReceiveProps(nextProps) {
if(nextProps.needConfirm) {
this.setState(
{
clickProps:{
display: 'none',
name:'筛选'
}
}
);
}
}
/*
* 'inline-block' == this.state.clickProps.display 条件:当前的state中display的值是否为 inline-block
* this.setState({clickProps:{display: 'none',name:'筛选'}}) 值: 如果是,则隐藏div并在button上显示'筛选'
* this.setState({clickProps:{display: 'inline-block',name:'取消'}}); 值: 如果不是,则显示div并在button上显示'取消'
*/
changeDisplay() {
'inline-block' == this.state.clickProps.display ? this.setState({clickProps:{display: 'none',name:'筛选'}}) : this.setState({clickProps:{display: 'inline-block',name:'取消'}});
this.props.click(this.state.clickProps.display);
}
//this.props.children为这个按钮的子组件
render(){
return(
<div className="box" style={{'margin': '20'}}>
<button ref="tip" className="btn btn-default" style={{'display':'block','marginTop': '118'}} onClick={this.changeDisplay.bind(this)}><span className="glyphicon glyphicon-th-list"></span> {this.state.clickProps.name}</button>
<div className="filter-box" style={{'display':this.state.clickProps.display,'height':'auto','padding':'10','border':'1px solid #ddd','borderRadius':'4','boxShadow':'0px 2px 4px #ccc','marginTop':'10','backgroundColor':'#fff','position':'fixed','left':'310px','zIndex':'9999','transition':'all 3s ease-in-out'}}>
{this.props.children}
</div>
</div>
);
}
}
【调用】组件 FilterButton
import React,{Component} from 'react';
import {render} from 'react-dom';
import Input from 'react-bootstrap/lib/Input.js';
import FilterButton from '../../../../public_component/button/FilterButton';
export default class InspectionResults extends Component {
constructor(props){
super(props);
}
render(){
//使用一个常量,调用FilterButton,并把它的子组件回传
const selectBtn = (
<FilterButton click={this.selectClick.bind(this)} needConfirm={this.state.needConfirm}>
<Input className="box-select" type='select'
placeholder="选择部门" onChange={this.changeDepartment.bind(this)} value={this.state.department}>
{department}
</Input>
<Input className="box-select" type='select'
placeholder="选择产品线" onChange={this.changeProductLine.bind(this)} value={this.state.productLine}>
{productLine1}
</Input>
<button type="button" name="新增" className="btn btn-jj-add mt24" onClick={this.selectConfirm.bind(this)}>
确定
</button>
</FilterButton>
);
return(
<div>{selectBtn}</div>
);
}
}
react.js 传子组件的另一个方法,也可以这样做:
const children = (
<Input className="box-select" type='select'
placeholder="测试加载" onChange={this.changeDepartment.bind(this)} value={this.state.department}>
{department}
</Input>
<Input className="box-select" type='select'
placeholder="测试加载" onChange={this.changeProductLine.bind(this)} value={this.state.productLine}>
{productLine1}
</Input>
<button type="button" name="新增" className="btn btn-jj-add mt24" onClick={this.selectConfirm.bind(this)}>
确定
</button>
); <FilterButton chlidren={this.props.children} />
React 点击按钮显示div与隐藏div,并给div传children的更多相关文章
- 点击按钮显示隐藏DIV,点击DIV外面隐藏DIV
点击按钮显示隐藏DIV,点击DIV外面隐藏DIV 注意:此方法对touch事件不行,因为stopPropagation并不能阻止touchend的冒泡 <style type="tex ...
- JavaScript点击按钮显示 确认对话框
//JavaScript点击按钮显示确认对话框 <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- js构建函数,点击按钮显示div,再点击按钮或其他区域,隐藏div
这只是一个例子,先看看效果: html代码: <nav> <span class="nav_logo"></span> <h1>云蚂 ...
- js实现输入密码之延迟星号和点击按钮显示或隐藏
缘由 手机打开segmentfalut时,长时间不登陆了,提示要重新登陆,输入的过程中看到输入密码时,延迟后再变成密文,很好奇,所以捣鼓了一下.本文实现了两种密码展示 代码实现 1 先明后密 js实现 ...
- 安卓TextView限定行数最大值,点击按钮显示所有内容
问题展示 如上图所示,在普通的TextView中,要求: 最多显示3行 超过三行显示展开按钮 且点击展开按钮显示完整内容 这个需求看似简单,但解决起来会遇到两个较为棘手的问题:1,如何判断是否填满了前 ...
- jq 点击按钮显示div,点击页面其他任何地方隐藏div
css .bl_rencai_32{ float: left; height: 35px; line-height: 35px; } .bl_rencai_32 >input{ width: 3 ...
- jquery点击按钮显示和隐藏DIv
function changeDisplay() { if ($("#btnShow").attr("value")== "添加附件") { ...
- js第一天 点击按钮显示与隐藏
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- js实现点击按钮显示某个区域 然后点击页面中任意其他位置,隐藏该区域
$(".licat-header-list").on("click",function(e){ $(this).addClass("active&qu ...
随机推荐
- AC日记——数字统计 openjudge 1.5 41
41:数字统计 总时间限制: 1000ms 内存限制: 65536kB 描述 请统计某个给定范围[L, R]的所有整数中,数字2出现的次数. 比如给定范围[2, 22],数字2在数2中出现了1次, ...
- maven-安装配置
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. maven是什么maven这个词可以翻译为“知识的积累”,也可以翻译为“专家”或“内行” ...
- iOS:CYLTabBarController【低耦合集成TabBarController】
导航 与其他自定义TabBarController的区别 集成后的效果 项目结构 使用CYLTabBarController 第一步:使用CocoaPods导入CYLTabBarController ...
- js判断是否在微信浏览器中打开
用JS来判断,无论是android 还是iphone,ipad 都可以 function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); ...
- BZOJ 1066 【SCOI2007】 蜥蜴
Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为$1$,蜥蜴的跳跃距离是d,即蜥蜴可以跳 ...
- BZOJ 1087 【SCOI2005】 互不侵犯King
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- Html5 Egret游戏开发 成语大挑战(三)开始界面
本篇需要在前面的素材准备完毕,才可以开始,使用egret的eui结合代码编辑,快速完成基本的界面搭建,这里写的可能比较细,目的是减少大家对于其中一些操作疑问,我去掉了很多无用的步骤,以最精简的流程来完 ...
- 李学斌:论复杂系统中的应用间协作V3
说明 本文主要讨论了巨型复杂业务系统的一种构建思路,力图实现决策意志的快速.准确.一致的下传并简化实施成本提供实施效率.通过全业务领域的即时流程编排,实现全网业务IT系统的快速建设与迭代.本文所讲的方 ...
- java 实现从15位~18位的身份证号码转换,校验中国大陆公民身份证、香港居民身份证、澳门身份证和台湾身份证。
package xidian.sl.netcredit.util; /** * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved. ...
- 通过源码分析MyBatis的缓存
前方高能! 本文内容有点多,通过实际测试例子+源码分析的方式解剖MyBatis缓存的概念,对这方面有兴趣的小伙伴请继续看下去~ MyBatis缓存介绍 首先看一段wiki上关于MyBatis缓存的介绍 ...