diff --git a/resources/js/lib/applied-form-reset.ts b/resources/js/lib/applied-form-reset.ts new file mode 100644 index 00000000..86f7f3b2 --- /dev/null +++ b/resources/js/lib/applied-form-reset.ts @@ -0,0 +1,19 @@ +/** + * Reset a search form to the filters the server applied. + * + * React only writes a select's defaultValue into the DOM on the first render, + * so a plain form.reset() after a search returns every select to the value the + * page opened with (for example "Todas"), while the URL keeps the chosen group. + * Selects that declare data-applied are aligned first, so the reset keeps the + * applied choice and the focus stays where the investor left it. + */ +export function resetToAppliedFilters(form: HTMLFormElement | null): void { + if (!form) return; + for (const select of Array.from(form.querySelectorAll("select"))) { + const applied = select.getAttribute("data-applied"); + if (applied === null) continue; + for (const option of Array.from(select.options)) + option.defaultSelected = option.value === applied; + } + form.reset(); +} diff --git a/resources/js/pages/investment/zones.tsx b/resources/js/pages/investment/zones.tsx index 3dc0961a..3561e528 100644 --- a/resources/js/pages/investment/zones.tsx +++ b/resources/js/pages/investment/zones.tsx @@ -7,6 +7,7 @@ import { Link, router, usePage } from '@inertiajs/react'; import { searchTerritoryHref } from '@/lib/search-territory'; import { useEffect, useRef, useState } from 'react'; import { territorialSearchParams } from '@/lib/territorial-search-params'; +import { resetToAppliedFilters } from '@/lib/applied-form-reset'; import CatalogueFilterContext from '@/components/catalogue-filter-context'; import { catalogueDetailHref } from '@/lib/catalogue-context'; import { @@ -116,7 +117,7 @@ export default function Zones({ const [searchError, setSearchError] = useState(''); const appliedKey = JSON.stringify([filters, territorySelection]); useEffect(() => { - searchForm.current?.reset(); + resetToAppliedFilters(searchForm.current); }, [appliedKey]); useEffect( () => () => { @@ -301,23 +302,34 @@ export default function Zones({ className={input} name="category" defaultValue={filters.category ?? ''} + data-applied={filters.category ?? ''} > - - - + {( + [ + ['zee', 'ZEE'], + ['zfi', 'ZFI'], + ['industrial_park', 'Parque industrial'], + ] as const + ).map(([value, label]) => { + const count = groupAvailability[value]; + const empty = + count === 0 && filters.category !== value; + return ( + + ); + })}
@@ -330,6 +342,7 @@ export default function Zones({ className={input} name="physical_category" defaultValue={filters.physical_category ?? ''} + data-applied={filters.physical_category ?? ''} > @@ -380,6 +394,7 @@ export default function Zones({ defaultValue={ filters.development_status ?? '' } + data-applied={filters.development_status ?? ''} > @@ -407,6 +422,7 @@ export default function Zones({ defaultValue={ filters.availability_status ?? '' } + data-applied={filters.availability_status ?? ''} >