Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Make API clearer to consumers of hook
  • Loading branch information
getdave committed May 25, 2023
commit 3e6f682364932cbad39628e1ce37602ce8ed3922
6 changes: 4 additions & 2 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function LinkControl( {
setInternalControlValue,
setInternalURLInputValue,
setInternalTextInputValue,
setInternalSettingValue,
createSetInternalSettingValueHandler,
] = useInternalValue( value );

const valueHasChanges =
Expand Down Expand Up @@ -388,7 +388,9 @@ function LinkControl( {
handleSubmitWithEnter={ handleSubmitWithEnter }
value={ internalControlValue }
settings={ settings }
onChange={ setInternalSettingValue( settingsKeys ) }
onChange={ createSetInternalSettingValueHandler(
settingsKeys
) }
/>
) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}