From ea0989ae23b6a23450491c4994bf4e46687dbeb2 Mon Sep 17 00:00:00 2001 From: Amal K Joy <153802538+amal-k-joy@users.noreply.github.com> Date: Wed, 11 Dec 2024 13:17:57 +0530 Subject: [PATCH] fix(Condition builder): initial state getting reset on re-rendering (#6555) --- .../ConditionBuilderContent.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/ibm-products/src/components/ConditionBuilder/ConditionBuilderContent/ConditionBuilderContent.tsx b/packages/ibm-products/src/components/ConditionBuilder/ConditionBuilderContent/ConditionBuilderContent.tsx index fd86105ec9..460c6235e9 100644 --- a/packages/ibm-products/src/components/ConditionBuilder/ConditionBuilderContent/ConditionBuilderContent.tsx +++ b/packages/ibm-products/src/components/ConditionBuilder/ConditionBuilderContent/ConditionBuilderContent.tsx @@ -99,8 +99,7 @@ const ConditionBuilderContent = ({ }, [actionState]); useEffect(() => { if (initialState?.enabledDefault) { - setRootState?.(initialConditionState.current as ConditionBuilderState); - initialConditionState.current = null; + setRootState?.(initialState.state); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [initialState]); @@ -120,12 +119,17 @@ const ConditionBuilderContent = ({ const onRemove = useCallback( (groupId) => { + const groups = rootState?.groups?.filter( + (group) => groupId !== group?.id + ); setRootState?.({ ...rootState, - groups: rootState - ? rootState?.groups?.filter((group) => groupId !== group?.id) - : [], + groups: rootState ? groups : [], }); + //set the initial state to empty. + if (groups?.length === 0) { + initialConditionState.current = null; + } }, [setRootState, rootState] );