♻️ SImplify InputSettingsBlock logic

This commit is contained in:
Philipp Stracker 2025-01-21 18:35:17 +01:00
parent e82f4c5f88
commit c7b80b6cd5
No known key found for this signature in database
3 changed files with 32 additions and 55 deletions

View file

@ -97,15 +97,6 @@
margin-left: 5px; margin-left: 5px;
} }
.ppcp-r-settings-block__action {
display: flex;
align-items: center;
.components-flex {
row-gap: 0;
}
}
+ .ppcp-r-settings-block:not(.no-gap) { + .ppcp-r-settings-block:not(.no-gap) {
margin-top: var(--block-separator-gap, 32px); margin-top: var(--block-separator-gap, 32px);
padding-top: var(--block-separator-gap, 32px); padding-top: var(--block-separator-gap, 32px);

View file

@ -1,4 +1,5 @@
import { TextControl } from '@wordpress/components'; import { TextControl } from '@wordpress/components';
import SettingsBlock from './SettingsBlock'; import SettingsBlock from './SettingsBlock';
import { import {
Title, Title,
@ -7,55 +8,40 @@ import {
SupplementaryLabel, SupplementaryLabel,
} from './SettingsBlockElements'; } from './SettingsBlockElements';
const DEFAULT_ELEMENT_ORDER = [ 'title', 'action', 'description' ];
const ELEMENT_RENDERERS = {
title: ( { title, supplementaryLabel } ) => (
<Title>
{ title }
{ supplementaryLabel && (
<SupplementaryLabel>{ supplementaryLabel }</SupplementaryLabel>
) }
</Title>
),
action: ( { actionProps } ) => (
<Action>
<TextControl
className="ppcp-r-vertical-text-control"
placeholder={ actionProps?.placeholder }
value={ actionProps?.value }
onChange={ ( newValue ) =>
actionProps?.callback( actionProps?.key, newValue )
}
/>
</Action>
),
description: ( { description } ) => (
<Description>{ description }</Description>
),
};
const InputSettingsBlock = ( { const InputSettingsBlock = ( {
title, title,
description, description,
supplementaryLabel, supplementaryLabel,
order = DEFAULT_ELEMENT_ORDER, showDescriptionFirst = false,
actionProps = {},
...props ...props
} ) => ( } ) => {
<SettingsBlock { ...props } className="ppcp-r-settings-block__input"> const TheDescription = <Description>{ description }</Description>;
{ order.map( ( elementKey ) => {
const RenderElement = ELEMENT_RENDERERS[ elementKey ]; return (
return RenderElement ? ( <SettingsBlock { ...props } className="ppcp-r-settings-block__input">
<RenderElement <Title>
key={ elementKey } { title }
title={ title } { supplementaryLabel && (
description={ description } <SupplementaryLabel>
supplementaryLabel={ supplementaryLabel } { supplementaryLabel }
actionProps={ props.actionProps } </SupplementaryLabel>
) }
</Title>
{ showDescriptionFirst && TheDescription }
<Action>
<TextControl
className="ppcp-r-vertical-text-control"
placeholder={ actionProps.placeholder }
value={ actionProps.value }
onChange={ ( newValue ) =>
actionProps.callback( actionProps.key, newValue )
}
/> />
) : null; </Action>
} ) } { ! showDescriptionFirst && TheDescription }
</SettingsBlock> </SettingsBlock>
); );
};
export default InputSettingsBlock; export default InputSettingsBlock;

View file

@ -87,6 +87,7 @@ const PaypalSettings = ( { updateFormValue, settings } ) => {
'What business name to show to your buyers during checkout and on receipts.', 'What business name to show to your buyers during checkout and on receipts.',
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
) } ) }
showDescriptionFirst={ true }
actionProps={ { actionProps={ {
value: settings.brandName, value: settings.brandName,
callback: updateFormValue, callback: updateFormValue,
@ -96,7 +97,6 @@ const PaypalSettings = ( { updateFormValue, settings } ) => {
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
), ),
} } } }
order={ [ 'title', 'description', 'action' ] }
/> />
<InputSettingsBlock <InputSettingsBlock
@ -105,6 +105,7 @@ const PaypalSettings = ( { updateFormValue, settings } ) => {
"The dynamic text used to construct the statement descriptor that appears on a payer's card statement. Applies to PayPal and Credit Card transactions. Max value of 22 characters.", "The dynamic text used to construct the statement descriptor that appears on a payer's card statement. Applies to PayPal and Credit Card transactions. Max value of 22 characters.",
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
) } ) }
showDescriptionFirst={ true }
actionProps={ { actionProps={ {
value: settings.softDescriptor, value: settings.softDescriptor,
callback: updateFormValue, callback: updateFormValue,
@ -114,7 +115,6 @@ const PaypalSettings = ( { updateFormValue, settings } ) => {
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
), ),
} } } }
order={ [ 'title', 'description', 'action' ] }
/> />
<RadioSettingsBlock <RadioSettingsBlock