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 ...
随机推荐
- poj 3468
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 90736 Acc ...
- AlertDialog禁止返回键
android 如何让dialog不消失,即使是用户按了返回键dialog也不消失 解决的问题:软件提示升级的dialog时候,用户有可能按了返回键,但是现在的需求是用户只能按"确定升级&q ...
- python-day02数据类型-字符串和列表的操作
while循环: while True: 条件语句....... 关于break和continue,break:跳出当前的循环 contnu ...
- Anyconnect的VPN环境部署(2)-在Linux客户机上连接Anyconnect
由于之前已经在机房IDC安装了Anyconnect的VPN服务环境(参考:Anyconnect的VPN环境部署(1)-OpenConnect server(ocserv)服务安装)今天介绍下在linu ...
- iOS运行时Runtime浅析
运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...
- 01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
1.什么是Spring : v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:u ...
- Html代码保存为Pdf文件
前段时间Insus.NET有实现了<上传Text文档并转换为PDF>http://www.cnblogs.com/insus/p/4313092.html 和<截取视图某一段另存为部 ...
- android mk odex问题 push apk 不生效
Android编译是否生成odex的设置 默认编译odex版本,如果需要非odex版本,请将device/huawei/k3v2_s10/BoardConfig.mk如下两个变量的值修改为:DISAB ...
- Integer.valueof(null)报错
原文 http://javacat360.iteye.com/blog/2024378 主题 Java 昨天,一同事问我一个问题,估计是他前段日子面试遇到的 问题很简单,String.valueof ...
- C语言 malloc()与sizeof运算的盲点
//malloc()与sizeof运算的盲点 #include <stdio.h> #include <stdlib.h> #include <string.h> ...