From 3707598329983b4a8ee88874b3eefea1324fb88d Mon Sep 17 00:00:00 2001 From: rakeshmusturi <122796348+rakeshmusturi@users.noreply.github.com> Date: Thu, 7 Mar 2024 15:44:58 +0530 Subject: [PATCH] [material-ui][Autocomplete] Fix the options list being added to the DOM in `freeSolo` mode even when there are no options, causing style problems (#41300) Co-authored-by: ZeeshanTamboli --- .../src/Autocomplete/Autocomplete.js | 135 +++++++++--------- .../src/Autocomplete/Autocomplete.test.js | 16 +++ 2 files changed, 87 insertions(+), 64 deletions(-) diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.js b/packages/mui-material/src/Autocomplete/Autocomplete.js index 18e0e9d58180c1..3672e8f334d4d6 100644 --- a/packages/mui-material/src/Autocomplete/Autocomplete.js +++ b/packages/mui-material/src/Autocomplete/Autocomplete.js @@ -582,6 +582,76 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) { const popperSlotProps = slotProps.popper ?? componentsProps.popper; const popupIndicatorSlotProps = slotProps.popupIndicator ?? componentsProps.popupIndicator; + const renderAutocompletePopperChildren = (children) => ( + + + {children} + + + ); + + let autocompletePopper = null; + if (!loading && groupedOptions.length > 0) { + autocompletePopper = renderAutocompletePopperChildren( + + {groupedOptions.map((option, index) => { + if (groupBy) { + return renderGroup({ + key: option.key, + group: option.group, + children: option.options.map((option2, index2) => + renderListOption(option2, option.index + index2), + ), + }); + } + return renderListOption(option, index); + })} + , + ); + } else if (loading && groupedOptions.length === 0) { + autocompletePopper = renderAutocompletePopperChildren( + + {loadingText} + , + ); + } else if (groupedOptions.length === 0 && !freeSolo && !loading) { + autocompletePopper = renderAutocompletePopperChildren( + { + // Prevent input blur when interacting with the "no options" content + event.preventDefault(); + }} + > + {noOptionsText} + , + ); + } + return ( - {anchorEl ? ( - - - {loading && groupedOptions.length === 0 ? ( - - {loadingText} - - ) : null} - {groupedOptions.length === 0 && !freeSolo && !loading ? ( - { - // Prevent input blur when interacting with the "no options" content - event.preventDefault(); - }} - > - {noOptionsText} - - ) : null} - {groupedOptions.length > 0 ? ( - - {groupedOptions.map((option, index) => { - if (groupBy) { - return renderGroup({ - key: option.key, - group: option.group, - children: option.options.map((option2, index2) => - renderListOption(option2, option.index + index2), - ), - }); - } - return renderListOption(option, index); - })} - - ) : null} - - - ) : null} + {anchorEl ? autocompletePopper : null} ); }); diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.test.js b/packages/mui-material/src/Autocomplete/Autocomplete.test.js index 5ae89516371bfd..c20e977925918e 100644 --- a/packages/mui-material/src/Autocomplete/Autocomplete.test.js +++ b/packages/mui-material/src/Autocomplete/Autocomplete.test.js @@ -2336,6 +2336,22 @@ describe('', () => { expect(container.querySelector(`.${classes.endAdornment}`)).to.equal(null); }); + + it('should not render popper when there are no options', () => { + render( + } + slotProps={{ + popper: { 'data-testid': 'popperRoot' }, + }} + />, + ); + const popper = screen.queryByTestId('popperRoot'); + expect(popper).to.equal(null); + }); }); describe('prop: onChange', () => {