Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add and remove classes to input? #605

Open
arendu-zz opened this issue Jan 12, 2019 · 1 comment
Open

add and remove classes to input? #605

arendu-zz opened this issue Jan 12, 2019 · 1 comment

Comments

@arendu-zz
Copy link

Hi,
I am trying to make an autosuggest component for a quiz activity. One of the requirements is to show when a selected suggestion is the correct answer or not. I want to show this by changing the background-color of the input field.

I want to do this by adding and removing css classes to the input. I understand the default classname is react-autosuggest__input. Is there a way to dynamically add/remove new classes? I want to add a wrong class or a correct class depending on the answer selected.

I am trying to do this via renderInputComponent feature kind of like this (but it does not work):

    render() {
      const {
        value,
        suggestions
      } = this.state;
      const inputProps = {
        placeholder: "Guess",
        value,
        onChange: this.onChange,
        onKeyUp: this.onKeyUp
      };

      const renderInputComponent = inputProps => (
        <div>
          <input className={this.state.classes.join(' ')}  {...inputProps}/>
        </div>
      );

      return (
        <Autosuggest
          suggestions={suggestions}
          onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
          onSuggestionsClearRequested={this.onSuggestionsClearRequested}
          onSuggestionSelected={this.onSuggestionSelected}
          getSuggestionValue={getSuggestionValue}
          renderSuggestion={renderSuggestion}
          renderInputComponent={renderInputComponent}
          inputProps={inputProps} />
      );
    }

Any suggestion on how to accomplish this?

@MitchTalmadge
Copy link

MitchTalmadge commented May 7, 2019

You have to remove className from the inputProps.

Like this:

const renderInputComponent = inputProps => {

    delete inputProps.className;
    
    return (
        <div>
            <input className={this.state.classes.join(' ')}  {...inputProps}/>
        </div>
    );
};

Or you can add your classes into the inputProps.className field if you still want to keep the theme classes from Autosuggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants