react - 官方搜索列表案例

复制代码
// 搜索框
class SearchBar extends React.Component {
  constructor (props) {
    super(props)

    this.handleChangeInput = this.handleChangeInput.bind(this)
    this.handleChangeCheckBox = this.handleChangeCheckBox.bind(this)

    this.searchInput = React.createRef()
    this.checkBoxRef = React.createRef()
  }

  handleChangeInput () {
    this.props.onInputChange(this.searchInput.current.value)
  }

  handleChangeCheckBox () {
    this.props.onCheckBoxChange(this.checkBoxRef.current.checked)
  }

  render () {
    return (
      <div>
        <div>
          <input ref={this.searchInput} value={this.props.searchKeyWord} onChange={this.handleChangeInput} />
        </div>
        <div>
          <input ref={this.checkBoxRef} type="checkBox" checked={ this.props.isChecked } onChange={this.handleChangeCheckBox} /> 是否有库存
        </div>
      </div>
    )
  }
  
}

// 显示分类标题的组件
function ProductCategoryRow (props) {
  return (
    <div>
      <h5>{props.title}</h5>
    </div>
  )
}

// 产品的每一行
class ProductRow extends React.Component {
  constructor (props) {
    super(props)
  }

  render () {
    return (
      <div>
        <span>{this.props.name}</span>
        <span>{this.props.price}</span>
        <span>{this.props.isChecked}</span>
      </div>
    )
  }
}

// 产品的列表
class ProductTable extends React.Component {
  constructor (props) {
    super(props)
  }

  // 处理数据分组
  groupDatas (keyWord, isChecked) {
    let obj = {}
    this.props.productDatas.forEach((item) => {
      if (item.name.includes(keyWord) && item.stocked === isChecked) {
        if (obj[item.category]) {
          obj[item.category].push(item)
        }else {
          obj[item.category] = [item]
        }
      }
    })
    return obj
  }
  
  render() {
    const datas = this.groupDatas(this.props.searchKeyWord, this.props.isChecked)
    console.log(datas, "分组数据", this.props.productDatas)

    const content = Object.keys(datas).map((val, index) =>
      <div key={index}>
        <ProductCategoryRow title={val} key={index} /> 
        {
          datas[val].map((item, i) => <ProductRow key={i} name={item.name} price={item.price} isChecked = {item.stocked} />)
        }
      </div>
    )
    
    return (
      <div>
        {
          content
        }
      </div>
    );
  }
}

class FilterableProductTable extends React.Component {
  constructor (props) {
    super(props)

    this.onInputChange = this.onInputChange.bind(this)
    this.onCheckBoxChange = this.onCheckBoxChange.bind(this)

    this.state = {
      searchKeyWord: '',
      isChecked: true,
      productDatas: []
    }
  }

  // 输入框输入触发事件
  onInputChange (searchKeyWord) {
    this.setState({
      searchKeyWord: searchKeyWord
    })
  }

  // 复选框勾选触发
  onCheckBoxChange (isChecked) {
    this.setState({
      isChecked: isChecked
    })
  }

  componentDidMount () {
    setTimeout(() => {
      let res = [
        {category: "国产", price: "$49.99", stocked: true, name: "华为"},
        {category: "国产", price: "$9.99", stocked: true, name: "中兴"},
        {category: "国产", price: "$29.99", stocked: false, name: "荣耀"},
        {category: "海外", price: "$99.99", stocked: true, name: "苹果"},
        {category: "海外", price: "$399.99", stocked: false, name: "黑莓"},
        {category: "海外", price: "$199.99", stocked: true, name: "索尼"}
      ]
      this.setState(() => ({
        productDatas: res
       })
      )
    }, 1000) 
  }

  render () {
    return (
      <div>
        <SearchBar isChecked = { this.state.isChecked } searchKeyWord = {this.state.searchKeyWord} onInputChange = {this.onInputChange} onCheckBoxChange = {this.onCheckBoxChange} />
        {
          this.state.productDatas.length > 0 &&  
          <ProductTable isChecked = { this.state.isChecked } searchKeyWord = {this.state.searchKeyWord} productDatas = { this.state.productDatas } />
        }
      </div>
    )
  }
}

ReactDOM.render(
  <FilterableProductTable />,
  document.getElementById('root')
)
复制代码

 

