Skip to content

Commit

Permalink
Block Editor: LinkControl: Incorporate settings in edit state
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Feb 5, 2020
1 parent 525dd41 commit 5596566
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
19 changes: 5 additions & 14 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { focus } from '@wordpress/dom';
/**
* Internal dependencies
*/
import LinkControlSettingsDrawer from './settings-drawer';
import LinkControlSearchItem from './search-item';
import LinkControlSearchInput from './search-input';

Expand Down Expand Up @@ -150,10 +149,6 @@ function LinkControl( {
setInputValue( val );
};

const resetInput = () => {
setInputValue( '' );
};

const handleDirectEntry = ( val ) => {
let type = 'URL';

Expand Down Expand Up @@ -308,15 +303,16 @@ function LinkControl( {
>
{ isEditingLink || ! value ? (
<LinkControlSearchInput
value={ inputValue }
inputValue={ inputValue }
value={ value }
onChange={ onInputChange }
onSelect={ ( suggestion ) => {
onChange( { ...value, ...suggestion } );
onSelect={ ( nextValue ) => {
onChange( nextValue );
stopEditing();
} }
settings={ settings }
renderSuggestions={ renderSearchResults }
fetchSuggestions={ getSearchHandler }
onReset={ resetInput }
showInitialSuggestions={ showInitialSuggestions }
/>
) : (
Expand Down Expand Up @@ -359,11 +355,6 @@ function LinkControl( {
{ __( 'Edit' ) }
</Button>
</div>
<LinkControlSettingsDrawer
value={ value }
settings={ settings }
onChange={ onChange }
/>
</Fragment>
) }
</div>
Expand Down
44 changes: 32 additions & 12 deletions packages/block-editor/src/components/link-control/search-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { close } from '@wordpress/icons';
import { chevronDown, chevronUp } from '@wordpress/icons';

/**
* Internal dependencies
*/
import { URLInput } from '../';
import LinkControlSettingsDrawer from './settings-drawer';

const handleLinkControlOnKeyDown = ( event ) => {
const { keyCode } = event;
Expand All @@ -32,14 +33,17 @@ const handleLinkControlOnKeyPress = ( event ) => {

const LinkControlSearchInput = ( {
value,
inputValue,
onChange,
onSelect,
settings,
renderSuggestions,
fetchSuggestions,
onReset,
showInitialSuggestions,
} ) => {
const [ selectedSuggestion, setSelectedSuggestion ] = useState();
const [ isSettingsExpanded, setIsSettingsExpanded ] = useState( false );
const [ pendingSettingsChanges, setPendingSettingsChanges ] = useState();

const selectItemHandler = ( selection, suggestion ) => {
onChange( selection );
Expand All @@ -51,15 +55,19 @@ const LinkControlSearchInput = ( {
event.preventDefault();

// Interpret the selected value as either the selected suggestion, if
// exists, or otherwise the current input value as entered.
onSelect( selectedSuggestion || { url: value } );
// exists, or otherwise the current input value as entered. Settings
// changes are held in state and merged into the changed value.
onSelect( {
...pendingSettingsChanges,
...( selectedSuggestion || { url: inputValue } ),
} );
}

return (
<form onSubmit={ selectSuggestionOrCurrentInputValue }>
<URLInput
className="block-editor-link-control__search-input"
value={ value }
value={ inputValue }
onChange={ selectItemHandler }
onKeyDown={ ( event ) => {
if ( event.keyCode === ENTER ) {
Expand All @@ -74,15 +82,27 @@ const LinkControlSearchInput = ( {
__experimentalHandleURLSuggestions={ true }
__experimentalShowInitialSuggestions={ showInitialSuggestions }
/>

<Button
disabled={ ! value.length }
type="reset"
label={ __( 'Reset' ) }
icon={ close }
className="block-editor-link-control__search-reset"
onClick={ onReset }
isPrimary
type="submit"
className="block-editor-link-control__search-action"
>
{ __( 'Apply' ) }
</Button>
<Button
className="block-editor-link-control__search-action"
icon={ isSettingsExpanded ? chevronUp : chevronDown }
label={ __( 'Link settings' ) }
onClick={ () => setIsSettingsExpanded( ! isSettingsExpanded ) }
aria-expanded={ isSettingsExpanded }
/>
{ isSettingsExpanded && (
<LinkControlSettingsDrawer
value={ { ...value, ...pendingSettingsChanges } }
settings={ settings }
onChange={ setPendingSettingsChanges }
/>
) }
</form>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { noop } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { ToggleControl } from '@wordpress/components';
import { CheckboxControl } from '@wordpress/components';

const defaultSettings = [
{
Expand All @@ -33,7 +33,7 @@ const LinkControlSettingsDrawer = ( {
};

const theSettings = settings.map( ( setting ) => (
<ToggleControl
<CheckboxControl
className="block-editor-link-control__setting"
key={ setting.id }
label={ setting.title }
Expand Down

0 comments on commit 5596566

Please sign in to comment.