index.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React from 'react';
  2. import styles from './index.less';
  3. import clear from './imgs/clear.png';
  4. import search from './imgs/search.png';
  5. import PropTypes from "prop-types";
  6. import config from '@config/index';
  7. import $ from 'jquery';
  8. /**
  9. * 前提条件父元素有定位
  10. * visible 搜索显示隐藏
  11. * handleChangeValue 函数参数为输入框的value值
  12. */
  13. class SearchOption extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. val:'',
  18. border:'',
  19. show:false,
  20. timer:null
  21. }
  22. this.textInput = React.createRef();
  23. this.handleClearVal = this.handleClearVal.bind(this);
  24. this.handleFocus = this.handleFocus.bind(this);
  25. this.handleBlur = this.handleBlur.bind(this);
  26. }
  27. /*componentWillReceiveProps(nextProps){
  28. if(nextProps.visible != this.props.visible) {
  29. if(!nextProps.visible) {
  30. this.props.handleChangeValue('');
  31. this.textInput.current.value = '';
  32. this.setState({show:false})
  33. }
  34. setTimeout(() => {
  35. this.textInput.current.focus();
  36. }, 0);
  37. }
  38. }*/
  39. componentDidMount(){
  40. this.props.handleChangeValue('');
  41. this.textInput.current.focus();
  42. const {windowHeight,pageTop,height,refreshScroller} = this.props;
  43. refreshScroller()&&refreshScroller().refresh(); //点开搜索弹窗更新滚动条,
  44. if(windowHeight - pageTop < height){
  45. //$("#searchOption")[0].scrollIntoView(false);
  46. refreshScroller()&&refreshScroller().scrollBottom();
  47. }
  48. }
  49. handleClearVal(){
  50. const { handleChangeValue } = this.props;
  51. this.textInput.current.value = '';
  52. this.textInput.current.focus();
  53. this.setState({
  54. val:'',
  55. show:false
  56. });
  57. handleChangeValue('');
  58. }
  59. handleInput(e){
  60. const { handleChangeValue } = this.props;
  61. clearTimeout(this.state.timer);
  62. let timer = setTimeout(()=>{
  63. clearTimeout(this.state.timer);
  64. if(e.target.value.trim() == ''){
  65. this.setState({
  66. show:false
  67. })
  68. return handleChangeValue('');
  69. }
  70. this.setState({
  71. val:e.target.value,
  72. show:true
  73. })
  74. handleChangeValue(e.target.value);
  75. },config.delayTime);
  76. this.setState({
  77. timer
  78. });
  79. }
  80. handleFocus(){
  81. if(this.state.val.trim() != ''){
  82. return;
  83. }else{
  84. this.setState({border:true})
  85. }
  86. }
  87. handleBlur(){
  88. this.setState({border:false,val:''})
  89. }
  90. render() {
  91. const { children,visible } = this.props;
  92. const { show } = this.state;
  93. return (
  94. <div id="searchOption" className={visible?`${styles.search} ${styles.show} searchOption`:`${styles.search} ${styles.hide} searchOption`}>
  95. <img className={styles.searchVal} src={search} alt="搜索" />
  96. <img style={{display:show?'block':'none'}} className={styles.clearVal} src={clear} onClick={this.handleClearVal} alt="清空" />
  97. <input
  98. className={this.state.border ?`${styles.border}`:`${styles.borderNone}`}
  99. type="text"
  100. maxLength="30"
  101. ref={this.textInput}
  102. onFocus={this.handleFocus}
  103. onBlur={this.handleBlur}
  104. onInput={(e) => {
  105. this.handleInput(e)
  106. }}
  107. onPropertyChange={(e) => { // 兼容ie
  108. this.handleInput(e)
  109. }}
  110. placeholder="搜索"
  111. />
  112. <div className={styles.autoList}>
  113. {children}
  114. </div>
  115. </div>
  116. )
  117. }
  118. }
  119. SearchOption.defaultProps = {
  120. visible: false
  121. };
  122. SearchOption.propTypes = {
  123. visible:PropTypes.bool,
  124. handleChangeValue: PropTypes.func
  125. };
  126. export default SearchOption;