|
@@ -82,13 +82,16 @@ export function GoodsSpecSelector({
|
|
|
}))
|
|
}))
|
|
|
setSpecOptions(childGoodsAsSpecs)
|
|
setSpecOptions(childGoodsAsSpecs)
|
|
|
|
|
|
|
|
- // 如果有当前选中的规格,设置选中状态
|
|
|
|
|
|
|
+ // 如果有当前选中的规格,设置选中状态(只在数据加载时初始化)
|
|
|
if (currentSpec) {
|
|
if (currentSpec) {
|
|
|
const foundSpec = childGoodsAsSpecs.find(spec => spec.name === currentSpec)
|
|
const foundSpec = childGoodsAsSpecs.find(spec => spec.name === currentSpec)
|
|
|
if (foundSpec) {
|
|
if (foundSpec) {
|
|
|
setSelectedSpec(foundSpec)
|
|
setSelectedSpec(foundSpec)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化数量为当前数量(只在数据加载时初始化)
|
|
|
|
|
+ setQuantity(currentQuantity)
|
|
|
} else {
|
|
} else {
|
|
|
// 尝试解析响应体获取具体错误消息
|
|
// 尝试解析响应体获取具体错误消息
|
|
|
let errorMsg = `获取子商品列表失败: ${response.status}`
|
|
let errorMsg = `获取子商品列表失败: ${response.status}`
|
|
@@ -119,7 +122,27 @@ export function GoodsSpecSelector({
|
|
|
// 如果不可见或parentGoodsId无效,清空规格选项
|
|
// 如果不可见或parentGoodsId无效,清空规格选项
|
|
|
setIsLoading(false)
|
|
setIsLoading(false)
|
|
|
}
|
|
}
|
|
|
- }, [visible, parentGoodsId, currentSpec])
|
|
|
|
|
|
|
+ }, [visible, parentGoodsId])
|
|
|
|
|
+
|
|
|
|
|
+ // 监听currentSpec变化,更新选中状态(不重新加载数据)
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (visible && specOptions.length > 0 && currentSpec) {
|
|
|
|
|
+ const foundSpec = specOptions.find(spec => spec.name === currentSpec)
|
|
|
|
|
+ if (foundSpec) {
|
|
|
|
|
+ setSelectedSpec(foundSpec)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果找不到对应的规格,清空选中状态
|
|
|
|
|
+ setSelectedSpec(null)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [visible, currentSpec, specOptions])
|
|
|
|
|
+
|
|
|
|
|
+ // 监听currentQuantity变化,更新数量状态(不重新加载数据)
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ if (visible) {
|
|
|
|
|
+ setQuantity(currentQuantity)
|
|
|
|
|
+ }
|
|
|
|
|
+ }, [visible, currentQuantity])
|
|
|
|
|
|
|
|
const handleSpecSelect = (spec: SpecOption) => {
|
|
const handleSpecSelect = (spec: SpecOption) => {
|
|
|
setSelectedSpec(spec)
|
|
setSelectedSpec(spec)
|
|
@@ -150,13 +173,6 @@ export function GoodsSpecSelector({
|
|
|
validatePriceCalculation()
|
|
validatePriceCalculation()
|
|
|
}, [selectedSpec, quantity])
|
|
}, [selectedSpec, quantity])
|
|
|
|
|
|
|
|
- // 监听 currentQuantity 变化,同步更新 quantity 状态
|
|
|
|
|
- useEffect(() => {
|
|
|
|
|
- if (visible) {
|
|
|
|
|
- setQuantity(currentQuantity)
|
|
|
|
|
- }
|
|
|
|
|
- }, [visible, currentQuantity])
|
|
|
|
|
-
|
|
|
|
|
// 模态窗口显示/隐藏时控制页面滚动
|
|
// 模态窗口显示/隐藏时控制页面滚动
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
if (visible) {
|
|
if (visible) {
|
|
@@ -168,6 +184,7 @@ export function GoodsSpecSelector({
|
|
|
}
|
|
}
|
|
|
}, [visible])
|
|
}, [visible])
|
|
|
|
|
|
|
|
|
|
+
|
|
|
// 获取最大可购买数量
|
|
// 获取最大可购买数量
|
|
|
const getMaxQuantity = () => {
|
|
const getMaxQuantity = () => {
|
|
|
if (!selectedSpec) return 999
|
|
if (!selectedSpec) return 999
|
|
@@ -282,7 +299,8 @@ export function GoodsSpecSelector({
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
onConfirm(selectedSpec, quantity, actionType)
|
|
onConfirm(selectedSpec, quantity, actionType)
|
|
|
- onClose()
|
|
|
|
|
|
|
+ // 不重置选中状态和数量,避免视觉闪烁
|
|
|
|
|
+ // 用户可以继续选择其他规格,或者再次添加相同规格
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!visible) return null
|
|
if (!visible) return null
|