react-search

//search页面
import React, { Component } from 'react';
import "../../style/search.styl"
import SongList from '../../components/SongList'
class Search extends Component {
    constructor() {
        super()
        this.state = {
            hotSearch: [],
            searchVal: "",
            flag: true,
            searchList: []
        }
    }
    componentDidMount () {
        this.$http.get('/search/hot').then(res => {
            console.log(res)
            this.setState({
                hotSearch: res.data.result.hots
            })
        })
    }
    search (e) {
        if (!e.target.value) {
            this.setState({
                searchVal: e.target.value,
                flag: true
            })
        } else {
            this.setState({
                searchVal: e.target.value,
                flag: false
            })
        }

    }
    keydown (e) {
        console.log(e.keyCode)
        if (e.keyCode == 13) {
            // 调用搜索接口
            this.$http.get("/search?keywords=" + e.target.value).then(res => {
                console.log(res)
                this.setState({
                    searchList: res.data.result.songs
                })
            })
        }
    }
    render () {
        let { hotSearch, searchVal, flag } = this.state
        return (
            <div className="search">
                <div className="search-box">
                    <input type="text" value={searchVal} onKeyDown={this.keydown.bind(this)} onChange={this.search.bind(this)} />
                </div>
                {
                    flag && <div className="box">
                        <p>热门搜索</p>
                        <div className="hot-search">
                            {
                                hotSearch.map((item, index) => {
                                    return <span key={index}>{item.first}</span>
                                })
                            }
                        </div>
                        <div className="search-history">
                            <ul>
                                <li>
                                    <span></span>
                                    <span>x</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                }
                <SongList newMusic={this.state.searchList} />

            </div>
        );
    }
}

export default Search;
//组件
import React, { Component } from 'react'
import "../style/songList.styl"
// 可以让非路由组件拥有路由组件的属性  history location match
import { withRouter } from 'react-router-dom'
import BScroll from 'better-scroll'
// 1. 父元素 > 1个子元素 2. 子元素的高度必须大于父元素的高度 3. 父元素必须overflowhidden
class SongList extends Component {
    constructor() {
        super()
        this.state = {
            end: 30,
            flag: false
        }
    }
    componentDidMount () {
        let bs = new BScroll(".songList", {
            probeType: 2,
            click: true
        })
        var that = this
        bs.on("scroll", function () {
            // console.log(this.y)
            // 当上拉加载完全显示出来,我们要把上拉加载缓存释放加载
            if (this.y < this.maxScrollY - 50) {
                console.log("满足条件")
                that.setState({
                    flag: true
                })
            } else {
                that.setState({
                    flag: false
                })
            }
        })
        bs.on("scrollEnd", function () {
            if (that.state.flag) {
                that.setState({
                    end: that.state.end + 30
                })
            }
        })
    }
    play (id) {
        console.log(this)
        this.props.history.push("/play?id=" + id)
    }
    render () {
        let { end, flag } = this.state
        return (
            <div className="songList">
                <ul>
                    {
                        this.props.newMusic.slice(0, end).map(item => {
                            return <li onClick={this.play.bind(this, item.id)} key={item.id}>
                                <div className="songContent">
                                    <p>{item.name}</p>
                                    <p>
                                        {
                                            (function () {
                                                if (item.song) {
                                                    return item.song.artists.map((val, index) => {
                                                        return val.name
                                                    }).join(" / ")
                                                } else if (item.ar) {
                                                    return item.ar.map((val, index) => {
                                                        return val.name
                                                    }).join(" / ")
                                                } else {
                                                    return item.artists.map((val, index) => {
                                                        return val.name
                                                    }).join(" / ")
                                                }
                                            })()
                                        }
                                         - <span>{item.name}</span>
                                    </p>
                                </div>
                                <div className="goPlay">
                                    <i className="iconfont icon-ziyuan"></i>
                                </div>
                            </li>
                        })
                    }
                    <p className="pullUp">
                        {
                            flag ? "释放加载" : "上拉加载"
                        }
                    </p>
                </ul>

            </div>
        )
    }
}
export default withRouter(SongList)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值