From 3e6f682364932cbad39628e1ce37602ce8ed3922 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 25 May 2023 11:40:40 +0100 Subject: [PATCH] Make API clearer to consumers of hook --- .../src/components/link-control/index.js | 6 ++- .../link-control/use-internal-value.js | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/block-editor/src/components/link-control/index.js b/packages/block-editor/src/components/link-control/index.js index 3d93a8587ab30b..bbb56ab72db8de 100644 --- a/packages/block-editor/src/components/link-control/index.js +++ b/packages/block-editor/src/components/link-control/index.js @@ -146,7 +146,7 @@ function LinkControl( { setInternalControlValue, setInternalURLInputValue, setInternalTextInputValue, - setInternalSettingValue, + createSetInternalSettingValueHandler, ] = useInternalValue( value ); const valueHasChanges = @@ -388,7 +388,9 @@ function LinkControl( { handleSubmitWithEnter={ handleSubmitWithEnter } value={ internalControlValue } settings={ settings } - onChange={ setInternalSettingValue( settingsKeys ) } + onChange={ createSetInternalSettingValueHandler( + settingsKeys + ) } /> ) } diff --git a/packages/block-editor/src/components/link-control/use-internal-value.js b/packages/block-editor/src/components/link-control/use-internal-value.js index f0ea168e47bfc1..ac58c05b10a870 100644 --- a/packages/block-editor/src/components/link-control/use-internal-value.js +++ b/packages/block-editor/src/components/link-control/use-internal-value.js @@ -31,29 +31,30 @@ export default function useInternalValue( value ) { } ); }; - const setInternalSettingValue = ( settingsKeys ) => ( nextValue ) => { - // Only apply settings values which are defined in the settings prop. - const settingsUpdates = Object.keys( nextValue ).reduce( - ( acc, key ) => { - if ( settingsKeys.includes( key ) ) { - acc[ key ] = nextValue[ key ]; - } - return acc; - }, - {} - ); - - setInternalValue( { - ...internalValue, - ...settingsUpdates, - } ); - }; + const createSetInternalSettingValueHandler = + ( settingsKeys ) => ( nextValue ) => { + // Only apply settings values which are defined in the settings prop. + const settingsUpdates = Object.keys( nextValue ).reduce( + ( acc, key ) => { + if ( settingsKeys.includes( key ) ) { + acc[ key ] = nextValue[ key ]; + } + return acc; + }, + {} + ); + + setInternalValue( { + ...internalValue, + ...settingsUpdates, + } ); + }; return [ internalValue, setInternalValue, setInternalURLInputValue, setInternalTextInputValue, - setInternalSettingValue, + createSetInternalSettingValueHandler, ]; }