Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Extend componentsProps to include popper and popupIndicator slots #33283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/pages/material-ui/api/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"clearText": { "type": { "name": "string" }, "default": "'Clear'" },
"closeText": { "type": { "name": "string" }, "default": "'Close'" },
"componentsProps": {
"type": { "name": "shape", "description": "{ clearIndicator?: object, paper?: object }" },
"type": {
"name": "shape",
"description": "{ clearIndicator?: object, paper?: object, popper?: object, popupIndicator?: object }"
},
"default": "{}"
},
"defaultValue": {
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export interface AutocompleteProps<
componentsProps?: {
clearIndicator?: Partial<IconButtonProps>;
paper?: PaperProps;
popper?: Partial<PopperProps>;
popupIndicator?: Partial<IconButtonProps>;
};
/**
* If `true`, the component is disabled.
Expand Down
11 changes: 9 additions & 2 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,12 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
disabled={disabled}
aria-label={popupOpen ? closeText : openText}
title={popupOpen ? closeText : openText}
className={clsx(classes.popupIndicator)}
ownerState={ownerState}
{...componentsProps.popupIndicator}
className={clsx(
classes.popupIndicator,
componentsProps.popupIndicator?.className,
)}
>
{popupIcon}
</AutocompletePopupIndicator>
Expand All @@ -591,7 +595,6 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
{popupOpen && anchorEl ? (
<AutocompletePopper
as={PopperComponent}
className={clsx(classes.popper)}
disablePortal={disablePortal}
style={{
width: anchorEl ? anchorEl.clientWidth : null,
Expand All @@ -600,6 +603,8 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
role="presentation"
anchorEl={anchorEl}
open
{...componentsProps.popper}
className={clsx(classes.popper, componentsProps.popper?.className)}
>
<AutocompletePaper
ownerState={ownerState}
Expand Down Expand Up @@ -739,6 +744,8 @@ Autocomplete.propTypes /* remove-proptypes */ = {
componentsProps: PropTypes.shape({
clearIndicator: PropTypes.object,
paper: PropTypes.object,
popper: PropTypes.object,
popupIndicator: PropTypes.object,
}),
/**
* The default value. Use when the component is not controlled.
Expand Down
7 changes: 6 additions & 1 deletion packages/mui-material/src/Autocomplete/Autocomplete.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ function AutocompleteComponentsProps() {
<Autocomplete
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} />}
componentsProps={{ paper: { elevation: 2 } }}
componentsProps={{
clearIndicator: { size: 'large' },
paper: { elevation: 2 },
popper: { placement: 'bottom-end' },
popupIndicator: { size: 'large' },
}}
/>
);
}
61 changes: 61 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Autocomplete, {
createFilterOptions,
} from '@mui/material/Autocomplete';
import { paperClasses } from '@mui/material/Paper';
import { iconButtonClasses } from '@mui/material/IconButton';

function checkHighlightIs(listbox, expected) {
const focused = listbox.querySelector(`.${classes.focused}`);
Expand Down Expand Up @@ -2430,6 +2431,28 @@ describe('<Autocomplete />', () => {
});

describe('prop: componentsProps', () => {
it('should apply the props on the AutocompleteClearIndicator component', () => {
render(
<Autocomplete
open
options={['one', 'two']}
value="one"
renderInput={(params) => <TextField {...params} />}
componentsProps={{
clearIndicator: {
'data-testid': 'clearIndicator',
size: 'large',
className: 'my-class',
},
}}
/>,
);

const clearIndicator = screen.getByTestId('clearIndicator');
expect(clearIndicator).to.have.class(iconButtonClasses.sizeLarge);
expect(clearIndicator).to.have.class('my-class');
});

it('should apply the props on the Paper component', () => {
render(
<Autocomplete
Expand All @@ -2446,6 +2469,44 @@ describe('<Autocomplete />', () => {
expect(paperRoot).to.have.class(paperClasses.elevation2);
expect(paperRoot).to.have.class('my-class');
});

it('should apply the props on the Popper component', () => {
render(
<Autocomplete
open
options={['one', 'two']}
renderInput={(params) => <TextField {...params} />}
componentsProps={{
popper: { 'data-testid': 'popperRoot', placement: 'bottom-end', className: 'my-class' },
}}
/>,
);

const popperRoot = screen.getByTestId('popperRoot');
expect(popperRoot).to.have.attribute('data-popper-placement', 'bottom-end');
expect(popperRoot).to.have.class('my-class');
});

it('should apply the props on the AutocompletePopupIndicator component', () => {
render(
<Autocomplete
open
options={['one', 'two']}
renderInput={(params) => <TextField {...params} />}
componentsProps={{
popupIndicator: {
'data-testid': 'popupIndicator',
size: 'large',
className: 'my-class',
},
}}
/>,
);

const popupIndicator = screen.getByTestId('popupIndicator');
expect(popupIndicator).to.have.class(iconButtonClasses.sizeLarge);
expect(popupIndicator).to.have.class('my-class');
});
});

describe('prop: readOnly', () => {
Expand Down