Skip to content

Commit

Permalink
[Autocomplete] Listen for click on the root element (#36369)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Apr 4, 2023
1 parent 6776cb2 commit b4f0fa6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/mui-joy/src/Autocomplete/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,15 @@ describe('Joy <Autocomplete />', () => {
});
});

it('should open popup when clicked on the root element', () => {
const handleOpen = spy();
render(<Autocomplete onOpen={handleOpen} options={['one']} />);

const root = document.querySelector(`.${classes.root}`)!;
fireEvent.click(root);
expect(handleOpen.callCount).to.equal(1);
});

it('does not clear the textbox on Escape', () => {
const handleChange = spy();
render(
Expand Down
12 changes: 12 additions & 0 deletions packages/mui-joy/src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ const Autocomplete = React.forwardRef(function Autocomplete(
unstable_isActiveElementInListbox: defaultIsActiveElementInListbox,
});

const { onMouseDown: handleInputMouseDown } = getInputProps();
const { onClick: handleRootOnClick } = getRootProps();
const hasClearIcon = !disableClearable && !disabled && dirty && !readOnly;
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;

Expand Down Expand Up @@ -449,6 +451,16 @@ const Autocomplete = React.forwardRef(function Autocomplete(
externalForwardedProps: other,
ownerState,
getSlotProps: getRootProps,
additionalProps: {
onClick: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
if (handleRootOnClick) {
handleRootOnClick(event);
}
if (event.currentTarget === event.target && handleInputMouseDown) {
handleInputMouseDown(event as React.MouseEvent<HTMLInputElement, MouseEvent>);
}
},
},
});

const [SlotWrapper, wrapperProps] = useSlot('wrapper', {
Expand Down
7 changes: 7 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
const hasClearIcon = !disableClearable && !disabled && dirty && !readOnly;
const hasPopupIcon = (!freeSolo || forcePopupIcon === true) && forcePopupIcon !== false;

const { onMouseDown: handleInputMouseDown } = getInputProps();

// If you modify this, make sure to keep the `AutocompleteOwnerState` type in sync.
const ownerState = {
...props,
Expand Down Expand Up @@ -575,6 +577,11 @@ const Autocomplete = React.forwardRef(function Autocomplete(inProps, ref) {
ref: setAnchorEl,
className: classes.inputRoot,
startAdornment,
onClick: (event) => {
if (event.target === event.currentTarget) {
handleInputMouseDown(event);
}
},
...((hasClearIcon || hasPopupIcon) && {
endAdornment: (
<AutocompleteEndAdornment className={classes.endAdornment} ownerState={ownerState}>
Expand Down
17 changes: 17 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,23 @@ describe('<Autocomplete />', () => {
});
});

it('should open popup when clicked on the root element', () => {
const handleOpen = spy();
const ref = React.createRef();
render(
<Autocomplete
onOpen={handleOpen}
options={['one']}
renderInput={(params) => (
<TextField {...params} InputProps={{ ...params.InputProps, ref }} />
)}
/>,
);

fireEvent.click(ref.current);
expect(handleOpen.callCount).to.equal(1);
});

it('does not clear the textbox on Escape', () => {
const handleChange = spy();
render(
Expand Down

0 comments on commit b4f0fa6

Please sign in to comment.