typescript编写react组件2 —— 商品列表组件
发布时间:2020-12-15 08:23:43 所属栏目:百科 来源:网络整理
导读:前言 接上一篇文章的代码片段,今天为大家提供另一组件——商品列表,这也是根据react的官网给出的案例写的组件。 组件结构 /* * FilterableProductTable * SearchBar * ProductTable * ProductCategoryRow * ProductRow */ 代码片段 interface Product{ cat
前言接上一篇文章的代码片段,今天为大家提供另一组件——商品列表,这也是根据react的官网给出的案例写的组件。 组件结构/* * FilterableProductTable * SearchBar * ProductTable * ProductCategoryRow * ProductRow */ 代码片段interface Product{
category: string;
price: string;
stocked: boolean;
name: string;
}
// ProductCategoryRow
interface ProductCategoryRowProps{
category: string;
}
class ProductCategoryRow extends React.Component<ProductCategoryRowProps,any>{
public render(){
return (
<tr><th colSpan="2">{this.props.category}</th></tr>
);
}
}
// ProductRow
interface ProductRowProps{
product: Product;
}
class ProductRow extends React.Component<ProductRowProps,any>{
public render(){
let name = this.props.product.stocked ? this.props.product.name : <span style={{color: 'red'}}>{this.props.product.name}</span>;
return (
<tr>
<td>{name}</td>
<td>{this.props.product.price}</td>
</tr>
);
}
}
// ProductTable
interface ProductTableProps{
products: Product[];
filterText: string;
inStockOnly: boolean;
}
class ProductTable extends React.Component<ProductTableProps,any>{
public render(){
var rows = [];
var lastCategory = null;
this.props.products.map(product => {
if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)){
return;
}
if (product.category !== lastCategory) {
rows.push(<ProductCategoryRow category={product.category} key={product.category}/>);
}
rows.push(<ProductRow product={product} key={product.name}/>);
lastCategory = product.category;
});
return (
<table>
<thead>
<tr>
<th>名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
}
// SearchBar
interface SearchBarProps{
filterText: string;
inStockOnly: boolean;
onUserInput: (a: string,b: boolean) => any;
}
class SearchBar extends React.Component<SearchBarProps,any>{
public handleChange(){
// 函数调用
this.props.onUserInput(
this.refs.filterTextInput.value,this.refs.inStockOnlyInput.checked
);
}
public render(){
return (
<form>
<input type="text" placeholder="搜索···" value={this.props.filterText} ref="filterTextInput" onChange={this.handleChange.bind(this)}/>
<p>
<label>
<input type="checkbox" checked={this.props.inStockOnly} ref="inStockOnlyInput" onChange={this.handleChange.bind(this)}/>只显示有货的
</label>
</p>
</form>
);
}
}
// FilterableProductTable
interface FilterableProductTableProps{
products: Product[];
}
interface FilterableProductTableState{
filterText: string;
inStockOnly: boolean;
}
class FilterableProductTable extends React.Component<FilterableProductTableProps,FilterableProductTableState>{
constructor(props,FilterableProductTableProps) {
super(props);
this.state = {
filterText: '',inStockOnly: false
};
}
public handleUserInput(filterText,inStockOnly){
this.setState({filterText: filterText,inStockOnly:inStockOnly});
}
public render(){
return (
<div>
<SearchBar filterText={this.state.filterText} inStockOnly={this.state.inStockOnly} onUserInput={this.handleUserInput.bind(this)}/>
<ProductTable products={this.props.products} filterText={this.state.filterText} inStockOnly={this.state.inStockOnly}/>
</div>
);
}
}
let PRODUCTS:Product[] = [
{category: 'Sporting Goods',price: '$49.99',stocked: true,name: 'Football'},{category: 'Sporting Goods',price: '$9.99',name: 'Baseball'},price: '$29.99',stocked: false,name: 'Basketball'},{category: 'Electronics',price: '$99.99',name: 'iPod Touch'},price: '$399.99',name: 'iPhone 5'},price: '$199.99',name: 'Nexus 7'}
];
ReactDOM.render(
<FilterableProductTable products={PRODUCTS} />,document.getElementById('example')
);
结尾这里为大家附上实现的效果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
