-
Notifications
You must be signed in to change notification settings - Fork 932
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
Items as a function of inputValue #1144
Comments
Why is the |
The state of the component is all about By forcing me to manage this derived value as state, I'm working at the wrong layer, and I'm ending up having to build out workarounds for a circular dependency - I need You can see my rather messy WIP code here if you're interested: https://github.com/DestinyItemManager/DIM/blob/new-search-bar/src/app/search/SearchBar.tsx#L182-L227 |
Who's Ok, fine, it makes sense, don't keep it as a state value. Maybe I am missing something, but as far as I see, you will need some |
Sorry, I meant Obviously I'll need |
It's sounding like this contribution wouldn't be appreciated. I may take a look at forking Downshift for my needs - the current lifecycle of useCombobox results in unnecessary double renders and state management that could be avoided. |
I did manage to untangle this - I think I misunderstood how Downshift manages its internal state. |
@bhollis if we can improve stuff then we should. If you can give us details about how you figured it out it may help us further. Thank you! |
downshift
version: 6.0.2node
version: v14.2.0npm
(oryarn
) version: yarn 1.22.4Relevant code or config/What you did/What happened/Reproduction repository:
This is a new feature request, so N/A
Problem description:
I have what I believe is a pretty common usecase for
useCombobox
- I'm building an autocompleter that populates the dropdown with suggestions based on what the user has typed (I'm replacing previous usage of thetextcomplete
library, implementing something similar to the Chrome address bar). The list of items to show in the dropdown can be a function of theinputValue
- given a particular input value, I can compute what the items can be. The docs suggest that the way to do this is to set a list of items in state (useState
) when the input changes viaonInputValueChange
and that works, but that seems a bit wrong - the state of my component is just the text in the input field, not the list of completions - those are purely a function of that input text.I tried an approach where I maintained the
inputValue
state myself and useduseCombobox
as a controlled component, but I ran into #1108 that makes this unworkable, and it's still not as elegant as it could be.Suggested solution:
Allow the
items
prop to be a function:(inputValue: string, selectionStart: number) => Item[]
. This allows users to provide a function that can always compute the list of items based on the input. I'm also asking for aselectionStart
argument because in my case I'm including word-by-word suggestions so if the user were to type starting in the middle ofinputValue
I'd want to know where they are typing so as to provide appropriate suggestions in the string. This function would be evaluated on every render (to allow for closing over other state in the function) and memoizing it is the caller's responsibility.Happy to submit a PR for it, but I wanted to run it by you first.
The text was updated successfully, but these errors were encountered: