@react-fabric/data 1.0.0-beta.3 → 1.0.0-beta.30

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.
Files changed (80) hide show
  1. package/dist/cjs/checklist/CheckList.js +143 -0
  2. package/dist/cjs/histogram/Histogram.js +16 -5
  3. package/dist/cjs/hooks/usePagination.js +7 -4
  4. package/dist/cjs/hooks/useSelectableList.js +54 -0
  5. package/dist/cjs/index.js +2 -0
  6. package/dist/cjs/table/BodyCell.js +38 -5
  7. package/dist/cjs/table/BodyRow.js +107 -0
  8. package/dist/cjs/table/CheckboxCell.js +1 -1
  9. package/dist/cjs/table/HeaderCell.js +29 -36
  10. package/dist/cjs/table/Table.js +104 -66
  11. package/dist/cjs/table/useResizer.js +4 -4
  12. package/dist/cjs/table/useVirtualTable.js +118 -17
  13. package/dist/cjs/tree/TreeNode.js +44 -24
  14. package/dist/cjs/tree/TreePanel.js +28 -10
  15. package/dist/cjs/tree/useTree.js +96 -14
  16. package/dist/cjs/tree/utils.js +20 -5
  17. package/dist/cjs/virtual/Gallery.js +41 -27
  18. package/dist/cjs/virtual/List.js +41 -29
  19. package/dist/esm/checklist/CheckList.js +143 -0
  20. package/dist/esm/histogram/Histogram.js +16 -5
  21. package/dist/esm/hooks/usePagination.js +7 -4
  22. package/dist/esm/hooks/useSelectableList.js +54 -0
  23. package/dist/esm/index.js +1 -0
  24. package/dist/esm/table/BodyCell.js +38 -5
  25. package/dist/esm/table/BodyRow.js +107 -0
  26. package/dist/esm/table/CheckboxCell.js +1 -1
  27. package/dist/esm/table/HeaderCell.js +30 -37
  28. package/dist/esm/table/Table.js +106 -68
  29. package/dist/esm/table/useResizer.js +4 -4
  30. package/dist/esm/table/useVirtualTable.js +120 -19
  31. package/dist/esm/tree/TreeNode.js +46 -26
  32. package/dist/esm/tree/TreePanel.js +28 -10
  33. package/dist/esm/tree/useTree.js +98 -16
  34. package/dist/esm/tree/utils.js +20 -5
  35. package/dist/esm/virtual/Gallery.js +41 -27
  36. package/dist/esm/virtual/List.js +41 -29
  37. package/dist/types/checklist/CheckList.d.ts +30 -0
  38. package/dist/types/histogram/Histogram.d.ts +3 -3
  39. package/dist/types/hooks/useSelectableList.d.ts +28 -0
  40. package/dist/types/index.d.ts +2 -1
  41. package/dist/types/table/AddColumn.d.ts +2 -2
  42. package/dist/types/table/BodyCell.d.ts +4 -3
  43. package/dist/types/table/BodyRow.d.ts +20 -0
  44. package/dist/types/table/EmptyCell.d.ts +2 -2
  45. package/dist/types/table/HeaderCell.d.ts +2 -2
  46. package/dist/types/table/Table.d.ts +1 -1
  47. package/dist/types/table/types.d.ts +19 -8
  48. package/dist/types/table/useTableColumns.d.ts +6 -6
  49. package/dist/types/table/useVirtualTable.d.ts +20 -5
  50. package/dist/types/tree/TreeNode.d.ts +1 -1
  51. package/dist/types/tree/TreePanel.d.ts +1 -1
  52. package/dist/types/tree/types.d.ts +25 -5
  53. package/dist/types/tree/useTree.d.ts +2 -9
  54. package/dist/types/tree/utils.d.ts +4 -1
  55. package/dist/types/virtual/Gallery.d.ts +11 -3
  56. package/dist/types/virtual/List.d.ts +7 -3
  57. package/package.json +8 -7
  58. package/src/checklist/CheckList.tsx +222 -0
  59. package/src/histogram/Histogram.tsx +9 -2
  60. package/src/hooks/usePagination.tsx +13 -7
  61. package/src/hooks/useSelectableList.ts +94 -0
  62. package/src/index.ts +2 -1
  63. package/src/table/AddColumn.tsx +2 -2
  64. package/src/table/BodyCell.tsx +34 -5
  65. package/src/table/BodyRow.tsx +143 -0
  66. package/src/table/CheckboxCell.tsx +1 -1
  67. package/src/table/EmptyCell.tsx +2 -2
  68. package/src/table/HeaderCell.tsx +27 -13
  69. package/src/table/Table.tsx +141 -73
  70. package/src/table/types.ts +22 -7
  71. package/src/table/useResizer.ts +5 -3
  72. package/src/table/useTableColumns.ts +7 -7
  73. package/src/table/useVirtualTable.ts +142 -27
  74. package/src/tree/TreeNode.tsx +90 -56
  75. package/src/tree/TreePanel.tsx +23 -3
  76. package/src/tree/types.ts +32 -4
  77. package/src/tree/useTree.ts +102 -23
  78. package/src/tree/utils.ts +23 -8
  79. package/src/virtual/Gallery.tsx +43 -32
  80. package/src/virtual/List.tsx +38 -25
