代码之家  ›  专栏  ›  技术社区  ›  GothamCityRises

语义搜索-下拉选择不填充输入

  •  3
  • GothamCityRises  · 技术社区  · 6 年前

    here .

    以下是我的一些相关代码:

    constructor(props) {
      super(props);
      this.handleResultSelect = this.handleResultSelect.bind(this);
      this.handleFocusSearch = this.handleFocusSearch.bind(this);
      this.handleBlurSearch = this.handleBlurSearch.bind(this);
      this.state = ({
        results: [{
          "title": "Roob, Cummerata and Watsica"
        },
        {
          "title": "Stanton, Kessler and Walsh"
        },
        {
          "title": "Boyle, Schuppe and Renner"
        }],
        value: '',
        open: false,
      });
    }
    
    handleBlurSearch () {
      this.setState({ 
        open: false,
        focused: false,
      });
    }
    
    handleFocusSearch () {
      this.setState({ 
        open: true,
        focused: true,
      });
    }
    
    handleResultSelect(e, {result}) {
      this.setState({ value: result.title });
    }
    
    render() {
      var searchProps = {
        input: <input className='custom-form-field' placeholder={this.props.placeholder}/>,
        open: this.state.open,
        onFocus: this.handleFocusSearch,
        onBlur: this.handleBlurSearch,
        results: this.state.results,
        onResultSelect: this.handleResultSelect,
        value: this.state.value,
      };
    
      return ( 
        <SemanticUI.Search {...searchProps} />
      );
    }
    

    handleResultSelect 甚至没有人打电话。

    我的第一个猜测是 onBlur

    任何能帮我弄清楚的人都会很高兴的。

    3 回复  |  直到 6 年前
        1
  •  3
  •   R. Wright    6 年前

    尝试将value prop添加到searchProps。此外,onBlur事件和onResultSelect相互冲突,因此我使用lodash.debounce函数添加了一个延迟。

    所以,像这样的事情

    class SearchExampe extends React.Component {
      constructor(props) {
        super(props);
        this.handleResultSelect = this.handleResultSelect.bind(this);
        this.handleFocusSearch = this.handleFocusSearch.bind(this);
        this.handleBlurSearch = this.handleBlurSearch.bind(this);
        this.handleSearchChange = this.handleSearchChange.bind(this);
    
        this.state = {
          results: [
            {
              title: "Roob, Cummerata and Watsica"
            },
            {
              title: "Stanton, Kessler and Walsh"
            },
            {
              title: "Boyle, Schuppe and Renner"
            }
          ],
          value: " ",
          open: true
        };
      }
    
      handleSearchChange(e) {
        this.setState({ value: e.target.value });
      }
    
      handleBlurSearch() {
        this.setState({
          open: false,
          focused: false
        });
      }
    
      handleFocusSearch() {
        this.setState({
          open: true,
          focused: true
        });
      }
    
      handleResultSelect(e) {
        this.setState({ value: e.target.value, open: false });
      }
    
      render() {
        var searchProps = {
          input: <input className="custom-form-field" onChange={this.handleSearchChange} placeholder="placeholder" />,
          open: this.state.open,
          onFocus: this.handleFocusSearch,
          onBlur: _.debounce(this.handleBlurSearch, 100, {}),
          results: this.state.results,
          onResultSelect: this.handleResultSelect,
          value: this.state.value
        };
    
        return <Search {...searchProps} />;
      }
    }
    
        2
  •  1
  •   GothamCityRises    6 年前

    blur ,发现它有一个 relatedTarget 属性,该属性可用于查看单击的元素。

    注意,这只适用于具有 tabindex 属性,所以我还必须修改 Semantic Search 的结果渲染器,使每个结果都有一个属性 tabindex=0 TAB顺序

    用这个,我编辑了 handleBlur open: true 如果 _.contains(event.relatedTarget.classList, 'title') .

    以下是我的一些相关代码:

    constructor(props) {
      super(props);
      this.handleResultSelect = this.handleResultSelect.bind(this);
      this.handleFocusSearch = this.handleFocusSearch.bind(this);
      this.handleBlurSearch = this.handleBlurSearch.bind(this);
      this.state = ({
        results: [{
          "title": "Roob, Cummerata and Watsica"
        },
        {
          "title": "Stanton, Kessler and Walsh"
        },
        {
          "title": "Boyle, Schuppe and Renner"
        }],
        value: '',
        open: false,
      });
    }
    
    handleBlurSearch (event) {
      let open = _.contains(event.relatedTarget.classList, 'title');
      this.setState({ 
        open: open,
        focused: false,
      });
    }
    
    handleFocusSearch () {
      this.setState({ 
        open: true,
        focused: true,
      });
    }
    
    handleResultSelect(e, {result}) {
      this.setState({ value: result.title, open: false });
    }
    
    render() {
      var searchProps = {
        input: <input className='custom-form-field' placeholder={this.props.placeholder}/>,
        open: this.state.open,
        onFocus: this.handleFocusSearch,
        onBlur: this.handleBlurSearch,
        results: this.state.results,
        onResultSelect: this.handleResultSelect,
      };
    
      return ( 
        <SemanticUI.Search {...searchProps} />
      );
    }