"use client"; import { ArrowRightIcon } from "@radix-ui/react-icons"; import { useState } from "react"; import { iconSize, radius, spacing, } from "../../../core/design-system/index.js"; import { LastUsedBadge } from "../../ui/components/badge.js"; import { IconButton } from "../../ui/components/buttons.js"; import { Input, InputContainer } from "../../ui/components/formElements.js"; import { Spacer } from "../../ui/components/Spacer.js"; import { Text } from "../../ui/components/text.js"; import { CountrySelector, getCountrySelector } from "./CountrySelector.js"; import type { SupportedSmsCountry } from "./supported-sms-countries.js"; export function InputSelectionUI(props: { className?: string; onSelect: (data: string) => void; placeholder: string; name: string; type: string; errorMessage?: (input: string) => string | undefined; emptyErrorMessage?: string; submitButtonText: string; format?: "phone"; disabled?: boolean; defaultSmsCountryCode?: SupportedSmsCountry; allowedSmsCountryCodes?: SupportedSmsCountry[]; lastUsedBadge: boolean; }) { const [countryCodeInfo, setCountryCodeInfo] = useState( props.defaultSmsCountryCode ? getCountrySelector(props.defaultSmsCountryCode) : props.allowedSmsCountryCodes && props.allowedSmsCountryCodes.length > 0 && props.allowedSmsCountryCodes[0] ? getCountrySelector(props.allowedSmsCountryCodes[0]) : "US +1", ); const [input, setInput] = useState(""); const [error, setError] = useState(); const [showError, setShowError] = useState(false); const handleSelect = () => { setShowError(true); if (!input || !!error) { return; } props.onSelect( props.format === "phone" ? `+${countryCodeInfo.split("+")[1]}${input}` : input, ); }; const renderingError = (showError && !!error) || (!input && !!props.emptyErrorMessage && showError); return (
{props.lastUsedBadge && } {props.format === "phone" && ( )} { setInput(e.target.value); if (props.errorMessage) { setError(props.errorMessage(e.target.value)); } else { setError(undefined); } }} onKeyDown={(e) => { if (e.key === "Enter") { handleSelect(); } }} placeholder={props.placeholder} style={{ flexGrow: 1, padding: `${spacing.sm} ${ props.format === "phone" ? 0 : spacing.sm }`, }} tabIndex={-1} type={props.type} value={input} variant="transparent" /> {showError && error && ( <> {error} )} {!(showError && error) && !input && props.emptyErrorMessage && showError && ( <> {props.emptyErrorMessage} )}
); }