fix(useDataSource): state mutations in computed getters should be avoided (#3859)

* fix: state mutations in computed getters should be avoided

* fix: type about getDataSourceRef
This commit is contained in:
xachary 2024-05-23 08:20:41 +08:00 committed by GitHub
parent cfdb09fe5b
commit fee808198e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -114,32 +114,40 @@ export function useDataSource(
return unref(getAutoCreateKey) ? ROW_KEY : rowKey; return unref(getAutoCreateKey) ? ROW_KEY : rowKey;
}); });
const getDataSourceRef = computed(() => { const getDataSourceRef: Ref<Recordable<any>[]> = ref([]);
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) { watch(
if (!firstItem[ROW_KEY] || !lastItem[ROW_KEY]) { () => dataSourceRef.value,
const data = cloneDeep(unref(dataSourceRef)); () => {
data.forEach((item) => { const dataSource = unref(dataSourceRef);
if (!item[ROW_KEY]) { if (!dataSource || dataSource.length === 0) {
item[ROW_KEY] = buildUUID(); getDataSourceRef.value = unref(dataSourceRef);
} }
if (item.children && item.children.length) { if (unref(getAutoCreateKey)) {
setTableKey(item.children); const firstItem = dataSource[0];
} const lastItem = dataSource[dataSource.length - 1];
});
dataSourceRef.value = data; 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;
}
} }
} }
} getDataSourceRef.value = unref(dataSourceRef);
return unref(dataSourceRef); },
}); {
deep: true,
},
);
async function updateTableData(index: number, key: Key, value: any) { async function updateTableData(index: number, key: Key, value: any) {
const record = dataSourceRef.value[index]; const record = dataSourceRef.value[index];
@ -351,7 +359,7 @@ export function useDataSource(
}); });
return { return {
getDataSourceRef, getDataSourceRef: computed(() => getDataSourceRef.value),
getDataSource, getDataSource,
getRawDataSource, getRawDataSource,
searchInfoRef, searchInfoRef,