Skip to content

Commit 8e35862

Browse files
committed
simplify filter logic
1 parent badff02 commit 8e35862

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

packages/mui-joy/src/Autocomplete/Autocomplete.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import PropTypes from 'prop-types';
33
import { OverridableComponent } from '@mui/types';
44
import { chainPropTypes, integerPropType } from '@mui/utils';
55
import composeClasses from '@mui/base/composeClasses';
6-
import { useAutocomplete, AutocompleteGroupedOption } from '@mui/base/AutocompleteUnstyled';
6+
import {
7+
useAutocomplete,
8+
AutocompleteGroupedOption,
9+
UseAutocompleteProps,
10+
} from '@mui/base/AutocompleteUnstyled';
711
import PopperUnstyled, {
812
PopperUnstyledProps,
913
PopperUnstyledTypeMap,
@@ -190,6 +194,27 @@ const AutocompleteLimitTag = styled('span', {
190194
marginBlockStart: 'var(--_Input-paddingBlock)',
191195
});
192196

197+
const excludeUseAutocompleteParams = <
198+
T extends Partial<UseAutocompleteProps<any, undefined, undefined, undefined>>,
199+
>({
200+
autoComplete,
201+
autoHighlight,
202+
autoSelect,
203+
blurOnSelect,
204+
clearOnBlur,
205+
clearOnEscape,
206+
defaultValue,
207+
disableCloseOnSelect,
208+
disabledItemsFocusable,
209+
disableListWrap,
210+
filterSelectedOptions,
211+
handleHomeEndKeys,
212+
includeInputInList,
213+
openOnFocus,
214+
selectOnFocus,
215+
...other
216+
}: T) => other as T;
217+
193218
const Autocomplete = React.forwardRef(function Autocomplete(
194219
inProps,
195220
ref: React.ForwardedRef<HTMLDivElement>,
@@ -239,17 +264,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(
239264
value: valueProp,
240265
...otherProps
241266
} = props;
242-
const other = {} as typeof otherProps;
243-
(Object.keys(otherProps) as Array<keyof typeof otherProps>).forEach((k) => {
244-
if (
245-
!k.match(
246-
// Do not spread these props to the root DOM
247-
/^(autoComplete|autoHighlight|autoSelect|blurOnSelect|clearOnBlur|clearOnEscape|defaultValue|disableCloseOnSelect|disabledItemsFocusable|disableListWrap|filterSelectedOptions|handleHomeEndKeys|includeInputInList|openOnFocus|selectOnFocus)$/,
248-
)
249-
) {
250-
other[k] = otherProps[k];
251-
}
252-
});
267+
const other = excludeUseAutocompleteParams(otherProps);
253268

254269
const formControl = React.useContext(FormControlContext);
255270
const size = inProps.size ?? formControl?.size ?? sizeProp;

packages/mui-joy/src/AutocompleteListbox/AutocompleteListbox.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ const useUtilityClasses = (ownerState: AutocompleteListboxOwnerState) => {
2929
return composeClasses(slots, getAutocompleteListboxUtilityClass, {});
3030
};
3131

32+
const excludePopperProps = <T extends Record<string, any>>({
33+
anchorEl,
34+
direction,
35+
disablePortal,
36+
modifiers,
37+
open,
38+
placement,
39+
popperOptions,
40+
popperRef,
41+
TransitionProps,
42+
...other
43+
}: T) => other;
44+
3245
export const StyledAutocompleteListbox = styled(StyledList)<{
3346
ownerState: AutocompleteListboxOwnerState;
3447
}>(({ theme, ownerState }) => {
@@ -82,7 +95,7 @@ const AutocompleteListbox = React.forwardRef(function AutocompleteListbox(inProp
8295
color = 'neutral',
8396
variant = 'outlined',
8497
size = 'md',
85-
...other
98+
...otherProps
8699
} = props;
87100

88101
const ownerState = {
@@ -96,24 +109,14 @@ const AutocompleteListbox = React.forwardRef(function AutocompleteListbox(inProp
96109
wrap: false,
97110
};
98111

99-
const filteredOther: typeof other = {};
100-
// ignore props that might be injected by PopperUnstyled
101-
(Object.keys(other) as Array<keyof typeof other>).forEach((k) => {
102-
if (
103-
!k.match(
104-
/^(anchorEl|direction|disablePortal|modifiers|open|placement|popperOptions|popperRef|TransitionProps)$/,
105-
)
106-
) {
107-
filteredOther[k] = other[k];
108-
}
109-
});
112+
const other = excludePopperProps(otherProps);
110113

111114
const classes = useUtilityClasses(ownerState);
112115

113116
const rootProps = useSlotProps({
114117
elementType: AutocompleteListbox,
115118
externalSlotProps: {},
116-
externalForwardedProps: filteredOther,
119+
externalForwardedProps: other,
117120
ownerState,
118121
additionalProps: {
119122
ref,

0 commit comments

Comments
 (0)