@@ -0,0 +1,222 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { Badge, CoreIcons, EmptyContent, Icon } from "@react-fabric/core";
25
+ import {
26
+ type BadgeType,
27
+ type TestProps,
28
+ } from "@react-fabric/core/dist/types/types";
29
+ import { isString } from "@react-fabric/utilities";
30
+ import {
31
+ Fragment,
32
+ isValidElement,
33
+ useEffect,
34
+ useMemo,
35
+ useState,
36
+ type FC,
37
+ } from "react";
38
+ import { useTranslation } from "react-i18next";
39
+ import {
40
+ useSelectableList,
41
+ type SelectableProps,
42
+ } from "../hooks/useSelectableList";
43
+
44
+ export interface CheckListItem extends KeyValue {
45
+ id: string;
46
+ label: string;
47
+ icon?: string;
48
+ badge?: BadgeType;
49
+ isDisabled?: boolean;
50
+ }
51
+
52
+ export interface CheckListProps extends SelectableProps, TestProps {
53
+ /**
54
+ * list items
55
+ */
56
+ items: CheckListItem[];
57
+ /**
58
+ * allow negative selections
59
+ */
60
+ allowNegative?: boolean;
61
+ /**
62
+ * max list items
63
+ */
64
+ maxCount?: number;
65
+ /**
66
+ * message for empty list
67
+ */
68
+ emptyMessage?: string;
69
+
70
+ sortItems?: boolean;
71
+ }
72
+
73
+ const CheckItem: FC<
74
+ Omit<CheckListItem, "selected"> & {
75
+ selected?: number;
76
+ allowNegative?: boolean;
77
+ }
78
+ > = ({
79
+ id,
80
+ allowNegative,
81
+ selected,
82
+ icon,
83
+ label,
84
+ isDisabled,
85
+ onClick,
86
+ badge,
87
+ ...aria
88
+ }: Omit<CheckListItem, "selected"> & {
89
+ selected?: number;
90
+ allowNegative?: boolean;
91
+ }) => {
92
+ return (
93
+ <div
94
+ role="none"
95
+ className="ax-checkList__item"
96
+ data-disabled={isDisabled}
97
+ onClick={() => onClick(id, false)}
98
+ onContextMenu={(e) => [
99
+ allowNegative && onClick(id, true),
100
+ e.preventDefault(),
101
+ ]}
102
+ {...aria}
103
+ >
104
+ {!allowNegative && (
105
+ <Icon
106
+ data-type="checkbox"
107
+ className="ax-checkList__checkbox"
108
+ data-selected={selected === 1}
109
+ icon={selected ? CoreIcons.checkboxInt : CoreIcons.checkboxOff}
110
+ />
111
+ )}
112
+ {allowNegative && (
113
+ <Fragment>
114
+ <Icon
115
+ data-type="multiple"
116
+ className="ax-checkList__checkbox"
117
+ icon={
118
+ selected === 1
119
+ ? CoreIcons.expand
120
+ : selected === -1
121
+ ? CoreIcons.collapse
122
+ : CoreIcons.checkboxOff
123
+ }
124
+ data-selected={selected}
125
+ />
126
+ <div className="ax-checkList__checkbox">
127
+ <Icon
128
+ data-type="positive"
129
+ onClick={(e) => [(onClick(id, false), e.stopPropagation())]}
130
+ icon={selected === 1 ? CoreIcons.expandActive : CoreIcons.expand}
131
+ data-selected={selected === 1}
132
+ />
133
+ <Icon
134
+ data-type="negative"
135
+ onClick={(e) => [(onClick(id, true), e.stopPropagation())]}
136
+ icon={
137
+ selected === -1 ? CoreIcons.collapseActive : CoreIcons.collapse
138
+ }
139
+ data-selected={selected === -1}
140
+ />
141
+ </div>
142
+ </Fragment>
143
+ )}
144
+ {isValidElement(icon) && icon}
145
+ {isString(icon) && <Icon icon={icon} />}
146
+ <label>{label}</label>
147
+ <Badge {...badge} />
148
+ </div>
149
+ );
150
+ };
151
+
152
+ export const CheckList: FC<CheckListProps> = ({
153
+ items = [],
154
+ maxCount = 0,
155
+ allowNegative,
156
+ emptyMessage,
157
+ sortItems = true,
158
+ selected,
159
+ nonselected,
160
+ onChange,
161
+ onClick,
162
+ ...aria
163
+ }: CheckListProps) => {
164
+ const { t } = useTranslation("data");
165
+ const { selection, toggleSelection } = useSelectableList({
166
+ items,
167
+ selected,
168
+ nonselected,
169
+ onChange,
170
+ onClick,
171
+ });
172
+ const [showMore, setShowMore] = useState(false);
173
+
174
+ useEffect(() => {
175
+ setShowMore(false);
176
+ }, [items]);
177
+
178
+ const listItems = useMemo(() => {
179
+ return [...items]
180
+ .sort((a, b) => {
181
+ if (sortItems && selection[a.id] !== selection[b.id]) {
182
+ if (selection[a.id] === 1) return -1;
183
+ if (selection[b.id] === 1) return 1;
184
+ if (selection[a.id] === -1) return -1;
185
+ if (selection[b.id] === -1) return 1;
186
+ }
187
+ return 0;
188
+ })
189
+ .slice(0, !showMore && maxCount > 0 ? maxCount : undefined);
190
+ }, [items, selection, maxCount, showMore, sortItems]);
191
+
192
+ return (
193
+ <div {...aria}>
194
+ {listItems.map((item, index) => (
195
+ <CheckItem
196
+ {...item}
197
+ key={index}
198
+ onClick={toggleSelection}
199
+ allowNegative={allowNegative}
200
+ selected={selection[item.id]}
201
+ />
202
+ ))}
203
+ {maxCount > 0 && items.length > maxCount && (
204
+ <div className="ax-moreLink">
205
+ <span
206
+ role="none"
207
+ className="link"
208
+ onClick={() => setShowMore(!showMore)}
209
+ >
210
+ ...{t(`core:action.${showMore ? "less" : "more"}`)}
211
+ </span>
212
+ </div>
213
+ )}
214
+ {listItems.length === 0 && (
215
+ <EmptyContent
216
+ className="text-sm"
217
+ message={emptyMessage ?? t("checkList.empty")}
218
+ />
219
+ )}
220
+ </div>
221
+ );
222
+ };
@@ -31,6 +31,7 @@ import {
31
31
  import {
32
32
  type ColorType,
33
33
  type PaletteType,
34
+ type TestProps,
34
35
  } from "@react-fabric/core/dist/types/types";
35
36
  import { compareValues, Format } from "@react-fabric/utilities";
36
37
  import classNames from "classnames";
@@ -49,7 +50,7 @@ interface HistogramItem {
49
50
  disabled?: boolean;
50
51
  }
51
52
 
52
- export interface HistogramProps {
53
+ export interface HistogramProps extends TestProps {
53
54
  /**
54
55
  * histogram items list
55
56
  */
@@ -88,6 +89,7 @@ export const Histogram = ({
88
89
  emptyTitle,
89
90
  emptyMessage,
90
91
  onClick,
92
+ ...aria
91
93
  }: HistogramProps) => {
92
94
  const { t } = useTranslation("data");
93
95
  const maxTotal = useMemo(() => {
@@ -104,6 +106,7 @@ export const Histogram = ({
104
106
  return (
105
107
  <div
106
108
  className={classes.histogram}
109
+ {...aria}
107
110
  style={
108
111
  {
109
112
  "--color": `var(--${color})`,
@@ -125,8 +128,10 @@ export const Histogram = ({
125
128
  className={classNames(
126
129
  "relative cursor-pointer flex flex-nowrap items-center select-none overflow-hidden min-h-6 my-px",
127
130
  classes.histoLabel,
128
- (item.count === 0 || item.disabled) && "pointer-events-none",
131
+ ((item.total ?? item.count) === 0 || item.disabled) &&
132
+ "pointer-events-none",
129
133
  )}
134
+ data-testid={item.id}
130
135
  onClick={(e) => [onClick?.(item.id), e.stopPropagation()]}
131
136
  >
132
137
  <div
@@ -145,6 +150,7 @@ export const Histogram = ({
145
150
  key="negative"
146
151
  color="danger"
147
152
  size="sm"
153
+ stopPropagation
148
154
  icon={CoreIcons.funnelMinus}
149
155
  aria-label="Filter Negative"
150
156
  onClick={() => onClick?.(item.id, true)}
@@ -153,6 +159,7 @@ export const Histogram = ({
153
159
  key="positive"
154
160
  color="primary"
155
161
  size="sm"
162
+ stopPropagation
156
163
  icon={CoreIcons.funnelPlus}
157
164
  aria-label="Filter"
158
165
  onClick={() => onClick?.(item.id)}
@@ -111,9 +111,12 @@ export const usePagination = ({
111
111
 
112
112
  /** ***************** set page *******************/
113
113
  useEffect(() => {
114
- setPage(Math.min(currentPage, totalPages - 1));
115
- onChange?.(Math.min(currentPage, totalPages - 1));
116
- }, [currentPage, totalPages]);
114
+ if (totalRecords > -1) {
115
+ const actual = Math.min(currentPage, totalPages - 1);
116
+ setPage(actual);
117
+ actual !== currentPage && onChange?.(actual);
118
+ }
119
+ }, [currentPage, totalRecords, totalPages]);
117
120
 
118
121
  const headLabel = useMemo(
119
122
  () => (
@@ -126,10 +129,13 @@ export const usePagination = ({
126
129
  [page, perPage, totalRecords],
127
130
  );
128
131
 
129
- const onPageChange = useCallback((page: number) => {
130
- setPage(page);
131
- startTransition(() => onChange?.(page));
132
- }, []);
132
+ const onPageChange = useCallback(
133
+ (page: number) => {
134
+ setPage(page);
135
+ startTransition(() => onChange?.(page));
136
+ },
137
+ [onChange],
138
+ );
133
139
 
134
140
  return { page, totalPages, headLabel, ranges, onPageChange };
135
141
  };
@@ -0,0 +1,94 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { useCallback, useEffect, useRef, useState, useTransition } from "react";
25
+
26
+ export interface SelectableProps {
27
+ /**
28
+ * list items
29
+ */
30
+ items: Array<{ label: string } & KeyValue>;
31
+ /**
32
+ * selected item ids
33
+ */
34
+ selected?: string[];
35
+ /**
36
+ * selected negative item ids
37
+ */
38
+ nonselected?: string[];
39
+ /**
40
+ * change callback
41
+ */
42
+ onChange?: (ids: string[], nonIds?: string[]) => void;
43
+ /**
44
+ * item click callback
45
+ */
46
+ onClick?: (id: string, isNegative: boolean) => void;
47
+ }
48
+
49
+ export const useSelectableList = ({
50
+ items,
51
+ selected,
52
+ nonselected,
53
+ onChange,
54
+ onClick,
55
+ }: SelectableProps) => {
56
+ const selectionRef = useRef<KeyValue<number>>({});
57
+ const [selection, setSelection] = useState<KeyValue<number>>({});
58
+ const [, startTransition] = useTransition();
59
+ useEffect(() => {
60
+ selectionRef.current = items.reduce(
61
+ (ret, { id }) => ({
62
+ ...ret,
63
+ [id]: selected?.includes?.(id)
64
+ ? 1
65
+ : nonselected?.includes?.(id)
66
+ ? -1
67
+ : 0,
68
+ }),
69
+ {},
70
+ );
71
+ setSelection({ ...selectionRef.current });
72
+ }, [items, selected, nonselected]);
73
+
74
+ const toggleSelection = useCallback((id: string, isNegative = false) => {
75
+ const check = isNegative ? -1 : 1;
76
+ if (selectionRef.current[id] !== check) selectionRef.current[id] = check;
77
+ else if (selectionRef.current[id] === check) selectionRef.current[id] = 0;
78
+ const change: string[][] = [[], []];
79
+ Object.entries(selectionRef.current).forEach(([id, select]) =>
80
+ select === 1
81
+ ? change[0].push(id)
82
+ : select === -1
83
+ ? change[1].push(id)
84
+ : undefined,
85
+ );
86
+ setSelection({ ...selectionRef.current });
87
+ startTransition(() => {
88
+ onClick?.(id, isNegative);
89
+ onChange?.(change[0], change[1]);
90
+ });
91
+ }, []);
92
+
93
+ return { selection, toggleSelection };
94
+ };
package/src/index.ts CHANGED
@@ -23,11 +23,12 @@
23
23
 
24
24
  import "./i18n";
25
25
 
26
+ export { CheckList } from "./checklist/CheckList";
26
27
  export { Histogram } from "./histogram/Histogram";
27
28
  export { JsonViewer } from "./json/JsonViewer";
28
29
  export { Pagination } from "./pagination/Pagination";
29
30
  export { Table } from "./table/Table";
30
- export { type TableRef } from "./table/types";
31
+ export { type TableColumn, type TableRef } from "./table/types";
31
32
  export { TreePanel } from "./tree/TreePanel";
32
33
  export { type TreeNodeType as TreeNode } from "./tree/types";
33
34
  export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
@@ -22,13 +22,13 @@
22
22
  */
23
23
 
24
24
  import { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
25
- import { type ColumnType } from "./types";
25
+ import { type TableColumn } from "./types";
26
26
 
27
27
  export const AddColumn = ({
28
28
  columns,
29
29
  onClick,
30
30
  }: {
31
- columns: ColumnType[];
31
+ columns: TableColumn[];
32
32
  onClick: (id: string) => void;
33
33
  }) => {
34
34
  return (
@@ -21,6 +21,8 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
+ import { DateDisplay } from "@react-fabric/date";
25
+ import { Format, getByPath, isTrue } from "@react-fabric/utilities";
24
26
  import classNames from "classnames";
25
27
  import { useMemo } from "react";
26
28
  import { useTableContext } from "./Context";
@@ -28,15 +30,17 @@ import {
28
30
  COL_DEFAULT_WIDTH,
29
31
  COL_MAX_WIDTH,
30
32
  COL_MIN_WIDTH,
31
- type ColumnType,
33
+ type TableColumn,
32
34
  } from "./types";
33
35
 
34
36
  export const BodyCell = ({
35
37
  column,
36
38
  item,
39
+ index,
37
40
  }: {
38
- column: ColumnType;
41
+ column: TableColumn;
39
42
  item: KeyValue;
43
+ index: number;
40
44
  }) => {
41
45
  const { widths } = useTableContext();
42
46
 
@@ -45,9 +49,32 @@ export const BodyCell = ({
45
49
  [widths, column],
46
50
  );
47
51
 
52
+ const content = useMemo(() => {
53
+ const value = getByPath(item, column.id.toString());
54
+ if (column.renderer != null) {
55
+ return column.renderer(item[column.id], item, index);
56
+ }
57
+ if (column.dataType === "boolean") {
58
+ const map = column.valueMap ?? { true: "Yes", false: "no" };
59
+ return map[`${isTrue(value) ? "true" : "false"}`];
60
+ }
61
+ if (column.valueMap != null) {
62
+ if (column.dataType === "string" && value in column.valueMap) {
63
+ return column.valueMap[value];
64
+ }
65
+ }
66
+ if (["date", "time", "datetime"].includes(column.dataType ?? "")) {
67
+ return <DateDisplay date={value} format={column.format} />;
68
+ }
69
+ if (column.dataType === "number") {
70
+ return Format.number(value, column.format);
71
+ }
72
+ return value ?? "";
73
+ }, [item, column]);
74
+
48
75
  return (
49
76
  <div
50
- className="overflow-hidden bg-inherit start-0"
77
+ className="overflow-hidden bg-inherit start-0 border-e"
51
78
  style={{
52
79
  width: width ?? COL_DEFAULT_WIDTH,
53
80
  minWidth: column.minWidth ?? COL_MIN_WIDTH,
@@ -56,12 +83,14 @@ export const BodyCell = ({
56
83
  >
57
84
  <div
58
85
  className={classNames(
59
- "px-2 py-1 border-e truncate",
86
+ "px-2 py-1",
87
+ column.className,
88
+ column.truncate && "truncate",
60
89
  column.align === "center" && "text-center",
61
90
  column.align === "end" && "text-end",
62
91
  )}
63
92
  >
64
- {column.renderer?.(item[column.id], item) ?? item[column.id]}
93
+ {content}
65
94
  </div>
66
95
  </div>
67
96
  );
@@ -0,0 +1,143 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import classNames from "classnames";
25
+ import { BodyCell } from "./BodyCell";
26
+ import { CheckboxCell } from "./CheckboxCell";
27
+ import { type TableColumn } from "./types";
28
+ import { CoreIcons, Icon } from "@react-fabric/core";
29
+
30
+ export const BodyRow = ({
31
+ rowKey,
32
+ index,
33
+ data,
34
+ keyProperty,
35
+ measureElement,
36
+ state,
37
+ hideableColumns,
38
+ wrapperStart,
39
+ wrapperEnd,
40
+ onRowClick,
41
+ checkableRows,
42
+ checked,
43
+ toggleChecked,
44
+ canExpand,
45
+ expanded,
46
+ toggleExpand,
47
+ expandRow,
48
+ }: {
49
+ rowKey: Key;
50
+ index: number;
51
+ data: KeyValue;
52
+ keyProperty?: Key;
53
+ measureElement: AnyObject;
54
+ state: KeyValue<TableColumn[]>;
55
+ hideableColumns?: boolean;
56
+ wrapperStart: string;
57
+ wrapperEnd: string;
58
+ checkableRows?: boolean;
59
+ checked: 0 | 1;
60
+ onRowClick?: AnyObject;
61
+ canExpand?: AnyObject;
62
+ expandRow?: AnyObject;
63
+ expanded: boolean;
64
+ toggleExpand: (key: Key) => void;
65
+ toggleChecked: (key: Key) => void;
66
+ }) => {
67
+ return (
68
+ <div
69
+ ref={measureElement}
70
+ data-index={index}
71
+ className={index % 2 ? "bg-even" : "bg-odd"}
72
+ >
73
+ <div
74
+ role="none"
75
+ data-row={index}
76
+ className={classNames(
77
+ "flex flex-nowrap datatable-row data-[hilight]:outline-1 -outline-offset-1 outline-primary-500",
78
+ onRowClick && "hover:bg-primary-50 active:bg-primary-100",
79
+ index % 2 ? "bg-even" : "bg-odd",
80
+ )}
81
+ onClick={() =>
82
+ checkableRows && keyProperty && !onRowClick
83
+ ? toggleChecked(data[keyProperty])
84
+ : onRowClick?.(data)
85
+ }
86
+ >
87
+ <div className={wrapperStart}>
88
+ {!!canExpand && (
89
+ <div
90
+ className={classNames(
91
+ "group font-medium w-6 flex flex-nowrap text-start py-1",
92
+ !canExpand(data) && "pointer-events-none opacity-35",
93
+ )}
94
+ >
95
+ <Icon
96
+ className="p-1 text-tint-700 hover:text-primary-600"
97
+ icon={expanded ? CoreIcons.collapse : CoreIcons.expand}
98
+ onClick={(e) => [
99
+ keyProperty && toggleExpand(data[keyProperty]),
100
+ e.stopPropagation(),
101
+ ]}
102
+ />
103
+ </div>
104
+ )}
105
+ {checkableRows && keyProperty && (
106
+ <CheckboxCell
107
+ onClick={() => toggleChecked(data[keyProperty])}
108
+ state={checked}
109
+ />
110
+ )}
111
+ {state.start?.map((col, ids) => (
112
+ <BodyCell
113
+ key={`${String(rowKey)}:${ids}`}
114
+ column={col}
115
+ item={data}
116
+ index={ids}
117
+ />
118
+ ))}
119
+ </div>
120
+ {state.cols?.map((col, idc) => (
121
+ <BodyCell
122
+ key={`${String(rowKey)}:${idc}`}
123
+ column={col}
124
+ item={data}
125
+ index={idc}
126
+ />
127
+ ))}
128
+ <div className={wrapperEnd}>
129
+ {state.end?.map((col, ide) => (
130
+ <BodyCell
131
+ key={`${String(rowKey)}:${ide}`}
132
+ column={col}
133
+ item={data}
134
+ index={ide}
135
+ />
136
+ ))}
137
+ {hideableColumns && <div className="w-6" />}
138
+ </div>
139
+ </div>
140
+ {expanded && expandRow(data)}
141
+ </div>
142
+ );
143
+ };
@@ -31,7 +31,7 @@ export const CheckboxCell = ({
31
31
  onClick?: () => void;
32
32
  }) => {
33
33
  return (
34
- <div className="group font-medium border-e w-6 flex flex-nowrap text-start items-center">
34
+ <div className="group font-medium border-e w-6 flex flex-nowrap text-start py-1">
35
35
  <Icon
36
36
  icon={
37
37
  state === 2
@@ -27,7 +27,7 @@ import {
27
27
  COL_DEFAULT_WIDTH,
28
28
  COL_MAX_WIDTH,
29
29
  COL_MIN_WIDTH,
30
- type ColumnType,
30
+ type TableColumn,
31
31
  } from "./types";
32
32
 
33
33
  export const EmptyCell = ({
@@ -35,7 +35,7 @@ export const EmptyCell = ({
35
35
  width: _width,
36
36
  maxWidth,
37
37
  minWidth,
38
- }: ColumnType) => {
38
+ }: TableColumn) => {
39
39
  const { widths } = useTableContext();
40
40
 
41
41
  const width = useMemo(