@marktiderman/genesis-ui-native 2.7.1 → 2.8.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/data/index.d.ts +11 -8
- package/dist/data/index.js +11 -6
- package/dist/data/index.js.map +1 -1
- package/package.json +2 -2
- package/src/data/resource-view/__tests__/native-resource-view.test.tsx +18 -1
- package/src/data/resource-view/index.ts +1 -0
- package/src/data/resource-view/use-native-resource-view.ts +17 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-ui-native",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@expo/ui": ">=58.0.0",
|
|
70
70
|
"@gorhom/bottom-sheet": ">=5.0.0",
|
|
71
71
|
"@hookform/resolvers": "^3.0.0 || ^4.0.0 || ^5.0.0",
|
|
72
|
-
"@marktiderman/genesis-core": "^1.
|
|
72
|
+
"@marktiderman/genesis-core": "^1.16.0",
|
|
73
73
|
"@marktiderman/genesis-design-system": "^1.5.0",
|
|
74
74
|
"@react-native-community/datetimepicker": ">=8.0.0",
|
|
75
75
|
"@shopify/flash-list": ">=2.0.0",
|
|
@@ -56,6 +56,7 @@ import {
|
|
|
56
56
|
screenValues,
|
|
57
57
|
sectionsFor,
|
|
58
58
|
serverSort,
|
|
59
|
+
serverSorts,
|
|
59
60
|
formatInstant,
|
|
60
61
|
resolveZone,
|
|
61
62
|
swipeActionsFor,
|
|
@@ -773,7 +774,7 @@ describe("paging and search", () => {
|
|
|
773
774
|
it("keeps at most 10 pages loaded by default, and takes the screen's number", () => {
|
|
774
775
|
hooks.infiniteOptions.length = 0;
|
|
775
776
|
renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(provider()) });
|
|
776
|
-
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10,
|
|
777
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10, defaultSorts: [{ field: "status", order: "asc" }, { field: "name", order: "asc" }] });
|
|
777
778
|
renderHook(() => useNativeResourceView(summaryView, { maxPages: 3 }), { wrapper: wrap(provider()) });
|
|
778
779
|
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 3 });
|
|
779
780
|
});
|
|
@@ -788,6 +789,22 @@ describe("paging and search", () => {
|
|
|
788
789
|
expect(serverSort(summaryView)).toBeUndefined();
|
|
789
790
|
});
|
|
790
791
|
|
|
792
|
+
it("sorts within each group by orderBy: the group first, then the order", () => {
|
|
793
|
+
const grouped = defineView(brands, { groupBy: "status", orderBy: { field: "name", direction: "desc" } });
|
|
794
|
+
expect(serverSorts(grouped)).toEqual([
|
|
795
|
+
{ field: "status", order: "asc" },
|
|
796
|
+
{ field: "name", order: "desc" },
|
|
797
|
+
]);
|
|
798
|
+
// A provider that sorts by one field keeps the sections whole.
|
|
799
|
+
expect(serverSort(grouped)).toEqual({ field: "status", order: "asc" });
|
|
800
|
+
expect(serverSorts(defineView(brands, { groupBy: "status" }))).toEqual([{ field: "status", order: "asc" }]);
|
|
801
|
+
expect(serverSorts(summaryView)).toEqual([]);
|
|
802
|
+
hooks.infiniteOptions.length = 0;
|
|
803
|
+
renderHook(() => useNativeResourceView(grouped), { wrapper: wrap(provider()) });
|
|
804
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ defaultSorts: serverSorts(grouped) });
|
|
805
|
+
expect(hooks.infiniteOptions.at(-1)).not.toHaveProperty("defaultSort");
|
|
806
|
+
});
|
|
807
|
+
|
|
791
808
|
it("doesn't redraw rows while the person types", async () => {
|
|
792
809
|
const summary = vi.fn((r: Brand) => ({ title: r.name }));
|
|
793
810
|
const counted = defineView(brands, { summary });
|
|
@@ -124,18 +124,25 @@ export function screenValues(...filters: (Filter | undefined)[]): Record<string,
|
|
|
124
124
|
/** Pages kept loaded by default (cursor paging). */
|
|
125
125
|
export const DEFAULT_MAX_PAGES = 10;
|
|
126
126
|
|
|
127
|
+
type ServerSort = { field: string; order: "asc" | "desc" };
|
|
128
|
+
|
|
127
129
|
/**
|
|
128
|
-
* The server
|
|
129
|
-
* page never lands in the middle of an earlier section
|
|
130
|
-
*
|
|
130
|
+
* The server sorts: the `groupBy` field first, so every page arrives in section order and a later
|
|
131
|
+
* page never lands in the middle of an earlier section, then the view's `orderBy` within each
|
|
132
|
+
* group. A provider without `multiSort` applies only the first.
|
|
131
133
|
*/
|
|
132
|
-
export function
|
|
134
|
+
export function serverSorts<T extends Row>(view: AnyResourceView<T>): ServerSort[] {
|
|
133
135
|
const groupBy = view.definition.groupBy as string | undefined;
|
|
134
136
|
const orderBy = view.definition.orderBy;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
return
|
|
137
|
+
const order: ServerSort | undefined = orderBy ? { field: orderBy.field as string, order: orderBy.direction ?? "asc" } : undefined;
|
|
138
|
+
if (!groupBy) return order ? [order] : [];
|
|
139
|
+
if (order?.field === groupBy) return [order];
|
|
140
|
+
return [{ field: groupBy, order: "asc" }, ...(order ? [order] : [])];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The first of {@link serverSorts}: what a provider that sorts by one field gets. */
|
|
144
|
+
export function serverSort<T extends Row>(view: AnyResourceView<T>): ServerSort | undefined {
|
|
145
|
+
return serverSorts(view)[0];
|
|
139
146
|
}
|
|
140
147
|
|
|
141
148
|
export function useNativeResourceView<T extends Row = Row>(
|
|
@@ -153,14 +160,14 @@ export function useNativeResourceView<T extends Row = Row>(
|
|
|
153
160
|
// By value, so an inline `filter={{ client_id }}` doesn't rebuild the form every render.
|
|
154
161
|
const createKey = JSON.stringify(screenValues(tabFilter, filter));
|
|
155
162
|
const createDefaults = useMemo(() => JSON.parse(createKey) as Record<string, string | number | boolean>, [createKey]);
|
|
156
|
-
const
|
|
163
|
+
const sorts = useMemo(() => serverSorts(view), [view]);
|
|
157
164
|
const searchFields = useMemo(
|
|
158
165
|
() => searchOption ?? (view.labelField ? [view.labelField] : undefined),
|
|
159
166
|
[searchOption, view.labelField],
|
|
160
167
|
);
|
|
161
168
|
const pages = useInfiniteResource<T & BaseRecord>(view.resource, {
|
|
162
169
|
defaultFilters,
|
|
163
|
-
...(
|
|
170
|
+
...(sorts.length ? { defaultSorts: sorts } : {}),
|
|
164
171
|
...(perPage ? { perPage } : {}),
|
|
165
172
|
maxPages,
|
|
166
173
|
...(searchFields ? { searchFields } : {}),
|