@povio/ui 2.2.12 → 2.3.0
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/dist/config/router.context.d.ts +3 -4
- package/dist/config/router.context.js +1 -3
- package/dist/hooks/useFilters.js +27 -23
- package/dist/hooks/usePagination.js +8 -4
- package/dist/hooks/useSorting.js +7 -4
- package/dist/index.js +1 -1
- package/dist/utils/routing.utils.d.ts +1 -0
- package/dist/utils/routing.utils.js +14 -0
- package/package.json +2 -2
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
2
|
interface UIRouterProviderProps {
|
|
3
|
-
push: (
|
|
4
|
-
replace: (
|
|
5
|
-
pathname: string;
|
|
3
|
+
push: (path: string, params?: Record<string, unknown>) => Promise<boolean>;
|
|
4
|
+
replace: (path: string, params?: Record<string, unknown>) => Promise<boolean>;
|
|
6
5
|
searchString: string;
|
|
7
6
|
}
|
|
8
7
|
interface UIRouterContextValue extends UIRouterProviderProps {
|
|
9
8
|
searchParams: URLSearchParams;
|
|
10
9
|
}
|
|
11
10
|
export declare namespace UIRouter {
|
|
12
|
-
const UIRouterProvider: ({ children,
|
|
11
|
+
const UIRouterProvider: ({ children, push, replace, searchString, }: PropsWithChildren<UIRouterProviderProps>) => import("react/jsx-runtime").JSX.Element;
|
|
13
12
|
const useUIRouter: () => UIRouterContextValue;
|
|
14
13
|
}
|
|
15
14
|
export {};
|
|
@@ -5,20 +5,18 @@ import { RouterProvider } from "react-aria";
|
|
|
5
5
|
var UIRouter;
|
|
6
6
|
(function(_UIRouter) {
|
|
7
7
|
const UIRouterContext = createContext(null);
|
|
8
|
-
_UIRouter.UIRouterProvider = ({ children,
|
|
8
|
+
_UIRouter.UIRouterProvider = ({ children, push, replace, searchString }) => {
|
|
9
9
|
const searchParams = useMemo(() => {
|
|
10
10
|
return new URLSearchParams(searchString);
|
|
11
11
|
}, [searchString]);
|
|
12
12
|
return /* @__PURE__ */ jsx(UIRouterContext, {
|
|
13
13
|
value: useMemo(() => ({
|
|
14
14
|
searchParams,
|
|
15
|
-
pathname,
|
|
16
15
|
push,
|
|
17
16
|
searchString,
|
|
18
17
|
replace
|
|
19
18
|
}), [
|
|
20
19
|
searchParams,
|
|
21
|
-
pathname,
|
|
22
20
|
push,
|
|
23
21
|
searchString,
|
|
24
22
|
replace
|
package/dist/hooks/useFilters.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { UIRouter } from "../config/router.context.js";
|
|
2
|
+
import { RoutingUtils } from "../utils/routing.utils.js";
|
|
2
3
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
3
4
|
import { z } from "zod";
|
|
4
5
|
//#region src/hooks/useFilters.ts
|
|
@@ -18,43 +19,47 @@ var serializeFiltersToQuery = (filterData, prefix) => {
|
|
|
18
19
|
for (const [key, value] of Object.entries(filterData)) {
|
|
19
20
|
if (value === null || value === void 0) continue;
|
|
20
21
|
const filterKey = `filter[${prefix && `${prefix}-`}${key}]`;
|
|
21
|
-
|
|
22
|
-
else if (typeof value === "boolean") query[filterKey] = value ? "true" : "false";
|
|
23
|
-
else query[filterKey] = value.toString();
|
|
22
|
+
query[filterKey] = value;
|
|
24
23
|
}
|
|
25
24
|
return query;
|
|
26
25
|
};
|
|
26
|
+
function normalizeBooleanQueryValue(value) {
|
|
27
|
+
if (value === "\"true\"") return "true";
|
|
28
|
+
if (value === "\"false\"") return "false";
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
27
31
|
var parseFilterFromQuery = (searchParams, schema) => {
|
|
28
32
|
const filter = {};
|
|
29
33
|
for (const [key, value] of searchParams.entries()) {
|
|
34
|
+
const normalizedValue = normalizeBooleanQueryValue(value);
|
|
30
35
|
const match = /^filter\[(.+?)\]$/.exec(key);
|
|
31
36
|
if (!match) continue;
|
|
32
37
|
const filterKey = match[1];
|
|
33
|
-
const isArray =
|
|
34
|
-
const isObject =
|
|
35
|
-
const isNumber = !Number.isNaN(Number(
|
|
36
|
-
const isBoolean = ["true", "false"].includes(
|
|
37
|
-
if (isArray || isObject || isNumber) filter[filterKey] = JSON.parse(
|
|
38
|
-
else if (isBoolean) filter[filterKey] =
|
|
38
|
+
const isArray = normalizedValue.startsWith("[") && normalizedValue.endsWith("]");
|
|
39
|
+
const isObject = normalizedValue.startsWith("{") && normalizedValue.endsWith("}");
|
|
40
|
+
const isNumber = !Number.isNaN(Number(normalizedValue)) && normalizedValue !== "";
|
|
41
|
+
const isBoolean = ["true", "false"].includes(normalizedValue);
|
|
42
|
+
if (isArray || isObject || isNumber) filter[filterKey] = JSON.parse(normalizedValue);
|
|
43
|
+
else if (isBoolean) filter[filterKey] = normalizedValue === "true";
|
|
39
44
|
else {
|
|
40
45
|
const expectedType = getFieldType(schema, filterKey);
|
|
41
|
-
if (expectedType === "number" && !Number.isNaN(Number(
|
|
42
|
-
else if (expectedType === "boolean" && ["true", "false"].includes(
|
|
43
|
-
else filter[filterKey] =
|
|
46
|
+
if (expectedType === "number" && !Number.isNaN(Number(normalizedValue)) && normalizedValue !== "" && normalizedValue !== null) filter[filterKey] = Number(normalizedValue);
|
|
47
|
+
else if (expectedType === "boolean" && ["true", "false"].includes(normalizedValue)) filter[filterKey] = normalizedValue === "true";
|
|
48
|
+
else filter[filterKey] = normalizedValue;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
return filter;
|
|
47
52
|
};
|
|
48
53
|
var useFilters = (defaultFilterValues, prefix = "", schema) => {
|
|
49
|
-
const { searchParams,
|
|
54
|
+
const { searchParams, replace } = UIRouter.useUIRouter();
|
|
50
55
|
const [filterData, setFilterData] = useState(() => parseFilterFromQuery(searchParams, schema));
|
|
51
56
|
useEffect(() => {
|
|
52
57
|
const setUrlToDefaultFilters = () => {
|
|
53
58
|
const flatFilterQuery = serializeFiltersToQuery(defaultFilterValues, prefix);
|
|
54
|
-
replace(
|
|
55
|
-
...
|
|
59
|
+
replace(".", {
|
|
60
|
+
...RoutingUtils.toRouterSearch(searchParams),
|
|
56
61
|
...flatFilterQuery
|
|
57
|
-
})
|
|
62
|
+
});
|
|
58
63
|
};
|
|
59
64
|
const currentFilters = parseFilterFromQuery(searchParams, schema);
|
|
60
65
|
const hasFiltersSet = Object.keys(currentFilters).length > 0;
|
|
@@ -77,20 +82,19 @@ var useFilters = (defaultFilterValues, prefix = "", schema) => {
|
|
|
77
82
|
else newFilters[key] = value;
|
|
78
83
|
});
|
|
79
84
|
const prefixPart = prefix ? `${prefix}-` : "";
|
|
80
|
-
const cleanedQuery =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
85
|
+
const cleanedQuery = RoutingUtils.toRouterSearch(searchParams);
|
|
86
|
+
Object.keys(cleanedQuery).forEach((key) => {
|
|
87
|
+
if (key.startsWith(`filter[${prefixPart}`)) delete cleanedQuery[key];
|
|
88
|
+
});
|
|
84
89
|
const flatFilterQuery = serializeFiltersToQuery(newFilters, prefix);
|
|
85
|
-
replace(
|
|
90
|
+
replace(".", {
|
|
86
91
|
...cleanedQuery,
|
|
87
92
|
...flatFilterQuery
|
|
88
|
-
})
|
|
93
|
+
});
|
|
89
94
|
setFilterData(newFilters);
|
|
90
95
|
}, [
|
|
91
96
|
filterData,
|
|
92
97
|
prefix,
|
|
93
|
-
pathname,
|
|
94
98
|
replace,
|
|
95
99
|
defaultFilterValues,
|
|
96
100
|
searchParams
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { UIRouter } from "../config/router.context.js";
|
|
2
|
+
import { RoutingUtils } from "../utils/routing.utils.js";
|
|
2
3
|
import { useEffect, useState } from "react";
|
|
3
4
|
//#region src/hooks/usePagination.ts
|
|
4
5
|
var DEFAULT_STATE = {
|
|
@@ -6,16 +7,19 @@ var DEFAULT_STATE = {
|
|
|
6
7
|
pageSize: 20
|
|
7
8
|
};
|
|
8
9
|
function usePagination(defaultPagination) {
|
|
9
|
-
const {
|
|
10
|
+
const { replace, searchParams } = UIRouter.useUIRouter();
|
|
10
11
|
const [pagination, setPagination] = useState(defaultPagination ?? {
|
|
11
12
|
pageIndex: searchParams.has("page") ? Number.parseInt(searchParams.get("page") ?? "", 10) : DEFAULT_STATE.pageIndex,
|
|
12
13
|
pageSize: searchParams.has("size") ? Number.parseInt(searchParams.get("size") ?? "", 10) : DEFAULT_STATE.pageSize
|
|
13
14
|
});
|
|
14
15
|
useEffect(() => {
|
|
15
16
|
const params = new URLSearchParams(searchParams);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
params.delete("size");
|
|
18
|
+
params.delete("page");
|
|
19
|
+
if (pagination.pageSize !== DEFAULT_STATE.pageSize) params.set("size", pagination.pageSize.toString());
|
|
20
|
+
if (pagination.pageIndex !== DEFAULT_STATE.pageIndex) params.set("page", pagination.pageIndex.toString());
|
|
21
|
+
if (params.toString() === searchParams.toString()) return;
|
|
22
|
+
replace(".", RoutingUtils.toRouterSearch(params));
|
|
19
23
|
}, [pagination]);
|
|
20
24
|
return {
|
|
21
25
|
pagination,
|
package/dist/hooks/useSorting.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { UIRouter } from "../config/router.context.js";
|
|
2
|
+
import { RoutingUtils } from "../utils/routing.utils.js";
|
|
2
3
|
import { useEffect, useMemo, useState } from "react";
|
|
3
4
|
//#region src/hooks/useSorting.ts
|
|
4
5
|
function useSorting(defaultSorting, prefix = "") {
|
|
5
|
-
const {
|
|
6
|
+
const { replace, searchParams } = UIRouter.useUIRouter();
|
|
6
7
|
const [sorting, setSorting] = useState(defaultSorting ?? searchParams.get(`order${prefix && `-${prefix}`}`)?.split(",").map((item) => {
|
|
7
8
|
if (item.startsWith("-")) return {
|
|
8
9
|
id: item.slice(1),
|
|
@@ -22,9 +23,11 @@ function useSorting(defaultSorting, prefix = "") {
|
|
|
22
23
|
}, [sorting]);
|
|
23
24
|
useEffect(() => {
|
|
24
25
|
const params = new URLSearchParams(searchParams);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const orderKey = prefix ? `order-${prefix}` : "order";
|
|
27
|
+
params.delete(orderKey);
|
|
28
|
+
if (order) params.set(orderKey, order);
|
|
29
|
+
if (params.toString() === searchParams.toString()) return;
|
|
30
|
+
replace(".", RoutingUtils.toRouterSearch(params));
|
|
28
31
|
}, [order]);
|
|
29
32
|
return {
|
|
30
33
|
sorting,
|
package/dist/index.js
CHANGED
|
@@ -126,6 +126,7 @@ import { ThemeContext } from "./config/theme.context.js";
|
|
|
126
126
|
import { DateUtils } from "./utils/date.utils.js";
|
|
127
127
|
import { dynamicColumns } from "./helpers/dynamicColumns.js";
|
|
128
128
|
import { useAutosave } from "./hooks/useAutosave.js";
|
|
129
|
+
import { RoutingUtils } from "./utils/routing.utils.js";
|
|
129
130
|
import { useFilters } from "./hooks/useFilters.js";
|
|
130
131
|
import { useForm } from "./hooks/useForm.js";
|
|
131
132
|
import { useFormAutosave } from "./hooks/useFormAutosave.js";
|
|
@@ -134,5 +135,4 @@ import { useSorting } from "./hooks/useSorting.js";
|
|
|
134
135
|
import { useTableColumnConfig } from "./hooks/useTableColumnConfig.js";
|
|
135
136
|
import { ArrayUtils } from "./utils/array.utils.js";
|
|
136
137
|
import { QueriesUtils } from "./utils/queries.utils.js";
|
|
137
|
-
import { RoutingUtils } from "./utils/routing.utils.js";
|
|
138
138
|
export { Accordion, ActionModal, Alert, AlignCenterIcon, AlignLeftIcon, AlignLeftRightIcon, AlignRightIcon, ArrayUtils, ArrowDropDownIcon, ArrowDropUpIcon, ArrowLeftIcon, ArrowRightIcon, Autocomplete, BoldIcon, BottomSheet, Breadcrumbs, BulletedListIcon, Button, CalendarIcon, CellText, CheckIcon, Checkbox, CheckboxCheckmarkIcon, CheckboxIndeterminateIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, ChevronUpIcon, ChevronsLeftIcon, ChevronsRightIcon, ClockIcon, CloseIcon, ColumnConfigModal, Confirmation, DatePicker, DateRangePicker, DateTimeIcon, DateTimePicker, DateTimeUtils, DateUtils, DomUtils, Drawer, FileUpload, FileUploadContainer, FileUtils, Form, FormField, HeaderText, HighlightIcon, HighlightOnIcon, HomeIcon, IconButton, InfiniteTable, InfoIcon, InlineIconButton, InputUpload, Inputs, ItalicIcon, Link, LinkContext, LinkIcon, Loader, Menu, MenuIcon, MenuItem, MenuPopover, Modal, NumberInput, NumberedListIcon, ObjectUtils, PaginatedTable, Pagination, PaginationList, PasswordInput, PillButton, PointerHorizontalIcon, PointerVerticalIcon, ProgressBar, QueriesUtils, QueryAutocomplete, RadioGroup, ResponsivePopover, RestUtils, RoutingUtils, Segment, Select, SendIcon, Slider, SplitButton, Stepper, StrikethroughIcon, StringUtils, Table, Tag, TextArea, TextButton, TextColorIcon, TextInput, ThemeContext, TimePicker, Toast, ToastContainer, Toggle, ToggleButton, Tooltip, TooltipEllipsis, Typography, UIConfig, UIRouter, UIStyle, UnderlinedIcon, VisibilityIcon, VisibilityOffIcon, compoundMapper, dynamicColumns, dynamicInputs, isEqual, logger, ns, resources, uiOutlineClass, useAutosave, useBreakpoint, useDebounceCallback, useDeepCompareEffect, useDeepCompareLayoutEffect, useDeepCompareMemo, useFilters, useForm, useFormAutosave, useIntersectionObserver, useLocalStorage, useLongPressRepeat, usePagination, useScrollableListBox, useSorting, useStateAndRef, useTableColumnConfig, useTableNav, useToast, useTranslationMemo };
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
//#region src/utils/routing.utils.ts
|
|
2
2
|
var RoutingUtils;
|
|
3
3
|
(function(_RoutingUtils) {
|
|
4
|
+
const isNumericString = (value) => {
|
|
5
|
+
return /^-?(0|[1-9]\d*)(\.\d+)?$/.test(value);
|
|
6
|
+
};
|
|
7
|
+
const coerceQueryValue = (value) => {
|
|
8
|
+
if (value === "true") return true;
|
|
9
|
+
if (value === "false") return false;
|
|
10
|
+
if (isNumericString(value)) return Number(value);
|
|
11
|
+
return value;
|
|
12
|
+
};
|
|
13
|
+
_RoutingUtils.toRouterSearch = (params) => {
|
|
14
|
+
const search = {};
|
|
15
|
+
for (const [key, value] of params.entries()) search[key] = coerceQueryValue(value);
|
|
16
|
+
return search;
|
|
17
|
+
};
|
|
4
18
|
_RoutingUtils.addQueryParams = (path, params) => {
|
|
5
19
|
const sParams = new URLSearchParams();
|
|
6
20
|
for (const [key, value] of Object.entries(params)) if (value) sParams.append(key, value.toString());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@povio/ui",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"react-stately": "^3.44.0",
|
|
49
49
|
"react-toastify": "^11.0.5",
|
|
50
50
|
"tailwindcss": "^4.2.4",
|
|
51
|
-
"tailwindcss-react-aria-components": "^2.0
|
|
51
|
+
"tailwindcss-react-aria-components": "^2.1.0",
|
|
52
52
|
"zod": "^4.4.3"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|