2020-05-13 15:48:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { checkPayment } from '@woocommerce/icons';
|
|
|
|
import {
|
|
|
|
Icon,
|
|
|
|
institution as bank,
|
|
|
|
currencyDollar as bill,
|
|
|
|
payment as card,
|
|
|
|
} from '@wordpress/icons';
|
2021-08-20 11:55:30 +00:00
|
|
|
import { isString, objectHasProp } from '@woocommerce/types';
|
|
|
|
import { useCallback } from '@wordpress/element';
|
2020-05-13 15:48:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2021-08-20 11:55:30 +00:00
|
|
|
interface NamedIcons {
|
|
|
|
bank: JSX.Element;
|
|
|
|
bill: JSX.Element;
|
|
|
|
card: JSX.Element;
|
|
|
|
checkPayment: JSX.Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
const namedIcons: NamedIcons = {
|
2020-05-13 15:48:03 +00:00
|
|
|
bank,
|
|
|
|
bill,
|
|
|
|
card,
|
|
|
|
checkPayment,
|
|
|
|
};
|
|
|
|
|
2021-08-20 11:55:30 +00:00
|
|
|
interface PaymentMethodLabelProps {
|
|
|
|
icon: '' | keyof NamedIcons | SVGElement;
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:48:03 +00:00
|
|
|
/**
|
|
|
|
* Exposed to payment methods for the label shown on checkout. Allows icons to be added as well as
|
|
|
|
* text.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Component props.
|
|
|
|
* @param {*} props.icon Show an icon beside the text if provided. Can be a string to use a named
|
|
|
|
* icon, or an SVG element.
|
2020-05-13 15:48:03 +00:00
|
|
|
* @param {string} props.text Text shown next to icon.
|
|
|
|
*/
|
2021-08-20 11:55:30 +00:00
|
|
|
export const PaymentMethodLabel = ( {
|
|
|
|
icon = '',
|
|
|
|
text = '',
|
|
|
|
}: PaymentMethodLabelProps ): JSX.Element => {
|
2020-05-13 15:48:03 +00:00
|
|
|
const hasIcon = !! icon;
|
2021-08-20 11:55:30 +00:00
|
|
|
const hasNamedIcon = useCallback(
|
|
|
|
(
|
|
|
|
iconToCheck: '' | keyof NamedIcons | SVGElement
|
|
|
|
): iconToCheck is keyof NamedIcons =>
|
|
|
|
hasIcon &&
|
|
|
|
isString( iconToCheck ) &&
|
|
|
|
objectHasProp( namedIcons, iconToCheck ),
|
|
|
|
[ hasIcon ]
|
|
|
|
);
|
2020-06-17 09:53:42 +00:00
|
|
|
const className = classnames( 'wc-block-components-payment-method-label', {
|
|
|
|
'wc-block-components-payment-method-label--with-icon': hasIcon,
|
2020-05-13 15:48:03 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={ className }>
|
2021-08-20 11:55:30 +00:00
|
|
|
{ hasNamedIcon( icon ) ? (
|
2022-02-01 16:54:38 +00:00
|
|
|
<Icon icon={ namedIcons[ icon ] } />
|
2021-08-20 11:55:30 +00:00
|
|
|
) : (
|
|
|
|
icon
|
|
|
|
) }
|
2020-05-13 15:48:03 +00:00
|
|
|
{ text }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethodLabel;
|