"use client"; import { useEffect, useId, useRef, useState } from "react"; import { P1FieldLabel } from "@/components/priority1/feathery-card"; import { filterPhoneCountries, findPhoneCountry, type PhoneCountry, } from "@/lib/priority1/phone-countries"; interface P1PhoneFieldProps { country: string; value: string; disabled?: boolean; error?: string; onCountryChange: (code: string) => void; onChange: (digits: string) => void; } function formatUsPhoneDisplay(digits: string): string { const d = digits.replace(/\D/g, "").slice(0, 10); if (d.length <= 3) return d; if (d.length <= 6) return `${d.slice(0, 3)} ${d.slice(3)}`; return `${d.slice(0, 3)} ${d.slice(3, 6)} ${d.slice(6)}`; } function formatPhoneDisplay(country: PhoneCountry, digits: string): string { if (country.code === "US" || country.dial === "+1") { return formatUsPhoneDisplay(digits); } return digits.replace(/\D/g, "").slice(0, 15); } function maxPhoneDigits(country: PhoneCountry): number { if (country.code === "US" || (country.dial === "+1" && country.code !== "DO")) { return 10; } return 15; } /** 电话 — 可搜索国家列表(A–Z)+ 自动显示区号前缀 */ export function P1PhoneField({ country, value, disabled, error, onCountryChange, onChange, }: P1PhoneFieldProps) { const listId = useId(); const rootRef = useRef(null); const [open, setOpen] = useState(false); const [search, setSearch] = useState(""); const selected = findPhoneCountry(country); const filtered = filterPhoneCountries(search); useEffect(() => { if (!open) return; const onDoc = (e: MouseEvent) => { if (!rootRef.current?.contains(e.target as Node)) setOpen(false); }; document.addEventListener("mousedown", onDoc); return () => document.removeEventListener("mousedown", onDoc); }, [open]); const pickCountry = (c: PhoneCountry) => { onCountryChange(c.code); setOpen(false); setSearch(""); }; return (
电话
{open ? (
setSearch(e.target.value)} className="h-9 w-full rounded-sm border border-neutral-300 px-2 text-sm focus:border-neutral-600 focus:outline-none" />
    {filtered.map((c) => (
  • ))} {filtered.length === 0 ? (
  • 无匹配国家
  • ) : null}
) : null}
{selected.dial} onChange( e.target.value.replace(/\D/g, "").slice(0, maxPhoneDigits(selected)), ) } className="h-11 min-w-0 flex-1 bg-transparent focus:outline-none disabled:opacity-60" />
{error ?

{error}

: null}
); }