rez-table-listing-mui 0.0.9 → 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.
@@ -28,27 +28,57 @@ function TableWrapper<T>({
28
28
  featureOptions,
29
29
  topbarOptions,
30
30
  nestedComponent,
31
- loadingOptions = {
32
- isLoading: false,
33
- },
31
+ loadingOptions = { isLoading: false },
32
+ customRenderFn = {},
33
+ shouldHideColumn,
34
34
  }: CraftTableProps<T>) {
35
- // Throw an error if data is not an array
36
35
  if (!Array.isArray(data)) {
37
36
  throw new Error("data must be an array of objects.");
38
37
  }
38
+ const [metaColumns, setMetaColumns] = useState<ColumnDef<T>[]>([]);
39
39
 
40
- const [newColumns] = useState<ColumnDef<T>[]>(() =>
41
- columns.map((col, index) => ({
42
- ...col,
43
- id:
44
- "accessorKey" in col
45
- ? (col as { accessorKey: string }).accessorKey
46
- : `col_${index}`,
47
- }))
48
- );
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]);
49
79
 
50
80
  const [columnOrder, setColumnOrder] = useState<string[]>(() =>
51
- newColumns.map((c) => c.id!)
81
+ metaColumns.map((c) => c.id!)
52
82
  );
53
83
 
54
84
  const [isCompactTable, setIsCompactTable] = useState<boolean>(
@@ -132,7 +162,7 @@ function TableWrapper<T>({
132
162
 
133
163
  const table = useReactTable({
134
164
  data,
135
- columns,
165
+ columns: metaColumns,
136
166
  state: {
137
167
  sorting,
138
168
  pagination,
@@ -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
  );
@@ -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
  );
@@ -39,17 +39,38 @@ export interface TopbarOptionsProps {
39
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;