diff --git a/sdk/src/components/channel-form.tsx b/sdk/src/components/channel-form.tsx index f64185a..d176a5b 100644 --- a/sdk/src/components/channel-form.tsx +++ b/sdk/src/components/channel-form.tsx @@ -45,7 +45,10 @@ const ChannelForm = forwardRef( const form = formRef.current; if (!form) return; Array.from(form.elements) - .filter((el) => el instanceof HTMLInputElement) + .filter( + (el) => + el instanceof HTMLInputElement || el instanceof HTMLSelectElement, + ) .forEach((input) => { if (!input.name) { // only mark named fields as touched diff --git a/sdk/src/components/field-country.tsx b/sdk/src/components/field-country.tsx index 8886cde..28fd754 100644 --- a/sdk/src/components/field-country.tsx +++ b/sdk/src/components/field-country.tsx @@ -1,9 +1,15 @@ -import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks"; +import { + useCallback, + useLayoutEffect, + useMemo, + useRef, + useState, +} from "preact/hooks"; import { FieldProps } from "./field"; import { CountryCode, getCountries } from "libphonenumber-js"; import { Dropdown, DropdownOption } from "./core/dropdown"; import { formFieldId, formFieldName, usePrevious } from "../utils"; -import { FunctionComponent } from "preact"; +import { FunctionComponent, TargetedEvent } from "preact"; import { useChannelComponentData } from "./channel-root"; type FlagIconProps = { @@ -42,7 +48,7 @@ export const CountryField: FunctionComponent = (props) => { (option) => option.value === selectedCountry, ); - const hiddenFieldRef = useRef(null); + const hiddenFieldRef = useRef(null); useOnCardCountryChange((newCountry: CountryCode) => { if (hiddenFieldRef.current) { @@ -75,9 +81,55 @@ export const CountryField: FunctionComponent = (props) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + const handleNativeSelectChange = useCallback( + (event: TargetedEvent) => { + const filledValue = event.currentTarget.value; + + if (!filledValue) { + // browser cleared the field, so clear our copy too + setSelectedCountry(undefined); + onChange(); + return; + } + + const option = COUNTRIES_AS_DROPDOWN_OPTIONS.find( + (o) => o.value === filledValue, + ); + if (option) { + onChangeWrapper(option); + } else if (hiddenFieldRef.current) { + // not a country we offer + hiddenFieldRef.current.value = selectedCountry ?? ""; + } + }, + [onChange, onChangeWrapper, selectedCountry], + ); + + // the country list never changes + const selectOptions = useMemo( + () => + COUNTRIES_AS_DROPDOWN_OPTIONS.map((option) => ( + + )), + [], + ); + return (
- + {/* a ` +