Skip to content

Commit

Permalink
1.1.1
Browse files Browse the repository at this point in the history
* Fixed issue that prevent RadioControl changes.
* Ensure to use the field default value if field value is not set.
* Make conditions able to work with checkboxes.
* Make async select load a set of default options.
  • Loading branch information
rubengc committed Mar 16, 2022
1 parent a642ad1 commit 27aaa75
Show file tree
Hide file tree
Showing 7 changed files with 2,802 additions and 3,593 deletions.
2 changes: 1 addition & 1 deletion dist/blocks.build.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gamipress-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: GamiPress - Gutenberg Blocks
* Plugin URI: https://github.com/rubengc/GamiPress-Gutenberg-Blocks
* Description: GamiPress blocks for Gutenberg.
* Version: 1.1.0
* Version: 1.1.1
* Author: GamiPress
* Author URI: https://gamipress.com/
* Text Domain: gamipress-gutenberg-blocks
Expand Down Expand Up @@ -53,7 +53,7 @@ public static function instance() {
*/
private function constants() {
// Plugin version
define( 'GAMIPRESS_GUTEMBERG_BLOCKS_VER', '1.1.0' );
define( 'GAMIPRESS_GUTEMBERG_BLOCKS_VER', '1.1.1' );

// Plugin path
define( 'GAMIPRESS_GUTEMBERG_BLOCKS_DIR', plugin_dir_path( __FILE__ ) );
Expand Down
6,319 changes: 2,756 additions & 3,563 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "gamipress-gutenberg-blocks",
"version": "1.1.0",
"version": "1.1.1",
"private": true,
"scripts": {
"start": "cgb-scripts start",
"build": "cgb-scripts build",
"eject": "cgb-scripts eject"
},
"dependencies": {
"cgb-scripts": "^1.18.1",
"npm": "^6.10.2",
"react-select": "^3.0.4"
"cgb-scripts": "^1.23.1",
"classnames": "^2.3.1",
"npm": "^8.5.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-select": "^5.2.2"
}
}
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ npm run build

### 1.1.1 ###

* Fixed issue that prevent RadioControl changes.
* Ensure to use the field default value if field value is not set.
* Make conditions able to work with checkboxes.
* Make async select load a set of default options.

### 1.1.0 ###
Expand Down
47 changes: 31 additions & 16 deletions src/block/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Select from 'react-select';
import AsyncSelect from 'react-select/async';

// MultiCheckboxControl
import MultiCheckboxControl from '../components/multicheckbox-control';
import MultiCheckboxControl from './../components/multicheckbox-control';

// WordPress dependencies
const { __ } = wp.i18n;
Expand Down Expand Up @@ -95,7 +95,7 @@ Object.keys(gamipress_blocks.shortcodes).map(function( slug ) {
if( field !== undefined ) {
field.id = field_id;

fields.push( gamipress_blocks_get_field_html( field, props ) );
fields.push( gamipress_blocks_get_field_html( field, props, shortcode ) );
}
});

Expand All @@ -121,11 +121,11 @@ Object.keys(gamipress_blocks.shortcodes).map(function( slug ) {
if( field !== undefined ) {
field.id = field_id;

fields.push( gamipress_blocks_get_field_html( field, props ) );
fields.push( gamipress_blocks_get_field_html( field, props, shortcode ) );
}
});

// Add fields to a PanelBoddy
// Add fields to a PanelBody
form.push(
<PanelBody title={ __( 'Settings' ) }>
{fields}
Expand Down Expand Up @@ -187,10 +187,11 @@ function gamipress_blocks_get_icon( icon ) {
*
* @param {Object} field Field object
* @param {Object} props Block properties
* @param {Object} shortcode Shortcode object
*
* @return {string} Field HTML
*/
function gamipress_blocks_get_field_html( field, props ) {
function gamipress_blocks_get_field_html( field, props, shortcode ) {

// Init field HTML
let field_html = [];
Expand All @@ -202,18 +203,22 @@ function gamipress_blocks_get_field_html( field, props ) {
let field_args = {};

// Check field visibility
if( ! gamipress_blocks_is_visible( field, attributes ) ) {
if( ! gamipress_blocks_is_visible( field, attributes, shortcode ) ) {
return '';
}

// Common field args
field_args.id = field.id;
field_args.label = field.name;
field_args.help = ( field.description !== undefined ? field.description : field.desc );
field_args.value = gamipress_blocks_get_field_value( field, attributes );
field_args.className = 'gamipress-field-type-' + field.type.replace( new RegExp('_', 'g'), '-' );
field_args.className = 'gamipress-field-type-' + field.type.replace( new RegExp('_', 'g'), '-' ) + ( field.classes !== undefined ? ' ' + field.classes : '' );
field_args.onChange = ( value ) => setAttributes({ [field.id]: value });

// Prevent to override value to radio controls
if( field.type !== 'radio' ) {
field_args.value = gamipress_blocks_get_field_value( field, attributes );
}

if( field.type === 'checkbox' ) {

// Checkbox
Expand Down Expand Up @@ -272,13 +277,13 @@ function gamipress_blocks_get_field_html( field, props ) {
[field.id]: values.join(',')
});

field_args.options = gamipress_blocks_get_field_options( field, true );
field_args.options = gamipress_blocks_get_field_options( field, true );
field_args.selected = ( field_args.value === undefined && field.default !== undefined ? field.default : field_args.value );

} else if( field.type === 'radio' && field.options !== undefined ) {

// Radio
field_args.options = gamipress_blocks_get_field_options( field, true );
field_args.options = gamipress_blocks_get_field_options( field, true );
field_args.selected = ( attributes[field.id] === undefined && field.default !== undefined ? field.default : attributes[field.id] );

}
Expand Down Expand Up @@ -467,6 +472,16 @@ function gamipress_blocks_get_field_value( field, attributes ) {

}

if( attributes[field.id] === undefined && field.default !== undefined ) {

// For checkbox, if default value is "yes", return true instead
if( field.type === 'checkbox' ) {
return field.default === 'yes';
}

return field.default;
}

// Common field value
return attributes[field.id];

Expand Down Expand Up @@ -627,10 +642,11 @@ function gamipress_blocks_load_options( field, value, callback ) {
*
* @param {Object} field Field object
* @param {Object} attributes Block attributes
* @param {Object} shortcode Shortcode object
*
* @return {boolean}
*/
function gamipress_blocks_is_visible( field, attributes ) {
function gamipress_blocks_is_visible( field, attributes, shortcode ) {

let is_visible = true;

Expand All @@ -655,7 +671,6 @@ function gamipress_blocks_is_visible( field, attributes ) {
// 'field_id' => 'value',
// )
let required_value = field.conditions[field_id];
let value = attributes[field_id];
let compare = '=';

// Check if condition has been defined as Object:
Expand All @@ -675,8 +690,6 @@ function gamipress_blocks_is_visible( field, attributes ) {

// Update required value and value
required_value = required_value.value;
value = attributes[field_id]; // Refresh values since field id has been updated

}

// Check if condition has been defined as Array:
Expand All @@ -696,10 +709,12 @@ function gamipress_blocks_is_visible( field, attributes ) {

// Update required value and value
required_value = required_value['value'];
value = attributes[field_id]; // Refresh values since field id has been updated

}

// Set the value to compare
let required_field = shortcode.fields[field_id];
let value = gamipress_blocks_get_field_value( required_field, attributes );

let comparison = undefined;

switch( compare ) {
Expand Down
9 changes: 2 additions & 7 deletions src/components/multicheckbox-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
* @author Ruben Garcia <[email protected]>
*/

/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -40,7 +35,7 @@ const MultiCheckboxControl = ( { label, className, selected, help, instanceId, o
};

return Array.isArray( options ) && options.length && (
<BaseControl label={ label } id={ id } help={ help } className={ classnames( className, 'components-multicheckbox-control' ) }>
<BaseControl label={ label } id={ id } help={ help } className={ className + 'components-multicheckbox-control' }>
{ options.map( ( option, index ) =>
<div
key={ `${ id }-${ index }` }
Expand All @@ -65,4 +60,4 @@ const MultiCheckboxControl = ( { label, className, selected, help, instanceId, o
);
}

export default MultiCheckboxControl;
export default MultiCheckboxControl;

0 comments on commit 27aaa75

Please sign in to comment.