-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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 with freeSolo and custom onChange breaks on Enter key pressed #18123
Comments
@oziniak Thank you for the report! What do you think of this fix? diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 7f28ddca2..d7394c18b 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -166,6 +166,8 @@ export default function useAutocomplete(props) {
let newInputValue;
if (multiple) {
newInputValue = '';
+ } else if (freeSolo && typeof newValue === 'string') {
+ newInputValue = newValue;
} else {
newInputValue = newValue != null ? getOptionLabel(newValue) : '';
} Do you want to submit a pull request? :) |
Is PR #18117 related to this? If yes, has it been shipped yet? |
@ad-das #18117 solves a different problem (with the multi-select). You can check the added test case to get a better idea of the problem it solves. This bug is different, it's about using free solo with rich (object) options, it's a combination I didn't encounter during the initial development of the component. |
@oliviertassinari I'll be able to submit a PR in an hour or so |
@oziniak Awesome! If you struggle with the addition of a test case, I can have a look at it. |
@oliviertassinari not sure why, but I can't make a test to fail.
|
@oziniak Try focusing the input first and then |
@eps1lon @oliviertassinari is it possible that there's some caching in built/compiled files? because I'm facing the near-to-magic issue. I've removed the fix from
But the issue is FIXED somehow, and it drives me crazy 😄 Maybe that's why I can't manage to break the tests. |
@oziniak I'm not aware of any caching logic that might impact here. Can you try this reproduction? describe.only('free solo', () => {
it('should accept any value', () => {
const handleChange = spy();
const { container } = render(
<Autocomplete
freeSolo
options={[{ name: 'test' }, { name: 'foo' }]}
onChange={handleChange}
renderInput={params => <TextField {...params} />}
/>,
);
const input = container.querySelector('input');
fireEvent.change(input, { target: { value: 'a' } });
fireEvent.keyDown(input, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.equal('a');
});
}); |
I've also ran into a similar issue, but I assume it's rooted from the same problem. You can even reproduce it on this example. Select another choice, then press Enter. The error is different from what was reported on this though.
According to the sample of code, the first two AutoCompletes have no custom onChange or freeSolo. Edit: I see that there are other closed issues related to this, so assume that it is well known. |
@ronaldporch yeah, in my example I used this hack to intercept onChange for text input.
@oliviertassinari I've ran into issue where I can no longer reproduce a bug on local dev environment. I'm trying to test it on |
Ok, so I investigated: the issue with breaking of autocomplete with freeSolo on enter pressed, appears only when you have options as an array of objects, for example |
OK, nevermind my above comments. I wasn't reading the thread carefully and only now I understood that the issue from the start was in having options as not an array of strings. |
@ad-das This issue is not visible in the documentation. What you report is different. |
I am running into the exact same issue. If I remove either of freeSolo or onChange custom values, the problem is not visible (but neither is the expected functionality). |
@vash500 Do you have a reproduction? |
Sorry for the delay, I had not worked on this project for a whole week. So, it turns out that I am full of stupid and what I had is not a reproduction of this case. When tried to construct a minimal working example I found that it was my onInputChange function that had a typo:
It says |
Current Behavior 😯
I use Autocomplete with redux, to simulate async search.
search
is provided from the redux store, when action fired withonSearchCustom
gets to the reducer and changes the state. Everything works, I see results populated into Combobox.But when I press Enter, everything breaks with this error.
And I can't use
event.keyCode
,event.charCode
,event.key
in myonChange
handler to input, because everything isundefined
there.Expected Behavior 🤔
It would be great if I could use a component to fit my needs. I was hoping that if I put any plug on Enter key in my onChange handler to input, but I can't even check if that would work, because I can't tell if Enter is pressed or any other key.
Steps to Reproduce 🕹
https://codesandbox.io/s/create-react-app-with-typescript-1te6e
Steps:
1.Write any text, it works ok
2. Press enter, it breaks with error in console
Context 🔦
Your Environment 🌎
The text was updated successfully, but these errors were encountered: