-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Autocomplete] - Passing props to ListboxComponent #18822
Comments
We have a related issue in #18450, I would love to learn more about your load more use case. |
The ref warning in your codesandbox is expected, it creates a new component at each render without forwarding the ref. You could use the context to be able to listen to your scroll bottom event. |
In my case, I am not using the internal filterOptions function. We are using AWS cloud search. I query the API on throttled keystroke. A user can search their company’s address book. When a user searches for “BBQ” AWS may return the first 10 of 45 results. If the user doesn’t see their match they can either refine the search “BBQ Austin” and return new results or scroll to the bottom of the ListboxComponent and load the next 10 results appending them to the options. |
What prevents you from getting, say, 100 results, instead of 10? In which case, the pagination is likely no longer useful. I don't think that somebody will scan more than 100 items, the user will likely refine his query after this order of magnitude. Do you display some sort of indicator at the bottom of the option list to let the user know more options are available/loading? What do you think of a new If we had a non-MIT API adapter in the enterprise version of Material-UI for AWS cloud search, would that something you would be potentially interested in? |
Nothing, but the infinite scroll does look cool
I agree.
Not at the moment but I am playing around this idea. API results come back in fast enough that not sure it matters for my case but it could for others.
I think that would be great. I think it would be the best solution.
Probably not, not familiar. Didn't know there was an enterprise version |
We have added this |
Hi oliviertassinari, |
What do you think about this diff? I still have to adjust the scroll position after options are changed. In this codesandbox the list jumps to the first option when more items are added. diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 5b4ef231c..cec1a01f5 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -88,11 +88,13 @@ export default function useAutocomplete(props) {
id: idProp,
includeInputInList = false,
inputValue: inputValueProp,
+ ListboxProps = {},
multiple = false,
onChange,
onClose,
onOpen,
onInputChange,
+ onScrollToBottom,
open: openProp,
options = [],
value: valueProp,
@@ -688,6 +690,12 @@ export default function useAutocomplete(props) {
setHighlightedIndex(highlightedIndexRef.current);
});
+ const handleListboxScroll = ({ target }) => {
+ if (target.scrollHeight - target.scrollTop === target.clientHeight && onScrollToBottom) {
+ onScrollToBottom();
+ }
+ }
+
const handlePopupIndicator = event => {
inputRef.current.focus();
@@ -800,6 +808,8 @@ export default function useAutocomplete(props) {
// Prevent blur
event.preventDefault();
},
+ onScroll: handleListboxScroll,
+ ...ListboxProps
}),
getOptionProps: ({ index, option }) => {
const selected = (multiple ? value : [value]).some( |
@m4theushw This codesandbox is not valid, it remounts the Listbox component at each update, I don't think that we can make any decision based on it.
I don't think that a strict equality check is enough. There is a 8 pixels bottom padding. I would include it to work with keyboard navigation. Regarding the |
@oliviertassinari I agree with you, this is a particular case. Also, adding more items would increase the number of DOM elements and decrease performance. An ideal implementation should not create new elements but reuse the ones not visible. I think that in favor of infinite scroll the developer should add a way to let the user know that more results are available and they can only be visualised in another page. Like what GitHub does. But to reproduce the behavior above we don't need the |
Would you like me to make a PR for ListboxProps? Need to update useAutocomplete, docs and add a test? |
This would be great :) (I don't think that we should add tests, they would slow us down without providing much value for this one) |
What about adding prop passing support to all these too? Just to keep things consistent or is that overkill
|
Please don't, the fewer we have, the better. I had this problem when doing POCs for styled-components built-in. I suspect we will have to find a systematic solution for v5 anyway. |
Oh boy #18885 🙃 |
@ChrisWiles have you find a way to prevent the "jump to start" scrolling glitch? |
Never heard of that, this is how I set it up const [isScrollBottom, setIsScrollBottom] = useState(false);
const nearBottom = (target = {}) => {
const diff = Math.round(target.scrollHeight - target.scrollTop);
return diff - 25 <= target.clientHeight;
};
// Autocomplete
ListboxProps={{
onScroll: ({ target }) => setIsScrollBottom(nearBottom(target)),
}} |
|
After X long hours spent on trying do 'hacks' with scroll/fix warning type listBoxComponent props i couldn't find any working way do handle that... the way which i've chosen is to use hook useAutocomplete and provide custom list box build based on material-ui components like there is a good example (easy to convert to TypeScript) https://codesandbox.io/s/material-demo-0fbyb?file=/demo.js in my case i only imported getRootProps( for parent div), getInputProps( for inputProps TextField) and everything works |
@oliviertassinari hi, |
This thread helped me figure out what I wanted to do all these years later. :) |
Not sure if this is a bug or not.
I am trying to fetch more options when ListboxComponent scroll is at the bottom. Having issues with the custom ListboxComponent.
When I pass props I get a fowardRef warning and the list items won't render correctly even tho fowardRef was used.
Demo
https://codesandbox.io/s/jolly-matsumoto-ugs5d
The text was updated successfully, but these errors were encountered: