Fix commit filter logic for empty search strings

This commit is contained in:
Cooldude2606 committed 2026-07-10 23:10:54 +01:00
1 parent 40407e52d6
commit 3cc99a1ea6
2 files changed
+8 -23

No files matched your search

@@ -136,15 +136,12 @@ export function useUserFilter(
: {}; : {};
// Update the live filter as the user types/toggles; the URL is only written when the // Update the live filter as the user types/toggles; the URL is only written when the
// dropdown closes (see onOpenChange), or immediately when `commit` is set (e.g. clear). // dropdown closes (see onOpenChange) or when enter is pressed (see onPressEnter)
function update(next: Partial<UserFilter>, commit = false) { function update(next: Partial<UserFilter>) {
const encoded = encodeFilter({ ...filter, ...next }); const encoded = encodeFilter({ ...filter, ...next });
const values = encoded === "{}" ? null : [encoded]; const values = encoded === "{}" ? null : [encoded];
setSelectedKeys(values ?? []); setSelectedKeys(values ?? []);
tableState.setFilter(columnKey, values); tableState.setFilter(columnKey, values);
if (commit) {
tableState.commitFilter(columnKey, values);
}
} }
return ( return (
@@ -155,13 +152,9 @@ export function useUserFilter(
placeholder="Search username" placeholder="Search username"
value={filter.search} value={filter.search}
allowClear allowClear
onSearch={value => { onPressEnter={e => {
if (value) { close();
close(); tableState.commitFilter(columnKey);
} else {
// Clear (X) fires onSearch with an empty value before onChange.
update({ search: undefined }, true);
}
}} }}
onChange={(e) => update({ search: e.target.value !== "" ? e.target.value : undefined })} onChange={(e) => update({ search: e.target.value !== "" ? e.target.value : undefined })}
/> />
+3 -11
View File
@@ -43,17 +43,9 @@ export default function useColumnSearch<T>(
setSelectedKeys(values); setSelectedKeys(values);
tableState.setFilter(columnKey, values.length ? values : null); tableState.setFilter(columnKey, values.length ? values : null);
}} }}
onSearch={value => { onPressEnter={e => {
if (value) { close();
close(); tableState.commitFilter(columnKey);
} else {
// antd fires onSearch with an empty value when the clear (X) is clicked
// (before onChange); commit the cleared state instead of closing, which
// would re-commit the still-live typed value.
setSelectedKeys([]);
tableState.setFilter(columnKey, null);
tableState.commitFilter(columnKey, null);
}
}} }}
/> />
</div> </div>