downshift icon indicating copy to clipboard operation
downshift copied to clipboard

Using as a typeahead?

Open ozziexsh opened this issue 9 years ago • 8 comments

First off, thanks for all of the hard work that went into this library! Sorry for not following the issue template, downshift/npm/node version are irrelevant as this is more of a question.

Problem description:

It seems like this module was built with flexibility in mind but right now, as far as I can tell, you cannot enter a custom value not present in the "options" array. E.g. if the options are [ 'Apples', 'Oranges', 'Bananas' ], I can't enter a value of Kiwi. Once you type into the input and lose focus, the input clears.

Consider the following example, and try typing "kiwi" and then tabbing / clicking outside of the input:

https://codesandbox.io/s/r9mzy8zo

Suggested solution:

Typing in a value and not selecting an option persists the inputValue prop.

If this doesn't align with the views of this package, I totally get it. But it seems like it has all of the foundation to make this an option :)

ozziexsh avatar Aug 25 '17 18:08 ozziexsh

Hey, sorry I don't have much time (will come back later to help more when I have more time), but this is totally possible by controlling the selectedItem (and potentially inputValue) state. See the README for how to do that.

kentcdodds avatar Aug 26 '17 21:08 kentcdodds

Type Ahead example here. This is based on Bare Bones autocomplete but enhanced to support type ahead.

Two changes required to Bare Bones to make this work convert to stateful component and use control props. Specifically these two arguments passed to Downshift:

onStateChange={({ inputValue }) => this.setState({ inputValue })}
selectedItem={this.state.inputValue}

@kentcdodds I added the downshift:example tag to this CodeSandbox, will that add it to the README? If not I will manually add.

codiemullins avatar Aug 30 '17 23:08 codiemullins

Thanks @codiemullins! Do you think that you could do a little more in the example to show how that could be useful?

Then you'll need to make a PR to add it in the README. Thanks again!

kentcdodds avatar Aug 31 '17 02:08 kentcdodds

@kentcdodds I'm happy to do so. If you or @nehero have any thoughts on how to make the example more useful let me know! I am struggling to think of ideas

codiemullins avatar Aug 31 '17 16:08 codiemullins

Thanks for the solution @codiemullins! Sure, I'll come back to this in a bit with an example

ozziexsh avatar Aug 31 '17 16:08 ozziexsh

If you or @nehero have any thoughts on how to make the example more useful let me know! I am struggling to think of ideas

I think that you could just render the selectedItem in another place on the page and that would be sufficient for now...

kentcdodds avatar Aug 31 '17 18:08 kentcdodds

Sounds good! PR for readme coming. Updated https://codesandbox.io/s/82m2px40q9 to display the value on the page.

@nehero Note: I had to improve the onStateChange in the example because after the input lost focus I noticed if the typed in value wasn't in the items array then it would set it to undefined, checking for undefined resolves the issue.

codiemullins avatar Aug 31 '17 19:08 codiemullins

Thanks for the example! I'll go ahead and close this now.

As an after-thought, it'd be really cool if the example could autofill the input with the first result with the part I haven't yet typed "selected" in the input field (so I can keep typing). That would actually be a very awesome example.

kentcdodds avatar Aug 31 '17 20:08 kentcdodds