import { ComponentPropsWithoutRef, forwardRef, useImperativeHandle, useRef, } from "react" import { TrianglesMini } from "@medusajs/icons" import { clx } from "@medusajs/ui" import { useTranslation } from "react-i18next" import { getCountryProvinceObjectByIso2 } from "../../../lib/data/country-states" interface ProvinceSelectProps extends ComponentPropsWithoutRef<"select"> { /** * ISO 3166-1 alpha-2 country code */ country_code: string /** * Whether to use the ISO 3166-1 alpha-2 code or the name of the province as the value * * @default "iso_2" */ valueAs?: "iso_2" | "name" placeholder?: string } export const ProvinceSelect = forwardRef< HTMLSelectElement, ProvinceSelectProps >( ( { className, disabled, placeholder, country_code, valueAs = "iso_2", ...props }, ref ) => { const { t } = useTranslation() const innerRef = useRef(null) useImperativeHandle(ref, () => innerRef.current as HTMLSelectElement) const isPlaceholder = innerRef.current?.value === "" const provinceObject = getCountryProvinceObjectByIso2(country_code) if (!provinceObject) { disabled = true } const options = Object.entries(provinceObject?.options ?? {}).map( ([iso2, name]) => { return ( ) } ) const placeholderText = provinceObject ? t(`taxRegions.fields.sublevels.placeholders.${provinceObject.type}`) : "" const placeholderOption = provinceObject ? ( ) : null return (
) } ) ProvinceSelect.displayName = "CountrySelect"