@arrai-innovations/reactive-helpers 11.0.2 → 11.0.3
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 +1 -1
- package/tests/unit/use/listSort.spec.js +56 -2
- package/use/listSort.js +12 -14
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { doAwaitTimeout } from "../../../utils/index.js";
|
|
2
|
-
import { reactive } from "vue";
|
|
1
|
+
import { doAwaitTimeout, doAwaitNot } from "../../../utils/index.js";
|
|
2
|
+
import { reactive, ref } from "vue";
|
|
3
3
|
import { deepUnref } from "vue-deepunref";
|
|
4
4
|
|
|
5
5
|
describe("use/useListSort", () => {
|
|
@@ -10,6 +10,7 @@ describe("use/useListSort", () => {
|
|
|
10
10
|
useListInstances,
|
|
11
11
|
useListRelated,
|
|
12
12
|
useListCalculated,
|
|
13
|
+
useListFilter,
|
|
13
14
|
useListSort,
|
|
14
15
|
useListSorts,
|
|
15
16
|
setListSortDefaultOptions,
|
|
@@ -46,6 +47,7 @@ describe("use/useListSort", () => {
|
|
|
46
47
|
useListInstances = imported.useListInstances;
|
|
47
48
|
useListRelated = imported.useListRelated;
|
|
48
49
|
useListCalculated = imported.useListCalculated;
|
|
50
|
+
useListFilter = imported.useListFilter;
|
|
49
51
|
orderByRules = [
|
|
50
52
|
{ key: "organization", desc: true, localeCompare: false },
|
|
51
53
|
{ key: "lexical_name", desc: false, localeCompare: true },
|
|
@@ -339,4 +341,56 @@ describe("use/useListSort", () => {
|
|
|
339
341
|
expect(listSort.state.objectsInOrder).toEqual(testOrder4.map((id) => listInstance.state.objects[id]));
|
|
340
342
|
});
|
|
341
343
|
});
|
|
344
|
+
it("pass through correctly when parentState changes their order", async () => {
|
|
345
|
+
const listInstance = useListInstance({
|
|
346
|
+
props: reactive({}),
|
|
347
|
+
});
|
|
348
|
+
const allowedFilter = ref((obj) => obj.name !== "two");
|
|
349
|
+
const listFilter = useListFilter({
|
|
350
|
+
parentState: listInstance.state,
|
|
351
|
+
allowedFilter,
|
|
352
|
+
});
|
|
353
|
+
const orderByRules = ref([]);
|
|
354
|
+
const listSort = useListSort({
|
|
355
|
+
parentState: listFilter.state,
|
|
356
|
+
orderByRules,
|
|
357
|
+
});
|
|
358
|
+
await doAwaitNot({
|
|
359
|
+
obj: listFilter.state,
|
|
360
|
+
prop: "running",
|
|
361
|
+
});
|
|
362
|
+
await waitForListSort(listSort);
|
|
363
|
+
expect(listFilter.state.order).toEqual([]);
|
|
364
|
+
expect(listFilter.state.objectsInOrder.map((obj) => obj.id)).toEqual([]);
|
|
365
|
+
expect(listSort.state.order).toEqual([]);
|
|
366
|
+
expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([]);
|
|
367
|
+
listInstance.addListObject({ id: 1, name: "one" });
|
|
368
|
+
listInstance.addListObject({ id: 2, name: "two" });
|
|
369
|
+
listInstance.addListObject({ id: 3, name: "three" });
|
|
370
|
+
await doAwaitNot({
|
|
371
|
+
obj: listFilter.state,
|
|
372
|
+
prop: "running",
|
|
373
|
+
});
|
|
374
|
+
await waitForListSort(listSort);
|
|
375
|
+
expect(listFilter.state.order).toEqual(["1", "3"]);
|
|
376
|
+
expect(listFilter.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
|
|
377
|
+
expect(listSort.state.order).toEqual(["1", "3"]);
|
|
378
|
+
expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
|
|
379
|
+
listInstance.updateListObject({ id: 2, name: "twotwo" });
|
|
380
|
+
await doAwaitNot({
|
|
381
|
+
obj: listFilter.state,
|
|
382
|
+
prop: "running",
|
|
383
|
+
});
|
|
384
|
+
await waitForListSort(listSort);
|
|
385
|
+
expect(listSort.state.order).toEqual(["1", "2", "3"]);
|
|
386
|
+
expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 2, 3]);
|
|
387
|
+
listInstance.updateListObject({ id: 2, name: "two" });
|
|
388
|
+
await doAwaitNot({
|
|
389
|
+
obj: listFilter.state,
|
|
390
|
+
prop: "running",
|
|
391
|
+
});
|
|
392
|
+
await waitForListSort(listSort);
|
|
393
|
+
expect(listSort.state.order).toEqual(["1", "3"]);
|
|
394
|
+
expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
|
|
395
|
+
});
|
|
342
396
|
});
|
package/use/listSort.js
CHANGED
|
@@ -18,7 +18,6 @@ import isUndefined from "lodash-es/isUndefined.js";
|
|
|
18
18
|
import throttle from "lodash-es/throttle.js";
|
|
19
19
|
import zip from "lodash-es/zip.js";
|
|
20
20
|
import { effectScope, reactive, toRef, unref, watch, computed } from "vue";
|
|
21
|
-
import { deepUnref } from "vue-deepunref";
|
|
22
21
|
|
|
23
22
|
const collator = new Intl.Collator(undefined, { numeric: true });
|
|
24
23
|
|
|
@@ -132,14 +131,17 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
|
|
|
132
131
|
|
|
133
132
|
function sortCriteriaWatch() {
|
|
134
133
|
try {
|
|
135
|
-
if (!state.orderByRules || !state.orderByRules.filter(identity).length) {
|
|
134
|
+
if (!state.orderByRules?.length || !state.orderByRules.filter(identity).length) {
|
|
136
135
|
if (!isEmpty(state.sortCriteria)) {
|
|
137
136
|
for (const removedKey of Object.keys(state.sortCriteria)) {
|
|
138
137
|
removeSortCriteria(removedKey);
|
|
139
138
|
}
|
|
140
139
|
}
|
|
141
|
-
|
|
142
|
-
assignReactiveObject(
|
|
140
|
+
state.order = [...parentState.order];
|
|
141
|
+
assignReactiveObject(
|
|
142
|
+
state.objectsInOrderRefs,
|
|
143
|
+
state.order.map((e) => toRef(parentState.objects, e))
|
|
144
|
+
);
|
|
143
145
|
return;
|
|
144
146
|
}
|
|
145
147
|
const { removedKeys, addedKeys } = keyDiff(
|
|
@@ -152,9 +154,9 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
|
|
|
152
154
|
|
|
153
155
|
es.run(() => {
|
|
154
156
|
for (const addedKey of addedKeys) {
|
|
155
|
-
const object = toRef(
|
|
156
|
-
const relatedObj = toRef(
|
|
157
|
-
const calculatedObj = toRef(
|
|
157
|
+
const object = toRef(parentState.objects, addedKey);
|
|
158
|
+
const relatedObj = toRef(parentState.relatedObjects, addedKey);
|
|
159
|
+
const calculatedObj = toRef(parentState.calculatedObjects, addedKey);
|
|
158
160
|
addSortCriteria(object, relatedObj, calculatedObj, addedKey);
|
|
159
161
|
}
|
|
160
162
|
});
|
|
@@ -169,17 +171,15 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
|
|
|
169
171
|
|
|
170
172
|
function sortWatch() {
|
|
171
173
|
try {
|
|
172
|
-
if (!state.orderByRules
|
|
174
|
+
if (!state.orderByRules?.length) {
|
|
173
175
|
state.order = [...parentState.order];
|
|
174
176
|
assignReactiveObject(
|
|
175
177
|
state.objectsInOrderRefs,
|
|
176
178
|
state.order.map((e) => toRef(parentState.objects, e))
|
|
177
179
|
);
|
|
178
|
-
3;
|
|
179
180
|
return;
|
|
180
181
|
}
|
|
181
|
-
|
|
182
|
-
let idList = Object.keys(parentState.objects);
|
|
182
|
+
let idList = [...parentState.order];
|
|
183
183
|
idList.sort((xKey, yKey) => {
|
|
184
184
|
const xCriteria = state.sortCriteria[xKey];
|
|
185
185
|
const yCriteria = state.sortCriteria[yKey];
|
|
@@ -238,9 +238,7 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
|
|
|
238
238
|
watch([toRef(state, "orderByDesc"), toRef(state, "sortCriteria")], throttledSortWatch, {
|
|
239
239
|
deep: true,
|
|
240
240
|
});
|
|
241
|
-
|
|
242
|
-
watch(() => Object.keys(parentState.objects), sortCriteriaWatch);
|
|
243
|
-
watch(toRef(state, "orderByRules"), sortCriteriaWatch, {
|
|
241
|
+
watch([toRef(state, "orderByRules"), toRef(parentState, "order")], sortCriteriaWatch, {
|
|
244
242
|
deep: true,
|
|
245
243
|
immediate: true,
|
|
246
244
|
});
|