Skip to content

Commit

Permalink
[Autocomplete] Allow tooltip text selection (#36503)
Browse files Browse the repository at this point in the history
Co-authored-by: Amirreza safehian <[email protected]>
  • Loading branch information
safeamiiir and Amirreza safehian committed May 12, 2023
1 parent e0f6979 commit 5d8c58a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,13 +1010,21 @@ export default function useAutocomplete(props) {

// Prevent input blur when interacting with the combobox
const handleMouseDown = (event) => {
// Prevent focusing the input if click is anywhere outside the Autocomplete
if (!event.currentTarget.contains(event.target)) {
return;
}
if (event.target.getAttribute('id') !== id) {
event.preventDefault();
}
};

// Focus the input when interacting with the combobox
const handleClick = () => {
const handleClick = (event) => {
// Prevent focusing the input if click is anywhere outside the Autocomplete
if (!event.currentTarget.contains(event.target)) {
return;
}
inputRef.current.focus();

if (
Expand Down
36 changes: 36 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Autocomplete, {
} from '@mui/material/Autocomplete';
import { paperClasses } from '@mui/material/Paper';
import { iconButtonClasses } from '@mui/material/IconButton';
import InputAdornment from '@mui/material/InputAdornment';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/system/Box';

function checkHighlightIs(listbox, expected) {
Expand Down Expand Up @@ -1938,6 +1940,40 @@ describe('<Autocomplete />', () => {
fireEvent.mouseDown(textbox);
expect(textbox).to.have.attribute('aria-expanded', 'true');
});

it('should not focus when tooltip clicked', () => {
const { getByRole, getByText } = render(
<Autocomplete
options={['one', 'two', 'three']}
renderInput={(params) => {
return (
<TextField
{...params}
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="end">
<Tooltip title="tooltip" open>
<div>ICON</div>
</Tooltip>
</InputAdornment>
),
}}
/>
);
}}
/>,
);

const textbox = getByRole('combobox');
const tooltip = getByText('tooltip');

act(() => {
fireEvent.click(tooltip);
});

expect(textbox).not.toHaveFocus();
});
});

describe('controlled', () => {
Expand Down

0 comments on commit 5d8c58a

Please sign in to comment.