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

Add keyboardReturn submit button back to LinkControl #52620

Merged
merged 7 commits into from
Sep 6, 2023
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
13 changes: 13 additions & 0 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ENTER } from '@wordpress/keycodes';
import { isShallowEqualObjects } from '@wordpress/is-shallow-equal';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';
import { keyboardReturn } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -355,6 +356,7 @@ function LinkControl( {
className={ classnames( {
'block-editor-link-control__search-input-wrapper': true,
'has-text-control': showTextControl,
'has-actions': showActions,
} ) }
>
{ showTextControl && (
Expand Down Expand Up @@ -388,6 +390,17 @@ function LinkControl( {
}
hideLabelFromVision={ ! showTextControl }
/>
{ ! showActions && (
<div className="block-editor-link-control__search-enter">
<Button
onClick={ isDisabled ? noop : handleSubmit }
label={ __( 'Submit' ) }
icon={ keyboardReturn }
className="block-editor-link-control__search-submit"
aria-disabled={ isDisabled }
/>
</div>
) }
</div>
{ errorMessage && (
<Notice
Expand Down
21 changes: 19 additions & 2 deletions packages/block-editor/src/components/link-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ $preview-image-height: 140px;
}

// Provides positioning context for search actions
.block-editor-link-control__search-input-container {
.block-editor-link-control__search-input-container,
.block-editor-link-control__search-input-wrapper {
position: relative;
}

Expand Down Expand Up @@ -79,6 +80,17 @@ $preview-image-height: 140px;
margin: -$grid-unit-20 * 0.5 $grid-unit-20 $grid-unit-20; // negative margin to bring the error a bit closer to the button
}

.block-editor-link-control__search-enter {
position: absolute;
right: 19px; // specific to place the button properly.
top: 3px;

svg {
position: relative;
top: -2px; // the icon itself is not centered within the bounds; this centers it.
}
}

.block-editor-link-control__search-actions {
display: flex;
flex-direction: row-reverse; // put "Cancel" on the left but retain DOM order.
Expand Down Expand Up @@ -452,10 +464,15 @@ $preview-image-height: 140px;
left: auto;
bottom: auto;
top: calc(50% - #{$spinner-size} / 2);
right: $grid-unit-20;
right: $grid-unit-50;
}
}

.block-editor-link-control .block-editor-link-control__search-input-wrapper.has-actions .components-spinner {
top: calc(50% + #{$spinner-size} / 4); // Add top spacing because this input has a visual label.
right: $grid-unit-15;
}

.block-editor-link-control__search-item-action {
margin-left: auto; // push to far right hand side
flex-shrink: 0;
Expand Down
89 changes: 86 additions & 3 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,6 @@ describe( 'Manual link entry', () => {
name: 'Save',
} );

// debug the UI state
// screen.debug();

// Verify the submission UI is disabled.
expect( submitButton ).toBeVisible();
expect( submitButton ).toHaveAttribute(
Expand Down Expand Up @@ -939,6 +936,92 @@ describe( 'Manual link entry', () => {
} );
} );

describe( 'Link submission', () => {
it( 'should show a submit button when creating a link', async () => {
const user = userEvent.setup();

const LinkControlConsumer = () => {
const [ link, setLink ] = useState( {} );

return <LinkControl value={ link } onChange={ setLink } />;
};

render( <LinkControlConsumer /> );

const searchInput = screen.getByRole( 'combobox', {
name: 'Link',
} );

const submitButton = screen.getByRole( 'button', {
name: 'Submit',
} );

expect( submitButton ).toBeVisible();
expect( submitButton ).toHaveAttribute( 'aria-disabled', 'true' );

// Click the button and check it's not possible to prematurely submit the link.
await user.click( submitButton );

expect( searchInput ).toBeVisible();
expect( submitButton ).toBeVisible();

await user.type( searchInput, 'https://wordpress.org' );

expect( submitButton ).toHaveAttribute( 'aria-disabled', 'false' );
} );

it( 'should show a submit button when editing a link', async () => {
const user = userEvent.setup();

const LinkControlConsumer = () => {
const [ link, setLink ] = useState( fauxEntitySuggestions[ 0 ] );

return (
<LinkControl
forceIsEditingLink
value={ link }
onChange={ setLink }
/>
);
};

render( <LinkControlConsumer /> );

const searchInput = screen.getByRole( 'combobox', {
name: 'Link',
} );

const createSubmitButton = screen.queryByRole( 'button', {
name: 'Submit',
} );

// Check the submit button for "creation" of links is not displayed.
expect( createSubmitButton ).not.toBeInTheDocument();

const editSubmitButton = screen.getByRole( 'button', {
name: 'Save',
} );

expect( editSubmitButton ).toBeVisible();
expect( editSubmitButton ).toHaveAttribute( 'aria-disabled', 'true' );

// Click the button and check it's not possible to prematurely submit the link.
await user.click( editSubmitButton );

expect( searchInput ).toBeVisible();
expect( editSubmitButton ).toBeVisible();

await user.type( searchInput, '#appendtolinktext' );

// As typing triggers the search handler, we need to wait for the
// search results to be returned. We can use the presence of the
// search results listbox as a proxy for this.
expect( await screen.findByRole( 'listbox' ) ).toBeVisible();

expect( editSubmitButton ).toHaveAttribute( 'aria-disabled', 'false' );
} );
} );

describe( 'Default search suggestions', () => {
it( 'should display a list of initial search suggestions when there is no search value or suggestions', async () => {
render( <LinkControl showInitialSuggestions /> );
Expand Down