import { Button, Input, ScrollView, Text, View } from "@tarojs/components"; import Taro, { Component, pxTransform } from "@tarojs/taro"; import { BG_COLOR_LIST, TEXT_COLOR_LIST } from "../../lib/model"; import { IProps } from "../../../@types/searchBar"; import ClCard from "../card"; import ClSearchResult from "./searchResult"; import { classNames } from "../../lib"; import ClIcon from "../icon"; interface IState { showSearch?: boolean; value?: string; } export default class ClSearchBar extends Component { static options = { addGlobalClass: true }; static defaultProps: IProps = { shape: "radius", fix: false, searchType: "button", leftIcons: [], bgColor: undefined, rightButtonColor: "white", rightTextColor: "black", placeholder: "请搜索", showLoading: false, showResult: false, result: [], autoFocus: false, clear: false, onClear: () => {}, onTouchResult: () => {}, onScrollToUpper: () => {}, onScrollToLower: () => {}, onBlur: () => {}, onFocus: () => {} }; state: IState = { showSearch: false, value: "" }; onIconClick(index: number) { this.props.onIconClick && this.props.onIconClick(index); } onSearch(e) { e.stopPropagation(); this.props.onSearch && this.props.onSearch(this.state.value || ""); } onFocus(e) { this.props.onFocus && this.props.onFocus(this.state.value || ""); this.setState({ showSearch: true, value: e.detail.value }); } onBlur(e) { this.props.onBlur && this.props.onBlur(this.state.value || ""); this.setState({ showSearch: false, value: e.detail.value }); } onInput(e) { this.setState({ value: e.detail.value }); this.props.onInput && this.props.onInput(e.detail.value); } static onPreventProp(e) { e.stopPropagation(); } render(): any { const { bgColor, rightButtonColor, rightTextColor, leftIcons, shape, placeholder, clear, fix, className, style, searchType, showResult, result, showLoading, onScrollToLower, onScrollToUpper, onTouchResult } = this.props; const { showSearch, value } = this.state; const bgColorClassName = bgColor ? BG_COLOR_LIST[bgColor] : ""; const buttonColorClassName = rightButtonColor ? BG_COLOR_LIST[rightButtonColor] : ""; const textColorClassName = rightTextColor ? TEXT_COLOR_LIST[rightTextColor] : ""; const leftIconComponent = leftIcons ? leftIcons.map((item, index) => ( )) : ""; const searchComponent = ( {clear && value ? ( { const { onClear } = this.props; e.stopPropagation(); this.setState({ value: "" }); onClear && onClear(); }} > ) : ( "" )} ); const buttonComponent = ( ); const textComponent = ( 搜索 ); return ( {leftIconComponent} {searchComponent} {searchType === "text" ? textComponent : searchType === "none" ? "" : buttonComponent} {showResult ? ( {(result && result.length) || showLoading ? ( { onScrollToLower && onScrollToLower(); }} onScrollToUpper={() => { onScrollToUpper && onScrollToUpper(); }} > { onTouchResult && onTouchResult(index); }} /> ) : ( "" )} ) : ( "" )} ); } }