// 19. 案例

// 搜索框
class SearchBar extends React.Component {
  constructor (props) {
    super(props)

    this.handleChangeInput = this.handleChangeInput.bind(this)
    this.handleChangeCheckBox = this.handleChangeCheckBox.bind(this)

    this.searchInput = React.createRef()
    this.checkBoxRef = React.createRef()
  }

  handleChangeInput () {
    this.props.onInputChange(this.searchInput.current.value)
  }

  handleChangeCheckBox () {
    this.props.onCheckBoxChange(this.checkBoxRef.current.checked)
  }

  render () {
    return (
      <div>
        <div>
          <input ref={this.searchInput} value={this.props.searchKeyWord} onChange={this.handleChangeInput} />
        </div>
        <div>
          <input ref={this.checkBoxRef} type="checkBox" checked={ this.props.isChecked } onChange={this.handleChangeCheckBox} /> 是否有库存
        </div>
      </div>
    )
  }
  
}

// 显示分类标题的组件
function ProductCategoryRow (props) {
  return (
    <div>
      <h5>{props.title}</h5>
    </div>
  )
}

// 产品的每一行
class ProductRow extends React.Component {
  constructor (props) {
    super(props)
  }

  render () {
    return (
      <div>
        <span>{this.props.name}</span>
        <span>{this.props.price}</span>
        <span>{this.props.isChecked}</span>
      </div>
    )
  }
}

// 产品的列表
class ProductTable extends React.Component {
  constructor (props) {
    super(props)
  }

  // 处理数据分组
  groupDatas (keyWordisChecked) {
    let obj = {}
    this.props.productDatas.forEach((item=> {
      if (item.name.includes(keyWord) && item.stocked === isChecked) {
        if (obj[item.category]) {
          obj[item.category].push(item)
        }else {
          obj[item.category] = [item]
        }
      }
    })
    return obj
  }
  
  render() {
    const datas = this.groupDatas(this.props.searchKeyWordthis.props.isChecked)
    console.log(datas"分组数据"this.props.productDatas)

    const content = Object.keys(datas).map((valindex=>
      <div key={index}>
        <ProductCategoryRow title={val} key={index} /> 
        {
          datas[val].map((itemi=> <ProductRow key={i} name={item.name} price={item.price} isChecked = {item.stocked} />)
        }
      </div>
    )
    
    return (
      <div>
        {
          content
        }
      </div>
    );
  }
}

class FilterableProductTable extends React.Component {
  constructor (props) {
    super(props)

    this.onInputChange = this.onInputChange.bind(this)
    this.onCheckBoxChange = this.onCheckBoxChange.bind(this)

    this.state = {
      searchKeyWord'',
      isCheckedtrue,
      productDatas: []
    }
  }

  // 输入框输入触发事件
  onInputChange (searchKeyWord) {
    this.setState({
      searchKeyWordsearchKeyWord
    })
  }

  // 复选框勾选触发
  onCheckBoxChange (isChecked) {
    this.setState({
      isCheckedisChecked
    })
  }

  componentDidMount () {
    setTimeout(() => {
      let res = [
        {category"国产"price"$49.99"stockedtruename"华为"},
        {category"国产"price"$9.99"stockedtruename"中兴"},
        {category"国产"price"$29.99"stockedfalsename"荣耀"},
        {category"海外"price"$99.99"stockedtruename"苹果"},
        {category"海外"price"$399.99"stockedfalsename"黑莓"},
        {category"海外"price"$199.99"stockedtruename"索尼"}
      ]
      this.setState(() => ({
        productDatasres
       })
      )
    }, 1000
  }

  render () {
    return (
      <div>
        <SearchBar isChecked = { this.state.isChecked } searchKeyWord = {this.state.searchKeyWord} onInputChange = {this.onInputChange} onCheckBoxChange = {this.onCheckBoxChange} />
        {
          this.state.productDatas.length > 0 &&  
          <ProductTable isChecked = { this.state.isChecked } searchKeyWord = {this.state.searchKeyWord} productDatas = { this.state.productDatas } />
        }
      </div>
    )
  }
}

ReactDOM.render(
  <FilterableProductTable />,
  document.getElementById('root')
)
posted @   monkey-K  阅读(502)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示