From dd0bb69fe7575524b92e1080877d045b127a98be Mon Sep 17 00:00:00 2001 From: Zeeshan Tamboli Date: Wed, 25 Dec 2024 18:33:56 +0530 Subject: [PATCH] [Autocomplete] Revert: Fix options list rendering in freeSolo mode (#44857) --- .../src/Autocomplete/Autocomplete.js | 135 +++++++++--------- .../src/Autocomplete/Autocomplete.test.js | 16 --- 2 files changed, 64 insertions(+), 87 deletions(-) diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.js b/packages/mui-material/src/Autocomplete/Autocomplete.js index 097ded86f4f109..d8ad7ba52b3567 100644 --- a/packages/mui-material/src/Autocomplete/Autocomplete.js +++ b/packages/mui-material/src/Autocomplete/Autocomplete.js @@ -617,76 +617,6 @@ 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 (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 ? autocompletePopper : null} + {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} ); }); diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.test.js b/packages/mui-material/src/Autocomplete/Autocomplete.test.js index f787d0ed8b80f0..2bc796a3d4071f 100644 --- a/packages/mui-material/src/Autocomplete/Autocomplete.test.js +++ b/packages/mui-material/src/Autocomplete/Autocomplete.test.js @@ -2362,22 +2362,6 @@ 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', () => {