Skip to content

Commit

Permalink
[Autocomplete] Add Mui-expanded class (#33312)
Browse files Browse the repository at this point in the history
Signed-off-by: Marija Najdova <[email protected]>
Co-authored-by: Michał Dudak <[email protected]>
Co-authored-by: Marija Najdova <[email protected]>
Co-authored-by: ZeeshanTamboli <[email protected]>
  • Loading branch information
4 people authored Feb 28, 2023
1 parent 0361cdd commit 3ca7a1b
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/pages/base/api/use-autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@
"default": "false",
"required": true
},
"expanded": {
"type": { "name": "boolean", "description": "boolean" },
"default": "false",
"required": true
},
"focused": {
"type": { "name": "boolean", "description": "boolean" },
"default": "false",
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/material-ui/api/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"classes": [
"root",
"fullWidth",
"expanded",
"focused",
"tag",
"tagSizeSmall",
Expand All @@ -129,7 +130,7 @@
"groupLabel",
"groupUl"
],
"globalClasses": { "focused": "Mui-focused" },
"globalClasses": { "expanded": "Mui-expanded", "focused": "Mui-focused" },
"name": "MuiAutocomplete"
},
"spread": true,
Expand Down
5 changes: 5 additions & 0 deletions docs/translations/api-docs/autocomplete/autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
"nodeName": "the root element",
"conditions": "<code>fullWidth={true}</code>"
},
"expanded": {
"description": "State class applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "the listbox is displayed"
},
"focused": {
"description": "State class applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"returnValueDescriptions": {
"anchorEl": "An HTML element that is used to set the position of the component.",
"dirty": "If <code>true</code>, the component input has some values.",
"expanded": "If <code>true</code>, the listbox is being displayed.",
"focused": "If <code>true</code>, the component is focused.",
"focusedTag": "Index of the focused tag for the component.",
"getClearProps": "Resolver for the <code>clear</code> button element's props.",
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ export interface UseAutocompleteReturnValue<
* @default false
*/
dirty: boolean;
/**
* If `true`, the listbox is being displayed.
* @default false
*/
expanded: boolean;
/**
* If `true`, the popup is open on the component.
* @default false
Expand Down
1 change: 1 addition & 0 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,7 @@ export default function useAutocomplete(props) {
inputValue,
value,
dirty,
expanded: popupOpen && anchorEl,
popupOpen,
focused: focused || focusedTag !== -1,
anchorEl,
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type AutocompleteOwnerState<
ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
> = AutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent> & {
disablePortal: boolean;
expanded: boolean;
focused: boolean;
fullWidth: boolean;
hasClearIcon: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const useUtilityClasses = (ownerState) => {
const {
classes,
disablePortal,
expanded,
focused,
fullWidth,
hasClearIcon,
Expand All @@ -40,6 +41,7 @@ const useUtilityClasses = (ownerState) => {
const slots = {
root: [
'root',
expanded && 'expanded',
focused && 'focused',
fullWidth && 'fullWidth',
hasClearIcon && 'hasClearIcon',
Expand Down Expand Up @@ -456,6 +458,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
getOptionProps,
value,
dirty,
expanded,
id,
popupOpen,
focused,
Expand All @@ -473,6 +476,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
const ownerState = {
...props,
disablePortal,
expanded,
focused,
fullWidth,
hasClearIcon,
Expand Down
35 changes: 35 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2895,4 +2895,39 @@ describe('<Autocomplete />', () => {
expect(container.querySelectorAll(`.${chipClasses.root}`)).to.have.length(0);
});
});

describe('should apply the expanded class', () => {
it('when listbox having options is opened', () => {
const { container } = render(
<Autocomplete
options={['one', 'two', 'three']}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);

const root = container.querySelector(`.${classes.root}`);

expect(root).not.to.have.class(classes.expanded);

const textbox = screen.getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' }); // open listbox

expect(root).to.have.class(classes.expanded);
});

it('when listbox having no options is opened', () => {
const { container } = render(
<Autocomplete options={[]} renderInput={(params) => <TextField {...params} autoFocus />} />,
);

const root = container.querySelector(`.${classes.root}`);

expect(root).not.to.have.class(classes.expanded);

const textbox = screen.getByRole('combobox');
fireEvent.keyDown(textbox, { key: 'ArrowDown' }); // open listbox

expect(root).to.have.class(classes.expanded);
});
});
});
3 changes: 3 additions & 0 deletions packages/mui-material/src/Autocomplete/autocompleteClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface AutocompleteClasses {
root: string;
/** Styles applied to the root element if `fullWidth={true}`. */
fullWidth: string;
/** State class applied to the root element if the listbox is displayed. */
expanded: string;
/** State class applied to the root element if focused. */
focused: string;
/** Styles applied to the tag elements, e.g. the chips. */
Expand Down Expand Up @@ -60,6 +62,7 @@ export function getAutocompleteUtilityClass(slot: string): string {

const autocompleteClasses: AutocompleteClasses = generateUtilityClasses('MuiAutocomplete', [
'root',
'expanded',
'fullWidth',
'focused',
'focusVisible',
Expand Down

0 comments on commit 3ca7a1b

Please sign in to comment.