@react-fabric/data 1.0.0-beta.0 → 1.0.0-beta.11

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 (83) hide show
  1. package/dist/cjs/checklist/CheckList.js +143 -0
  2. package/dist/cjs/histogram/Histogram.js +14 -5
  3. package/dist/cjs/hooks/useSelectableList.js +54 -0
  4. package/dist/cjs/index.js +2 -0
  5. package/dist/cjs/table/AddColumn.js +56 -0
  6. package/dist/cjs/table/BodyCell.js +47 -6
  7. package/dist/cjs/table/BodyRow.js +107 -0
  8. package/dist/cjs/table/CheckboxCell.js +44 -0
  9. package/dist/cjs/table/Context.js +43 -0
  10. package/dist/cjs/table/EmptyCell.js +52 -0
  11. package/dist/cjs/table/HeaderCell.js +78 -32
  12. package/dist/cjs/table/Table.js +173 -96
  13. package/dist/cjs/table/types.js +32 -0
  14. package/dist/cjs/table/useResizer.js +65 -0
  15. package/dist/cjs/table/useTableColumns.js +45 -8
  16. package/dist/cjs/table/useVirtualTable.js +56 -25
  17. package/dist/cjs/tree/TreePanel.js +14 -2
  18. package/dist/cjs/tree/useTree.js +7 -0
  19. package/dist/cjs/virtual/Gallery.js +11 -4
  20. package/dist/cjs/virtual/List.js +11 -4
  21. package/dist/esm/checklist/CheckList.js +143 -0
  22. package/dist/esm/histogram/Histogram.js +14 -5
  23. package/dist/esm/hooks/useSelectableList.js +54 -0
  24. package/dist/esm/index.js +1 -0
  25. package/dist/esm/table/AddColumn.js +56 -0
  26. package/dist/esm/table/BodyCell.js +47 -6
  27. package/dist/esm/table/BodyRow.js +107 -0
  28. package/dist/esm/table/CheckboxCell.js +44 -0
  29. package/dist/esm/table/Context.js +42 -0
  30. package/dist/esm/table/EmptyCell.js +52 -0
  31. package/dist/esm/table/HeaderCell.js +80 -34
  32. package/dist/esm/table/Table.js +174 -97
  33. package/dist/esm/table/types.js +30 -0
  34. package/dist/esm/table/useResizer.js +65 -0
  35. package/dist/esm/table/useTableColumns.js +46 -9
  36. package/dist/esm/table/useVirtualTable.js +57 -26
  37. package/dist/esm/tree/TreePanel.js +15 -3
  38. package/dist/esm/tree/useTree.js +7 -0
  39. package/dist/esm/virtual/Gallery.js +11 -4
  40. package/dist/esm/virtual/List.js +11 -4
  41. package/dist/types/checklist/CheckList.d.ts +30 -0
  42. package/dist/types/histogram/Histogram.d.ts +3 -3
  43. package/dist/types/hooks/useSelectableList.d.ts +28 -0
  44. package/dist/types/index.d.ts +3 -0
  45. package/dist/types/table/AddColumn.d.ts +5 -0
  46. package/dist/types/table/BodyCell.d.ts +4 -3
  47. package/dist/types/table/BodyRow.d.ts +20 -0
  48. package/dist/types/table/CheckboxCell.d.ts +4 -0
  49. package/dist/types/table/Context.d.ts +8 -0
  50. package/dist/types/table/EmptyCell.d.ts +2 -0
  51. package/dist/types/table/HeaderCell.d.ts +2 -2
  52. package/dist/types/table/Table.d.ts +1 -1
  53. package/dist/types/table/types.d.ts +41 -7
  54. package/dist/types/table/useResizer.d.ts +1 -0
  55. package/dist/types/table/useTableColumns.d.ts +8 -6
  56. package/dist/types/table/useVirtualTable.d.ts +13 -1
  57. package/dist/types/tree/TreePanel.d.ts +1 -1
  58. package/dist/types/tree/types.d.ts +6 -2
  59. package/dist/types/tree/useTree.d.ts +1 -0
  60. package/dist/types/virtual/Gallery.d.ts +3 -2
  61. package/dist/types/virtual/List.d.ts +3 -2
  62. package/package.json +6 -5
  63. package/src/checklist/CheckList.tsx +222 -0
  64. package/src/histogram/Histogram.tsx +7 -2
  65. package/src/hooks/useSelectableList.ts +94 -0
  66. package/src/index.ts +3 -0
  67. package/src/table/AddColumn.tsx +55 -0
  68. package/src/table/BodyCell.tsx +47 -6
  69. package/src/table/BodyRow.tsx +142 -0
  70. package/src/table/CheckboxCell.tsx +48 -0
  71. package/src/table/Context.tsx +70 -0
  72. package/src/table/EmptyCell.tsx +57 -0
  73. package/src/table/HeaderCell.tsx +93 -25
  74. package/src/table/Table.tsx +194 -87
  75. package/src/table/types.ts +42 -6
  76. package/src/table/useResizer.ts +66 -0
  77. package/src/table/useTableColumns.ts +52 -19
  78. package/src/table/useVirtualTable.ts +61 -2
  79. package/src/tree/TreePanel.tsx +12 -1
  80. package/src/tree/types.ts +8 -1
  81. package/src/tree/useTree.ts +5 -0
  82. package/src/virtual/Gallery.tsx +4 -1
  83. package/src/virtual/List.tsx +4 -1
