From 26a4109bd9eb1a333bdf0fcfcd74d59bbe62b218 Mon Sep 17 00:00:00 2001 From: Gerald Monaco Date: Thu, 24 Oct 2024 11:04:31 -0700 Subject: [PATCH] POC: Add transitions to Autocomplete --- .../src/Autocomplete/Autocomplete.js | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/mui-material/src/Autocomplete/Autocomplete.js b/packages/mui-material/src/Autocomplete/Autocomplete.js index f249219d26da5e..373a0625c6ee73 100644 --- a/packages/mui-material/src/Autocomplete/Autocomplete.js +++ b/packages/mui-material/src/Autocomplete/Autocomplete.js @@ -24,6 +24,8 @@ import { useDefaultProps } from '../DefaultPropsProvider'; import autocompleteClasses, { getAutocompleteUtilityClass } from './autocompleteClasses'; import capitalize from '../utils/capitalize'; import useSlot from '../utils/useSlot'; +import { unstable_useDeferredValueCompat } from '@mui/utils'; +import Fade from '../Fade'; const useUtilityClasses = (ownerState) => { const { @@ -422,6 +424,7 @@ const AutocompleteGroupUl = styled('ul', { export { createFilterOptions }; +const EMPTY_ARRAY = []; const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) { const props = useDefaultProps({ props: inProps, name: 'MuiAutocomplete' }); @@ -572,6 +575,11 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) { className: classes.paper, }); + const groupedOptionsDeferred = unstable_useDeferredValueCompat( + popupOpen ? groupedOptions : EMPTY_ARRAY, + ); + const isLoading = loading || groupedOptionsDeferred !== groupedOptions; + const [PopperSlot, popperProps] = useSlot('popper', { elementType: Popper, externalForwardedProps, @@ -672,19 +680,25 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) { const renderAutocompletePopperChildren = (children) => ( - - {children} - + + + {children} + + ); let autocompletePopper = null; - if (groupedOptions.length > 0) { + if (groupedOptionsDeferred.length > 0) { autocompletePopper = renderAutocompletePopperChildren( // TODO v7: remove `as` prop and move ListboxComponentProp to externalForwardedProps or remove ListboxComponentProp entirely // https://github.com/mui/material-ui/pull/43994#issuecomment-2401945800 - {groupedOptions.map((option, index) => { + {groupedOptionsDeferred.map((option, index) => { if (groupBy) { return renderGroup({ key: option.key, @@ -698,13 +712,13 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) { })} , ); - } else if (loading && groupedOptions.length === 0) { + } else if (isLoading && groupedOptionsDeferred.length === 0) { autocompletePopper = renderAutocompletePopperChildren( {loadingText} , ); - } else if (groupedOptions.length === 0 && !freeSolo && !loading) { + } else if (groupedOptionsDeferred.length === 0 && !freeSolo && !isLoading) { autocompletePopper = renderAutocompletePopperChildren(