/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { type CartShippingAddress, type CartBillingAddress, type CountryData, objectHasProp, isString, } from '@woocommerce/types'; import { FormFieldsConfig, getSetting } from '@woocommerce/settings'; import { formatAddress } from '@woocommerce/blocks/checkout/utils'; /** * Internal dependencies */ import './style.scss'; const AddressCard = ( { address, onEdit, target, fieldConfig, }: { address: CartShippingAddress | CartBillingAddress; onEdit: () => void; target: string; fieldConfig: FormFieldsConfig; } ): JSX.Element | null => { const countryData = getSetting< Record< string, CountryData > >( 'countryData', {} ); let formatToUse = getSetting< string >( 'defaultAddressFormat', '{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}' ); if ( objectHasProp( countryData, address?.country ) && objectHasProp( countryData[ address.country ], 'format' ) && isString( countryData[ address.country ].format ) ) { // `as string` is fine here because we check if it's a string above. formatToUse = countryData[ address.country ].format as string; } const { name: formattedName, address: formattedAddress } = formatAddress( address, formatToUse ); return (
{ formattedName }
{ formattedAddress .filter( ( field ) => !! field ) .map( ( field, index ) => ( { field } ) ) }
{ address.phone && ! fieldConfig.phone.hidden ? (
{ address.phone }
) : ( '' ) }
{ onEdit && ( { onEdit(); e.preventDefault(); } } > { __( 'Edit', 'woocommerce' ) } ) }
); }; export default AddressCard;