Skip to content

Commit e6c1d79

Browse files
github-actions[bot]imadxZeeshanTamboli
authored
[material-ui][Autocomplete] Prevent shrink animation in controlled Autocomplete when initial value is provided (@imadx) (#45735)
Co-authored-by: Ishan Madhusanka <[email protected]> Co-authored-by: ZeeshanTamboli <[email protected]>
1 parent 614048c commit e6c1d79

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/mui-material/src/useAutocomplete/useAutocomplete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ function useAutocomplete(props) {
146146
const highlightedIndexRef = React.useRef(defaultHighlighted);
147147

148148
// Calculate the initial inputValue on mount only.
149-
// Using useRef since defaultValue doesn't need to update inputValue dynamically.
149+
// useRef ensures it doesn't update dynamically with defaultValue or value props.
150150
const initialInputValue = React.useRef(
151-
getInputValue(defaultValue, multiple, getOptionLabel),
151+
getInputValue(defaultValue ?? valueProp, multiple, getOptionLabel),
152152
).current;
153153

154154
const [value, setValueState] = useControlled({

packages/mui-material/src/useAutocomplete/useAutocomplete.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,25 @@ describe('useAutocomplete', () => {
415415
expect(onInputChange.callCount).to.equal(0);
416416
});
417417
});
418+
419+
describe('prop: value', () => {
420+
it('should not trigger onInputChange when value is provided', () => {
421+
const onInputChange = spy();
422+
423+
function Test() {
424+
const [value, setValue] = React.useState('foo');
425+
const { getInputProps } = useAutocomplete({
426+
value,
427+
onChange: (event, valueParam) => setValue(valueParam),
428+
onInputChange,
429+
options: ['foo', 'bar'],
430+
});
431+
432+
return <input {...getInputProps()} />;
433+
}
434+
435+
render(<Test />);
436+
expect(onInputChange.callCount).to.equal(0);
437+
});
438+
});
418439
});

0 commit comments

Comments
 (0)