import React, { Component } from 'react' import Taro, { getCurrentInstance } from '@tarojs/taro' import { View, Form, Text, Image, Input } from '@tarojs/components' import { AtSearchBar, AtIcon, AtInput } from 'taro-ui' import { isWeb, classNames } from '@/utils' import { toggleTouchMove } from '@/utils/dom' import './index.scss' export default class SearchBar extends Component { static defaultProps = { isOpened: false, keyword: '', showDailog: true, localStorageKey: 'searchHistory' } constructor(props) { super(props) this.state = { searchValue: '', showSearchDailog: false, historyList: [], isShowAction: false } } static options = { addGlobalClass: true } componentDidMount () { if (process.env.TARO_ENV === 'h5') { toggleTouchMove(this.refs.container) } } handleFocusSearchHistory = (isOpened) => { this.props.onFocus?.() this.setState({ showSearchDailog: isOpened, isShowAction: true }) Taro.getStorage({ key: this.props.localStorageKey }) .then((res) => { let stringArr = res.data.split(',').filter((item) => { const isHave = item.trim() return isHave }) this.setState({ historyList: stringArr }) }) .catch(() => { }) } handleChangeSearch = (value, event) => { //h5中value为空 需从event里面拿值 // value = value.replace(/\s+/g,'') console.log('handleChangeSearch', value) this.props.onChange?.(isWeb ? event?.detail?.value : value) } handleClear = () => { this.props.onClear() } handleConfirm = (e) => { const keywords = e.detail.value.trim() if (keywords) { const value = Taro.getStorageSync(this.props.localStorageKey) let defaultValue = [] if (value) { const array = value.split(',') if (!array.includes(keywords)) { array.unshift(keywords) } defaultValue = array } else { defaultValue.push(keywords) } Taro.setStorage({ key: this.props.localStorageKey, data: defaultValue.toString() }) } this.props.onConfirm(e.detail.value) this.setState({ showSearchDailog: false, isShowAction: false }) } handleClickCancel = (isOpened) => { this.props.onCancel?.() this.setState({ showSearchDailog: isOpened, isShowAction: false }) this.handleClear() } handleClickDelete = () => { Taro.removeStorage({ key: this.props.localStorageKey }).then(() => { this.setState({ historyList: [] }) }) } handleClickTag = (item) => { this.props.onConfirm(item) this.setState({ showSearchDailog: false, isShowAction: false }) } handleClickHotItem = (item) => { this.handleConfirm({ detail: { value: item } }) } handleBlurSearch = () => { this.props.onBlur?.() this.setState({ showSearchDailog: false, isShowAction: false }) } render () { const { isFixed, keyword, showDailog, placeholder, hotSearch } = this.props const { showSearchDailog, historyList, isShowAction, searchValue } = this.state return ( {/**/} {/*
*/} {/**/} {/*{*/} {/*showSearchDailog === false && className === 'home-index-search' ? PJJ : null*/} {/*}*/} {/**/} {/**/} {/**/} {/**/} {/*
*/} {/**/} {/**/} {/*最近搜索*/} {/**/} {/**/} {/**/} {/*{*/} {/*historyList.map((item, index) => {item} )*/} {/*}*/} {/**/} {/**/} {/*
*/} {/* */} {/* {微信浏览器form enter自动刷新页面} */} {/* */} {showDailog && ( 最近搜索 清除搜索历史 {historyList.map((item, index) => ( {item} ))} 热门搜索 近期大家搜索最多 {hotSearch.map((item, index) => {item.content ? '#' + item.content : ''} {/* */} )} )} ) } }