Skip to content

Commit

Permalink
fix(useDataSource): state mutations in computed getters should be avo…
Browse files Browse the repository at this point in the history
…ided (#3859)

* fix: state mutations in computed getters should be avoided

* fix: type about getDataSourceRef
  • Loading branch information
xachary committed May 23, 2024
1 parent cfdb09f commit fee8081
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/components/Table/src/hooks/useDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,32 +114,40 @@ export function useDataSource(
return unref(getAutoCreateKey) ? ROW_KEY : rowKey;
});

const getDataSourceRef = computed(() => {
const dataSource = unref(dataSourceRef);
if (!dataSource || dataSource.length === 0) {
return unref(dataSourceRef);
}
if (unref(getAutoCreateKey)) {
const firstItem = dataSource[0];
const lastItem = dataSource[dataSource.length - 1];

if (firstItem && lastItem) {
if (!firstItem[ROW_KEY] || !lastItem[ROW_KEY]) {
const data = cloneDeep(unref(dataSourceRef));
data.forEach((item) => {
if (!item[ROW_KEY]) {
item[ROW_KEY] = buildUUID();
}
if (item.children && item.children.length) {
setTableKey(item.children);
}
});
dataSourceRef.value = data;
const getDataSourceRef: Ref<Recordable<any>[]> = ref([]);

watch(
() => dataSourceRef.value,
() => {
const dataSource = unref(dataSourceRef);
if (!dataSource || dataSource.length === 0) {
getDataSourceRef.value = unref(dataSourceRef);
}
if (unref(getAutoCreateKey)) {
const firstItem = dataSource[0];
const lastItem = dataSource[dataSource.length - 1];

if (firstItem && lastItem) {
if (!firstItem[ROW_KEY] || !lastItem[ROW_KEY]) {
const data = cloneDeep(unref(dataSourceRef));
data.forEach((item) => {
if (!item[ROW_KEY]) {
item[ROW_KEY] = buildUUID();
}
if (item.children && item.children.length) {
setTableKey(item.children);
}
});
dataSourceRef.value = data;
}
}
}
}
return unref(dataSourceRef);
});
getDataSourceRef.value = unref(dataSourceRef);
},
{
deep: true,
},
);

async function updateTableData(index: number, key: Key, value: any) {
const record = dataSourceRef.value[index];
Expand Down Expand Up @@ -351,7 +359,7 @@ export function useDataSource(
});

return {
getDataSourceRef,
getDataSourceRef: computed(() => getDataSourceRef.value),
getDataSource,
getRawDataSource,
searchInfoRef,
Expand Down

2 comments on commit fee8081

@doraemonxxx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xachary this revision is not working to the BasicTable Component. Please see https://vben.vvbin.cn/#/comp/table/basic

@doraemonxxx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I open a new ticket for this issue: #3887

Please sign in to comment.