Skip to content

Commit d492b4c

Browse files
authored
[Social Link]: Remove block on DELETE if empty url (#50903)
* [Social Link]: Remove block on `DELETE` if empty url * also remove on BACKSPACE * add `onKeydown` on URLInput
1 parent 3d68509 commit d492b4c

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

packages/block-editor/src/components/url-input/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ _Required._ Called when the value changes. The second parameter is `null` unless
139139
}
140140
```
141141

142+
### `onKeydown`: `( event: KeyboardEvent ) => void`
143+
144+
A callback invoked on the keydown event.
145+
146+
- Required: No
147+
142148
### `label: String`
143149

144150
_Optional._ If this property is added, a label will be generated using label property as the content.

packages/block-editor/src/components/url-input/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ class URLInput extends Component {
267267
}
268268

269269
onKeyDown( event ) {
270+
this.props.onKeyDown?.( event );
270271
const { showSuggestions, selectedSuggestion, suggestions, loading } =
271272
this.state;
272273

packages/block-library/src/social-link/edit.js

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import classNames from 'classnames';
66
/**
77
* WordPress dependencies
88
*/
9+
import { DELETE, BACKSPACE } from '@wordpress/keycodes';
10+
import { useDispatch } from '@wordpress/data';
11+
912
import {
1013
InspectorControls,
1114
URLPopover,
1215
URLInput,
1316
useBlockProps,
17+
store as blockEditorStore,
1418
} from '@wordpress/block-editor';
1519
import { useState } from '@wordpress/element';
1620
import {
@@ -32,40 +36,60 @@ const SocialLinkURLPopover = ( {
3236
setAttributes,
3337
setPopover,
3438
popoverAnchor,
35-
} ) => (
36-
<URLPopover anchor={ popoverAnchor } onClose={ () => setPopover( false ) }>
37-
<form
38-
className="block-editor-url-popover__link-editor"
39-
onSubmit={ ( event ) => {
40-
event.preventDefault();
41-
setPopover( false );
42-
} }
39+
clientId,
40+
} ) => {
41+
const { removeBlock } = useDispatch( blockEditorStore );
42+
return (
43+
<URLPopover
44+
anchor={ popoverAnchor }
45+
onClose={ () => setPopover( false ) }
4346
>
44-
<div className="block-editor-url-input">
45-
<URLInput
46-
__nextHasNoMarginBottom
47-
value={ url }
48-
onChange={ ( nextURL ) =>
49-
setAttributes( { url: nextURL } )
50-
}
51-
placeholder={ __( 'Enter address' ) }
52-
disableSuggestions={ true }
47+
<form
48+
className="block-editor-url-popover__link-editor"
49+
onSubmit={ ( event ) => {
50+
event.preventDefault();
51+
setPopover( false );
52+
} }
53+
>
54+
<div className="block-editor-url-input">
55+
<URLInput
56+
__nextHasNoMarginBottom
57+
value={ url }
58+
onChange={ ( nextURL ) =>
59+
setAttributes( { url: nextURL } )
60+
}
61+
placeholder={ __( 'Enter address' ) }
62+
disableSuggestions={ true }
63+
onKeyDown={ ( event ) => {
64+
if (
65+
!! url ||
66+
event.defaultPrevented ||
67+
! [ BACKSPACE, DELETE ].includes(
68+
event.keyCode
69+
)
70+
) {
71+
return;
72+
}
73+
removeBlock( clientId );
74+
} }
75+
/>
76+
</div>
77+
<Button
78+
icon={ keyboardReturn }
79+
label={ __( 'Apply' ) }
80+
type="submit"
5381
/>
54-
</div>
55-
<Button
56-
icon={ keyboardReturn }
57-
label={ __( 'Apply' ) }
58-
type="submit"
59-
/>
60-
</form>
61-
</URLPopover>
62-
);
82+
</form>
83+
</URLPopover>
84+
);
85+
};
6386

6487
const SocialLinkEdit = ( {
6588
attributes,
6689
context,
6790
isSelected,
6891
setAttributes,
92+
clientId,
6993
} ) => {
7094
const { url, service, label, rel } = attributes;
7195
const { showLabels, iconColorValue, iconBackgroundColorValue } = context;
@@ -143,6 +167,7 @@ const SocialLinkEdit = ( {
143167
setAttributes={ setAttributes }
144168
setPopover={ setPopover }
145169
popoverAnchor={ popoverAnchor }
170+
clientId={ clientId }
146171
/>
147172
) }
148173
</Button>

0 commit comments

Comments
 (0)