when i change tab it removes data in tab inside render why?
The issue you are experiencing with data being removed from the tab when changing tabs is due to how the renderCards function is implemented and how the filtering logic interacts with the selected tab.
The getFilteredGroups logic: In your getFilteredGroups function, you filter the rules based on the search term and you call this function for every rendered tab, but it is using the entire rules from all tabs rather than just the rules specific to the currently active tab.
Reassigning groups: When you change the activeTab, you are not preserving the previously filtered results for each tab independently. As a result, the rules relevant to the new tab may not be fetched when switching, which can lead to an empty display.
Fixing the Issue:
To keep the filtered groups for each tab, you can refactor the state management and how the filtering is applied. Here’s a suggested approach to maintain separate filtered results for each tab:
Create a state to hold the filtered results for each tab separately.
Update the filtered results when searching in the specific tab.
Here’s an updated version of the AutomationTabs component considering the necessary changes:
Filtered Groups State: Added a state filteredGroups to maintain the filtered rules specific to each tab.
Filtering Logic: The filtering logic is now executed inside the handleFiltering function, which updates the filtered results whenever the rules or search term change.
Rendering Cards: The renderCards function now gets the filtered results from the filteredGroups state specific to the active tab.
Advantages of This Approach:
Maintains the filtered state for each tab across different search inputs.
Ensures the data for a tab remains consistent and relevant even after switching tabs.
Cleaner separation of concerns in your component structure.
By applying these changes, the issue of losing data in the tabs when switched should be resolved.
import React, { useState, useEffect } from "react";
import { FwTabs, FwTab, FwTabPanel, FwSpinner, FwPagination, FwButton } from "@freshworks/crayons/react";
import AutomationCard from "./AutomationCard";
import { getTicketCreationRules, getTicketUpdationRules, getHourlyRules } from '../common';
Removed Search State: Removed any variables related to search such as searchTerm and searchTriggered.
Removed Search UI: Deleted the search input field and button in the JSX.
Adjust Filtering Logic: The getFilteredGroups function has been removed, and the groups retrieved for each tab are directly grouped without any search filtering.
Rendering Logic: Cards are rendered based on the currently chosen tab without the complexity of filter conditions.
With these changes, the component now displays all the rules relevant to the selected tab without any search functionality.
To set the "Ticket Update" tab as the default active tab when the component first renders, you only need to change the initial value of the activeTab state from "ticketcreate" to "ticketupdate".
Set Default Active Tab: Changed the default value for activeTab to "ticketupdate" so that it is shown as the active tab when the component first loads.
Now, when your component renders, the "Ticket Update" tab will be displayed by default.