Skip to content
Open
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
2 changes: 1 addition & 1 deletion modules/option-field/assets/build/checkbox.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '0a3ef3c1811be5626bbd');
<?php return array('dependencies' => array(), 'version' => '707e41f232e360f0cef0');
2 changes: 1 addition & 1 deletion modules/option-field/assets/build/checkbox.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ CheckboxData.prototype.setNode = function ( node ) {
this.nodes = node.getElementsByClassName(
'jet-form-builder__field checkboxes-field' );

this.rawName = this.nodes[ 0 ].name;
const firstNode = this.nodes[ 0 ];
this.rawName = firstNode.name || getCustomCheckboxInput( firstNode ).name;
this.name = getParsedName( this.rawName );
this.inputType = 'checkbox';

Expand Down Expand Up @@ -186,6 +187,9 @@ CheckboxData.prototype.processValueFormSingleChoice = function ( node, value ) {
const input = getCustomCheckboxInput( node );

if ( !node.checked && !input.value ) {
if ( this.nodes.length === 1 ) {
return;
}
value.push( null );
return;
}
Expand Down Expand Up @@ -241,4 +245,17 @@ CheckboxData.prototype.getCustomNodes = function () {
);
};

export default CheckboxData;
CheckboxData.prototype.removeCustomOption = function ( node ) {
// Keep one empty input when there are no predefined options.
if ( this.nodes.length === 1 ) {
const input = getCustomCheckboxInput( node );
node.checked = false;
input.value = '';
input.disabled = true;
return;
}

node.closest( '.custom-option' ).remove();
};

export default CheckboxData;
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ function sanitizeCheckbox( value, input ) {

// value has been removed
if ( undefined === currentValue ) {
currentNode.closest( '.custom-option' ).remove();
input.removeCustomOption( currentNode );
}
}
}

return;
return value;
}

const limit = Math.max( customNodes.length, copyValue.length );
Expand All @@ -58,7 +58,7 @@ function sanitizeCheckbox( value, input ) {
// value has been removed
if ( null === currentValue || undefined === currentValue ) {
if ( currentNode ) {
currentNode.closest( '.custom-option' ).remove();
input.removeCustomOption( currentNode );
}

continue;
Expand All @@ -76,6 +76,7 @@ function sanitizeCheckbox( value, input ) {
}

const inputNode = getCustomCheckboxInput( currentNode );
currentNode.checked = false !== currentValue;
inputNode.disabled = false === currentValue;

// add 1 if custom option not deselected, not with empty value
Expand All @@ -99,4 +100,4 @@ function sanitizeCheckbox( value, input ) {
return value.filter( val => null !== val );
}

export default sanitizeCheckbox;
export default sanitizeCheckbox;
26 changes: 14 additions & 12 deletions modules/option-field/blocks/checkbox/block-render.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,21 @@ public function render_option( $value, $option ): string {
}

protected function render_custom_option(): string {
$html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap custom-option">';
$class_name = $this->args['class_name'] ?? '';
$html = '<div class="jet-form-builder__field-wrap checkboxes-wrap checkradio-wrap custom-option">';
$has_options = ! empty( $this->args['field_options'] );

$name = esc_attr( $this->block_type->get_field_name() . $this->get_name_suffix() );
$checkbox = sprintf(
'<input %1$s value="">',
'<input %1$s %2$s value="">',
Builder_Helper::attrs(
array(
array( 'type', 'checkbox' ),
array( 'checked', 'checked' ),
array( 'checked', $has_options ? 'checked' : '' ),
array( 'data-field-name', esc_attr( $this->args['name'] ) ),
array( 'data-custom', 1 ),
array(
'class',
'jet-form-builder__field checkboxes-field checkradio-field' . (
$class_name ? " {$class_name}" : ''
),
),
)
)
),
$this->get_attributes_string_save()
);

$input = sprintf(
Expand Down Expand Up @@ -207,13 +202,20 @@ protected function render_custom_option(): string {
$html . $item_template . '</div>'
);

if ( ! $has_options ) {
return $html . $item_template . '</div>' . $html . $button . '</div>';
}

$html .= $button . '</div>';

return $html;
}

public function get_name_suffix(): string {
return count( $this->args['field_options'] ) > 1 ? '[]' : '';
return (
count( $this->args['field_options'] ) > 1
|| ! empty( $this->args['custom_option']['allow'] )
) ? '[]' : '';
}

/**
Expand Down
Loading