Skip to content

Commit 3940798

Browse files
committed
cherry-pick(#36260): fix(html-reporter): race condition where form submission used stale filterText state
1 parent 8ecfb12 commit 3940798

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

packages/html-reporter/src/headerView.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ export const GlobalFilterView: React.FC<{
6060
event => {
6161
event.preventDefault();
6262
const url = new URL(window.location.href);
63-
url.hash = filterText ? '?' + new URLSearchParams({ q: filterText }) : '';
63+
// If <form/> onSubmit happens immediately after <input/> onChange, the filterText state is not updated yet.
64+
// Using FormData here is a workaround to get the latest value.
65+
const q = new FormData(event.target as HTMLFormElement).get('q') as string;
66+
url.hash = q ? '?' + new URLSearchParams({ q }) : '';
6467
navigate(url);
6568
}
6669
}>
6770
{icons.search()}
6871
{/* Use navigationId to reset defaultValue */}
69-
<input spellCheck={false} className='form-control subnav-search-input input-contrast width-full' value={filterText} onChange={e => {
72+
<input name='q' spellCheck={false} className='form-control subnav-search-input input-contrast width-full' value={filterText} onChange={e => {
7073
setFilterText(e.target.value);
7174
}}></input>
7275
</form>

0 commit comments

Comments
 (0)