Skip to content
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
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/add-forms-number-input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Forms block: add number input
1 change: 1 addition & 0 deletions projects/packages/forms/src/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `jetpack/field-date`
- `jetpack/field-email`
- `jetpack/field-name`
- `jetpack/field-number`
- `jetpack/field-option-checkbox`
- `jetpack/field-option-radio`
- `jetpack/field-radio`
Expand Down
27 changes: 27 additions & 0 deletions projects/packages/forms/src/blocks/contact-form/child-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ const FieldDefaults = {
},
transforms: {
to: [
{
type: 'block',
blocks: [ 'jetpack/field-number' ],
transform: attributes => createBlock( 'jetpack/field-number', attributes ),
},
{
type: 'block',
blocks: [ 'jetpack/field-text' ],
Expand Down Expand Up @@ -380,6 +385,28 @@ export const childBlocks = [
},
},
},
{
name: 'field-number',
settings: {
...FieldDefaults,
title: __( 'Number Input Field', 'jetpack-forms' ),
description: __( 'Collect numbers from site visitors.', 'jetpack-forms' ),
icon: renderMaterialIcon(
<Path
fill={ getIconColor() }
d="M12 7H4V8.5H12V7ZM19.75 17.25V10.75H4.25V17.25H19.75ZM5.75 15.75V12.25H18.25V15.75H5.75Z"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a follow up PR we should probably design a different icon?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! :-)

/>
),
edit: editField( 'number' ),
attributes: {
...FieldDefaults.attributes,
label: {
type: 'string',
default: __( 'Number', 'jetpack-forms' ),
},
},
},
},
{
name: 'field-name',
settings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public static function register_child_blocks() {
)
);

Blocks::jetpack_register_block(
'jetpack/field-number',
array(
'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_number' ),
)
);

$blocks_variation = apply_filters( 'jetpack_blocks_variation', \Automattic\Jetpack\Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' ) );
if ( 'beta' === $blocks_variation ) {
self::register_beta_blocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const JetpackField = props => {
setAttributes={ setAttributes }
placeholder={ placeholder }
attributes={ attributes }
hidePlaceholder={ type === 'number' }
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const getContactFieldBlockName = type => {
select: `${ prefix }/field-select`,
email: `${ prefix }/field-email`,
name: `${ prefix }/field-name`,
number: `${ prefix }/field-number`,
default: `${ prefix }/field-text`,
};
return fieldTypes[ type ] ? fieldTypes[ type ] : fieldTypes.default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ public function validate() {
$this->add_error( sprintf( __( '%s requires at least one selection', 'jetpack-forms' ), $field_label ) );
}
break;
case 'number':
// Make sure the number address is valid
if ( ! is_numeric( $field_value ) ) {
/* translators: %s is the name of a form field */
$this->add_error( sprintf( __( '%s requires a number', 'jetpack-forms' ), $field_label ) );
}
break;
default:
// Just check for presence of any text
if ( ! is_string( $field_value ) || ! strlen( trim( $field_value ) ) ) {
Expand Down Expand Up @@ -910,6 +917,25 @@ public function render_date_field( $id, $label, $value, $class, $required, $requ
return $field;
}

/**
* Return the HTML for the number field.
*
* @param int $id - the ID.
* @param string $label - the label.
* @param string $value - the value of the field.
* @param string $class - the field class.
* @param bool $required - if the field is marked as required.
* @param string $required_field_text - the text in the required text field.
* @param string $placeholder - the field placeholder content.
*
* @return string HTML
*/
public function render_number_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
$field = $this->render_label( 'number', $id, $label, $required, $required_field_text );
$field .= $this->render_input_field( 'number', $id, $value, $class, $placeholder, $required );
return $field;
}

/**
* Return the HTML for the default field.
*
Expand Down Expand Up @@ -1102,6 +1128,9 @@ public function render_field( $type, $id, $label, $value, $class, $placeholder,
case 'consent':
$field .= $this->render_consent_field( $id, $field_class );
break;
case 'number':
$field .= $this->render_number_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
break;
default: // text field
$field .= $this->render_default_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder, $type );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,19 @@ public static function gutenblock_render_field_file( $atts, $content ) {
return Contact_Form::parse_contact_field( $atts, $content );
}

/**
* Render the number field.
*
* @param array $atts - the block attributes.
* @param string $content - html content.
*
* @return string HTML for the file upload field.
*/
public static function gutenblock_render_field_number( $atts, $content ) {
$atts = self::block_attributes_to_shortcode_attributes( $atts, 'number' );
return Contact_Form::parse_contact_field( $atts, $content );
}

/**
* Add the 'Form Responses' menu item as a submenu of Feedback.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ public static function get_compiled_form_for_email( $feedback_id, $form ) {
* @return string
*/
public static function escape_and_sanitize_field_value( $value ) {
if ( $value === null ) {
return '';
}

$value = str_replace( array( '[', ']' ), array( '&#91;', '&#93;' ), $value );
return nl2br( wp_kses( $value, array() ) );
}
Expand Down Expand Up @@ -868,6 +872,9 @@ public static function get_default_label_from_type( $type ) {
case 'name':
$str = __( 'Name', 'jetpack-forms' );
break;
case 'number':
$str = __( 'Number', 'jetpack-forms' );
break;
case 'email':
$str = __( 'Email', 'jetpack-forms' );
break;
Expand Down
9 changes: 7 additions & 2 deletions projects/packages/forms/src/contact-form/css/grunion.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
.contact-form input[type='email'],
.contact-form input[type='tel'],
.contact-form input[type='url'],
.contact-form input[type='number'],
.contact-form textarea
) {
box-sizing: border-box;
Expand Down Expand Up @@ -203,11 +204,13 @@
.textwidget .contact-form input[type='email'],
.textwidget .contact-form input[type='tel'],
.textwidget .contact-form input[type='url'],
.textwidget .contact-form input[type='number'],
.textwidget .contact-form textarea,
.wp-block-column .contact-form input[type='text'],
.wp-block-column .contact-form input[type='email'],
.wp-block-column .contact-form input[type='tel'],
.wp-block-column .contact-form input[type='url'],
.wp-block-column .contact-form input[type='number'],
.wp-block-column .contact-form textarea {
width: 100%;
}
Expand Down Expand Up @@ -309,7 +312,8 @@
.contact-form input[type='text'],
.contact-form input[type='email'],
.contact-form input[type='tel'],
.contact-form input[type='url'] {
.contact-form input[type='url'],
.contact-form input[type='number'] {
width: 50%;
}

Expand All @@ -321,7 +325,8 @@
.wp-block-jetpack-contact-form input[type='text'],
.wp-block-jetpack-contact-form input[type='email'],
.wp-block-jetpack-contact-form input[type='tel'],
.wp-block-jetpack-contact-form input[type='url'] {
.wp-block-jetpack-contact-form input[type='url'],
.wp-block-jetpack-contact-form input[type='number'] {
width: 100%;
}
}
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-forms-number-input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Forms block: add number input
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// All Jetpack Form blocks to extend
export const JETPACK_FORM_CHILDREN_BLOCKS = [
'jetpack/field-name',
'jetpack/field-number',
'jetpack/field-email',
'jetpack/field-text',
'jetpack/field-textarea',
Expand Down