@@ -0,0 +1,20 @@
|
||||
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>;
|
||||
}
|
||||
@@ -19,7 +19,7 @@ const emptyTerritoryPath: TerritoryOption[] = [];
|
||||
export function ProvinceSelect({
|
||||
value,
|
||||
onChange,
|
||||
endpoint = '/api/v1/catalogs/jurisdictions',
|
||||
endpoint = '/api/v1/investment/territory-options',
|
||||
placeholder = 'Seleccione uma província',
|
||||
className = control,
|
||||
label = 'Província',
|
||||
@@ -107,7 +107,7 @@ export function ProvinceSelect({
|
||||
export function TerritoryFields({
|
||||
initial = emptyTerritoryPath,
|
||||
onChange,
|
||||
endpoint = '/api/v1/catalogs/jurisdictions',
|
||||
endpoint = '/api/v1/investment/territory-options',
|
||||
}: {
|
||||
initial?: TerritoryOption[];
|
||||
onChange?: (path: TerritoryOption[]) => void;
|
||||
@@ -174,7 +174,9 @@ export function TerritoryLevel({
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [retry, setRetry] = useState(0);
|
||||
const [search, setSearch] = useState('');
|
||||
const enabled = index === 0 || Boolean(parent);
|
||||
useEffect(() => { setPage(1); setSearch(''); }, [parent, index]);
|
||||
useEffect(() => {
|
||||
setOptions([]);
|
||||
setMore(false);
|
||||
@@ -189,6 +191,7 @@ export function TerritoryLevel({
|
||||
type: levels[index],
|
||||
parent_id: parent,
|
||||
page: String(page),
|
||||
q: search,
|
||||
});
|
||||
const response = await fetch(`${endpoint}?${params}`, {
|
||||
signal: controller.signal,
|
||||
@@ -215,12 +218,13 @@ export function TerritoryLevel({
|
||||
clearTimeout(timer);
|
||||
controller.abort();
|
||||
};
|
||||
}, [index, parent, page, enabled, endpoint, retry]);
|
||||
}, [index, parent, page, enabled, endpoint, retry, search]);
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<label htmlFor={id} className="font-medium">
|
||||
{labels[index]}
|
||||
</label>
|
||||
<input className={control} aria-label={`Pesquisar ${labels[index]}`} disabled={!enabled} maxLength={100} value={search} onChange={(event) => { setSearch(event.target.value); setPage(1); }} placeholder={`Pesquisar ${labels[index].toLowerCase()}`} />
|
||||
<select
|
||||
id={id}
|
||||
aria-invalid={Boolean(fieldError)}
|
||||
|
||||
Reference in New Issue
Block a user