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

Block Editor: LinkControl: Avoid lingering value properties from previous value #20020

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 29 additions & 4 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { noop, startsWith } from 'lodash';
import { noop, startsWith, pick, map } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -83,6 +83,18 @@ import LinkControlSearchInput from './search-input';
* @property {boolean=} showInitialSuggestions Whether to present initial suggestions immediately.
*/

/**
* Default settings configuration.
*
* @type {WPLinkControlSetting[]}
*/
const DEFAULT_SETTINGS = [
{
id: 'opensInNewTab',
title: __( 'Open in New Tab' ),
},
];

/**
* Renders a link control. A link control is a controlled input which maintains
* a value associated with a link (HTML anchor element) and relevant settings
Expand All @@ -92,7 +104,7 @@ import LinkControlSearchInput from './search-input';
*/
function LinkControl( {
value,
settings,
settings = DEFAULT_SETTINGS,
onChange = noop,
showInitialSuggestions,
} ) {
Expand Down Expand Up @@ -211,6 +223,19 @@ function LinkControl( {
setIsEditingLink( false );
}

/**
* Given a selected suggestion, returns the merged next value. The merged
* value inherits only settings properties from the previous value.
*
* @param {WPLinkControlValue} suggestion Next value suggestion.
*
* @return {WPLinkControlValue} Merged next value.
*/
const getNextValue = ( suggestion ) => ( {
...pick( value, map( settings, 'id' ) ),
...suggestion,
} );

// Effects
const getSearchHandler = useCallback(
( val, args ) => {
Expand Down Expand Up @@ -285,7 +310,7 @@ function LinkControl( {
) }
suggestion={ suggestion }
onClick={ () => {
onChange( { ...value, ...suggestion } );
onChange( getNextValue( suggestion ) );
stopEditing();
} }
isSelected={ index === selectedSuggestion }
Expand All @@ -311,7 +336,7 @@ function LinkControl( {
value={ inputValue }
onChange={ onInputChange }
onSelect={ ( suggestion ) => {
onChange( { ...value, ...suggestion } );
onChange( getNextValue( suggestion ) );
stopEditing();
} }
renderSuggestions={ renderSearchResults }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,7 @@ import { noop } from 'lodash';
import { __ } from '@wordpress/i18n';
import { ToggleControl } from '@wordpress/components';

const defaultSettings = [
{
id: 'opensInNewTab',
title: __( 'Open in New Tab' ),
},
];

const LinkControlSettingsDrawer = ( {
value,
onChange = noop,
settings = defaultSettings,
} ) => {
const LinkControlSettingsDrawer = ( { value, onChange = noop, settings } ) => {
if ( ! settings || ! settings.length ) {
return null;
}
Expand Down
48 changes: 48 additions & 0 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,54 @@ describe( 'Selecting links', () => {
);
expect( isExpectedFocusTarget ).toBe( true );
} );

it( 'should update a selected link value', ( done ) => {
// Regression: Previously, the behavior of updating a value was to merge
// the previous value with the newly selected suggestion. If the keys
// between the two objects were not the same, it could wrongly leave
// lingering values from the previous value.

const LinkControlConsumer = () => (
<LinkControl
value={ { url: 'https://wordpress.org', title: 'WordPress' } }
onChange={ ( nextValue ) => {
expect( nextValue ).toEqual( {
url: 'https://example.com',
} );

done();
} }
/>
);

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

// Toggle edit.
document
.querySelector( '.block-editor-link-control__search-item-action' )
.click();

// 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 );
} );
} );
} );

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