@arrai-innovations/reactive-helpers 10.3.0 → 10.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "10.3.0",
3
+ "version": "10.4.0",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,4 +1,5 @@
1
1
  import { doAwaitTimeout } from "../../../utils/index.js";
2
+ import { reactive } from "vue";
2
3
  import { deepUnref } from "vue-deepunref";
3
4
 
4
5
  describe("use/useListSort", () => {
@@ -7,6 +8,8 @@ describe("use/useListSort", () => {
7
8
  sortThrottleWait,
8
9
  useListInstance,
9
10
  useListInstances,
11
+ useListRelated,
12
+ useListCalculated,
10
13
  useListSort,
11
14
  useListSorts,
12
15
  setListSortDefaultOptions,
@@ -41,6 +44,8 @@ describe("use/useListSort", () => {
41
44
  const imported = await import("../../../use");
42
45
  useListInstance = imported.useListInstance;
43
46
  useListInstances = imported.useListInstances;
47
+ useListRelated = imported.useListRelated;
48
+ useListCalculated = imported.useListCalculated;
44
49
  orderByRules = [
45
50
  { key: "organization", desc: true, localeCompare: false },
46
51
  { key: "lexical_name", desc: false, localeCompare: true },
@@ -207,6 +212,72 @@ describe("use/useListSort", () => {
207
212
  expect(deepUnref(listSorts.A.state)).toEqual(deepUnref(listSortA.state));
208
213
  expect(deepUnref(listSorts.B.state)).toEqual(deepUnref(listSortB.state));
209
214
  });
215
+ it("orderByRules can refer to relatedItem or calculatedItem", async () => {
216
+ const listInstance = useListInstance({
217
+ props: reactive({
218
+ crudArgs: { stream: "test_stream" },
219
+ listArgs: { user: 1 },
220
+ retrieveArgs: {
221
+ fields: ["id", "__str__", "name", "relatedItem", "calculatedItem"],
222
+ },
223
+ }),
224
+ });
225
+ const relatedListInstance = useListInstance({
226
+ props: reactive({
227
+ crudArgs: { stream: "test_related_stream" },
228
+ listArgs: { user: 1 },
229
+ retrieveArgs: {
230
+ fields: ["id", "__str__", "name"],
231
+ },
232
+ }),
233
+ });
234
+ const listRelated = useListRelated({
235
+ parentState: listInstance.state,
236
+ relatedObjectsRules: reactive({
237
+ relatedItemName: {
238
+ objects: relatedListInstance.state.objects,
239
+ pkKey: "relatedItem",
240
+ },
241
+ }),
242
+ });
243
+ const listCalculated = useListCalculated({
244
+ parentState: listRelated.state,
245
+ calculatedObjectsRules: reactive({
246
+ calculatedItemName: (obj, relatedObj) => {
247
+ return obj.sameValue + ":" + relatedObj.relatedItemName?.oppositeOrder;
248
+ },
249
+ }),
250
+ });
251
+ const orderByRules = reactive([
252
+ { key: "calculatedItem.calculatedItemName", desc: false, localeCompare: false },
253
+ ]);
254
+ const listSort = useListSort({
255
+ parentState: listCalculated.state,
256
+ orderByRules,
257
+ });
258
+ for (let i = 1; i <= 4; i++) {
259
+ listInstance.addListObject({
260
+ id: i,
261
+ name: `item${i}`,
262
+ relatedItem: i,
263
+ sameValue: "sameValue",
264
+ });
265
+ relatedListInstance.addListObject({
266
+ id: i,
267
+ name: `relatedItem${i}`,
268
+ oppositeOrder: 5 - i,
269
+ });
270
+ }
271
+ await waitForListSort(listSort);
272
+ expect(listSort.state.order).toEqual(["4", "3", "2", "1"]);
273
+ orderByRules[0].key = "relatedItemName.name";
274
+ await waitForListSort(listSort);
275
+ expect(listSort.state.order).toEqual(["1", "2", "3", "4"]);
276
+ orderByRules[0].key = "relatedItemName.sameValue";
277
+ orderByRules[1] = { key: "calculatedItem.calculatedItemName", desc: false, localeCompare: false };
278
+ await waitForListSort(listSort);
279
+ expect(listSort.state.order).toEqual(["4", "3", "2", "1"]);
280
+ });
210
281
  describe("useListSort/sortThrottleWait", () => {
211
282
  it("respects throttle time prior to triggering", async () => {
212
283
  setListSortDefaultOptions({
package/use/listSort.js CHANGED
@@ -77,7 +77,7 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
77
77
  delete state.sortCriteria[removedKey];
78
78
  }
79
79
 
80
- function addSortCriteria(object, key) {
80
+ function addSortCriteria(object, relatedObject, calculatedObject, key) {
81
81
  const oldScope = state.sortCriteriaEffectScopes[key];
82
82
  if (oldScope) {
83
83
  oldScope.stop();
@@ -88,16 +88,24 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
88
88
  state.sortCriteria[key] = [];
89
89
  }
90
90
  watch(
91
- [object, toRef(state, "orderByRules")],
91
+ [object, relatedObject, calculatedObject, toRef(state, "orderByRules")],
92
92
  () => {
93
93
  const obj = unref(object);
94
+ const relatedObj = unref(relatedObject);
95
+ const calculatedObj = unref(calculatedObject);
94
96
  const newSearchCriteria = [];
95
97
  for (const orderByObj of state.orderByRules.filter(identity)) {
96
98
  let newItem;
97
99
  if (orderByObj.keyFn) {
98
100
  newItem = orderByObj.keyFn(obj, state);
99
101
  } else {
100
- newItem = get(obj, orderByObj.key);
102
+ if (orderByObj.key.startsWith("relatedItem.")) {
103
+ newItem = get(relatedObj, orderByObj.key.slice(12));
104
+ } else if (orderByObj.key.startsWith("calculatedItem.")) {
105
+ newItem = get(calculatedObj, orderByObj.key.slice(15));
106
+ } else {
107
+ newItem = get(obj, orderByObj.key);
108
+ }
101
109
  }
102
110
  newSearchCriteria.push(newItem);
103
111
  }
@@ -141,7 +149,9 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
141
149
  es.run(() => {
142
150
  for (const addedKey of addedKeys) {
143
151
  const object = toRef(() => parentState.objects[addedKey]);
144
- addSortCriteria(object, addedKey);
152
+ const relatedObj = toRef(() => parentState.relatedObjects?.[addedKey]);
153
+ const calculatedObj = toRef(() => parentState.calculatedObjects?.[addedKey]);
154
+ addSortCriteria(object, relatedObj, calculatedObj, addedKey);
145
155
  }
146
156
  });
147
157
  assignReactiveObject(