From 73a599a8136535af8b58909398716737b3b51d84 Mon Sep 17 00:00:00 2001 From: Saul Clavijo Date: Tue, 26 Nov 2019 08:26:52 -0300 Subject: [PATCH] [Autocomplete] Only trigger onInputChange when the value changes (#18571) --- .../material-ui-lab/src/Autocomplete/Autocomplete.test.js | 7 +++---- .../src/useAutocomplete/useAutocomplete.js | 8 ++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index a53d7bfbfaac16..fee476af934dd8 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -650,11 +650,10 @@ describe('', () => { const { getByRole } = render(); const textbox = getByRole('textbox'); - expect(handleChange.callCount).to.equal(1); - expect(handleChange.args[0][0]).to.equal(''); + expect(handleChange.callCount).to.equal(0); fireEvent.change(textbox, { target: { value: 'a' } }); - expect(handleChange.callCount).to.equal(2); - expect(handleChange.args[1][0]).to.equal('a'); + expect(handleChange.callCount).to.equal(1); + expect(handleChange.args[0][0]).to.equal('a'); expect(textbox.value).to.equal(''); }); }); diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index f833a0c0c37135..cc44ea60a6cb79 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -210,6 +210,10 @@ export default function useAutocomplete(props) { newInputValue = typeof optionLabel === 'string' ? optionLabel : ''; } + if (inputValue === newInputValue) { + return; + } + setInputValue(newInputValue); if (onInputChange) { @@ -635,6 +639,10 @@ export default function useAutocomplete(props) { handleOpen(event); } + if (inputValue === newValue) { + return; + } + setInputValue(newValue); if (onInputChange) {