0.3.0.5
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import type React from "react"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useState, useEffect } from "react"
|
||||
import type { FamilyMember, FamilyPhoto } from "@/types/family"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
@@ -93,7 +93,62 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
|
||||
const [errors, setErrors] = useState<string[]>([])
|
||||
|
||||
// 监听 initialData 变化,更新 formData
|
||||
useEffect(() => {
|
||||
if (!initialData) return
|
||||
|
||||
setFormData((prev) => {
|
||||
const normalizedPhotos: FamilyPhoto[] = initialData.photos && initialData.photos.length > 0
|
||||
? initialData.photos
|
||||
: (initialData.photoIds || []).map((url) => ({
|
||||
url,
|
||||
uploadedAt: initialData.updatedAt || new Date().toISOString(),
|
||||
}))
|
||||
|
||||
const newMotherId = initialData.motherId !== undefined ? initialData.motherId : prev.motherId
|
||||
console.log('Calculating new motherId:', {
|
||||
initial: initialData.motherId,
|
||||
prev: prev.motherId,
|
||||
result: newMotherId,
|
||||
hasInitialMother: initialData.motherId !== undefined
|
||||
})
|
||||
|
||||
return {
|
||||
...prev,
|
||||
...initialData,
|
||||
// 确保特定字段被正确更新
|
||||
gender: initialData.gender ? (initialData.gender.toUpperCase() as "MALE" | "FEMALE") : prev.gender,
|
||||
spouseIds: initialData.spouseIds || prev.spouseIds || [],
|
||||
childrenIds: initialData.childrenIds || prev.childrenIds || [],
|
||||
tags: initialData.tags || prev.tags || [],
|
||||
fatherId: initialData.fatherId || prev.fatherId,
|
||||
motherId: newMotherId,
|
||||
photos: initialData.photos ? normalizedPhotos : prev.photos,
|
||||
photoIds: initialData.photoIds || (initialData.photos ? normalizedPhotos.map((p) => p.url) : prev.photoIds),
|
||||
}
|
||||
})
|
||||
}, [initialData])
|
||||
|
||||
const handleChange = (field: keyof FamilyMember, value: any) => {
|
||||
console.log('handleChange:', field, value)
|
||||
|
||||
// 防御性检查:防止 Select 组件意外清空 initialData 中已有的值
|
||||
// 这可能是由于 Select 组件在某些情况下(如选项尚未完全渲染)触发了无效的 onChange
|
||||
if ((field === 'motherId' || field === 'fatherId') && !value) {
|
||||
const initialValue = initialData?.[field]
|
||||
const currentValue = formData[field]
|
||||
// 只有当当前值等于初始值且尝试清空时才阻止
|
||||
if (initialValue && currentValue === initialValue) {
|
||||
console.warn(`Prevented accidental clearing of ${field}`)
|
||||
return
|
||||
}
|
||||
// 如果当前值不为空,但尝试清空为空字符串,也阻止(因为Select组件可能发送空字符串而不是undefined)
|
||||
if (currentValue && value === '') {
|
||||
console.warn(`Prevented clearing of ${field} to empty string`)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
setFormData((prev) => {
|
||||
const newData = { ...prev, [field]: value }
|
||||
|
||||
@@ -108,7 +163,11 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
if (father && father.spouseIds && father.spouseIds.length > 0) {
|
||||
// 找到父亲的配偶(母亲)
|
||||
const motherId = father.spouseIds[0]
|
||||
newData.motherId = motherId
|
||||
// 只有当前母亲ID为空时才自动设置
|
||||
if (!newData.motherId) {
|
||||
newData.motherId = motherId
|
||||
console.log('Auto-set mother from father:', { fatherId: value, motherId })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +177,11 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
if (mother && mother.spouseIds && mother.spouseIds.length > 0) {
|
||||
// 找到母亲的配偶(父亲)
|
||||
const fatherId = mother.spouseIds[0]
|
||||
newData.fatherId = fatherId
|
||||
// 只有当前父亲ID为空时才自动设置
|
||||
if (!newData.fatherId) {
|
||||
newData.fatherId = fatherId
|
||||
console.log('Auto-set father from mother:', { motherId: value, fatherId })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,6 +337,13 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
}
|
||||
|
||||
const potentialRelatives = existingMembers.filter((m) => m.id !== formData.id)
|
||||
|
||||
// 调试日志
|
||||
console.log('MemberForm render:', {
|
||||
motherId: formData.motherId,
|
||||
existingMembersCount: existingMembers.length,
|
||||
motherInList: existingMembers.find(m => m.id === formData.motherId)
|
||||
})
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-8 max-w-4xl mx-auto p-6">
|
||||
@@ -373,7 +443,11 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
<div className="flex gap-2">
|
||||
<Select
|
||||
value={formData.fatherId || "none"}
|
||||
onValueChange={(val) => handleChange("fatherId", val === "none" ? undefined : val)}
|
||||
onValueChange={(val) => {
|
||||
const newValue = val === "none" ? undefined : val
|
||||
console.log('Father select change:', { val, newValue })
|
||||
handleChange("fatherId", newValue)
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="flex-1">
|
||||
<SelectValue placeholder="选择父亲" />
|
||||
@@ -381,7 +455,11 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
<SelectContent>
|
||||
<SelectItem value="none">无</SelectItem>
|
||||
{potentialRelatives
|
||||
.filter((m) => m.gender === "MALE" && m.generation === (formData.generation || 1) - 1)
|
||||
.filter((m) => {
|
||||
// 如果已经被选中,必须显示
|
||||
if (m.id === formData.fatherId) return true
|
||||
return m.gender === "MALE" && m.generation === (formData.generation || 1) - 1
|
||||
})
|
||||
.map((m) => (
|
||||
<SelectItem key={m.id} value={m.id}>
|
||||
<MemberNameWithStatus
|
||||
@@ -416,7 +494,11 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
<div className="flex gap-2">
|
||||
<Select
|
||||
value={formData.motherId || "none"}
|
||||
onValueChange={(val) => handleChange("motherId", val === "none" ? undefined : val)}
|
||||
onValueChange={(val) => {
|
||||
const newValue = val === "none" ? undefined : val
|
||||
console.log('Mother select change:', { val, newValue })
|
||||
handleChange("motherId", newValue)
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className="flex-1">
|
||||
<SelectValue placeholder="选择母亲" />
|
||||
@@ -424,7 +506,18 @@ export function MemberForm({ initialData, existingMembers = [], onSubmit, onCanc
|
||||
<SelectContent>
|
||||
<SelectItem value="none">无</SelectItem>
|
||||
{potentialRelatives
|
||||
.filter((m) => m.gender === "FEMALE" && m.generation === (formData.generation || 1) - 1)
|
||||
.filter((m) => {
|
||||
// 如果已经被选中,必须显示
|
||||
if (m.id === formData.motherId) {
|
||||
console.log('Mother already selected, showing:', { id: m.id, name: m.fullName })
|
||||
return true
|
||||
}
|
||||
const isMatch = m.gender === "FEMALE" && m.generation === (formData.generation || 1) - 1
|
||||
if (isMatch) {
|
||||
console.log('Mother match found:', { id: m.id, name: m.fullName, gender: m.gender, generation: m.generation })
|
||||
}
|
||||
return isMatch
|
||||
})
|
||||
.map((m) => (
|
||||
<SelectItem key={m.id} value={m.id}>
|
||||
<MemberNameWithStatus
|
||||
|
||||
Reference in New Issue
Block a user