react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Minimum number of characters to start the search

Open hossammourad opened this issue 8 years ago • 12 comments

Hi, Is there an option to start the search with the first character typed? Currently I have to type 2 characters for the search to kick in, is it possible to make it customizable?

Thanks.

hossammourad avatar Feb 19 '18 06:02 hossammourad

I have the same issue

yana95 avatar Mar 13 '18 09:03 yana95

@yana95 There is 815 open issues, I don't expect we get an answer soon.

hossammourad avatar Mar 13 '18 09:03 hossammourad

the same problem

AuthorProxy avatar Jun 26 '18 22:06 AuthorProxy

you can use onInputChange and call a function and check if the length of entered content I greater than the minimum required if yes then change a state variable (option_true) to true other wise false.

<Select
  value={tagsSkills}
  onChange={this.function}
  options={this.state.option_true ? options : []}
  onInputChange={this.new_function}
  multi={true}
  placeholder="enter you choice"
/>

ghost avatar Sep 10 '18 12:09 ghost

Can someone elaborate? I see "resolved" ... how? And I can't make sense of @hbansal0122 answer. How can I set minLength to 1?

dzg avatar Apr 16 '19 22:04 dzg

@dzg this issue is resolved in v2. If you're using v1 this may still be an issue.

The minLength is already 1, a filter isn't performed without text.

The method above involves taking control of the input value, using the onInputChange prop, and performing your own filter. Then pass those filtered options on to the select.

jossmac avatar Apr 16 '19 23:04 jossmac

@dzg Hey, I am not able to understand fully, what do you mean by min length = 1. How can we perform any action without entering a single character and if we are entering a single character then "onInputChange={this.new_function}" this should work or some hover thing can be used to perform something else.

ghost avatar Apr 17 '19 07:04 ghost

@hbansal0122 : can you please show a reference of new_function. I'm new to react would like to see how you apply it on select drop down?

ShivaniBali avatar Jan 27 '20 19:01 ShivaniBali

Here you go

class SearchBar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showOptions: false
    }
  }


  handleInputChange = (typedOption) => {
    if( typedOption.length > 1 ) {
      this.setState(
        { showOptions : true },
        () => console.log('Shown')
      )
    }
    else {
      this.setState(
        { showOptions : false },
        console.log('Option lenght is short {} {}', typedOption, typedOption.length)
      )
    }
  }

  render() {
    console.log(this.props);
    const { selectedOption } = this.state;
    return(
      <Select value={selectedOption} options={ this.state.showOptions ? options : [] } onChange={ this.handleChange }
        onInputChange = { this.handleInputChange }
        components={
          {
            DropdownIndicator: () => null,
          }
      />
)
}

@hbansal0122 : can you please show a reference of new_function. I'm new to react would like to see how you apply it on select drop down?

Flamingo09 avatar Jan 31 '20 10:01 Flamingo09

I'm new to React functions and hooks and I am struggling adapting this example to that new paradigm, instead of the older class-based paradigm. Can you rewrite the handleInputChange() event handler logic in the procedural style?

amfriedman avatar Oct 06 '21 22:10 amfriedman

@amfriedman , I'll try to explain the core differences to help your transition to the time before hooks.

Class syntax relied on declaring its own instance of a Component "object". As part of the constructor for this object, you have the opportunity to set the components initial state. The methods are declared within this component object so that they have access to this internal state.

The class syntax adds a lot of boilerplate and also relies on having one single state object to encapsulate all of the state changes within the component.

Hooks were designed to move this logic out so that state management could be more easily accessible or customized.

Here we can also assume that the handleChange, options, and selectedOption are all passed in as props from its parent component.

const SearchBar = props => {
  const { handleChange, options, selectedOption } = props;

  const [ showOptions, setShowOptions ] = useState(false);

  const handleInputChange = useCallback((typedOption) => {
    if (typedOption.length > 1 ) {
      setShowOptions(true);
      console.log('Shown');
    }
    else {
      setShowOptions(false);
      console.log('Option length is short {} {}', typedOption, typedOption.length);
    }
  }, []);

  return(
    <Select 
      value={selectedOption} 
      options={ showOptions ? options : [] } 
      onChange={ handleChange }
      onInputChange = { handleInputChange }
      components={{
        DropdownIndicator: () => null,
      }}
    />
  )
}

ebonow avatar Oct 07 '21 01:10 ebonow

I like simple solutions for achieving simple tasks so I found out that onInputChange={(v) => v.length > 1 ? v : ""} works for my v5 implementation.

mysuf avatar Jan 17 '25 21:01 mysuf