mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
♻️ SImplify InputSettingsBlock logic
This commit is contained in:
parent
e82f4c5f88
commit
c7b80b6cd5
3 changed files with 32 additions and 55 deletions
|
@ -1,4 +1,5 @@
|
|||
import { TextControl } from '@wordpress/components';
|
||||
|
||||
import SettingsBlock from './SettingsBlock';
|
||||
import {
|
||||
Title,
|
||||
|
@ -7,55 +8,40 @@ import {
|
|||
SupplementaryLabel,
|
||||
} 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 = ( {
|
||||
title,
|
||||
description,
|
||||
supplementaryLabel,
|
||||
order = DEFAULT_ELEMENT_ORDER,
|
||||
showDescriptionFirst = false,
|
||||
actionProps = {},
|
||||
...props
|
||||
} ) => (
|
||||
<SettingsBlock { ...props } className="ppcp-r-settings-block__input">
|
||||
{ order.map( ( elementKey ) => {
|
||||
const RenderElement = ELEMENT_RENDERERS[ elementKey ];
|
||||
return RenderElement ? (
|
||||
<RenderElement
|
||||
key={ elementKey }
|
||||
title={ title }
|
||||
description={ description }
|
||||
supplementaryLabel={ supplementaryLabel }
|
||||
actionProps={ props.actionProps }
|
||||
} ) => {
|
||||
const TheDescription = <Description>{ description }</Description>;
|
||||
|
||||
return (
|
||||
<SettingsBlock { ...props } className="ppcp-r-settings-block__input">
|
||||
<Title>
|
||||
{ title }
|
||||
{ supplementaryLabel && (
|
||||
<SupplementaryLabel>
|
||||
{ supplementaryLabel }
|
||||
</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;
|
||||
} ) }
|
||||
</SettingsBlock>
|
||||
);
|
||||
</Action>
|
||||
{ ! showDescriptionFirst && TheDescription }
|
||||
</SettingsBlock>
|
||||
);
|
||||
};
|
||||
|
||||
export default InputSettingsBlock;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue