【问题标题】:How to prevent selecting value while enter key press in react-select如何在反应选择中输入按键时防止选择值
【发布时间】:2020-07-15 10:07:31
【问题描述】:

我试图在用户没有输入任何内容并按下回车键时阻止回车键选择。在当前情况下,当用户单击react-select 并按回车键然后从下拉列表中选择第一个值时,用户没有输入任何内容,但仍然从下拉列表中选择第一个值。有没有人有任何想法来阻止这个功能。

仅当用户在输入框中输入内容并选择或按回车键时才应选择。

这是一个简短的屏幕,用户没有输入任何内容,然后按 Enter 键,然后选择第一个值。

代码如下:

  import React from 'react';
  import Select from 'react-select';

  class CreateSelectOption extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        selected: this.props.options,
        questionsCount: props.questionsCount,
        inputValue: '',
    }
    this.processQuestions = this.processQuestions.bind(this);
}

componentWillReceiveProps(newProps) {
    if (newProps.questionsCount.length > 0) {
        this.setState({
            questionsCount: newProps.questionsCount
        })
    }
}

onChange = (e, props) =>{
    this.props.onChange(e, props.technology, props.id, props.type, props.noc, props.dataTech, props.dataType);
}

onInputValueChange = inputValue => {
    this.setState({ inputValue: this.allowOnlyOneSpace(inputValue) });
  };

allowOnlyOneSpace = str => {
    return str.endsWith(" ") ? str.trim() + " " : str.trim();
};

render() {
   
    const customStyles = {
        control: (styles, state) => ({
            ...styles,
            borderRadius: '1px', // default border color
            boxShadow: 'none', // no box-shadow
            color: '#007bff',
            height: '27px',
            minHeight: '30px'
        }),
        singleValue: (styles, state) => ({
            ...styles,
            color: '#007bff',
            top: '42%'
          }),
        dropdownIndicator: (styles, state) => ({
        ...styles,
        padding: "6px"
        }),
        menuPortal: base => ({ ...base, zIndex: 9999, fontSize: '12px' })
}
    const { selectedTech }=this.props;
    const selectedVal = selectedTech ? { value: selectedTech , label: selectedTech } : null;
    return(
    <Select
        className="select-width select-color"
        menuPortalTarget={document.body}
        styles={customStyles}
        value={selectedVal}
        onChange={(e) => { this.onChange(e, this.props) }}
        options={props.technologyArray && props.technologyArray.map(t => ({ value: t, label: t }))}
        openMenuOnClick={true}
        placeholder="Please select tech"
        components={{ IndicatorSeparator: () => null }}
        inputValue={this.state.inputValue}
        onInputChange={this.onInputValueChange}
        disabled={this.state.inputValue.length === 0}
        />
    )
}

} 导出默认 CreateSelectOption;

【问题讨论】:

标签: reactjs react-select


【解决方案1】:

UPD options={this.state.inputValue.length === 0 ? [] : colourOptions}

【讨论】:

【解决方案2】:

试试这些属性

    openMenuOnFocus={false}
    openMenuOnClick={false}

我猜(虽然现在无法检查)这也是有效的(如果 selectedVal 存在,则它变为 true else false)

    openMenuOnFocus={selectedVal}
    openMenuOnClick={selectedVal}

【讨论】:

  • 感谢您的回复。它一旦用户输入但一旦清除然后再次下拉仍然打开然后用户可以按回车键并再次选择第一个值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多