gbs-add-block 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # GBS Building Blocks 2.0 (v1.2.3)
1
+ # GBS Building Blocks 2.0 (v1.2.4)
2
2
 
3
3
  Latest and upgraded version of GBS building blocks with headless UI and removed dependencies.
4
4
 
@@ -6,9 +6,9 @@ Latest and upgraded version of GBS building blocks with headless UI and removed
6
6
 
7
7
  For detailed documentation on usage and props, Please visit: [Building Block Documentation v2.0](https://gramprokit.vercel.app)
8
8
 
9
- ## What's New 🎉 (Ver 1.2.3)
9
+ ## What's New 🎉 (Ver 1.2.4)
10
10
 
11
- - Bug Fix
11
+ - Bug Fix For Server Side Pagination in Data Grid Component
12
12
 
13
13
  ## Authors
14
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gbs-add-block",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "React Component Library",
5
5
  "type": "module",
6
6
  "files": [
@@ -41,6 +41,8 @@ export const GridProvider: React.FC<{
41
41
  showTotalPages = false,
42
42
  onSearch = () => {},
43
43
  onToolbarButtonClick = () => {},
44
+ initialFilters = [],
45
+ initialSearchParam = "",
44
46
  } = props;
45
47
 
46
48
  // Data source management
@@ -88,6 +90,7 @@ export const GridProvider: React.FC<{
88
90
  resetPage,
89
91
  setTotalPages,
90
92
  searchParamValue,
93
+ initialSearchParam,
91
94
  });
92
95
 
93
96
  // Filtering functionality
@@ -109,6 +112,8 @@ export const GridProvider: React.FC<{
109
112
  setTotalPages,
110
113
  pageSettings,
111
114
  activeFilterArrayValue,
115
+ lazy,
116
+ initialFilters,
112
117
  });
113
118
 
114
119
  // Row selection functionality
@@ -27,7 +27,7 @@ export const useColumns = (columns: any[], workingDataSource: any[]) => {
27
27
  return prevColumns;
28
28
  });
29
29
  }
30
- }, [columns, workingDataSource]);
30
+ }, [columns]); // Remove workingDataSource from dependencies to prevent reset on data fetch
31
31
 
32
32
  return { workingColumns, setWorkingColumns };
33
33
  };
@@ -17,6 +17,8 @@ interface UseFilteringProps {
17
17
  setTotalPages: (pages: number) => void;
18
18
  pageSettings: any;
19
19
  activeFilterArrayValue?: (filters: ActiveFilterArrayValue[]) => void;
20
+ lazy: boolean;
21
+ initialFilters?: any[];
20
22
  }
21
23
 
