index.jsx 3.7 KB

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