A lista de grupos voltava a «Todas» depois de cada pesquisa, embora o endereço guardasse o grupo. Os grupos sem registos para a selecção passam a dizê-lo e não podem ser escolhidos, salvo o que já está aplicado. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
20 lines
872 B
TypeScript
20 lines
872 B
TypeScript
/**
|
|
* 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();
|
|
}
|