react文档demo实现输入展示搜索结果列表
文档页面地址:https://doc.react-china.org/docs/thinking-in-react.html
该文档只给了具体实现思路,下面是我实现的代码。
初学react,如果有写的不规范的地方,望大家多多指正!
FilterableProductTable (橙色): 包含了整个例子
SearchBar (蓝色): 接受所有的用户输入
ProductTable (绿色): 根据用户输入过滤并展示数据集合
ProductCategoryRow (绿松石色): 展示每个分类的标题
ProductRow (红色): 用行来展示每个产品
文档中清楚的为我们划分出了具体组件模块,各个组件实现如下:
App.js
import React from 'react';
import ReactDOM from 'react-dom'; import FilterableProductTable from './FilterableProductTable' ReactDOM.render(
<FilterableProductTable/>,
document.getElementById('root')
);
FilterableProductTable.js
import React from 'react'
import SearchBar from './SearchBar'
import ProductTable from './ProductTable' /*
* 需要定义的state或值:
*原产品列表
*用户输入的搜索文本
*复选框的值
* 需要根据以上值计算的值:
*产品的筛选列表
*/
const data=[
{category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
{category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
{category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
{category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
{category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
{category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
];
class FilterableProductTable extends React.Component{
constructor(props){
super(props);
this.state = {
filterText: '',
inStockOnly: true,
list:data
};
this.handleSearch=this.handleSearch.bind(this);
this.handleCheckBox=this.handleCheckBox.bind(this);
}
handleSearch(keywods){
this.setState({
filterText:keywods
});
}
handleCheckBox(checkBoxStatus){
this.setState({
inStockOnly:checkBoxStatus
});
}
render(){
const filterText=this.state.filterText;
const inStockOnly=this.state.inStockOnly;
const list=this.state.list;
return(
<div>
<SearchBar
filterText={filterText}
inStockOnly={inStockOnly}
onSearch={this.handleSearch}
list={list}
onCheckBox={this.handleCheckBox}/>
<ProductTable
filterText={filterText}
inStockOnly={inStockOnly}
list={list}
/>
</div>
)
}
} export default FilterableProductTable
SearchBar.js:
import React from 'react' class SearchBar extends React.Component{
constructor(props){
super(props);
this.handleChange=this.handleChange.bind(this);
this.checkboxChange=this.checkboxChange.bind(this);
}
handleChange(e){
e.preventDefault();
let inputText=this.refs.inputText.value;
this.props.onSearch(inputText);
}
checkboxChange(e){
// let checkboxStatus=this.refs.checkbox.checked; 通过ref获取
let checkboxStatus=e.target.checked; //通过event.target事件获取
this.props.onCheckBox(checkboxStatus);
}
render(){
return(
<div>
<input type="text" ref='inputText' value={this.props.filterText} onChange={this.handleChange} placeholder="输入产品名称"/>
<div>
<input type="checkbox" ref="checkbox" checked={this.props.inStockOnly} onChange={this.checkboxChange}/>
仅仅展示有库存的商品
</div>
</div>
)
}
} export default SearchBar
ProductTable.js:
import React from 'react'
import ProductCategoryRow from './ProductCategoryRow'
import ProductRow from './ProductRow' class ProductTable extends React.Component{
/* constructor(props){
super(props);
}*/
render(){
let filterText=this.props.filterText;
let inStockOnly=this.props.inStockOnly;
let curCategory="";
return(
<table>
<thead>
<tr style={{fontWeight:'bold'}}>
<td>Name</td>
<td>Price</td>
</tr>
</thead>
{this.props.list.map((value,index) => {
let listItemShow;
if(value.name.toLowerCase().indexOf(filterText.toLowerCase())!==-1||filterText===''){
listItemShow=true;
if(inStockOnly===true&&value.stocked===true){
listItemShow=true;
}else if(inStockOnly===true&&value.stocked===false){
listItemShow=false;
}
}else{
listItemShow=false;
}
let categoryStatus=false;
if(value.category===curCategory){
categoryStatus=false;
}else{
categoryStatus=true;
}
curCategory=value.category;
return (
<tbody key={index}>
<ProductCategoryRow category={value.category} categoryStatus={categoryStatus}/>
<ProductRow
stocked={value.stocked}
name={value.name}
price={value.price}
show={listItemShow}/>
</tbody>
)
})
}
</table>
)
}
}
export default ProductTable
ProductCategoryRow.js
import React from 'react' class ProductCategoryRow extends React.Component{
/*constructor(props){
super(props);
}*/
render(){
let styleObj = {
display : this.props.categoryStatus ? 'block':'none',
fontWeight:"bold"
};
return(
<tr style={styleObj}>
<td>
{this.props.category}
</td>
</tr>
)
}
} export default ProductCategoryRow
ProductRow.js
import React from 'react' class ProductRow extends React.Component{
/*constructor(props){
super(props);
}*/
render(){
let styleObj = {
display : this.props.show ? 'block':'none',
};
return (
<tr style={styleObj}>
<td>
{this.props.name}
</td>
<td>
{this.props.price}
</td>
</tr>
)
}
} export default ProductRow
总结:react 的state要定义在几个组件的公共父组件上,这里定义在最外层的 FilterableProductTable.js 组件上,修改state 即setState也在定义state的组件上操作,所有修改的方法通过传入子组件,子组件通过props获得该方法,把改变之后的值传入父组件,即实现了父子组件数据的双向传递。
github源文件:https://github.com/beileixinqing/react-doc-demo-search
react文档demo实现输入展示搜索结果列表的更多相关文章
- React文档(十三)思考React
在我们的看来,React是使用js创建大型快速网站应用的首要方法.它在Facebook和Instagram的使用已经为我们展现了它自己. React的一个很好的地方就在于当你创建应用的时候它使你思考如 ...
- React文档(二十四)高阶组件
高阶组件(HOC)是React里的高级技术为了应对重用组件的逻辑.HOCs本质上不是React API的一部分.它是从React的组合性质中显露出来的模式. 具体来说,一个高阶组件就是一个获取一个组件 ...
- 快速实现office文档在线预览展示(doc,docx,xls,xlsx,ppt,pptx)
微软:https://view.officeapps.live.com/op/view.aspx?src=(输入你的文档在服务器中的地址):
- Java 处理word文档后在前端展示
最新新开发的这个项目需要使用word文档并要求能在前端页面上带格式展示,由于项目不是内部使用,所以不考虑插件类的处理模式,都必须要本地处理完成,前端不需要做什么更新或者说安装就能直接访问,类似于百度文 ...
- React文档(一)安装
React是一个灵活的可以用于各种不同项目的框架,你可以用它来写新应用,你也可以逐步将它引进已有的代码库而不用重写整个项目. 试用React 如果你想玩一玩React,那么就去CodePen上试一试. ...
- elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解
一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...
- React文档(十)表单
HTML表单元素和 React里的其他DOM元素有些不同,因为它们会保留一些内部的状态.举个例子,这个普通的表单接受唯一的name值: <form> <label> Name: ...
- React文档(十六)refs和DOM
Refs 提供了一种方式,用于访问在 render 方法中创建的 DOM 节点或 React 元素. 在标准的React数据流中,props是使得父组件和子组件之间交互的唯一方式.你通过props重新 ...
- React文档(三)介绍JSX
我们先看看这个变量声明: const element = <h1>Hello, world!</h1>; 这个有趣的标签语法既不是字符串也不是HTML. 这种写法叫做JSX,这 ...
随机推荐
- 超级牛皮的oracle的分析函数over(Partition by...) 及开窗函数 (转)
http://zonghl8006.blog.163.com/blog/static/4528311520083995931317/ over(Partition by...) 一个超级牛皮的ORAC ...
- 小甲鱼Python第七讲课后习题
0.if not(money < 100):上边这行代码相当于? if money>=100 1.assert 的作用是什么? assert “断言”,当这个关键字后边的条件为假的时候,程 ...
- 轻量级的Web框架——Nancy
最近想找一个简单的.Net下的轻量级Web框架,作为用户的本地的一个WebServer,实现同浏览器程序的一些简单交互,并调用本地服务,实现类似浏览器插件的功能.它有如下几点要求: 简单,能快速账务, ...
- app保存图片到用户相册时闪退解决办法
在iOS11中,app保存图片到用户相册时必须添加权限使用描述即NSPhotoLibraryAddUsageDescription,否则会闪退. 只需在info.plist—Property List ...
- IIS安装、配置 发布网站 报错解决方案
错误一: HTTP 错误 500.19- Internal Server Error法请求该页面配置,因为页面的相关配置数据无效#### HTTP 错误 500.21 - Internal S ...
- postman6 在Linux中,body和response字体显示不正常的解决方法
在Linux中,postman的body和response使用的默认字体如果没有安装的话,会导致字体和光标的位置不一致,例如字体显示长度只有30,而光标在70的位置,导致编辑困难. 经查找css的定义 ...
- android编码学习
虽然以下博客有点老,但很清晰,有不明白的基础知识,可以来这里找找. 2015年最新Android基础入门教程目录(完结版) 1. 环境配置 Android stodio gradle配置踩过的坑 An ...
- Python实现多进程
Python可以实现多线程,但是因为Global Interpreter Lock (GIL),Python的多线程只能使用一个CPU内核,即一个时间只有一个线程在运行,多线程只是不同线程之间的切换, ...
- webpack 配置缓存
1.输出文件的文件名 加hash 2.提取引导模板 3.模块标识符 https://webpack.docschina.org/guides/caching/#src/components/Sideb ...
- zabbix 界面翻译不完全的处理
zabbix是一个多语言监控系统,界面显示由对应的语言下的frontend.mo控制.当前对中文的翻译不完全,如下图 如果我们需要自己优化,将此翻译成中文,那么你需要修改zh_CN下的frontend ...