@@ -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
@@ -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,14 @@
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";
31
+ export { type TableColumn, type TableRef } from "./table/types";
30
32
  export { TreePanel } from "./tree/TreePanel";
33
+ export { type TreeNodeType as TreeNode } from "./tree/types";
31
34
  export { VirtualGallery, type VirtualGalleryRef } from "./virtual/Gallery";
32
35
  export { VirtualList, type VirtualListRef } from "./virtual/List";
33
36
 
@@ -0,0 +1,55 @@
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 { CoreIcons, Dropdown, Icon, Menu, MenuItem } from "@react-fabric/core";
25
+ import { type TableColumn } from "./types";
26
+
27
+ export const AddColumn = ({
28
+ columns,
29
+ onClick,
30
+ }: {
31
+ columns: TableColumn[];
32
+ onClick: (id: string) => void;
33
+ }) => {
34
+ return (
35
+ <div className="group font-medium w-6 flex flex-nowrap text-start items-end">
36
+ <Dropdown placement="bottom-end">
37
+ <Icon
38
+ icon={CoreIcons.insert}
39
+ className="p-1 rounded-t rounded-b-none hover:outline"
40
+ />
41
+ <Menu className="text-sm" onClick={onClick}>
42
+ {columns.map((col) => (
43
+ <MenuItem
44
+ key={String(col.id)}
45
+ id={String(col.id)}
46
+ data-dropdown-dismiss="false"
47
+ label={`${col.label ?? String(col.id)}`}
48
+ icon={!col.hidden ? CoreIcons.tick : ""}
49
+ />
50
+ ))}
51
+ </Menu>
52
+ </Dropdown>
53
+ </div>
54
+ );
55
+ };
@@ -21,23 +21,64 @@
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
- import { type ColumnType } from "./types";
27
+ import { useMemo } from "react";
28
+ import { useTableContext } from "./Context";
29
+ import {
30
+ COL_DEFAULT_WIDTH,
31
+ COL_MAX_WIDTH,
32
+ COL_MIN_WIDTH,
33
+ type TableColumn,
34
+ } from "./types";
26
35
 
