21 lines
1.3 KiB
TypeScript
21 lines
1.3 KiB
TypeScript
import { useState } from 'react';
|
|
|
|
export type Country = { code: string; name: string; name_en: string };
|
|
|
|
export default function CountrySelect({ countries, value, onChange, id, disabled = false }: {
|
|
countries: Country[]; value: string; onChange: (value: string) => void; id: string; disabled?: boolean;
|
|
}) {
|
|
const [search, setSearch] = useState('');
|
|
const matches = countries.filter((country) => country.code === value ||
|
|
`${country.name} ${country.name_en} ${country.code}`.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()
|
|
.includes(search.normalize('NFD').replace(/\p{Diacritic}/gu, '').toLowerCase()));
|
|
return <div className="space-y-2">
|
|
<input disabled={disabled} className="w-full rounded border p-2" aria-label="Pesquisar país" value={search} onChange={(event) => setSearch(event.target.value)} placeholder="Pesquisar nome ou código do país" />
|
|
<select disabled={disabled} id={id} className="w-full rounded border p-2" value={value} onChange={(event) => onChange(event.target.value)}>
|
|
<option value="">Não informado</option>
|
|
{matches.map((country) => <option key={country.code} value={country.code}>{country.name} ({country.code})</option>)}
|
|
</select>
|
|
{matches.length === 0 && <p role="status">Nenhum país corresponde à pesquisa.</p>}
|
|
</div>;
|
|
}
|