Minimum number of characters to start the search
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.
I have the same issue
@yana95 There is 815 open issues, I don't expect we get an answer soon.
the same problem
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"
/>
Can someone elaborate? I see "resolved" ... how? And I can't make sense of @hbansal0122 answer. How can I set minLength to 1?
@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.
@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.
@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?
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?
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 , 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,
}}
/>
)
}
I like simple solutions for achieving simple tasks so I found out that onInputChange={(v) => v.length > 1 ? v : ""} works for my v5 implementation.