From e204b91102f91c5ebaf8efc5eb838a906010cc57 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 30 Jul 2026 00:57:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=A8=E9=80=89/=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E5=85=A8=E9=80=89=E4=BB=85=E6=93=8D=E4=BD=9C=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=8F=AF=E8=A7=81=E9=A1=B9=EF=BC=8C=E9=81=BF=E5=85=8D=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E7=8A=B6=E6=80=81=E4=B8=8B=E6=95=B0=E6=8D=AE=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/useProductList/useBatchSelection.ts | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/web/src/pages/products/hooks/useProductList/useBatchSelection.ts b/apps/web/src/pages/products/hooks/useProductList/useBatchSelection.ts index 5de32c7e0..ee1301d35 100644 --- a/apps/web/src/pages/products/hooks/useProductList/useBatchSelection.ts +++ b/apps/web/src/pages/products/hooks/useProductList/useBatchSelection.ts @@ -1,4 +1,4 @@ -import { useState } from "react" +import { useState, useCallback } from "react" import type { ProductItem } from "../../types" export const useBatchSelection = (filteredProducts: ProductItem[]) => { @@ -9,15 +9,25 @@ export const useBatchSelection = (filteredProducts: ProductItem[]) => { const allSelected = filteredProducts.length > 0 && filteredProducts.every((p) => selectedIds.has(p.id)) - const handleSelectAll = () => { + const handleSelectAll = useCallback(() => { if (allSelected) { - setSelectedIds(new Set()) + // 仅取消选中当前可见的项,保留筛选外的选中状态 + setSelectedIds((prev) => { + const next = new Set(prev) + filteredProducts.forEach((p) => next.delete(p.id)) + return next + }) } else { - setSelectedIds(new Set(filteredProducts.map((p) => p.id))) + // 选中所有当前可见项 + setSelectedIds((prev) => { + const next = new Set(prev) + filteredProducts.forEach((p) => next.add(p.id)) + return next + }) } - } + }, [allSelected, filteredProducts]) - const handleToggleSelect = (id: string) => { + const handleToggleSelect = useCallback((id: string) => { setSelectedIds((prev) => { const next = new Set(prev) if (next.has(id)) { @@ -27,9 +37,9 @@ export const useBatchSelection = (filteredProducts: ProductItem[]) => { } return next }) - } + }, []) - const clearSelection = () => setSelectedIds(new Set()) + const clearSelection = useCallback(() => setSelectedIds(new Set()), []) return { selectedIds,