fix: 修复账单编辑后刷新才生效的问题
- 在分析页面添加 handleBillUpdated 回调,编辑后同步更新 records 和 allRecords - 为 TopExpenses、CategoryRanking、DailyTrendChart 组件添加 onUpdate prop - 修复 TopExpenses 组件内响应式更新,使用新数组引用触发更新 - 建立完整的更新传播链:BillDetailDrawer -> BillRecordsTable -> 各分析组件 -> 分析页面 - 确保所有派生数据(topExpenses、categoryStats、pieChartData)自动刷新
This commit is contained in:
@@ -16,9 +16,25 @@
|
||||
totalExpense: number;
|
||||
records: UIBill[];
|
||||
categories?: string[];
|
||||
onUpdate?: (updated: UIBill, original: UIBill) => void;
|
||||
}
|
||||
|
||||
let { categoryStats, pieChartData, totalExpense, records = $bindable(), categories = [] }: Props = $props();
|
||||
let { categoryStats, pieChartData, totalExpense, records = $bindable(), categories = [], onUpdate }: Props = $props();
|
||||
|
||||
function handleRecordUpdated(updated: UIBill, original: UIBill) {
|
||||
// 更新本地 records 数组
|
||||
const idx = records.findIndex(r =>
|
||||
r === original ||
|
||||
(r.time === original.time && r.merchant === original.merchant && r.amount === original.amount)
|
||||
);
|
||||
if (idx !== -1) {
|
||||
records[idx] = updated;
|
||||
records = [...records];
|
||||
}
|
||||
|
||||
// 传播到父组件
|
||||
onUpdate?.(updated, original);
|
||||
}
|
||||
|
||||
let mode = $state<'bar' | 'pie'>('bar');
|
||||
let dialogOpen = $state(false);
|
||||
@@ -213,7 +229,7 @@
|
||||
</Drawer.Header>
|
||||
|
||||
<div class="flex-1 overflow-auto px-4 md:px-0">
|
||||
<BillRecordsTable records={selectedRecords} showDescription={true} {categories} />
|
||||
<BillRecordsTable records={selectedRecords} showDescription={true} {categories} onUpdate={handleRecordUpdated} />
|
||||
</div>
|
||||
|
||||
<Drawer.Footer>
|
||||
|
||||
@@ -17,9 +17,35 @@
|
||||
interface Props {
|
||||
records: UIBill[];
|
||||
categories?: string[];
|
||||
onUpdate?: (updated: UIBill, original: UIBill) => void;
|
||||
}
|
||||
|
||||
let { records = $bindable(), categories = [] }: Props = $props();
|
||||
let { records = $bindable(), categories = [], onUpdate }: Props = $props();
|
||||
|
||||
function handleRecordUpdated(updated: UIBill, original: UIBill) {
|
||||
// 更新 records 数组
|
||||
const idx = records.findIndex(r =>
|
||||
r === original ||
|
||||
(r.time === original.time && r.merchant === original.merchant && r.amount === original.amount)
|
||||
);
|
||||
if (idx !== -1) {
|
||||
records[idx] = updated;
|
||||
records = [...records];
|
||||
}
|
||||
|
||||
// 更新 selectedDateRecords(如果账单在当前选中的日期记录中)
|
||||
const dateIdx = selectedDateRecords.findIndex(r =>
|
||||
r === original ||
|
||||
(r.time === original.time && r.merchant === original.merchant && r.amount === original.amount)
|
||||
);
|
||||
if (dateIdx !== -1) {
|
||||
selectedDateRecords[dateIdx] = updated;
|
||||
selectedDateRecords = [...selectedDateRecords];
|
||||
}
|
||||
|
||||
// 传播到父组件
|
||||
onUpdate?.(updated, original);
|
||||
}
|
||||
|
||||
// Dialog 状态
|
||||
let dialogOpen = $state(false);
|
||||
@@ -896,6 +922,7 @@
|
||||
showDescription={false}
|
||||
pageSize={8}
|
||||
{categories}
|
||||
onUpdate={handleRecordUpdated}
|
||||
/>
|
||||
</div>
|
||||
{:else}
|
||||
|
||||
@@ -24,7 +24,11 @@
|
||||
|
||||
function handleRecordUpdated(updated: UIBill, original: UIBill) {
|
||||
const idx = records.findIndex(r => r === original);
|
||||
if (idx !== -1) records[idx] = updated;
|
||||
if (idx !== -1) {
|
||||
records[idx] = updated;
|
||||
// 触发响应式更新
|
||||
records = [...records];
|
||||
}
|
||||
selectedRecord = updated;
|
||||
onUpdate?.(updated);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user