From 5f28e672ed867ca8195796aa07caa3a616ca21ac Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 27 Jul 2023 13:34:39 +1200 Subject: [PATCH 1/3] Account for the undefined value set by spacing presets select list control in getPresetValueFromCustomValueControl --- .../src/components/spacing-sizes-control/test/utils.js | 10 +++++++++- .../src/components/spacing-sizes-control/utils.js | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/spacing-sizes-control/test/utils.js b/packages/block-editor/src/components/spacing-sizes-control/test/utils.js index d6b2fa2b4850b..a631cbfcff3ef 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/test/utils.js +++ b/packages/block-editor/src/components/spacing-sizes-control/test/utils.js @@ -49,7 +49,15 @@ describe( 'getCustomValueFromPreset', () => { } ); describe( 'getPresetValueFromCustomValue', () => { - const spacingSizes = [ { name: 'Small', slug: 20, size: '8px' } ]; + const spacingSizes = [ + { name: 'Default', slug: 'default', size: undefined }, + { name: 'Small', slug: 20, size: '8px' }, + ]; + it( 'should return undefined even if an undefined value exist in spacing sizes as occurs if spacingSizes has > 7 entries', () => { + expect( getPresetValueFromCustomValue( undefined, spacingSizes ) ).toBe( + undefined + ); + } ); it( 'should return original value if a string in spacing presets var format', () => { expect( getPresetValueFromCustomValue( diff --git a/packages/block-editor/src/components/spacing-sizes-control/utils.js b/packages/block-editor/src/components/spacing-sizes-control/utils.js index 340abb0322e9a..9bfe0d4855d8b 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/utils.js +++ b/packages/block-editor/src/components/spacing-sizes-control/utils.js @@ -101,6 +101,9 @@ export function getCustomValueFromPreset( value, spacingSizes ) { * @return {string} The preset value if it can be found. */ export function getPresetValueFromCustomValue( value, spacingSizes ) { + if ( ! value ) { + return value; + } // Return value as-is if it is already a preset; if ( isValueSpacingPreset( value ) || value === '0' ) { return value; From 360f11a627f1c921f7bb76ad168f7ef1760251e6 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 27 Jul 2023 16:37:00 +1200 Subject: [PATCH 2/3] combine checks for early return --- .../src/components/spacing-sizes-control/utils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/spacing-sizes-control/utils.js b/packages/block-editor/src/components/spacing-sizes-control/utils.js index 9bfe0d4855d8b..e2d77cd0c71e5 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/utils.js +++ b/packages/block-editor/src/components/spacing-sizes-control/utils.js @@ -101,11 +101,8 @@ export function getCustomValueFromPreset( value, spacingSizes ) { * @return {string} The preset value if it can be found. */ export function getPresetValueFromCustomValue( value, spacingSizes ) { - if ( ! value ) { - return value; - } - // Return value as-is if it is already a preset; - if ( isValueSpacingPreset( value ) || value === '0' ) { + // Return value as-is if it undefined or is already a preset; + if ( ! value || isValueSpacingPreset( value ) || value === '0' ) { return value; } From b32d3750b7afbaacf259936be1ee5ecd02c5dc10 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 27 Jul 2023 16:38:10 +1200 Subject: [PATCH 3/3] fix typo --- .../block-editor/src/components/spacing-sizes-control/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/spacing-sizes-control/utils.js b/packages/block-editor/src/components/spacing-sizes-control/utils.js index e2d77cd0c71e5..7ca0e1b6f660f 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/utils.js +++ b/packages/block-editor/src/components/spacing-sizes-control/utils.js @@ -101,7 +101,7 @@ export function getCustomValueFromPreset( value, spacingSizes ) { * @return {string} The preset value if it can be found. */ export function getPresetValueFromCustomValue( value, spacingSizes ) { - // Return value as-is if it undefined or is already a preset; + // Return value as-is if it is undefined or is already a preset, or '0'; if ( ! value || isValueSpacingPreset( value ) || value === '0' ) { return value; }