rez-table-listing-mui 0.0.8 → 0.0.10

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.
@@ -19,7 +19,6 @@ import Topbar from "./topbar";
19
19
  import DefaultPagination from "./pagination/default";
20
20
  import TableDND from "./table-dnd";
21
21
  import Table from "./table";
22
- import { useFullscreenPopoverContainer } from "../libs/hooks/useFullScreen";
23
22
 
24
23
  function TableWrapper<T>({
25
24
  data = [],
@@ -29,27 +28,57 @@ function TableWrapper<T>({
29
28
  featureOptions,
30
29
  topbarOptions,
31
30
  nestedComponent,
32
- loadingOptions = {
33
- isLoading: false,
34
- },
31
+ loadingOptions = { isLoading: false },
32
+ customRenderFn = {},
33
+ shouldHideColumn,
35
34
  }: CraftTableProps<T>) {
36
- // Throw an error if data is not an array
37
35
  if (!Array.isArray(data)) {
38
36
  throw new Error("data must be an array of objects.");
39
37
  }
38
+ const [metaColumns, setMetaColumns] = useState<ColumnDef<T>[]>([]);
40
39
 
41
- const [newColumns] = useState<ColumnDef<T>[]>(() =>
42
- columns.map((col, index) => ({
43
- ...col,
44
- id:
45
- "accessorKey" in col
46
- ? (col as { accessorKey: string }).accessorKey
47
- : `col_${index}`,
48
- }))
49
- );
40
+ useEffect(() => {
41
+ const updatedColumns = columns
42
+ .filter((col) => {
43
+ const accessorKey =
44
+ "accessorKey" in col ? (col as { accessorKey: string }).accessorKey : undefined;
45
+ return typeof shouldHideColumn === "function"
46
+ ? !shouldHideColumn(accessorKey)
47
+ : true;
48
+ })
49
+ .map((col, index) => {
50
+ const id =
51
+ "accessorKey" in col
52
+ ? (col as { accessorKey: string }).accessorKey
53
+ : `col_${index}`;
54
+
55
+ const cell = (ctx: any) => {
56
+ if (col.meta?.type === "custom" && col?.meta?.propName) {
57
+ const customFn = customRenderFn?.[col.meta.propName];
58
+ return typeof customFn === "function"
59
+ ? customFn({
60
+ value: ctx.getValue(),
61
+ row: ctx.row,
62
+ table: ctx.table.getRowModel().rows,
63
+ })
64
+ : ctx.getValue();
65
+ }
66
+
67
+ if (typeof col.cell === "function") {
68
+ return col.cell(ctx);
69
+ }
70
+
71
+ return ctx.getValue();
72
+ };
73
+
74
+ return { ...col, id, cell };
75
+ });
76
+
77
+ setMetaColumns(updatedColumns);
78
+ }, [columns, tableStates]);
50
79
 
51
80
  const [columnOrder, setColumnOrder] = useState<string[]>(() =>
52
- newColumns.map((c) => c.id!)
81
+ metaColumns.map((c) => c.id!)
53
82
  );
54
83
 
55
84
  const [isCompactTable, setIsCompactTable] = useState<boolean>(
@@ -57,7 +86,6 @@ function TableWrapper<T>({
57
86
  );
58
87
 
59
88
  const tableRef = useRef<HTMLDivElement>(null);
60
- const { isFullscreen } = useFullscreenPopoverContainer();
61
89
 
62
90
  useEffect(() => {
63
91
  setIsCompactTable(featureOptions?.compactTable ?? false);
@@ -78,13 +106,13 @@ function TableWrapper<T>({
78
106
  const craftTopbarOptions: TopbarOptionsProps = {
79
107
  showColumnToggle: true,
80
108
  showCompactTableToggle: true,
81
- // showFullscreenToggle: true,
109
+ showFullscreenToggle: true,
82
110
  showChangeLayoutToggle: true,
83
111
  viewMoreToggle: true,
84
112
  showAddNewButton: true,
85
- // showSearch: true,
86
- showFilterToggle: true,
87
- showSearchToggle: true,
113
+ showSearch: true,
114
+ showFilter: true,
115
+ showCustomizationToggle: true,
88
116
 
89
117
  // Add other conditions above topbarOptions
90
118
  ...topbarOptions,
@@ -134,7 +162,7 @@ function TableWrapper<T>({
134
162
 
135
163
  const table = useReactTable({
136
164
  data,
137
- columns,
165
+ columns: metaColumns,
138
166
  state: {
139
167
  sorting,
140
168
  pagination,
@@ -229,11 +257,7 @@ function TableWrapper<T>({
229
257
  />
230
258
  )}
231
259
 
232
- <div
233
- className={`ts__table__wrapper ${
234
- isFullscreen ? "is-fullscreen" : ""
235
- }`}
236
- >
260
+ <div className="ts__table__wrapper">
237
261
  {enableColumnReordering ? (
238
262
  <TableDND
239
263
  table={table}
@@ -29,11 +29,7 @@
29
29
 
30
30
  .ts__table__wrapper {
31
31
  overflow-x: auto;
32
- // max-height: calc(100vh - 106px);
33
-
34
- &.is-fullscreen {
35
- max-height: calc(100vh - 10px); // override in fullscreen
36
- }
32
+ max-height: calc(100vh - 106px);
37
33
 
38
34
  .ts__table.ts--striped > .ts__body > .ts__body__tr:nth-of-type(odd) {
39
35
  background-color: var(--grey-200);
@@ -98,12 +98,9 @@ function TableBody<T>({
98
98
  <React.Fragment key={row.id}>
99
99
  {renderedRow}
100
100
  <tr>
101
- {" "}
102
- {/* // this is for the nested row. Extra line is coming in ui when
103
- opening the nested data. */}
104
- {/* <td colSpan={table.getAllLeafColumns().length}> */}
105
- {NestedComponent && <NestedComponent {...{ row }} />}
106
- {/* </td> */}
101
+ <td colSpan={table.getAllLeafColumns().length}>
102
+ {NestedComponent && <NestedComponent {...{ row }} />}
103
+ </td>
107
104
  </tr>
108
105
  </React.Fragment>
109
106
  );
@@ -3,8 +3,8 @@ import {
3
3
  ChangeLayoutIcon,
4
4
  SearchIcon,
5
5
  SortingIcon,
6
+ CustomizationIcon,
6
7
  HideColumnIcon,
7
- FilterIcon,
8
8
  } from "../../assets/svg";
9
9
  import { Popover } from "@mui/material";
10
10
  import LayoutSelector from "../table-change-layout";
@@ -68,9 +68,9 @@ function Topbar<T>({
68
68
  showColumnToggle,
69
69
  showChangeLayoutToggle,
70
70
  viewMoreToggle,
71
- showSearchToggle,
72
- // showFilter,
73
- showFilterToggle,
71
+ showSearch,
72
+ showFilter,
73
+ showCustomizationToggle,
74
74
  } = topbarOptions ?? {};
75
75
 
76
76
  const { container: fullscreenContainer } = useFullscreenPopoverContainer();
@@ -115,7 +115,7 @@ function Topbar<T>({
115
115
  </div>
116
116
  )} */}
117
117
 
118
- {showSearchToggle && (
118
+ {showSearch && (
119
119
  <div className="search-wrapper" ref={searchContainerRef}>
120
120
  <div
121
121
  className="search-icon ts--button"
@@ -183,7 +183,7 @@ function Topbar<T>({
183
183
  </>
184
184
  )}
185
185
 
186
- {showFilterToggle && (
186
+ {showFilter && (
187
187
  <>
188
188
  <div
189
189
  className="filter ts--button"
@@ -204,9 +204,9 @@ function Topbar<T>({
204
204
  </>
205
205
  )}
206
206
 
207
- {showFilterToggle && (
207
+ {showCustomizationToggle && (
208
208
  <div className="customization ts--button" title="Filter">
209
- <FilterIcon />
209
+ <CustomizationIcon />
210
210
  </div>
211
211
  )}
212
212
 
@@ -135,15 +135,6 @@ const ViewMore = ({
135
135
  color: "#000",
136
136
  fontSize: "13px",
137
137
  }}
138
- MenuProps={{
139
- container: fullscreenContainer,
140
- disablePortal: false,
141
- PaperProps: {
142
- style: {
143
- zIndex: 1500,
144
- },
145
- },
146
- }}
147
138
  >
148
139
  <MenuItem value="Comfy">Comfy</MenuItem>
149
140
  <MenuItem value="Compact">Compact</MenuItem>
@@ -170,15 +161,6 @@ const ViewMore = ({
170
161
  color: "#000",
171
162
  fontSize: "13px",
172
163
  }}
173
- MenuProps={{
174
- container: fullscreenContainer,
175
- disablePortal: false,
176
- PaperProps: {
177
- style: {
178
- zIndex: 1500,
179
- },
180
- },
181
- }}
182
164
  >
183
165
  <MenuItem value="None">None</MenuItem>
184
166
  <MenuItem value="Status">Status</MenuItem>
@@ -10,7 +10,7 @@ import { CraftTableOptionsProps } from "../../types/table-options";
10
10
  export function useCraftTable() {
11
11
  const [pagination, setPagination] = useState<PaginationState>({
12
12
  pageIndex: 0,
13
- pageSize: 500,
13
+ pageSize: 25,
14
14
  });
15
15
  const [sorting, setSorting] = useState<SortingState>([]);
16
16
  const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
@@ -26,9 +26,7 @@ export const useDefaultColumns = () => {
26
26
  // ),
27
27
  cell: ({ row, getValue }) => {
28
28
  return (
29
- <div
30
- style={{ paddingLeft: `${row.depth * 2}rem`, display: "flex" }}
31
- >
29
+ <div style={{ paddingLeft: `${row.depth * 2}rem` }}>
32
30
  {row.getCanExpand() ? (
33
31
  <button
34
32
  style={{
@@ -50,8 +48,7 @@ export const useDefaultColumns = () => {
50
48
  ) : (
51
49
  <span style={{ marginRight: "0.8rem" }}></span>
52
50
  )}
53
- {/* {getValue<boolean>()} */}
54
- <p style={{ marginTop: "0.5rem" }}>{getValue<boolean>()}</p>
51
+ {getValue<boolean>()}
55
52
  </div>
56
53
  );
57
54
  },
@@ -86,17 +83,30 @@ export const useDefaultColumns = () => {
86
83
  size: 150,
87
84
  meta: { label: "Progress" },
88
85
  },
89
- {
90
- header: "Status",
91
- accessorKey: "status",
92
- meta: { label: "Status" },
93
- },
94
86
  {
95
87
  header: "Department",
96
88
  accessorKey: "department",
97
89
  size: 150,
98
90
  meta: { label: "Department" },
99
91
  },
92
+ {
93
+ header: "Status",
94
+ accessorKey: "status",
95
+ meta: {
96
+ label: "Status",
97
+ type: "custom",
98
+ propName: "renderStatus",
99
+ },
100
+ },
101
+ {
102
+ header: "Action",
103
+ accessorKey: "action",
104
+ meta: {
105
+ label: "Action",
106
+ type: "custom",
107
+ propName: "renderAction",
108
+ },
109
+ },
100
110
  ],
101
111
  []
102
112
  );
@@ -28,28 +28,49 @@ interface LoadingOptionsProps {
28
28
  export interface TopbarOptionsProps {
29
29
  leftSideComponent?: React.ReactNode;
30
30
  rightSideComponent?: React.ReactNode;
31
- // showFullscreenToggle?: boolean;
31
+ showFullscreenToggle?: boolean;
32
32
  showColumnToggle?: boolean;
33
33
  showCompactTableToggle?: boolean;
34
34
  showChangeLayoutToggle?: boolean;
35
35
  viewMoreToggle?: boolean;
36
- showAddNewButton?: boolean; // Currently not used
37
- // showSearch?: boolean;
38
- showSearchToggle?: boolean;
39
- showFilterToggle?: boolean;
36
+ showAddNewButton?: boolean;
37
+ showSearch?: boolean;
38
+ showFilter?: boolean;
39
+ showCustomizationToggle?: boolean;
40
40
  }
41
41
 
42
+ export interface CustomRenderContext<T> {
43
+ value: unknown;
44
+ row: Row<T>;
45
+ table: Row<T>[];
46
+ }
47
+
48
+ export type CustomRenderFnMap<T> = {
49
+ [key: string]: (ctx: CustomRenderContext<T>) => React.ReactNode;
50
+ };
51
+
52
+ type CustomColumnMeta = {
53
+ type?: "custom";
54
+ propName?: string;
55
+ [key: string]: any;
56
+ };
57
+
58
+ type CustomColumnDef<T> = ColumnDef<T> & {
59
+ meta?: CustomColumnMeta;
60
+ };
61
+
42
62
  export interface CraftTableProps<T> {
43
63
  data: T[];
44
- columns: ColumnDef<T>[];
64
+ columns: CustomColumnDef<T>[];
45
65
  tableStates: CraftTableOptionsProps;
46
66
  paginationOptions?: CraftTablePaginationProps;
47
67
  featureOptions?: CraftTableFeatureProps;
48
68
  nestedComponent?: React.ComponentType<{ row: Row<T> }>;
49
69
  loadingOptions?: LoadingOptionsProps;
50
70
  topbarOptions?: TopbarOptionsProps;
71
+ customRenderFn?: CustomRenderFnMap<T>;
72
+ shouldHideColumn?: (accessorKey?: string) => boolean;
51
73
  }
52
-
53
74
  export interface CraftTableComponentProps<T> {
54
75
  table: Table<T>;
55
76
  featureOptions: CraftTableFeatureProps;