From dc87097dfefd35042627489fd786665e55003bed Mon Sep 17 00:00:00 2001 From: dongchengjie <37543964+dongchengjie@users.noreply.github.com> Date: Wed, 3 Jul 2024 03:08:01 +0800 Subject: [PATCH] fix: search-box takes no effect in rule-editor #1288 --- .../profile/rules-editor-viewer.tsx | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/components/profile/rules-editor-viewer.tsx b/src/components/profile/rules-editor-viewer.tsx index 6cf0f233..39d7ea18 100644 --- a/src/components/profile/rules-editor-viewer.tsx +++ b/src/components/profile/rules-editor-viewer.tsx @@ -1,4 +1,4 @@ -import { ReactNode, useEffect, useState } from "react"; +import { ReactNode, useEffect, useMemo, useState } from "react"; import { useLockFn } from "ahooks"; import yaml from "js-yaml"; import { useTranslation } from "react-i18next"; @@ -247,6 +247,11 @@ export const RulesEditorViewer = (props: Props) => { const [appendSeq, setAppendSeq] = useState([]); const [deleteSeq, setDeleteSeq] = useState([]); + const filteredRuleList = useMemo( + () => ruleList.filter((rule) => match(rule)), + [ruleList, match] + ); + const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { @@ -482,7 +487,7 @@ export const RulesEditorViewer = (props: Props) => { 0 ? 1 : 0) + (appendSeq.length > 0 ? 1 : 0) } @@ -518,24 +523,29 @@ export const RulesEditorViewer = (props: Props) => { ); - } else if (index < ruleList.length + shift) { + } else if (index < filteredRuleList.length + shift) { let newIndex = index - shift; return ( { - if (deleteSeq.includes(ruleList[newIndex])) { + if (deleteSeq.includes(filteredRuleList[newIndex])) { setDeleteSeq( - deleteSeq.filter((v) => v !== ruleList[newIndex]) + deleteSeq.filter( + (v) => v !== filteredRuleList[newIndex] + ) ); } else { - setDeleteSeq((prev) => [...prev, ruleList[newIndex]]); + setDeleteSeq((prev) => [ + ...prev, + filteredRuleList[newIndex], + ]); } }} />