-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename ContextControls to BlockBindingControls * Add "Show label" control
- Loading branch information
1 parent
f6133bd
commit c20f178
Showing
8 changed files
with
211 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/blocks/remote-data-container/components/block-binding-controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { CheckboxControl, SelectControl } from '@wordpress/components'; | ||
|
||
import { TEXT_FIELD_TYPES } from '@/blocks/remote-data-container/config/constants'; | ||
|
||
interface BlockBindingFieldControlProps { | ||
availableBindings: AvailableBindings; | ||
fieldTypes: string[]; | ||
label: string; | ||
target: string; | ||
updateFieldBinding: ( target: string, field: string ) => void; | ||
value: string; | ||
} | ||
|
||
export function BlockBindingFieldControl( props: BlockBindingFieldControlProps ) { | ||
const { availableBindings, fieldTypes, label, target, updateFieldBinding, value } = props; | ||
const options = Object.entries( availableBindings ) | ||
.filter( ( [ _key, mapping ] ) => fieldTypes.includes( mapping.type ) ) | ||
.map( ( [ key, mapping ] ) => { | ||
return { label: mapping.name, value: key }; | ||
} ); | ||
|
||
return ( | ||
<SelectControl | ||
label={ label } | ||
name={ target } | ||
options={ [ { label: 'Select a field', value: '' }, ...options ] } | ||
onChange={ ( field: string ) => updateFieldBinding( target, field ) } | ||
value={ value } | ||
/> | ||
); | ||
} | ||
|
||
interface BlockBindingControlsProps { | ||
attributes: ContextInnerBlockAttributes; | ||
availableBindings: AvailableBindings; | ||
blockName: string; | ||
removeBinding: ( target: string ) => void; | ||
updateBinding: ( target: string, args: Omit< RemoteDataBlockBindingArgs, 'name' > ) => void; | ||
} | ||
|
||
export function BlockBindingControls( props: BlockBindingControlsProps ) { | ||
const { attributes, availableBindings, blockName, removeBinding, updateBinding } = props; | ||
const contentArgs = attributes.metadata?.bindings?.content?.args; | ||
const contentField = contentArgs?.field ?? ''; | ||
const imageAltField = attributes.metadata?.bindings?.image_alt?.args?.field ?? ''; | ||
const imageUrlField = attributes.metadata?.bindings?.image_url?.args?.field ?? ''; | ||
|
||
function updateFieldBinding( target: string, field: string ): void { | ||
if ( ! field ) { | ||
removeBinding( target ); | ||
return; | ||
} | ||
|
||
const args = attributes.metadata?.bindings?.[ target ]?.args ?? {}; | ||
updateBinding( target, { ...args, field } ); | ||
} | ||
|
||
function updateFieldLabel( showLabel: boolean ): void { | ||
if ( ! contentField ) { | ||
// Form input should be disabled in this state, but check anyway. | ||
return; | ||
} | ||
|
||
const label = showLabel | ||
? Object.entries( availableBindings ).find( ( [ key ] ) => key === contentField )?.[ 1 ]?.name | ||
: undefined; | ||
updateBinding( 'content', { ...contentArgs, field: contentField, label } ); | ||
} | ||
|
||
switch ( blockName ) { | ||
case 'core/heading': | ||
case 'core/paragraph': | ||
return ( | ||
<> | ||
<BlockBindingFieldControl | ||
availableBindings={ availableBindings } | ||
fieldTypes={ TEXT_FIELD_TYPES } | ||
label="Content" | ||
target="content" | ||
updateFieldBinding={ updateFieldBinding } | ||
value={ contentField } | ||
/> | ||
<CheckboxControl | ||
checked={ Boolean( contentArgs?.label ) } | ||
disabled={ ! contentField } | ||
label="Show label" | ||
name="show_label" | ||
onChange={ updateFieldLabel } | ||
/> | ||
</> | ||
); | ||
|
||
case 'core/image': | ||
return ( | ||
<> | ||
<BlockBindingFieldControl | ||
availableBindings={ availableBindings } | ||
fieldTypes={ [ 'image_url' ] } | ||
label="Image URL" | ||
target="image_url" | ||
updateFieldBinding={ updateFieldBinding } | ||
value={ imageUrlField } | ||
/> | ||
<BlockBindingFieldControl | ||
availableBindings={ availableBindings } | ||
fieldTypes={ [ 'image_alt' ] } | ||
label="Image alt text" | ||
target="image_alt" | ||
updateFieldBinding={ updateFieldBinding } | ||
value={ imageAltField } | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
return null; | ||
} |
65 changes: 0 additions & 65 deletions
65
src/blocks/remote-data-container/components/context-controls.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.