import React from 'react'; import styles from './index.less'; import clear from './imgs/clear.png'; import search from './imgs/search.png'; import slide from '../../images/show.png'; import PropTypes from "prop-types"; import config from '@config/index'; import $ from 'jquery'; /** * 前提条件父元素有定位 * visible 搜索显示隐藏 * handleChangeValue 函数参数为输入框的value值 */ class SearchOption extends React.Component { constructor(props) { super(props); this.state = { val:'', border:'', show:false, timer:null, showInsp:false, txt:'' } this.textInput = React.createRef(); this.handleClearVal = this.handleClearVal.bind(this); this.handleFocus = this.handleFocus.bind(this); this.handleBlur = this.handleBlur.bind(this); this.setShowInsp = this.setShowInsp.bind(this); this.detailClick = this.detailClick.bind(this); } componentDidMount(){ this.props.handleChangeValue(''); const {windowHeight,pageTop,height,refreshScroller,searchType} = this.props; if(searchType==1){ this.setState({txt:'细项'}) }else if(searchType==2){ this.setState({txt:'药品'}) } refreshScroller()&&refreshScroller().refresh(); //点开搜索弹窗更新滚动条, if(windowHeight - pageTop < height){ //$("#searchOption")[0].scrollIntoView(false); refreshScroller()&&refreshScroller().scrollBottom(); } this.textInput.current.focus(); } setShowInsp(){ this.setState({ showInsp:!this.state.showInsp }) } handleClearVal(){ const { handleChangeValue } = this.props; this.textInput.current.value = ''; this.textInput.current.focus(); this.setState({ val:'', show:false }); handleChangeValue('','clear'); } detailClick(val,txt){ const { detailClick,handleChangeValue } = this.props; detailClick(val) this.setState({txt:txt}) let value = this.textInput.current.value||'' if(value.trim() == ''){ this.setState({ show:false }) return handleChangeValue(''); } this.setState({ show:true }) handleChangeValue(value) } handleInput(e){ const { handleChangeValue } = this.props; clearTimeout(this.state.timer); let timer = setTimeout(()=>{ clearTimeout(this.state.timer); if(e.target.value.trim() == ''){ this.setState({ show:false }) return handleChangeValue(''); } this.setState({ val:e.target.value, show:true }) handleChangeValue(e.target.value); },config.delayTime); this.setState({ timer }); } handleFocus(){ this.setState({showInsp:false}) if(this.state.val.trim() != ''){ return; }else{ this.setState({border:true}) } } handleBlur(){ this.setState({border:false,val:''}) } render() { const { children,visible,searchType } = this.props; const { show,showInsp,txt } = this.state; return ( searchType == 1||searchType == 2?