"use client" import { useController, useFormContext } from "react-hook-form" import { useEffect, useRef, useState } from "react" import { _ } from "@/lib/translation" import { countries } from "./countries" import { Button, ComboBox, FieldError, Input, ListBox, ListBoxItem, Popover, type Key, } from "react-aria-components" import { ErrorMessage } from "@hookform/error-message" import SelectChevron from "../SelectChevron" import styles from "./country.module.css" import type { CountryProps } from "./country" export default function CountrySelect({ name = "country", placeholder = "Select a country", registerOptions, }: CountryProps) { const divRef = useRef(null) const [divElement, setDivElement] = useState(divRef.current) const { control, setValue } = useFormContext() const { field } = useController({ control, name, rules: registerOptions, }) const [selectedKey, setSelectedKey] = useState(() => { if (field.value) { const selected = countries.find( (country) => country.name === field.value || country.code === field.value ) if (selected) { return selected.name } } return "" }) function handleChange(country: Key) { setSelectedKey(String(country)) setValue(name, country) } useEffect(() => { if (divRef.current) { setDivElement(divRef.current) } }, [divRef.current]) return (
{countries.map((country, idx) => ( {country.name} ))}
) }