@middag-io/react 0.51.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -23,6 +23,28 @@ import { renderLabel } from "@/i18n/render-label";
|
|
|
23
23
|
import { useTranslation } from "@/i18n/useTranslation";
|
|
24
24
|
import { resolveActionTarget } from "@/lib/actions/resolve-action-target";
|
|
25
25
|
|
|
26
|
+
/** Query keys the server-mode table writes on every request. */
|
|
27
|
+
const TABLE_OWNED_PARAMS = new Set(["page", "per_page", "sort", "direction", "search"]);
|
|
28
|
+
const TABLE_FILTER_PARAM_PREFIX = "filter[";
|
|
29
|
+
|
|
30
|
+
function isTableOwnedParam(key: string): boolean {
|
|
31
|
+
return TABLE_OWNED_PARAMS.has(key) || key.startsWith(TABLE_FILTER_PARAM_PREFIX);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The page's query string without the params the table owns, as `?...` or "".
|
|
36
|
+
* The table sends its full state on each request, so whatever it leaves out
|
|
37
|
+
* (a cleared sort, a removed filter) has to be absent from the request too.
|
|
38
|
+
*/
|
|
39
|
+
function pageQueryWithoutTableParams(search: string): string {
|
|
40
|
+
const kept = new URLSearchParams();
|
|
41
|
+
new URLSearchParams(search).forEach((value, key) => {
|
|
42
|
+
if (!isTableOwnedParam(key)) kept.append(key, value);
|
|
43
|
+
});
|
|
44
|
+
const query = kept.toString();
|
|
45
|
+
return query ? `?${query}` : "";
|
|
46
|
+
}
|
|
47
|
+
|
|
26
48
|
export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): ReactElement {
|
|
27
49
|
const { t } = useTranslation();
|
|
28
50
|
const { data, key, meta } = block;
|
|
@@ -137,9 +159,11 @@ export function DenseTableBlock({ block }: BlockProps<DenseTableBlockData>): Rea
|
|
|
137
159
|
// Inertia builds the request URL from the href plus these params, with
|
|
138
160
|
// the params winning on matching keys. Keep the page's own query string
|
|
139
161
|
// in the href so params the table does not govern (tabs, collection,
|
|
140
|
-
// groups, ...) survive a sort/search/filter/page change
|
|
141
|
-
//
|
|
142
|
-
|
|
162
|
+
// groups, ...) survive a sort/search/filter/page change. Strip the keys
|
|
163
|
+
// the table owns from it first: the params above carry their current
|
|
164
|
+
// values, and a value the user just cleared (a removed sort) must not
|
|
165
|
+
// come back from a stale copy in the URL.
|
|
166
|
+
const href = window.location.pathname + pageQueryWithoutTableParams(window.location.search);
|
|
143
167
|
router.get(href, queryParams as Record<string, string>, {
|
|
144
168
|
preserveState: true,
|
|
145
169
|
preserveScroll: true,
|