fix: 设置持仓输入回滚问题
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { CloseIcon, SettingsIcon } from './Icons';
|
import { CloseIcon, SettingsIcon } from './Icons';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@@ -12,12 +12,21 @@ export default function HoldingEditModal({ fund, holding, onClose, onSave }) {
|
|||||||
const [mode, setMode] = useState('amount'); // 'amount' | 'share'
|
const [mode, setMode] = useState('amount'); // 'amount' | 'share'
|
||||||
|
|
||||||
const dwjz = fund?.dwjz || fund?.gsz || 0;
|
const dwjz = fund?.dwjz || fund?.gsz || 0;
|
||||||
|
const dwjzRef = useRef(dwjz);
|
||||||
|
useEffect(() => {
|
||||||
|
dwjzRef.current = dwjz;
|
||||||
|
}, [dwjz]);
|
||||||
|
|
||||||
const [share, setShare] = useState('');
|
const [share, setShare] = useState('');
|
||||||
const [cost, setCost] = useState('');
|
const [cost, setCost] = useState('');
|
||||||
const [amount, setAmount] = useState('');
|
const [amount, setAmount] = useState('');
|
||||||
const [profit, setProfit] = useState('');
|
const [profit, setProfit] = useState('');
|
||||||
|
|
||||||
|
const holdingSig = useMemo(() => {
|
||||||
|
if (!holding) return '';
|
||||||
|
return `${holding.id ?? ''}|${holding.share ?? ''}|${holding.cost ?? ''}`;
|
||||||
|
}, [holding]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (holding) {
|
if (holding) {
|
||||||
const s = holding.share || 0;
|
const s = holding.share || 0;
|
||||||
@@ -25,14 +34,17 @@ export default function HoldingEditModal({ fund, holding, onClose, onSave }) {
|
|||||||
setShare(String(s));
|
setShare(String(s));
|
||||||
setCost(String(c));
|
setCost(String(c));
|
||||||
|
|
||||||
if (dwjz > 0) {
|
const price = dwjzRef.current;
|
||||||
const a = s * dwjz;
|
if (price > 0) {
|
||||||
const p = (dwjz - c) * s;
|
const a = s * price;
|
||||||
|
const p = (price - c) * s;
|
||||||
setAmount(a.toFixed(2));
|
setAmount(a.toFixed(2));
|
||||||
setProfit(p.toFixed(2));
|
setProfit(p.toFixed(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [holding, fund, dwjz]);
|
// 只在“切换持仓/初次打开”时初始化,避免净值刷新覆盖用户输入
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [holdingSig]);
|
||||||
|
|
||||||
const handleModeChange = (newMode) => {
|
const handleModeChange = (newMode) => {
|
||||||
if (newMode === mode) return;
|
if (newMode === mode) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user