Skip to content

Commit a50939c

Browse files
authored
Forms: Create new default block when pressing Enter on text inputs (#41177)
1 parent 818dafb commit a50939c

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: added
3+
4+
Forms: Create new default block when pressing Enter on text inputs

projects/packages/forms/src/blocks/contact-form/child-blocks.js

+1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ const editField = type => props => {
274274
id={ props.attributes.id }
275275
width={ props.attributes.width }
276276
attributes={ props.attributes }
277+
insertBlocksAfter={ props.insertBlocksAfter }
277278
/>
278279
);
279280
};

projects/packages/forms/src/blocks/contact-form/components/jetpack-field-datepicker.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useBlockProps } from '@wordpress/block-editor';
2+
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
23
import { SelectControl } from '@wordpress/components';
34
import { compose } from '@wordpress/compose';
45
import { __ } from '@wordpress/i18n';
@@ -34,7 +35,7 @@ const DATE_FORMATS = [
3435
];
3536

3637
const JetpackDatePicker = props => {
37-
const { attributes, clientId, isSelected, name, setAttributes } = props;
38+
const { attributes, clientId, isSelected, name, setAttributes, insertBlocksAfter } = props;
3839
const { id, label, required, requiredText, width, placeholder, dateFormat } = attributes;
3940

4041
useFormWrapper( { attributes, clientId, name } );
@@ -67,6 +68,12 @@ const JetpackDatePicker = props => {
6768
style={ fieldStyle }
6869
type="text"
6970
value={ placeholder }
71+
onKeyDown={ event => {
72+
if ( event.defaultPrevented || event.key !== 'Enter' ) {
73+
return;
74+
}
75+
insertBlocksAfter( createBlock( getDefaultBlockName() ) );
76+
} }
7077
/>
7178
</div>
7279

projects/packages/forms/src/blocks/contact-form/components/jetpack-field.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useBlockProps } from '@wordpress/block-editor';
2+
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
23
import { createHigherOrderComponent, compose } from '@wordpress/compose';
34
import { addFilter } from '@wordpress/hooks';
45
import clsx from 'clsx';
@@ -21,6 +22,7 @@ const JetpackField = props => {
2122
setAttributes,
2223
placeholder,
2324
width,
25+
insertBlocksAfter,
2426
} = props;
2527

2628
const { blockStyle, fieldStyle } = useJetpackFieldStyles( attributes );
@@ -50,6 +52,12 @@ const JetpackField = props => {
5052
style={ fieldStyle }
5153
type="text"
5254
value={ placeholder }
55+
onKeyDown={ event => {
56+
if ( event.defaultPrevented || event.key !== 'Enter' ) {
57+
return;
58+
}
59+
insertBlocksAfter( createBlock( getDefaultBlockName() ) );
60+
} }
5361
/>
5462
</div>
5563

0 commit comments

Comments
 (0)