-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] Add ability to override key down events handlers #23487
Conversation
@hessaam Do you have a reproduction? |
When the freeSolo prop was true, if a user clears an input, "option.length" causes the Uncaught TypeError error. |
@hessaam Could you provide a codesandbox or add a regression test case? |
226778e
to
bec6be1
Compare
03b3dfd
to
6cf4eb5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we use preventDefault()
and .defaultPrevented
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also: Please revert the changes to existing tests unless this is actually a breaking change.
@eps1lon For this reason #14991. I thought it would be assuming too much, so I went with Downshift's solution: downshift-js/downshift#358.
The change in the tests is related to testing the form submission behavior. This is a refactoring that allows writing more resilient tests. Without it, the way the event handlers are written in the Autocomplete can interfere. For instance, it might call prevent default, but if there is a return before calling the onKeydown handler, you won't get the correct assertion. |
Sure, please extract it to a separate PR. |
I don't understand what the reason is. I see a PR targetting modals onClose. But the issue is not talking about Escape key behavior as far as I can tell. What's the concrete issue with |
Ok, happy to do it. But note that this pull request would need to be based on the extraction. As the new logic breaks the tests.
First, I would like to raise that this isn't about a specific key, the purpose is to allow developers to override any key, it could be Escape. |
We can't solve abstract problems, only concrete ones. So in absent of a concrete problem, let's go with the API that already exists on the web: preventDefault().
So it is a breaking change. We should label it as such. |
#19887 and downshift-js/downshift#330 are concrete problems. They seem to want to submit the form no matter what, so preventDefault doesn't seem to be an option. However, happy to try it out, and learn from developers' feedback.
The tests were indirectly relying on it, it wasn't testing the behavior on purpose. It could have been a bug. But no objection to labeling it a breaking change if you think strongly about it. |
That we are solving here. Not sure why you consider it a separate problem That is still solvable with DOM API. No need to learn new mui-only expando properties on well defined interfaces. |
@eps1lon Ok label added.
Done in #23704.
From how I understand #19887, @ivan-jelev wants to validate the form as soon as Enter is pressed, no matter the state of it: So I think this makes a solution based on
If you think strongly about this, happy to encourage |
Sure, but we're exhibiting some cognitive dissonance here. On one hand we strongly declare "We don't want to validate the form. " but are ok with people wanting to validate. So either we don't want to validate it or we do. What we're now doing is punting to problem instead of clearly drawing requirements and coming up with an implementation for that. Right now we insist on a particular implementation and try to come up with requirements for it. That doesn't lead to a robust implementation. So I urge people again to not say "how" they want problems to be solved but start with the problems they have. We have more context to come up with a better implementation that solves this particular problem. What this means is that you shouldn't mention anything about events, bubbling, default behavior etc but rather how the user story looks. From that we can come up with solutions because that's our job. We're not here to paste the code you wish into the codebase but solve problems your users are facing by providing a component library. The only actual user story in the underlying issues seems to be
and I have to ask: Why? This is behavior that doesn't match the default web behavior for |
@eps1lon What if we reword this comment? I think that the idea is that the default should be to request Enter x2 as safest in most cases. However, when you have a single entry Combobox in your form, I think that it makes sense: diff --git a/packages/material-ui/src/useAutocomplete/useAutocomplete.js b/packages/material-ui/src/useAutocomplete/useAutocomplete.js
index 9ac3d25e9d..cbf6e0cf34 100644
--- a/packages/material-ui/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui/src/useAutocomplete/useAutocomplete.js
@@ -724,7 +724,7 @@ export default function useAutocomplete(props) {
const option = filteredOptions[highlightedIndexRef.current];
const disabled = getOptionDisabled ? getOptionDisabled(option) : false;
- // We don't want to validate the form.
+ // Avoid early form validation, let the end-users continue filling the form.
event.preventDefault();
if (disabled) {
Agree, the iconic: "I want a faster horse", nop, you want a car.
From my perspective the answer is straightforward: we definitely do, here and everywhere: "provide sane defaults, allow developers to do advanced things". I think that the feeling of freedom should be as if you wrote the component from scratch. I think it's why the hook/headless approach is appreciated by front-end developers. If a developer can't get this feeling, he will avoid using the library: https://twitter.com/markdalgleish/status/1328683488250195968, https://twitter.com/chofter/status/1332091815823020037.
What about sometimes, amazing things happen, being able to do worse also mean developers can do better? |
30eb5e9
to
caabadc
Compare
rebased and improved test case
@oliviertassinari Could you clarify what user story this PR is addressing? It's unclear from the linked issue. |
The developer story is taking full control of the handlers. A possible user story is a short form where the validation of an option (Enter) in the combo box directly triggers the form. |
Closes #19887