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

Allow custom input values #652

Open
ccemeraldeyes opened this issue Jun 18, 2019 · 1 comment
Open

Allow custom input values #652

ccemeraldeyes opened this issue Jun 18, 2019 · 1 comment

Comments

@ccemeraldeyes
Copy link

In my project, we want to also allow users to input values that are not returned in the suggestions list. We've instructed our users to save their custom input by hitting enter. However, because onSuggestionSelected calls onKeyDown with the original event and no additional data, we had to resort to using a local variable to track the last value selected so that we could figure out whether the onKeyDown event came from onSuggestionSelected or not.

I think the easiest way to get around this would be to explicitly include the suggestion or null in the params passed to onKeyDown, though I could also see modifying the event object to add this data, or simply adding an additional config parameter to allow this mode.

@drags
Copy link

drags commented Dec 5, 2019

Ran into a similar situation, though I wasn't seeing onSuggestionSelected called when a user pressed enter when the suggestions list was empty. This seems to be intentional since the onSuggestionSelected method is only called if (highlightedSuggestion != null).

I got the desired behavior by:

  • wrapping the <AutoSuggest> in a form with a hidden submit
  • passing an onSubmit via inputProps
  • clearing state[value, suggestions] manually.

Pasting this here in case it helps anyone and to invoke Cunningham's Law for feedback/improvements.

// The method to do the actual work comes in as a prop: this.props.addTag
// We want to call this.props.addTag when _either_ a suggestion is selected or "enter" is struck in the input field

class TagSuggest extends React.Component {

  // snip: constructor and other methods

  onSubmit = (event) => {
    event.preventDefault()                // prevent normal form submission
    this.props.addTag(this.state.value)
    this.setState({
      value: "",
      suggestions: [],
    })
  }

  onSuggestionSelected = (event, {suggestion, suggestionValue}) => {
    event.preventDefault()            // prevent onSubmit from being called
    this.props.addTag(suggestionValue)
    this.setState({
      value: "",
      suggestions: [],
    })
  }

  render() {
    const inputProps = {
      // ...
      onSubmit: this.onSubmit,
    };
    return (
      <form onSubmit={this.onSubmit}>
        <Autosuggest
          // snip: autosuggest props 
        />
        <input type="submit" style={{"display": "none"}} />
      </form>
    );
  }

}

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