| | |
| | | }, |
| | | |
| | | clearInput() { |
| | | // @ts-ignore |
| | | this.setData({ |
| | | value: '', |
| | | focus: true, |
| | | result: [] |
| | | }); // @ts-ignore |
| | | |
| | | this.triggerEvent('clear'); |
| | | }, |
| | | |
| | | // @ts-ignore |
| | | inputFocus(e) { |
| | | // this.setData({ |
| | | // searchState: true |
| | | // }) |
| | | // @ts-ignore |
| | | this.triggerEvent('focus', e.detail); |
| | | }, |
| | | |
| | | // @ts-ignore |
| | | inputBlur(e) { |
| | | this.setData({ |
| | | focus: false |
| | | }); |
| | | this.triggerEvent('blur', e.detail); |
| | | }, |
| | | |
| | | // @ts-ignore |
| | | inputChange(e) { |
| | | this.setData({ |
| | | value: e.detail.value |
| | | }); |
| | | this.triggerEvent('input', e.detail); |
| | | }, |
| | | |
| | | // if (Date.now() - this.lastSearch < this.data.throttle) { |
| | | // return; |
| | | // } |
| | | /** |
| | | * 搜索 |
| | | */ |
| | | search() { |
| | | |
| | | if (typeof this.data.search !== 'function') { |
| | | return; |
| | | } |
| | | }, |
| | | |
| | | this.lastSearch = Date.now(); |
| | | this.timerId = setTimeout(() => { |
| | | if (Date.now() - this.lastSearch < this.data.throttle) { |
| | | return; |
| | | } |
| | | this.data.search(e.detail.value).then(json => { |
| | | this.setData({ |
| | | result: json |
| | | }); |
| | | }).catch(err => { |
| | | console.error('search error', err); |
| | | }); |
| | | }, this.data.throttle); |
| | | /** |
| | | * 获取本地缓存搜索历史 |
| | | */ |
| | | getLocalHistory() { |
| | | |
| | | }, |
| | | |
| | | /** |
| | | * 获取热门搜索内容 |
| | | */ |
| | | getHotTopic() { |
| | | |
| | | }, |
| | | }) |
| | |
| | | <view class="search-bar" bindtap="search"> |
| | | <mp-icon icon="search" size="15" color="black"></mp-icon> |
| | | <text>{{searchTips}}</text> |
| | | <input type="text" class="search-bar__input" placeholder="{{placeholder}}" value="{{value}}" focus="{{focus}}" bindblur="inputBlur" bindfocus="inputFocus" bindinput="inputChange" /> |
| | | <input type="text" class="search-bar__input" placeholder="{{placeholder}}" value="{{value}}" focus="{{focus}}" bindinput="inputChange" /> |
| | | <text class="weui-icon-clear" hover-class="weui-active" wx:if="{{value.length > 0}}" bindtap="clearInput"></text> |
| | | </view> |
| | | <view class="btn" bindtap="search">搜索</view> |