Skip to content

Commit 0fdea4a

Browse files
authored
Forms: handle single input forms (#45689)
* changelog * detect when form has a single input so to not print the big red error box * add 'auto' as a width option for fields
1 parent ed233eb commit 0fdea4a

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Forms: add "auto" width for fields and detect when a form has a single input in it to show prettier validation errors

projects/packages/forms/src/blocks/contact-form/editor.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@
106106
max-width: 75%;
107107
}
108108

109+
&.jetpack-field__width-auto {
110+
flex: 1 1 auto;
111+
}
112+
109113
@media (max-width: #{ (gb.$break-mobile) }) {
110114

111115
&[class*="jetpack-field__width-"] {

projects/packages/forms/src/blocks/shared/components/jetpack-field-width.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@wordpress/components';
66
import { __ } from '@wordpress/i18n';
77

8-
const PERCENTAGE_WIDTHS = [ 25, 33, 50, 75, 100 ];
8+
const PERCENTAGE_WIDTHS = [ 25, 33, 50, 75, 100, 'auto' ];
99

1010
export default function JetpackFieldWidth( { setAttributes, width } ) {
1111
return (
@@ -29,7 +29,7 @@ export default function JetpackFieldWidth( { setAttributes, width } ) {
2929
return (
3030
<ToggleGroupControlOption
3131
key={ widthValue }
32-
label={ `${ widthValue }%` }
32+
label={ widthValue === 'auto' ? __( 'Auto', 'jetpack-forms' ) : `${ widthValue }%` }
3333
value={ widthValue }
3434
/>
3535
);

projects/packages/forms/src/contact-form/class-contact-form.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,14 @@ public static function parse( $attributes, $content, $context = array() ) {
735735
$version
736736
);
737737

738-
$container_classes = array( 'wp-block-jetpack-contact-form-container' );
738+
$is_single_input_form = count( $form->fields ) === 1;
739+
740+
$container_classes = array( 'wp-block-jetpack-contact-form-container' );
741+
742+
if ( $is_single_input_form ) {
743+
$container_classes[] = 'is-single-input-form';
744+
}
745+
739746
$container_classes[] = self::get_block_alignment_class( $attributes );
740747
$container_classes_string = implode( ' ', $container_classes );
741748

@@ -906,7 +913,7 @@ class='" . esc_attr( $form_classes ) . "' $form_aria_label
906913

907914
if ( $is_multistep ) {
908915
$r = preg_replace( '/<div class="wp-block-jetpack-form-step-navigation__wrapper/', self::render_error_wrapper() . ' <div class="wp-block-jetpack-form-step-navigation__wrapper', $r, 1 );
909-
} elseif ( $has_submit_button_block ) {
916+
} elseif ( $has_submit_button_block && ! $is_single_input_form ) {
910917
// Place the error wrapper before the FIRST button block only to avoid duplicates (e.g., navigation buttons in multistep forms).
911918
// Replace only the first occurrence.
912919
$r = preg_replace( '/<div class="wp-block-jetpack-button/', self::render_error_wrapper() . ' <div class="wp-block-jetpack-button', $r, 1 );

projects/packages/forms/src/contact-form/css/grunion.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ that needs to mimic the input element styles */
434434
max-width: 75%;
435435
}
436436

437+
.wp-block-jetpack-contact-form .grunion-field-width-auto-wrap {
438+
flex: 1 1 auto;
439+
}
440+
437441
@media only screen and ( max-width: 480px ) {
438442

439443
.wp-block-jetpack-contact-form .grunion-field-wrap {
@@ -1217,12 +1221,18 @@ on production builds, the attributes are being reordered, causing side-effects
12171221
}
12181222

12191223
.contact-form__input-error.has-errors {
1224+
12201225
margin: 0.25rem 0;
12211226
display: flex;
12221227
opacity: 1;
12231228
max-height: unset;
12241229
transform: translateY(0);
12251230
transition: opacity 200ms cubic-bezier(0.34, 0.8, 0.34, 1), transform 200ms cubic-bezier(0.34, 0.8, 0.34, 1);
1231+
1232+
.is-single-input-form & {
1233+
position: absolute;
1234+
}
1235+
12261236
}
12271237

12281238
.contact-form [aria-invalid="true"]:not(fieldset),

0 commit comments

Comments
 (0)