typescript编写react组件2 —— 商品列表组件

前言

接上一篇文章的代码片段,今天为大家提供另一组件——商品列表,这也是根据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', 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'}
];

ReactDOM.render(
  <FilterableProductTable products={PRODUCTS} />,
  document.getElementById('example')
);

结尾

这里为大家附上实现的效果:图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值