27
36
  export const BodyCell = ({
28
37
  column,
29
38
  item,
39
+ index,
30
40
  }: {
31
- column: ColumnType;
41
+ column: TableColumn;
32
42
  item: KeyValue;
43
+ index: number;
33
44
  }) => {
45
+ const { widths } = useTableContext();
46
+
47
+ const width = useMemo(
48
+ () => widths.get(column.id.toString()) ?? column.width,
49
+ [widths, column],
50
+ );
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 (column.dataType === "date") {
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
+
34
75
  return (
35
76
  <div
36
77
  className="overflow-hidden bg-inherit start-0"
37
78
  style={{
38
- width: column.width ?? "12rem",
39
- minWidth: column.minWidth ?? "2rem",
40
- maxWidth: column.maxWidth ?? "32rem",
79
+ width: width ?? COL_DEFAULT_WIDTH,
80
+ minWidth: column.minWidth ?? COL_MIN_WIDTH,
81
+ maxWidth: column.maxWidth ?? COL_MAX_WIDTH,
41
82
  }}
42
83
  >
43
84
  <div
@@ -47,7 +88,7 @@ export const BodyCell = ({
47
88
  column.align === "end" && "text-end",
48
89
  )}
49
90
  >
50
- {column.renderer?.(item[column.id], item) ?? item[column.id]}
91
+ {content}
51
92
  </div>
52
93
  </div>
53
94
  );
@@ -0,0 +1,142 @@
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
+ )}
80
+ onClick={() =>
81
+ checkableRows && keyProperty && !onRowClick
82
+ ? toggleChecked(data[keyProperty])
83
+ : onRowClick?.(data)
84
+ }
85
+ >
86
+ <div className={wrapperStart}>
87
+ {!!canExpand && (
88
+ <div
89
+ className={classNames(
90
+ "group font-medium w-6 flex flex-nowrap text-start items-center",
91
+ !canExpand(data) && "pointer-events-none opacity-35",
92
+ )}
93
+ >
94
+ <Icon
95
+ className="p-1 text-tint-700 hover:text-primary-600"
96
+ icon={expanded ? CoreIcons.collapse : CoreIcons.expand}
97
+ onClick={(e) => [
98
+ keyProperty && toggleExpand(data[keyProperty]),
99
+ e.stopPropagation(),
100
+ ]}
101
+ />
102
+ </div>
103
+ )}
104
+ {checkableRows && keyProperty && (
105
+ <CheckboxCell
106
+ onClick={() => toggleChecked(data[keyProperty])}
107
+ state={checked}
108
+ />
109
+ )}
110
+ {state.start?.map((col, ids) => (
111
+ <BodyCell
112
+ key={`${String(rowKey)}:${ids}`}
113
+ column={col}
114
+ item={data}
115
+ index={ids}
116
+ />
117
+ ))}
118
+ </div>
119
+ {state.cols?.map((col, idc) => (
120
+ <BodyCell
121
+ key={`${String(rowKey)}:${idc}`}
122
+ column={col}
123
+ item={data}
124
+ index={idc}
125
+ />
126
+ ))}
127
+ <div className={wrapperEnd}>
128
+ {state.end?.map((col, ide) => (
129
+ <BodyCell
130
+ key={`${String(rowKey)}:${ide}`}
131
+ column={col}
132
+ item={data}
133
+ index={ide}
134
+ />
135
+ ))}
136
+ {hideableColumns && <div className="w-6" />}
137
+ </div>
138
+ </div>
139
+ {expanded && expandRow(data)}
140
+ </div>
141
+ );
142
+ };
@@ -0,0 +1,48 @@
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 { CoreIcons, Icon } from "@react-fabric/core";
25
+
26
+ export const CheckboxCell = ({
27
+ state,
28
+ onClick,
29
+ }: {
30
+ state: 0 | 1 | 2;
31
+ onClick?: () => void;
32
+ }) => {
33
+ return (
34
+ <div className="group font-medium border-e w-6 flex flex-nowrap text-start items-center">
35
+ <Icon
36
+ icon={
37
+ state === 2
38
+ ? CoreIcons.checkboxInt
39
+ : state === 1
40
+ ? CoreIcons.checkboxOn
41
+ : CoreIcons.checkboxOff
42
+ }
43
+ className="p-1 text-tint-700 hover:text-primary-600"
44
+ onClick={(e) => [onClick?.(), e.stopPropagation()]}
45
+ />
46
+ </div>
47
+ );
48
+ };