-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AutocompleteUI: Close popup when click happens outside of the popover. (
#44795) * AutocompleteUI: Close popup when click happens outside of the popover. This prevents the popover from appearing above UI elements like the Patterns Explore Modal window. * AutocompleterUI: Rerun prettier as it failed to run on save. * AutocompleterUI: Update custom hook dependency list and comment. Co-authored-by: Marco Ciampini <[email protected]> * AutocompleterUI: Update comment to describe ref not being set. Co-authored-by: Marco Ciampini <[email protected]> * Autocompleter: Change click outside handler to be reset prop and not a new function on render. * Autocompleter: Move ref over to Popover component instead of inner contents. * AutocompleterUI: Add changelog entry describing bugfix. * AutocompleterUI: Add unit test for tracking on click outside behavior. Co-authored-by: Marco Ciampini <[email protected]>
- Loading branch information
1 parent
c97cf39
commit 64c1a15
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getAutoCompleterUI } from '../autocompleter-ui'; | ||
|
||
describe( 'AutocompleterUI', () => { | ||
describe( 'click outside behavior', () => { | ||
it( 'should call reset function when a click on another element occurs', async () => { | ||
const user = userEvent.setup( { | ||
advanceTimers: jest.advanceTimersByTime, | ||
} ); | ||
|
||
const resetSpy = jest.fn(); | ||
|
||
const autocompleter = { | ||
name: 'fruit', | ||
// The prefix that triggers this completer | ||
triggerPrefix: '~', | ||
// Mock useItems function to return a autocomplete item. | ||
useItems: () => { | ||
return [ | ||
[ | ||
{ | ||
isDisabled: false, | ||
key: 'Apple', | ||
value: 'Apple', | ||
label: ( | ||
<span> | ||
<span className="icon">🍎</span> | ||
{ 'Apple' } | ||
</span> | ||
), | ||
}, | ||
], | ||
]; | ||
}, | ||
}; | ||
|
||
const AutocompleterUI = getAutoCompleterUI( autocompleter ); | ||
|
||
const OtherElement = <div>Other Element</div>; | ||
|
||
const Container = () => { | ||
const contentRef = useRef(); | ||
|
||
return ( | ||
<div> | ||
<AutocompleterUI | ||
className={ 'test' } | ||
filterValue={ '~' } | ||
instanceId={ '1' } | ||
listBoxId={ '1' } | ||
selectedIndex={ 0 } | ||
onChangeOptions={ () => {} } | ||
onSelect={ () => {} } | ||
value={ { visual: '🍎', name: 'Apple', id: 1 } } | ||
contentRef={ contentRef } | ||
reset={ resetSpy } | ||
/> | ||
{ OtherElement } | ||
</div> | ||
); | ||
}; | ||
|
||
render( <Container /> ); | ||
|
||
// Click on autocompleter. | ||
await user.click( screen.getByText( 'Apple' ) ); | ||
|
||
expect( resetSpy ).toHaveBeenCalledTimes( 0 ); | ||
|
||
// Click on other element out side of the tree. | ||
await user.click( screen.getByText( 'Other Element' ) ); | ||
|
||
expect( resetSpy ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
} ); |