import React from "react"; import { FormHelperText } from "@mui/material"; import Grid from "@mui/material/Grid2"; import Stack from "@mui/material/Stack"; import TextField, { TextFieldProps } from "@mui/material/TextField"; import Typography from "@mui/material/Typography"; import { useCardContext } from "../../contexts/CardContext"; import { useI18n } from "../../contexts/I18nContext"; import { countryCodeFormatter } from "../../util/country_code_formatter"; import LabeledValue from "../LabeledValue"; import { CardFieldBaseProps } from "./shared"; export interface BillingAddress { street_address: string; street_address2: string; postal_code: string; city: string; country_code: string; region: string; care_of: string; } export interface CardFieldAddressProps extends Omit, CardFieldBaseProps, React.PropsWithChildren { type: "address"; value: BillingAddress; isEditable?: boolean; isCompany?: boolean; fieldErrors?: Partial>; } export const CardFieldAddress: React.FC> = ({ label, value: defaultValue, required = false, isDisabled = false, size = "grow", isReadable = true, isEditable = true, helperText, isCompany = false, fieldErrors = {}, }) => { const { isEditing } = useCardContext(); const { t } = useI18n(); label ??= t("Address"); const addressString = [ defaultValue?.street_address, defaultValue?.street_address2, defaultValue?.care_of, [defaultValue?.postal_code, defaultValue?.city].filter(Boolean).join(" "), defaultValue?.region, defaultValue?.country_code ? countryCodeFormatter.of(defaultValue.country_code) : undefined, ] .filter(Boolean) .join("\n"); return ( {isEditing && isEditable ? ( {label} {helperText != null && {helperText}} ) : ( isReadable && (isEditing || addressString.length > 0) && ( ) )} ); }; export default CardFieldAddress;