22
24
  export const useFiltering = ({
@@ -31,8 +33,10 @@ export const useFiltering = ({
31
33
  setTotalPages,
32
34
  pageSettings,
33
35
  activeFilterArrayValue,
36
+ lazy,
37
+ initialFilters = [],
34
38
  }: UseFilteringProps) => {
35
- const [activeFilterArray, setActiveFilterArray] = useState<any>([]);
39
+ const [activeFilterArray, setActiveFilterArray] = useState<any>(initialFilters);
36
40
 
37
41
  const toggleFilterPopup = useCallback(
38
42
  (index: number) => {
@@ -56,13 +60,21 @@ export const useFiltering = ({
56
60
  } = handleApplyFilterHelper(event, columns, workingDataSource);
57
61
 
58
62
  setWorkingColumns(updatedColumns);
59
- setWorkingDataSource(updatedFullDataSource);
60
- setActiveFilterArray(updatedActiveFilterArray);
61
- if (activeFilterArrayValue)
62
- activeFilterArrayValue?.(updatedActiveFilterArray);
63
+
64
+ const newActiveFilters = updatedActiveFilterArray;
65
+ setActiveFilterArray(newActiveFilters);
63
66
 
64
- const totalPages = resetPage(updatedFullDataSource, pageSettings);
65
- setTotalPages(totalPages);
67
+ if (!lazy) {
68
+ setWorkingDataSource(updatedFullDataSource);
69
+ const totalPages = resetPage(updatedFullDataSource, pageSettings);
70
+ setTotalPages(totalPages);
71
+ } else {
72
+ // In lazy mode, we reset page to 0 but keep totalPages from pageSettings
73
+ resetPage(workingDataSource, pageSettings);
74
+ }
75
+
76
+ if (activeFilterArrayValue)
77
+ activeFilterArrayValue?.(newActiveFilters);
66
78
  },
67
79
  [
68
80
  columns,
@@ -73,6 +85,8 @@ export const useFiltering = ({
73
85
  resetPage,
74
86
  pageSettings,
75
87
  setTotalPages,
88
+ lazy,
89
+ activeFilterArray,
76
90
  ]
77
91
  );
78
92
 
@@ -88,13 +102,28 @@ export const useFiltering = ({
88
102
  } = clearFilterHelper(event, workingColumns, clearDataSource);
89
103
 
90
104
  setWorkingColumns(updatedColumns);
91
- setWorkingDataSource(updatedDataSource);
92
- setActiveFilterArray(updatedActiveFilterArray);
93
- if (activeFilterArrayValue)
94
- activeFilterArrayValue?.(updatedActiveFilterArray);
95
105
 
96
- const totalPages = resetPage(updatedDataSource, pageSettings);
97
- setTotalPages(totalPages);
106
+ // Manual fallback: ensure the filter is removed from the active array
107
+ let newActiveFilters = updatedActiveFilterArray;
108
+ if (event.type === "clearFilter" && event.columnHeader) {
109
+ newActiveFilters = activeFilterArray.filter(
110
+ (f: any) => f.filterColumn !== event.columnHeader
111
+ );
112
+ }
113
+
114
+ setActiveFilterArray(newActiveFilters);
115
+
116
+ if (!lazy) {
117
+ setWorkingDataSource(updatedDataSource);
118
+ const totalPages = resetPage(updatedDataSource, pageSettings);
119
+ setTotalPages(totalPages);
120
+ } else {
121
+ // In lazy mode, we reset page to 0 but keep totalPages from pageSettings
122
+ resetPage(workingDataSource, pageSettings);
123
+ }
124
+
125
+ if (activeFilterArrayValue)
126
+ activeFilterArrayValue?.(newActiveFilters);
98
127
  },
99
128
  [
100
129
  dataSource,
@@ -106,6 +135,8 @@ export const useFiltering = ({
106
135
  resetPage,
107
136
  pageSettings,
108
137
  setTotalPages,
138
+ lazy,
139
+ activeFilterArray,
109
140
  ]
110
141
  );
111
142
 
@@ -41,33 +41,16 @@ export const usePagination = (
41
41
  // This will handle the case when dataSource changes
42
42
  // and adjust the current page accordingly in managed workflow
43
43
  useEffect(() => {
44
- if (lazy && dataSource && prevDataLength > 0) {
45
- const currentDataLength = dataSource.length;
46
-
47
- if (currentDataLength > prevDataLength) {
48
- const lastPage = totalPages - 1;
49
- setCurrentPage(lastPage);
50
- updatePageRange(lastPage);
51
- } else if (currentDataLength < prevDataLength) {
52
- const rowsPerPage = lazy ? 1 : pageSettings?.pageNumber || 10;
53
- const startIndex = currentPage * rowsPerPage;
44
+ setPrevDataLength(dataSource?.length || 0);
45
+ }, [dataSource?.length]);
54
46
 
55
- if (startIndex >= currentDataLength && currentPage > 0) {
56
- const newPage = currentPage - 1;
57
- setCurrentPage(newPage);
58
- updatePageRange(newPage);
59
- }
60
- }
47
+ // Defensive check: if current page is out of bounds due to totalPages shrinking, reset to 0
48
+ useEffect(() => {
49
+ if (totalPages > 0 && currentPage >= totalPages) {
50
+ setCurrentPage(0);
51
+ updatePageRange(0);
61
52
  }
62
- setPrevDataLength(dataSource?.length || 0);
63
- }, [
64
- dataSource?.length,
65
- totalPages,
66
- updatePageRange,
67
- prevDataLength,
68
- currentPage,
69
- lazy,
70
- ]);
53
+ }, [totalPages, currentPage, updatePageRange]);
71
54
 
72
55
  const nextPage = useCallback(() => {
73
56
  setCurrentPage((prevPage) => {
@@ -111,14 +94,16 @@ export const usePagination = (
111
94
  );
112
95
 
113
96
  const resetPage = useCallback((dataSource: any, pageSettings: any) => {
114
- const newTotalPages = Math.ceil(
115
- dataSource.length / pageSettings.pageNumber
116
- );
97
+ const newTotalPages =
98
+ lazy && pageSettings?.totalCount
99
+ ? Math.ceil(pageSettings.totalCount / pageSettings.pageNumber)
100
+ : Math.ceil((dataSource?.length || 0) / (pageSettings?.pageNumber || 10));
101
+
117
102
  setCurrentPage(0);
118
103
  setPageStart(0);
119
104
  setPageEnd(Math.min(10, newTotalPages));
120
105
  return newTotalPages;
121
- }, []);
106
+ }, [lazy]);
122
107
 
123
108
  return {
124
109
  currentPage,
@@ -10,6 +10,7 @@ interface UseSearchProps {
10
10
  resetPage: (data: any[], settings: any) => number;
11
11
  setTotalPages: (pages: number) => void;
12
12
  searchParamValue?: (value: string) => void;
13
+ initialSearchParam?: string;
13
14
  }
14
15
 
15
16
  export const useSearch = ({
@@ -22,8 +23,9 @@ export const useSearch = ({
22
23
  resetPage,
23
24
  setTotalPages,
24
25
  searchParamValue,
26
+ initialSearchParam = "",
25
27
  }: UseSearchProps) => {
26
- const [searchParam, setSearchParam] = useState("");
28
+ const [searchParam, setSearchParam] = useState(initialSearchParam);
27
29
 
28
30
  const handleSearchInput = useCallback(
29
31
  (e: any) => {
@@ -66,6 +68,8 @@ export const useSearch = ({
66
68
  setWorkingDataSource(filteredData);
67
69
  const totalPages = resetPage(filteredData, pageSettings);
68
70
  setTotalPages(totalPages);
71
+ } else {
72
+ resetPage(workingDataSource, pageSettings);
69
73
  }
70
74
  },
71
75
  [
@@ -1,10 +1,17 @@
1
1
  import React from "react";
2
2
  import { useState } from "react";
3
3
 
4
- export default function FilterPopup({ show, columnHeader, filterAction }: any) {
5
- const [filterValue, setFilterValue] = useState("");
6
- const [filterType, setFilterType] = useState("contains");
7
- const [isFilterActive, setIsFilterActive] = useState(false);
4
+ export default function FilterPopup({
5
+ show,
6
+ columnHeader,
7
+ filterAction,
8
+ activeFilter,
9
+ }: any) {
10
+ const [filterValue, setFilterValue] = useState(activeFilter?.filterValue || "");
11
+ const [filterType, setFilterType] = useState(
12
+ activeFilter?.filterCondition || activeFilter?.filterType || "contains"
13
+ );
14
+ const [isFilterActive, setIsFilterActive] = useState(!!activeFilter);
8
15
 
9
16
  const handleFilterInput = (e: any) => {
10
17
  const filterKeyword = e.target.value;
@@ -104,6 +104,9 @@ const GridBody = () => {
104
104
  <FilterPopup
105
105
  show={column.showFilterPopup ?? false}
106
106
  columnHeader={column.field}
107
+ activeFilter={activeFilterArray.find(
108
+ (f) => f.filterColumn === column.field
109
+ )}
107
110
  filterAction={(action: any) =>
108
111
  handleFilterAction(action, colIndex)
109
112
  }
@@ -41,6 +41,8 @@ export type GridProps = {
41
41
  showTotalPages?: boolean;
42
42
  onSearch?: (value: string) => void;
43
43
  onToolbarButtonClick?: (action: string) => void;
44
+ initialFilters?: ActiveFilterArrayValue[];
45
+ initialSearchParam?: string;
44
46
  };
45
47
 
46
48
  interface GridColumn {