Skip to content

Commit 865af12

Browse files
simisonmatticbot
authored andcommitted
Forms: add min/max options to number field (#41783)
Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13592887599 Upstream-Ref: Automattic/jetpack@e0b2fda
1 parent b6888a2 commit 865af12

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
This is an alpha version! The changes listed here are not final.
1111

12+
### Added
13+
- Forms: add min/max options to number field
14+
1215
### Changed
1316
- Contact Form: Updated editor styles for improved UI consistency and better alignment of form elements.
1417
- Forms: add accessible name field to advanced settings

dist/blocks/editor.asset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('jetpack-connection', 'lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '1bce0b68e02a4cfdd88e');
1+
<?php return array('dependencies' => array('jetpack-connection', 'lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => 'ff4ec9b76b23d86fbe89');

dist/blocks/editor.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/contact-form/class-contact-form-field.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public function __construct( $attributes, $content = null, $form = null ) {
113113
'labelcolor' => null,
114114
'labelfontsize' => null,
115115
'fieldfontsize' => null,
116+
'min' => null,
117+
'max' => null,
116118
),
117119
$attributes,
118120
'contact-field'
@@ -371,7 +373,18 @@ public function render() {
371373
$field_value = Contact_Form_Plugin::strip_tags( $this->value );
372374
$field_label = Contact_Form_Plugin::strip_tags( $field_label );
373375

374-
$rendered_field = $this->render_field( $field_type, $field_id, $field_label, $field_value, $field_class, $field_placeholder, $field_required, $field_required_text );
376+
$extra_attrs = array();
377+
378+
if ( $field_type === 'number' ) {
379+
if ( is_numeric( $this->get_attribute( 'min' ) ) ) {
380+
$extra_attrs['min'] = $this->get_attribute( 'min' );
381+
}
382+
if ( is_numeric( $this->get_attribute( 'max' ) ) ) {
383+
$extra_attrs['max'] = $this->get_attribute( 'max' );
384+
}
385+
}
386+
387+
$rendered_field = $this->render_field( $field_type, $field_id, $field_label, $field_value, $field_class, $field_placeholder, $field_required, $field_required_text, $extra_attrs );
375388

376389
/**
377390
* Filter the HTML of the Contact Form.
@@ -928,12 +941,13 @@ public function render_date_field( $id, $label, $value, $class, $required, $requ
928941
* @param bool $required - if the field is marked as required.
929942
* @param string $required_field_text - the text in the required text field.
930943
* @param string $placeholder - the field placeholder content.
944+
* @param array $extra_attrs - Extra attributes used in number field, namely `min` and `max`.
931945
*
932946
* @return string HTML
933947
*/
934-
public function render_number_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder ) {
948+
public function render_number_field( $id, $label, $value, $class, $required, $required_field_text, $placeholder, $extra_attrs = array() ) {
935949
$field = $this->render_label( 'number', $id, $label, $required, $required_field_text );
936-
$field .= $this->render_input_field( 'number', $id, $value, $class, $placeholder, $required );
950+
$field .= $this->render_input_field( 'number', $id, $value, $class, $placeholder, $required, $extra_attrs );
937951
return $field;
938952
}
939953

@@ -1039,10 +1053,11 @@ class="below-label__label ' . ( $this->is_error() ? ' form-error' : '' ) . '"
10391053
* @param string $placeholder - the field placeholder content.
10401054
* @param bool $required - if the field is marked as required.
10411055
* @param string $required_field_text - the text for a field marked as required.
1056+
* @param array $extra_attrs - extra attributes to be passed to render functions.
10421057
*
10431058
* @return string HTML
10441059
*/
1045-
public function render_field( $type, $id, $label, $value, $class, $placeholder, $required, $required_field_text ) {
1060+
public function render_field( $type, $id, $label, $value, $class, $placeholder, $required, $required_field_text, $extra_attrs = array() ) {
10461061
if ( ! $this->is_field_renderable( $type ) ) {
10471062
return null;
10481063
}
@@ -1130,7 +1145,7 @@ public function render_field( $type, $id, $label, $value, $class, $placeholder,
11301145
$field .= $this->render_consent_field( $id, $field_class );
11311146
break;
11321147
case 'number':
1133-
$field .= $this->render_number_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder );
1148+
$field .= $this->render_number_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder, $extra_attrs );
11341149
break;
11351150
default: // text field
11361151
$field .= $this->render_default_field( $id, $label, $value, $field_class, $required, $required_field_text, $field_placeholder, $type );

0 commit comments

Comments
 (0)