Skip to content

Commit

Permalink
Make API clearer to consumers of hook
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed May 25, 2023
1 parent 4f16173 commit 3e6f682
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
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,
];
}

0 comments on commit 3e6f682

Please sign in to comment.