2020-05-13 15:48:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Icon, bank, bill, card, checkPayment } from '@woocommerce/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.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @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 ) ? (
|
|
|
|
<Icon srcElement={ namedIcons[ icon ] } />
|
|
|
|
) : (
|
|
|
|
icon
|
|
|
|
) }
|
2020-05-13 15:48:03 +00:00
|
|
|
{ text }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PaymentMethodLabel;
|