Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try the link control in the link format #19462

Merged
merged 3 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ function LinkControl( {
) }
suggestion={ suggestion }
onClick={ () => {
stopEditing();
onChange( { ...value, ...suggestion } );
stopEditing();
} }
isSelected={ index === selectedSuggestion }
isURL={ manualLinkEntryTypes.includes(
Expand All @@ -318,8 +318,8 @@ function LinkControl( {
value={ inputValue }
onChange={ onInputChange }
onSelect={ ( suggestion ) => {
stopEditing();
onChange( { ...value, ...suggestion } );
stopEditing();
} }
renderSuggestions={ renderSearchResults }
fetchSuggestions={ getSearchHandler }
Expand Down
52 changes: 51 additions & 1 deletion packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { first, last, nth } from 'lodash';
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import { UP, DOWN, ENTER } from '@wordpress/keycodes';
/**
* Internal dependencies
Expand Down Expand Up @@ -766,6 +766,56 @@ describe( 'Selecting links', () => {
}
);
} );

it( 'does not forcefully regain focus if onChange handler had shifted it', () => {
// Regression: Previously, there had been issues where if `onChange`
// would programmatically shift focus, LinkControl would try to force it
// back, based on its internal logic to determine whether it had focus
// when finishing an edit occuring _before_ `onChange` having been run.
//
// See: https://github.com/WordPress/gutenberg/pull/19462

const LinkControlConsumer = () => {
const focusTarget = useRef();

return (
<>
<div tabIndex={ -1 } data-expected ref={ focusTarget } />
<LinkControl
onChange={ () => focusTarget.current.focus() }
/>
</>
);
};

act( () => {
render( <LinkControlConsumer />, container );
} );

// Change value.
const form = container.querySelector( 'form' );
const searchInput = container.querySelector(
'input[aria-label="URL"]'
);

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, {
target: { value: 'https://example.com' },
} );
} );
act( () => {
Simulate.keyDown( searchInput, { keyCode: ENTER } );
} );
act( () => {
Simulate.submit( form );
} );

const isExpectedFocusTarget = document.activeElement.hasAttribute(
'data-expected'
);
expect( isExpectedFocusTarget ).toBe( true );
} );
} );

describe( 'Addition Settings UI', () => {
Expand Down
Loading