首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输入无效数据时使用inputText -bootstrap-typeahead清除类型

输入无效数据时使用inputText -bootstrap-typeahead清除类型
EN

Stack Overflow用户
提问于 2017-09-01 02:03:14
回答 2查看 5.3K关注 0票数 4

我正在尝试使用react-bootstrap-typeahead验证选择,并在移动到页面的另一个区域时清除无效的输入文本。

我想不出一个好办法来做这件事。

Typeahead组件的onBlur不会在正确的时间触发以进行验证和清除。

如有任何建议,我们将不胜感激

下面是我的typeahead定义

代码语言:javascript
复制
        <Typeahead  bsSize="sm"
                    defaultSelected = {this.props.options.slice(selectedIndex)}
                    clearButton
                    labelKey="roomNumber"
                    options={this.props.options}
                    placeholder={this.props.placeholder}                          
                    name={this.props.nameId}
                    ref={ref => this.roomChoice = ref}
                    onChange={this.onChangeHandler.bind(this)}
                    onInputChange={this.onInputChangeHandler.bind(this)}
                    onBlur={this.onBlurHandler.bind(this)} 
                    onFocus={this.onFocusHandler.bind(this)} 
                  />

下面是我的onBlur函数:

代码语言:javascript
复制
onBlurHandler(event)
{   
         if (this.roomInputText)
        {
            if (!this.roomSelectedOption)
            {
                this.roomChoice.getInstance().clear()
            }
        }

}

如果有人输入文本,onBlur不会在正确的时间触发,使我无法选择一个选项。

EN

回答 2

Stack Overflow用户

发布于 2019-04-21 17:01:04

使用callback ref在TypeAhead实例中获取ref,并在onchange事件调用中使用。

代码语言:javascript
复制
<div>
  <Typeahead
    options={[...]}
    ref={(ref) => this._typeahead = ref}
    onInputChange={this.onInputChangeSelection}
 />
</div>




onInputChangePartsSelection = (value) => {
    if (value) {
    const instance = this._typeahead.getInstance()
    instance.clear()
    instance.focus()
    }
  }
票数 6
EN

Stack Overflow用户

发布于 2020-02-10 13:24:57

我使用的是AsyncTypeAhead,必须执行以下操作。

代码语言:javascript
复制
var asyncTypeAhead = typeAhead as AsyncTypeahead;
asyncTypeAhead.getInstance().clear();

我发现这篇文章很有用的https://github.com/ericgio/react-bootstrap-typeahead/issues/359

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45987582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档