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,这 ...
随机推荐
- NHibernate查询优化的相关资料
一.http://www.cnblogs.com/dddd218/archive/2009/09/01/1557640.html 1.立即加载(lazy=false)并不能在所有情况下都会减少SQL语 ...
- CodeForces 456-C Boredom
题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...
- angular $digest already in progress
angular.js:11706 Error: [$rootScope:inprog] $digest already in progresshttp://errors.angularjs.org/1 ...
- Gird Layout代码解释
<div class="wrapper"> <!--定义一个类名为wrapper的div盒子--> <div class="one" ...
- 运行VsCode缺少libxss.so.1
安装libXScrnSaver即可 yum install libXScrnSaver 使用的时候出现一个错误 bash: /usr/local/bin/rar: /lib/ld-linux. ...
- SpringBoot(十二):springboot2.0.2写测试用例
导入maven依赖: <dependency> <groupId>junit</groupId> <artifactId>junit</artif ...
- Android Studio3.1.2升级问题:Configuration 'compile' is obsolete and has been replaced with 'implementation'.
每次升级Android Studio时,一般情况下Gradle版本的也会相应的升级,我之前Android Studio 3.0.1.Gradle 是4.1升级后为:Android Studio 3.1 ...
- H5调用本地摄像头[转]
http://www.cnblogs.com/GoodPingGe/p/4726126.html <!DOCTYPE html><html><head lang=&quo ...
- alibaba的springcloud孵化器项目
Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式 ...
- Windows下python3生成UTF8的CSV文件和sha256sum踩坑记录
CSV的坑 在Ubuntu下是简单的写入完事 import csv ... with open(filename, 'w') as output: f = csv.writer(output) f.w ...