Skip to content

Commit 3ca7a1b

Browse files
makeenomichaldudakmnajdovaZeeshanTamboli
authored
[Autocomplete] Add Mui-expanded class (#33312)
Signed-off-by: Marija Najdova <mnajdova@gmail.com> Co-authored-by: Michał Dudak <michal.dudak@gmail.com> Co-authored-by: Marija Najdova <mnajdova@gmail.com> Co-authored-by: ZeeshanTamboli <zeeshan.tamboli@gmail.com>
1 parent 0361cdd commit 3ca7a1b

File tree

10 files changed

+62
-1
lines changed

10 files changed

+62
-1
lines changed

docs/pages/base/api/use-autocomplete.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@
155155
"default": "false",
156156
"required": true
157157
},
158+
"expanded": {
159+
"type": { "name": "boolean", "description": "boolean" },
160+
"default": "false",
161+
"required": true
162+
},
158163
"focused": {
159164
"type": { "name": "boolean", "description": "boolean" },
160165
"default": "false",

docs/pages/material-ui/api/autocomplete.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"classes": [
107107
"root",
108108
"fullWidth",
109+
"expanded",
109110
"focused",
110111
"tag",
111112
"tagSizeSmall",
@@ -129,7 +130,7 @@
129130
"groupLabel",
130131
"groupUl"
131132
],
132-
"globalClasses": { "focused": "Mui-focused" },
133+
"globalClasses": { "expanded": "Mui-expanded", "focused": "Mui-focused" },
133134
"name": "MuiAutocomplete"
134135
},
135136
"spread": true,

docs/translations/api-docs/autocomplete/autocomplete.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
"nodeName": "the root element",
7272
"conditions": "<code>fullWidth={true}</code>"
7373
},
74+
"expanded": {
75+
"description": "State class applied to {{nodeName}} if {{conditions}}.",
76+
"nodeName": "the root element",
77+
"conditions": "the listbox is displayed"
78+
},
7479
"focused": {
7580
"description": "State class applied to {{nodeName}} if {{conditions}}.",
7681
"nodeName": "the root element",

docs/translations/api-docs/use-autocomplete/use-autocomplete.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"returnValueDescriptions": {
4242
"anchorEl": "An HTML element that is used to set the position of the component.",
4343
"dirty": "If <code>true</code>, the component input has some values.",
44+
"expanded": "If <code>true</code>, the listbox is being displayed.",
4445
"focused": "If <code>true</code>, the component is focused.",
4546
"focusedTag": "Index of the focused tag for the component.",
4647
"getClearProps": "Resolver for the <code>clear</code> button element's props.",

packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ export interface UseAutocompleteReturnValue<
411411
* @default false
412412
*/
413413
dirty: boolean;
414+
/**
415+
* If `true`, the listbox is being displayed.
416+
* @default false
417+
*/
418+
expanded: boolean;
414419
/**
415420
* If `true`, the popup is open on the component.
416421
* @default false

packages/mui-base/src/useAutocomplete/useAutocomplete.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ export default function useAutocomplete(props) {
11511151
inputValue,
11521152
value,
11531153
dirty,
1154+
expanded: popupOpen && anchorEl,
11541155
popupOpen,
11551156
focused: focused || focusedTag !== -1,
11561157
anchorEl,

packages/mui-material/src/Autocomplete/Autocomplete.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type AutocompleteOwnerState<
3232
ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
3333
> = AutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent> & {
3434
disablePortal: boolean;
35+
expanded: boolean;
3536
focused: boolean;
3637
fullWidth: boolean;
3738
hasClearIcon: boolean;

packages/mui-material/src/Autocomplete/Autocomplete.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const useUtilityClasses = (ownerState) => {
2828
const {
2929
classes,
3030
disablePortal,
31+
expanded,
3132
focused,
3233
fullWidth,
3334
hasClearIcon,
@@ -40,6 +41,7 @@ const useUtilityClasses = (ownerState) => {
4041
const slots = {
4142
root: [
4243
'root',
44+
expanded && 'expanded',
4345
focused && 'focused',
4446
fullWidth && 'fullWidth',
4547
hasClearIcon && 'hasClearIcon',
@@ -456,6 +458,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
456458
getOptionProps,
457459
value,
458460
dirty,
461+
expanded,
459462
id,
460463
popupOpen,
461464
focused,
@@ -473,6 +476,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
473476
const ownerState = {
474477
...props,
475478
disablePortal,
479+
expanded,
476480
focused,
477481
fullWidth,
478482
hasClearIcon,

packages/mui-material/src/Autocomplete/Autocomplete.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,4 +2895,39 @@ describe('<Autocomplete />', () => {
28952895
expect(container.querySelectorAll(`.${chipClasses.root}`)).to.have.length(0);
28962896
});
28972897
});
2898+
2899+
describe('should apply the expanded class', () => {
2900+
it('when listbox having options is opened', () => {
2901+
const { container } = render(
2902+
<Autocomplete
2903+
options={['one', 'two', 'three']}
2904+
renderInput={(params) => <TextField {...params} autoFocus />}
2905+
/>,
2906+
);
2907+
2908+
const root = container.querySelector(`.${classes.root}`);
2909+
2910+
expect(root).not.to.have.class(classes.expanded);
2911+
2912+
const textbox = screen.getByRole('combobox');
2913+
fireEvent.keyDown(textbox, { key: 'ArrowDown' }); // open listbox
2914+
2915+
expect(root).to.have.class(classes.expanded);
2916+
});
2917+
2918+
it('when listbox having no options is opened', () => {
2919+
const { container } = render(
2920+
<Autocomplete options={[]} renderInput={(params) => <TextField {...params} autoFocus />} />,
2921+
);
2922+
2923+
const root = container.querySelector(`.${classes.root}`);
2924+
2925+
expect(root).not.to.have.class(classes.expanded);
2926+
2927+
const textbox = screen.getByRole('combobox');
2928+
fireEvent.keyDown(textbox, { key: 'ArrowDown' }); // open listbox
2929+
2930+
expect(root).to.have.class(classes.expanded);
2931+
});
2932+
});
28982933
});

packages/mui-material/src/Autocomplete/autocompleteClasses.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface AutocompleteClasses {
66
root: string;
77
/** Styles applied to the root element if `fullWidth={true}`. */
88
fullWidth: string;
9+
/** State class applied to the root element if the listbox is displayed. */
10+
expanded: string;
911
/** State class applied to the root element if focused. */
1012
focused: string;
1113
/** Styles applied to the tag elements, e.g. the chips. */
@@ -60,6 +62,7 @@ export function getAutocompleteUtilityClass(slot: string): string {
6062

6163
const autocompleteClasses: AutocompleteClasses = generateUtilityClasses('MuiAutocomplete', [
6264
'root',
65+
'expanded',
6366
'fullWidth',
6467
'focused',
6568
'focusVisible',

0 commit comments

Comments
 (0)