Skip to content

Commit 698f067

Browse files
jevin98sleeprite
authored andcommitted
!499 fix(table): 清空dataSource与selectedKeys同时进行,selectedKeys清空异常
* chore: updat test * fix(table): 清空dataSource与selectedKeys同时进行,selectedKeys清空异常
1 parent 2dea185 commit 698f067

File tree

3 files changed

+113
-47
lines changed

3 files changed

+113
-47
lines changed

packages/component/component/table/__tests__/table.test.tsx

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { DOMWrapper, type VueWrapper, mount } from "@vue/test-utils";
22
import LayTable from "../index.vue";
33
import LayCheckboxV2 from "@layui/component/component/checkboxV2/index.vue";
4-
import Button from '@layui/component/component/button/index.vue'
4+
import LayButton from '@layui/component/component/button/index.vue'
55

66
import { describe, expect, test, vi } from "vitest";
7-
import {h, nextTick, reactive, ref} from "vue";
7+
import {h, nextTick, reactive, ref, watch} from "vue";
88
import { sleep } from "../../../test-utils";
99

1010
describe("LayTable", () => {
@@ -533,7 +533,7 @@ describe("LayTable", () => {
533533
const defaultToolbars = [
534534
"filter",
535535
{
536-
render: () => h(Button, {
536+
render: () => h(LayButton, {
537537
onClick: () => {
538538
value++;
539539
},
@@ -942,4 +942,79 @@ describe("LayTable", () => {
942942

943943
expect(_class).toMatch(/layui-table-fixed-left-last/)
944944
});
945+
946+
// https://gitee.com/layui-vue/layui-vue/issues/ICKILA
947+
test("selectedKeys制空未生效", async () => {
948+
949+
const selectedKeys = ref([]);
950+
951+
const wrapper = mount({
952+
setup() {
953+
const columns = reactive([
954+
{ type: "checkbox", title: "复选" },
955+
{ title: "用户", width: "80px", key: "name" },
956+
]);
957+
const dataSource = ref<any>([]);
958+
959+
function generateData1() {
960+
dataSource.value = [
961+
{ id: "3", name: "张三3", city: "城市-3", sex: "男", age: "20", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
962+
{ id: "4", name: "张三4", city: "城市-4", sex: "男", age: "20", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
963+
{ id: "5", name: "张三5", city: "城市-5", sex: "男", age: "20", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" }
964+
];
965+
}
966+
967+
function generateData2() {
968+
dataSource.value = [
969+
{ id: "9", name: "张三9", city: "城市-9", sex: "男", age: "20", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
970+
{ id: "10", name: "张三10", city: "城市-10", sex: "男", age: "18", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
971+
{ id: "11", name: "张三11", city: "城市-11", sex: "男", age: "18", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
972+
{ id: "12", name: "张三12", city: "城市-12", sex: "男", age: "20", remark: "屈指古今多少事,都只是、镜中春", score: 100, sign: "已签到", joinTime: "2022-02-09" },
973+
];
974+
}
975+
976+
watch([dataSource], () => {
977+
selectedKeys.value = []
978+
});
979+
980+
return () => (
981+
<div>
982+
<LayButton class='custom-btn1' size="sm" onClick={generateData1}>
983+
数据1
984+
</LayButton>
985+
<LayButton class='custom-btn2' size="sm" onClick={generateData2}>
986+
数据2
987+
</LayButton>
988+
<LayTable
989+
columns={columns}
990+
dataSource={dataSource.value}
991+
v-model:selectedKeys={selectedKeys.value}
992+
></LayTable>
993+
</div>
994+
);
995+
},
996+
});
997+
998+
await nextTick()
999+
1000+
const btn1 = wrapper.find('.custom-btn1')
1001+
const btn2 = wrapper.find('.custom-btn2')
1002+
1003+
await btn1.trigger('click')
1004+
1005+
const trs1 = wrapper.findAll('.layui-table-body .layui-table tbody tr')
1006+
expect(trs1.length).toBe(3)
1007+
1008+
await trs1[0].findComponent('.layui-checkbox').trigger('click')
1009+
expect(selectedKeys.value.length).toBe(1)
1010+
1011+
await trs1[1].findComponent('.layui-checkbox').trigger('click')
1012+
expect(selectedKeys.value.length).toBe(2)
1013+
1014+
await btn2.trigger('click')
1015+
const trs2 = wrapper.findAll('.layui-table-body .layui-table tbody tr')
1016+
expect(trs2.length).toBe(4)
1017+
1018+
expect(selectedKeys.value.length).toBe(0)
1019+
});
9451020
});

packages/component/component/table/hooks/useTableSelected.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RequiredTableProps, TableEmit } from "../typing";
2-
import { isValueArray, loopForEach } from "@layui/component/utils";
2+
import { arrayEverySame, isValueArray, loopForEach } from "@layui/component/utils";
33

44
import { computed, reactive, ref, toRaw, watch } from "vue";
55

@@ -24,14 +24,9 @@ export function useTableSelected(props: RequiredTableProps, emit: TableEmit) {
2424
emit("update:selectedKey", key);
2525
}
2626

27-
/**
28-
* Multiple checkbox
29-
*/
30-
const allMSelected = ref(false);
31-
const someMSelected = ref(false);
3227
const tableMSelectedKeys = reactive<
3328
NonNullable<RequiredTableProps["selectedKeys"]>
34-
>([]);
29+
>(props.selectedKeys);
3530

3631
const allKeys = computed(() => {
3732
const keys: RequiredTableProps["selectedKeys"] = [];
@@ -59,29 +54,33 @@ export function useTableSelected(props: RequiredTableProps, emit: TableEmit) {
5954
);
6055
},
6156
{
62-
immediate: true,
6357
deep: true,
6458
},
6559
);
6660

67-
watch(
68-
() => [tableMSelectedKeys, allKeys.value],
69-
([tableMultipleSelectedKeysValue, allKeysValue]) => {
70-
allMSelected.value = allKeysValue.length > 0 && allKeysValue.every(key =>
71-
tableMultipleSelectedKeysValue.includes(key),
72-
);
73-
someMSelected.value = allKeysValue.length > 0 && allKeysValue.some(key =>
74-
tableMultipleSelectedKeysValue.includes(key),
75-
);
61+
/**
62+
* Multiple checkbox
63+
*/
64+
const allMSelected = computed(() => {
65+
return allKeys.value.length > 0 && allKeys.value.every(key =>
66+
tableMSelectedKeys.includes(key),
67+
);
68+
});
7669

77-
if (
78-
tableMultipleSelectedKeysValue.length !== props.selectedKeys?.length
79-
) {
70+
const someMSelected = computed(() => {
71+
return allKeys.value.length > 0 && allKeys.value.some(key =>
72+
tableMSelectedKeys.includes(key),
73+
);
74+
});
75+
76+
watch(
77+
tableMSelectedKeys,
78+
(tableMultipleSelectedKeysValue) => {
79+
if (!arrayEverySame(tableMultipleSelectedKeysValue, props.selectedKeys)) {
8080
emit("update:selectedKeys", [...tableMultipleSelectedKeysValue]);
8181
}
8282
},
8383
{
84-
immediate: true,
8584
deep: true,
8685
},
8786
);

packages/component/utils/arrayUtil.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
import { isFunction, isNumber } from "./type";
22

3-
export const check = (arr: any[], value: any) => {
4-
return arr.indexOf(value) > -1;
5-
};
6-
7-
/**
8-
* 计算数组差异
9-
*
10-
* @param arr1 数组
11-
* @param arr2 数组
12-
*/
13-
function diff(arr1: any[], arr2: any[]) {
14-
let newArr = [];
15-
arr1 = Array.from(new Set(arr1)); // 去重
16-
arr2 = Array.from(new Set(arr2)); // 去重
17-
newArr = arr1.concat(arr2);
18-
return newArr.filter((x) => !(arr1.includes(x) && arr2.includes(x)));
3+
export function check(arr: any[], value: any) {
4+
return arr.includes(value);
195
}
206

21-
export const isValueNull = (v: any) => {
7+
export function isValueNull(v: any) {
228
return Array.isArray(v) ? !v.length : !v;
23-
};
9+
}
2410

2511
export function isArray(val: any): val is Array<any> {
2612
return val && Array.isArray(val);
@@ -58,9 +44,10 @@ export function loopForEach(
5844
array: any[] | undefined,
5945
children: string | loopForEachCallback,
6046
callback: loopForEachCallback,
61-
parent?: any
47+
parent?: any,
6248
) {
63-
if (!isValueArray(array)) return;
49+
if (!isValueArray(array))
50+
return;
6451

6552
let _children: string;
6653
let _callback: callback[];
@@ -70,12 +57,13 @@ export function loopForEach(
7057
_children = "children";
7158
_callback = isFunction(children) ? [children] : children;
7259
_parent = callback;
73-
} else {
60+
}
61+
else {
7462
_children = children;
7563
_callback = isFunction(callback) ? [callback] : callback;
7664
}
7765

78-
if (!isValueArray(_callback) || _callback.some((cb) => !isFunction(cb)))
66+
if (!isValueArray(_callback) || _callback.some(cb => !isFunction(cb)))
7967
return;
8068

8169
for (let index = 0; index < array.length; index++) {
@@ -106,7 +94,7 @@ export function loopSomeLevelALLChildren(
10694
export function loopSomeLevelALLChildren(
10795
lists: any[],
10896
childrenKey: string | number,
109-
level = 1
97+
level = 1,
11098
): any[] {
11199
let _childrenKey: string;
112100
let _level: number = level;
@@ -134,3 +122,7 @@ export function loopSomeLevelALLChildren(
134122

135123
return result;
136124
}
125+
126+
export function arrayEverySame<T extends any[]>(arr1: T, arr2: T): boolean {
127+
return arr1.every(i => arr2.includes(i)) && arr2.every(i => arr1.includes(i));
128+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy