carbon-react 120.0.0 → 120.1.0

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 (66) hide show
  1. package/esm/components/flat-table/__internal__/build-position-map.d.ts +2 -0
  2. package/esm/components/flat-table/__internal__/build-position-map.js +14 -0
  3. package/esm/components/flat-table/__internal__/index.d.ts +2 -0
  4. package/esm/components/flat-table/__internal__/index.js +2 -0
  5. package/esm/components/flat-table/__internal__/use-calculate-sticky-cells.d.ts +12 -0
  6. package/esm/components/flat-table/__internal__/use-calculate-sticky-cells.js +28 -0
  7. package/esm/components/flat-table/flat-table-cell/flat-table-cell.component.d.ts +2 -41
  8. package/esm/components/flat-table/flat-table-cell/flat-table-cell.component.js +26 -25
  9. package/esm/components/flat-table/flat-table-cell/flat-table-cell.style.d.ts +7 -2
  10. package/esm/components/flat-table/flat-table-checkbox/flat-table-checkbox.component.d.ts +3 -25
  11. package/esm/components/flat-table/flat-table-checkbox/flat-table-checkbox.component.js +21 -14
  12. package/esm/components/flat-table/flat-table-checkbox/flat-table-checkbox.style.d.ts +3 -1
  13. package/esm/components/flat-table/flat-table-head/flat-table-head.component.d.ts +4 -0
  14. package/esm/components/flat-table/flat-table-head/flat-table-head.component.js +21 -24
  15. package/esm/components/flat-table/flat-table-header/flat-table-header.component.d.ts +2 -26
  16. package/esm/components/flat-table/flat-table-header/flat-table-header.component.js +15 -13
  17. package/esm/components/flat-table/flat-table-header/flat-table-header.style.d.ts +3 -1
  18. package/esm/components/flat-table/flat-table-row/__internal__/flat-table-row-context.d.ts +12 -0
  19. package/esm/components/flat-table/flat-table-row/__internal__/flat-table-row-context.js +6 -0
  20. package/esm/components/flat-table/flat-table-row/__internal__/flat-table-row-draggable.component.d.ts +3 -1
  21. package/esm/components/flat-table/flat-table-row/__internal__/flat-table-row-draggable.component.js +17 -3
  22. package/esm/components/flat-table/flat-table-row/__internal__/sub-row-provider.d.ts +15 -0
  23. package/esm/components/flat-table/flat-table-row/__internal__/sub-row-provider.js +28 -0
  24. package/esm/components/flat-table/flat-table-row/flat-table-row.component.d.ts +1 -15
  25. package/esm/components/flat-table/flat-table-row/flat-table-row.component.js +85 -74
  26. package/esm/components/flat-table/flat-table-row/flat-table-row.style.d.ts +4 -1
  27. package/esm/components/flat-table/flat-table-row/flat-table-row.style.js +1 -9
  28. package/esm/components/flat-table/flat-table-row-header/flat-table-row-header.component.d.ts +4 -42
  29. package/esm/components/flat-table/flat-table-row-header/flat-table-row-header.component.js +33 -20
  30. package/esm/components/flat-table/flat-table-row-header/flat-table-row-header.style.d.ts +7 -1
  31. package/esm/components/flat-table/flat-table-row-header/flat-table-row-header.style.js +5 -1
  32. package/esm/components/flat-table/flat-table.component.js +24 -7
  33. package/lib/components/flat-table/__internal__/build-position-map.d.ts +2 -0
  34. package/lib/components/flat-table/__internal__/build-position-map.js +21 -0
  35. package/lib/components/flat-table/__internal__/index.d.ts +2 -0
  36. package/lib/components/flat-table/__internal__/index.js +20 -0
  37. package/lib/components/flat-table/__internal__/package.json +6 -0
  38. package/lib/components/flat-table/__internal__/use-calculate-sticky-cells.d.ts +12 -0
  39. package/lib/components/flat-table/__internal__/use-calculate-sticky-cells.js +36 -0
  40. package/lib/components/flat-table/flat-table-cell/flat-table-cell.component.d.ts +2 -41
  41. package/lib/components/flat-table/flat-table-cell/flat-table-cell.component.js +25 -24
  42. package/lib/components/flat-table/flat-table-cell/flat-table-cell.style.d.ts +7 -2
  43. package/lib/components/flat-table/flat-table-checkbox/flat-table-checkbox.component.d.ts +3 -25
  44. package/lib/components/flat-table/flat-table-checkbox/flat-table-checkbox.component.js +20 -13
  45. package/lib/components/flat-table/flat-table-checkbox/flat-table-checkbox.style.d.ts +3 -1
  46. package/lib/components/flat-table/flat-table-head/flat-table-head.component.d.ts +4 -0
  47. package/lib/components/flat-table/flat-table-head/flat-table-head.component.js +23 -25
  48. package/lib/components/flat-table/flat-table-header/flat-table-header.component.d.ts +2 -26
  49. package/lib/components/flat-table/flat-table-header/flat-table-header.component.js +14 -12
  50. package/lib/components/flat-table/flat-table-header/flat-table-header.style.d.ts +3 -1
  51. package/lib/components/flat-table/flat-table-row/__internal__/flat-table-row-context.d.ts +12 -0
  52. package/lib/components/flat-table/flat-table-row/__internal__/flat-table-row-context.js +13 -0
  53. package/lib/components/flat-table/flat-table-row/__internal__/flat-table-row-draggable.component.d.ts +3 -1
  54. package/lib/components/flat-table/flat-table-row/__internal__/flat-table-row-draggable.component.js +17 -3
  55. package/lib/components/flat-table/flat-table-row/__internal__/sub-row-provider.d.ts +15 -0
  56. package/lib/components/flat-table/flat-table-row/__internal__/sub-row-provider.js +38 -0
  57. package/lib/components/flat-table/flat-table-row/flat-table-row.component.d.ts +1 -15
  58. package/lib/components/flat-table/flat-table-row/flat-table-row.component.js +84 -73
  59. package/lib/components/flat-table/flat-table-row/flat-table-row.style.d.ts +4 -1
  60. package/lib/components/flat-table/flat-table-row/flat-table-row.style.js +1 -9
  61. package/lib/components/flat-table/flat-table-row-header/flat-table-row-header.component.d.ts +4 -42
  62. package/lib/components/flat-table/flat-table-row-header/flat-table-row-header.component.js +32 -19
  63. package/lib/components/flat-table/flat-table-row-header/flat-table-row-header.style.d.ts +7 -1
  64. package/lib/components/flat-table/flat-table-row-header/flat-table-row-header.style.js +5 -1
  65. package/lib/components/flat-table/flat-table.component.js +24 -7
  66. package/package.json +1 -1
@@ -24,18 +24,6 @@ export interface FlatTableRowProps {
24
24
  selected?: boolean;
25
25
  /** Sub rows to be shown when the row is expanded, must be used with the `expandable` prop. */
26
26
  subRows?: React.ReactNode;
27
- /** @ignore @private */
28
- isSubRow?: boolean;
29
- /** @ignore @private */
30
- isFirstSubRow?: boolean;
31
- /** @ignore @private position in header if multiple rows */
32
- stickyOffset?: number;
33
- /** @ignore @private applies a border-left to the first child */
34
- applyBorderLeft?: boolean;
35
- /** ID for use in drag and drop functionality
36
- * @private
37
- * @ignore
38
- */
39
27
  id?: string | number;
40
28
  /**
41
29
  * @private
@@ -49,8 +37,6 @@ export interface FlatTableRowProps {
49
37
  moveItem?: FlatTableRowDraggableProps["moveItem"];
50
38
  /** @ignore @private position in header if multiple rows */
51
39
  draggable?: boolean;
52
- /** @ignore @private */
53
- ref?: React.RefObject<HTMLTableRowElement>;
54
40
  }
55
- export declare const FlatTableRow: React.ForwardRefExoticComponent<Pick<FlatTableRowProps, "draggable" | "id" | "children" | "onClick" | "selected" | "bgColor" | "expanded" | "findItem" | "moveItem" | "highlighted" | "expandable" | "horizontalBorderColor" | "stickyOffset" | "isSubRow" | "isFirstSubRow" | "applyBorderLeft" | "horizontalBorderSize" | "expandableArea" | "subRows"> & React.RefAttributes<HTMLTableRowElement>>;
41
+ export declare const FlatTableRow: React.ForwardRefExoticComponent<FlatTableRowProps & React.RefAttributes<HTMLTableRowElement>>;
56
42
  export default FlatTableRow;
@@ -10,11 +10,14 @@ var _invariant = _interopRequireDefault(require("invariant"));
10
10
  var _events = _interopRequireDefault(require("../../../__internal__/utils/helpers/events"));
11
11
  var _flatTableRow = _interopRequireDefault(require("./flat-table-row.style"));
12
12
  var _drawer = require("../../drawer");
13
- var _flatTableCheckbox = _interopRequireDefault(require("../flat-table-checkbox"));
14
13
  var _flatTableRowHeader = _interopRequireDefault(require("../flat-table-row-header"));
15
14
  var _flatTableRowDraggable = _interopRequireDefault(require("./__internal__/flat-table-row-draggable.component"));
16
15
  var _flatTable = require("../flat-table.component");
17
16
  var _guid = _interopRequireDefault(require("../../../__internal__/utils/helpers/guid"));
17
+ var _flatTableRowContext = _interopRequireDefault(require("./__internal__/flat-table-row-context"));
18
+ var _subRowProvider = _interopRequireWildcard(require("./__internal__/sub-row-provider"));
19
+ var _internal__ = require("../__internal__");
20
+ var _flatTableHead = require("../flat-table-head/flat-table-head.component");
18
21
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
22
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
20
23
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -25,37 +28,70 @@ const FlatTableRow = /*#__PURE__*/_react.default.forwardRef(({
25
28
  expandable,
26
29
  expandableArea = "wholeRow",
27
30
  expanded = false,
28
- isSubRow,
29
- isFirstSubRow,
30
- stickyOffset,
31
31
  highlighted,
32
32
  selected,
33
33
  subRows,
34
34
  bgColor,
35
35
  horizontalBorderColor,
36
36
  horizontalBorderSize = "small",
37
- applyBorderLeft,
38
37
  id,
39
38
  draggable,
40
39
  findItem,
41
40
  moveItem,
42
41
  ...rest
43
42
  }, ref) => {
44
- const internalId = (0, _react.useRef)(id ?? (0, _guid.default)());
43
+ const internalId = (0, _react.useRef)(id ? String(id) : (0, _guid.default)());
45
44
  const [isExpanded, setIsExpanded] = (0, _react.useState)(expanded);
46
45
  let rowRef = (0, _react.useRef)(null);
47
46
  if (ref) {
48
47
  rowRef = ref;
49
48
  }
50
49
  const firstColumnExpandable = expandableArea === "firstColumn";
51
- const [leftStickyCellWidths, setLeftStickyCellWidths] = (0, _react.useState)([]);
52
- const [rightStickyCellWidths, setRightStickyCellWidths] = (0, _react.useState)([]);
53
- const [leftPositions, setLeftPositions] = (0, _react.useState)([]);
54
- const [rightPositions, setRightPositions] = (0, _react.useState)([]);
55
- const childrenArray = (0, _react.useMemo)(() => _react.default.Children.toArray(children), [children]);
56
- const lhsRowHeaderIndex = (0, _react.useMemo)(() => childrenArray.findIndex(child => /*#__PURE__*/_react.default.isValidElement(child) && child.type.displayName === _flatTableRowHeader.default.displayName && child.props.stickyAlignment !== "right"), [childrenArray]);
57
- const rhsRowHeaderIndex = (0, _react.useMemo)(() => childrenArray.findIndex(child => /*#__PURE__*/_react.default.isValidElement(child) && child.type.displayName === _flatTableRowHeader.default.displayName && child.props.stickyAlignment === "right"), [childrenArray]);
50
+ const [leftPositions, setLeftPositions] = (0, _react.useState)({});
51
+ const [rightPositions, setRightPositions] = (0, _react.useState)({});
52
+ const [firstCellIndex, setFirstCellIndex] = (0, _react.useState)(0);
53
+ const [lhsRowHeaderIndex, setLhsRowHeaderIndex] = (0, _react.useState)(-1);
54
+ const [rhsRowHeaderIndex, setRhsRowHeaderIndex] = (0, _react.useState)(-1);
55
+ const [firstCellId, setFirstCellId] = (0, _react.useState)(null);
56
+ const [cellsArray, setCellsArray] = (0, _react.useState)([]);
58
57
  const [tabIndex, setTabIndex] = (0, _react.useState)(-1);
58
+ let interactiveRowProps = {};
59
+ (0, _react.useLayoutEffect)(() => {
60
+ const checkForPositionUpdates = (updated, current) => {
61
+ const updatedKeys = Object.keys(updated);
62
+ const currentKeys = Object.keys(current);
63
+ if (updatedKeys.length !== currentKeys.length) {
64
+ return true;
65
+ }
66
+ return updatedKeys.some(key => updated[key] !== current[key]);
67
+ };
68
+ const cells = rowRef.current?.querySelectorAll("th, td");
69
+ const cellArray = Array.from(cells || []);
70
+ setCellsArray(cellArray);
71
+ const firstIndex = cellArray.findIndex(cell => cell.getAttribute("data-component") !== "flat-table-checkbox");
72
+ const lhsIndex = cellArray.findIndex(cell => cell.getAttribute("data-sticky-align") === "left");
73
+ const rhsIndex = cellArray.findIndex(cell => cell.getAttribute("data-sticky-align") === "right");
74
+ setLhsRowHeaderIndex(lhsIndex);
75
+ setRhsRowHeaderIndex(rhsIndex);
76
+ if (firstIndex !== -1) {
77
+ setFirstCellIndex(firstIndex);
78
+ setFirstCellId(cellArray[firstIndex].getAttribute("id"));
79
+ } else {
80
+ setFirstCellIndex(0);
81
+ }
82
+ if (lhsIndex !== -1) {
83
+ const updatedLeftPositions = (0, _internal__.buildPositionMap)(cellArray.slice(0, lhsRowHeaderIndex + 1), "offsetWidth");
84
+ if (checkForPositionUpdates(updatedLeftPositions, leftPositions)) {
85
+ setLeftPositions(updatedLeftPositions);
86
+ }
87
+ }
88
+ if (rhsIndex !== -1) {
89
+ const updatedRightPositions = (0, _internal__.buildPositionMap)(cellArray.slice(rhsRowHeaderIndex, cellArray.length).reverse(), "offsetWidth");
90
+ if (checkForPositionUpdates(updatedRightPositions, rightPositions)) {
91
+ setRightPositions(updatedRightPositions);
92
+ }
93
+ }
94
+ }, [children, leftPositions, lhsRowHeaderIndex, rhsRowHeaderIndex, rightPositions]);
59
95
  const noStickyColumnsOverlap = (0, _react.useMemo)(() => {
60
96
  const hasLhsColumn = lhsRowHeaderIndex !== -1;
61
97
  const hasRhsColumn = rhsRowHeaderIndex !== -1;
@@ -72,22 +108,9 @@ const FlatTableRow = /*#__PURE__*/_react.default.forwardRef(({
72
108
  const {
73
109
  isInSidebar
74
110
  } = (0, _react.useContext)(_drawer.DrawerSidebarContext);
75
- const reportCellWidth = (0, _react.useCallback)((width, index) => {
76
- const isLeftSticky = index < lhsRowHeaderIndex;
77
- const copiedArray = isLeftSticky ? leftStickyCellWidths : rightStickyCellWidths;
78
- if (copiedArray[index] !== undefined) {
79
- copiedArray[index] = width;
80
- } else {
81
- copiedArray.push(width);
82
- }
83
- if (isLeftSticky) {
84
- setLeftStickyCellWidths(copiedArray);
85
- } else {
86
- setRightStickyCellWidths(copiedArray);
87
- }
88
- }, [lhsRowHeaderIndex, leftStickyCellWidths, rightStickyCellWidths]);
89
- let interactiveRowProps = {};
90
- const firstCellIndex = /*#__PURE__*/_react.default.isValidElement(childrenArray[0]) && childrenArray[0].type === _flatTableCheckbox.default ? 1 : 0;
111
+ const {
112
+ stickyOffsets
113
+ } = (0, _react.useContext)(_flatTableHead.FlatTableHeadContext);
91
114
  const toggleExpanded = () => setIsExpanded(!isExpanded);
92
115
  function onKeyDown(ev) {
93
116
  const isEnterOrSpaceKey = _events.default.isEnterKey(ev) || _events.default.isSpaceKey(ev);
@@ -123,32 +146,31 @@ const FlatTableRow = /*#__PURE__*/_react.default.forwardRef(({
123
146
  toggleExpanded();
124
147
  }
125
148
  }
126
- const buildPositionArray = (setter, widthsArray, length) => {
127
- setter([0, ...Array.from({
128
- length
129
- }).map((_, index) => widthsArray.slice(0, index + 1).reduce((a, b) => a + b, 0), 0)]);
130
- };
131
- (0, _react.useLayoutEffect)(() => {
132
- if (leftStickyCellWidths.length && lhsRowHeaderIndex !== -1) {
133
- buildPositionArray(setLeftPositions, leftStickyCellWidths, lhsRowHeaderIndex);
134
- }
135
- }, [lhsRowHeaderIndex, leftStickyCellWidths]);
136
- (0, _react.useLayoutEffect)(() => {
137
- if (rightStickyCellWidths.length && rhsRowHeaderIndex !== -1) {
138
- buildPositionArray(setRightPositions, rightStickyCellWidths, childrenArray.length - (rhsRowHeaderIndex + 1));
139
- }
140
- }, [rhsRowHeaderIndex, rightStickyCellWidths, childrenArray]);
141
149
  (0, _react.useEffect)(() => {
142
150
  setIsExpanded(expanded);
143
151
  }, [expanded]);
144
152
  (0, _react.useEffect)(() => {
145
153
  if (highlighted || selected) {
146
- setSelectedId(String(internalId.current));
154
+ setSelectedId(internalId.current);
147
155
  }
148
156
  }, [highlighted, selected, setSelectedId]);
149
157
  (0, _react.useEffect)(() => {
150
158
  setTabIndex(selectedId === internalId.current ? 0 : -1);
151
159
  }, [selectedId]);
160
+ const {
161
+ isSubRow,
162
+ firstRowId,
163
+ addRow,
164
+ removeRow
165
+ } = (0, _react.useContext)(_subRowProvider.SubRowContext);
166
+ (0, _react.useEffect)(() => {
167
+ const rowId = internalId.current;
168
+ addRow(rowId);
169
+ return () => {
170
+ removeRow(rowId);
171
+ };
172
+ }, [addRow, removeRow]);
173
+ const isFirstSubRow = firstRowId === internalId.current;
152
174
  const rowComponent = () => /*#__PURE__*/_react.default.createElement(_flatTableRow.default, _extends({
153
175
  isInSidebar: isInSidebar,
154
176
  expandable: expandable,
@@ -164,42 +186,34 @@ const FlatTableRow = /*#__PURE__*/_react.default.forwardRef(({
164
186
  rhsRowHeaderIndex: rhsRowHeaderIndex,
165
187
  colorTheme: colorTheme,
166
188
  size: size,
167
- stickyOffset: stickyOffset,
189
+ stickyOffset: stickyOffsets[internalId.current],
168
190
  bgColor: bgColor,
169
191
  horizontalBorderColor: horizontalBorderColor,
170
192
  horizontalBorderSize: horizontalBorderSize,
171
- applyBorderLeft: applyBorderLeft,
172
193
  draggable: draggable,
173
- totalChildren: childrenArray.length,
174
- id: String(internalId.current)
175
- }, interactiveRowProps, rest), _react.default.Children.map(children, (child, index) => {
176
- return /*#__PURE__*/_react.default.isValidElement(child) && /*#__PURE__*/_react.default.cloneElement(child, {
177
- expandable: expandable && index === firstCellIndex,
178
- onClick: expandable && index === firstCellIndex && firstColumnExpandable ? () => toggleExpanded() : undefined,
179
- onKeyDown: expandable && index === firstCellIndex && firstColumnExpandable ? handleCellKeyDown : undefined,
180
- cellIndex: index,
181
- reportCellWidth: index < lhsRowHeaderIndex || rhsRowHeaderIndex !== -1 && index > rhsRowHeaderIndex ? reportCellWidth : undefined,
182
- leftPosition: leftPositions[index],
183
- rightPosition: rightPositions[childrenArray.length - (index + 1)],
184
- ...child.props
185
- });
186
- }));
194
+ totalChildren: cellsArray.length,
195
+ id: internalId.current
196
+ }, interactiveRowProps, rest), /*#__PURE__*/_react.default.createElement(_flatTableRowContext.default.Provider, {
197
+ value: {
198
+ firstCellId,
199
+ expandable,
200
+ leftPositions,
201
+ rightPositions,
202
+ firstColumnExpandable,
203
+ onKeyDown: handleCellKeyDown,
204
+ onClick: () => toggleExpanded()
205
+ }
206
+ }, children));
187
207
  const draggableComponent = () => /*#__PURE__*/_react.default.createElement(_flatTableRowDraggable.default, {
188
208
  id: internalId.current,
189
209
  moveItem: moveItem,
190
- findItem: findItem
210
+ findItem: findItem,
211
+ rowRef: rowRef
191
212
  }, rowComponent());
192
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, draggable ? draggableComponent() : rowComponent(), isExpanded && subRows && _react.default.Children.map(subRows, (child, index) => child && /*#__PURE__*/_react.default.cloneElement(child, {
193
- isSubRow: true,
194
- isFirstSubRow: index === 0,
195
- ...( /*#__PURE__*/_react.default.isValidElement(child) && {
196
- ...child.props
197
- })
198
- })));
213
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, draggable ? draggableComponent() : rowComponent(), isExpanded && subRows && /*#__PURE__*/_react.default.createElement(_subRowProvider.default, null, subRows));
199
214
  });
200
215
  exports.FlatTableRow = FlatTableRow;
201
216
  FlatTableRow.propTypes = {
202
- "applyBorderLeft": _propTypes.default.bool,
203
217
  "bgColor": _propTypes.default.string,
204
218
  "children": _propTypes.default.node,
205
219
  "draggable": _propTypes.default.bool,
@@ -211,12 +225,9 @@ FlatTableRow.propTypes = {
211
225
  "horizontalBorderColor": _propTypes.default.string,
212
226
  "horizontalBorderSize": _propTypes.default.oneOf(["large", "medium", "small"]),
213
227
  "id": _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]),
214
- "isFirstSubRow": _propTypes.default.bool,
215
- "isSubRow": _propTypes.default.bool,
216
228
  "moveItem": _propTypes.default.func,
217
229
  "onClick": _propTypes.default.func,
218
230
  "selected": _propTypes.default.bool,
219
- "stickyOffset": _propTypes.default.number,
220
231
  "subRows": _propTypes.default.node
221
232
  };
222
233
  FlatTableRow.displayName = "FlatTableRow";
@@ -1,6 +1,6 @@
1
1
  import { FlatTableProps } from "..";
2
2
  import { FlatTableRowProps } from "./flat-table-row.component";
3
- interface StyledFlatTableRowProps extends Pick<FlatTableRowProps, "bgColor" | "horizontalBorderColor" | "stickyOffset" | "expandable" | "selected" | "highlighted" | "isSubRow" | "isFirstSubRow" | "applyBorderLeft" | "draggable"> {
3
+ interface StyledFlatTableRowProps extends Pick<FlatTableRowProps, "bgColor" | "horizontalBorderColor" | "expandable" | "selected" | "highlighted" | "draggable"> {
4
4
  isRowInteractive?: boolean;
5
5
  isFirstColumnInteractive?: boolean;
6
6
  lhsRowHeaderIndex: number;
@@ -13,6 +13,9 @@ interface StyledFlatTableRowProps extends Pick<FlatTableRowProps, "bgColor" | "h
13
13
  size: FlatTableProps["size"];
14
14
  isDragging?: boolean;
15
15
  horizontalBorderSize: NonNullable<FlatTableRowProps["horizontalBorderSize"]>;
16
+ isSubRow?: boolean;
17
+ isFirstSubRow?: boolean;
18
+ stickyOffset?: number;
16
19
  }
17
20
  declare const StyledFlatTableRow: import("styled-components").StyledComponent<"tr", any, StyledFlatTableRowProps, never>;
18
21
  export default StyledFlatTableRow;
@@ -124,7 +124,6 @@ const StyledFlatTableRow = _styledComponents.default.tr`
124
124
  isFirstSubRow,
125
125
  size,
126
126
  theme,
127
- applyBorderLeft,
128
127
  isDragging,
129
128
  draggable
130
129
  }) => {
@@ -197,7 +196,7 @@ const StyledFlatTableRow = _styledComponents.default.tr`
197
196
  `}
198
197
  }
199
198
 
200
- ${stickyOffset !== undefined && stickyOffset > 0 && (0, _styledComponents.css)`
199
+ ${stickyOffset !== undefined && (0, _styledComponents.css)`
201
200
  && th {
202
201
  top: ${stickyOffset}px;
203
202
  }
@@ -309,13 +308,6 @@ const StyledFlatTableRow = _styledComponents.default.tr`
309
308
  }
310
309
  `}
311
310
 
312
-
313
- ${applyBorderLeft && (0, _styledComponents.css)`
314
- th:first-of-type {
315
- border-left: 1px solid ${customBorderColor || borderColor(colorTheme)};
316
- }
317
- `}
318
-
319
311
  ${isInSidebar && (0, _styledComponents.css)`
320
312
  ${allCellTypes} {
321
313
  background-color: ${bgColor || "var(--colorsUtilityMajor040)"};
@@ -1,7 +1,8 @@
1
1
  import React from "react";
2
2
  import { PaddingProps } from "styled-system";
3
3
  import { TableBorderSize, TableCellAlign } from "..";
4
- export interface FlatTableRowHeaderProps extends PaddingProps {
4
+ import { TagProps } from "../../../__internal__/utils/helpers/tags/tags";
5
+ export interface FlatTableRowHeaderProps extends PaddingProps, TagProps {
5
6
  /** Content alignment */
6
7
  align?: TableCellAlign;
7
8
  /** RowHeader content */
@@ -22,50 +23,11 @@ export interface FlatTableRowHeaderProps extends PaddingProps {
22
23
  colspan?: number | string;
23
24
  /** Number of rows that a header cell should span */
24
25
  rowspan?: number | string;
25
- /** Sets an id string on the DOM element */
26
+ /** Sets an id string on the element */
26
27
  id?: string;
27
- /**
28
- * @private
29
- * @ignore
30
- */
31
- expandable?: boolean;
32
- /**
33
- * @private
34
- * @ignore
35
- */
36
- onClick?: (ev: React.MouseEvent<HTMLElement>) => void;
37
- /**
38
- * @private
39
- * @ignore
40
- */
41
- onKeyDown?: (ev: React.KeyboardEvent<HTMLElement>) => void;
42
- /**
43
- * @private
44
- * @ignore
45
- * Sets the left position when sticky column found
46
- */
47
- leftPosition?: number;
48
- /**
49
- * @private
50
- * @ignore
51
- * Sets the right position when sticky column found
52
- */
53
- rightPosition?: number;
54
- /**
55
- * @private
56
- * @ignore
57
- * Index of cell within row
58
- */
59
- cellIndex?: number;
60
- /**
61
- * @private
62
- * @ignore
63
- * Callback to report the offsetWidth
64
- */
65
- reportCellWidth?: (offset: number, index?: number) => void;
66
28
  }
67
29
  export declare const FlatTableRowHeader: {
68
- ({ align, children, width, py, px, expandable, onClick, onKeyDown, leftPosition, rightPosition, truncate, title, stickyAlignment, colspan, rowspan, ...rest }: FlatTableRowHeaderProps): React.JSX.Element;
30
+ ({ align, children, width, py, px, truncate, title, stickyAlignment, colspan, rowspan, id, ...rest }: FlatTableRowHeaderProps): React.JSX.Element;
69
31
  displayName: string;
70
32
  };
71
33
  export default FlatTableRowHeader;
@@ -10,6 +10,8 @@ var _icon = _interopRequireDefault(require("../../icon"));
10
10
  var _flatTableRowHeader = require("./flat-table-row-header.style");
11
11
  var _flatTable = require("../flat-table.component");
12
12
  var _guid = _interopRequireDefault(require("../../../__internal__/utils/helpers/guid"));
13
+ var _tags = _interopRequireDefault(require("../../../__internal__/utils/helpers/tags/tags"));
14
+ var _useCalculateStickyCells = _interopRequireDefault(require("../__internal__/use-calculate-sticky-cells"));
13
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
16
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
17
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -20,55 +22,66 @@ const FlatTableRowHeader = ({
20
22
  width,
21
23
  py,
22
24
  px,
23
- expandable = false,
24
- onClick,
25
- onKeyDown,
26
- leftPosition,
27
- rightPosition,
28
25
  truncate,
29
26
  title,
30
27
  stickyAlignment = "left",
31
28
  colspan,
32
29
  rowspan,
30
+ id,
33
31
  ...rest
34
32
  }) => {
35
- const id = (0, _react.useRef)((0, _guid.default)());
33
+ const internalId = (0, _react.useRef)(id || (0, _guid.default)());
36
34
  const [tabIndex, setTabIndex] = (0, _react.useState)(-1);
37
35
  const {
38
36
  selectedId
39
37
  } = (0, _react.useContext)(_flatTable.FlatTableThemeContext);
38
+ const {
39
+ leftPosition,
40
+ rightPosition,
41
+ expandable,
42
+ onClick,
43
+ onKeyDown,
44
+ isFirstCell,
45
+ isExpandableCell
46
+ } = (0, _useCalculateStickyCells.default)(internalId.current);
47
+ (0, _react.useEffect)(() => {
48
+ setTabIndex(isExpandableCell && selectedId === internalId.current ? 0 : -1);
49
+ }, [selectedId, isExpandableCell]);
40
50
  const handleOnClick = (0, _react.useCallback)(ev => {
41
- if (expandable && onClick) onClick(ev);
42
- }, [expandable, onClick]);
51
+ if (isExpandableCell && onClick) onClick(ev);
52
+ }, [isExpandableCell, onClick]);
43
53
  const handleOnKeyDown = (0, _react.useCallback)(ev => {
44
- if (expandable && onKeyDown) onKeyDown(ev);
45
- }, [expandable, onKeyDown]);
46
- (0, _react.useEffect)(() => {
47
- setTabIndex(selectedId === id.current ? 0 : -1);
48
- }, [selectedId]);
54
+ if (isExpandableCell && onKeyDown) {
55
+ onKeyDown(ev);
56
+ }
57
+ }, [isExpandableCell, onKeyDown]);
49
58
  return /*#__PURE__*/_react.default.createElement(_flatTableRowHeader.StyledFlatTableRowHeader, _extends({
50
59
  leftPosition: stickyAlignment === "left" ? leftPosition || 0 : undefined,
51
60
  rightPosition: stickyAlignment === "right" ? rightPosition || 0 : undefined,
52
- align: align,
61
+ align: align
62
+ }, (0, _tags.default)("flat-table-row-header", {
53
63
  "data-element": "flat-table-row-header",
64
+ ...rest
65
+ }), {
54
66
  width: width,
55
67
  py: py || "10px",
56
68
  px: px || 3,
57
69
  onClick: handleOnClick,
58
- tabIndex: expandable && onClick ? tabIndex : undefined,
70
+ tabIndex: isExpandableCell ? tabIndex : undefined,
59
71
  onKeyDown: handleOnKeyDown,
60
72
  truncate: truncate,
61
73
  expandable: expandable,
62
- stickyAlignment: stickyAlignment,
63
- id: id.current
74
+ stickyAlignment: stickyAlignment
64
75
  }, colspan !== undefined && {
65
76
  colSpan: Number(colspan)
66
77
  }, rowspan !== undefined && {
67
78
  rowSpan: Number(rowspan)
68
- }, rest), /*#__PURE__*/_react.default.createElement(_flatTableRowHeader.StyledFlatTableRowHeaderContent, {
79
+ }, rest, {
80
+ id: internalId.current
81
+ }), /*#__PURE__*/_react.default.createElement(_flatTableRowHeader.StyledFlatTableRowHeaderContent, {
69
82
  title: truncate && !title && typeof children === "string" ? children : title,
70
83
  expandable: expandable
71
- }, expandable && /*#__PURE__*/_react.default.createElement(_icon.default, {
84
+ }, expandable && isFirstCell && /*#__PURE__*/_react.default.createElement(_icon.default, {
72
85
  type: "chevron_down_thick",
73
86
  bgSize: "extra-small",
74
87
  mr: "8px"
@@ -1,5 +1,11 @@
1
1
  import { FlatTableRowHeaderProps } from "./flat-table-row-header.component";
2
- declare const StyledFlatTableRowHeader: import("styled-components").StyledComponent<"th", any, FlatTableRowHeaderProps, never>;
2
+ declare const StyledFlatTableRowHeader: import("styled-components").StyledComponent<"th", any, {
3
+ "data-sticky-align": "left" | "right" | undefined;
4
+ } & FlatTableRowHeaderProps & {
5
+ expandable?: boolean | undefined;
6
+ leftPosition?: number | undefined;
7
+ rightPosition?: number | undefined;
8
+ }, "data-sticky-align">;
3
9
  declare const StyledFlatTableRowHeaderContent: import("styled-components").StyledComponent<"div", any, {
4
10
  expandable?: boolean | undefined;
5
11
  }, never>;
@@ -16,7 +16,11 @@ const verticalBorderSizes = {
16
16
  medium: "2px",
17
17
  large: "4px"
18
18
  };
19
- const StyledFlatTableRowHeader = _styledComponents.default.th`
19
+ const StyledFlatTableRowHeader = _styledComponents.default.th.attrs(({
20
+ stickyAlignment
21
+ }) => ({
22
+ "data-sticky-align": stickyAlignment
23
+ }))`
20
24
  ${({
21
25
  align,
22
26
  theme,
@@ -17,7 +17,7 @@ const FlatTableThemeContext = /*#__PURE__*/_react.default.createContext({
17
17
  setSelectedId: () => {}
18
18
  });
19
19
  exports.FlatTableThemeContext = FlatTableThemeContext;
20
- const FOCUSABLE_ROW_AND_CELL_QUERY = "tbody tr, tbody tr td, tbody tr th";
20
+ const FOCUSABLE_ROW_AND_CELL_QUERY = "tbody tr[tabindex], tbody tr td[tabindex], tbody tr th[tabindex]";
21
21
  const FlatTable = ({
22
22
  caption,
23
23
  children,
@@ -108,7 +108,7 @@ const FlatTable = ({
108
108
  if (!focusableElements) {
109
109
  return;
110
110
  }
111
- const focusableElementsArray = Array.from(focusableElements).filter(el => el.getAttribute("tabindex") !== null);
111
+ const focusableElementsArray = Array.from(focusableElements);
112
112
  const currentFocusIndex = focusableElementsArray.findIndex(el => el === document.activeElement);
113
113
  if (_events.default.isDownKey(ev)) {
114
114
  ev.preventDefault();
@@ -135,13 +135,30 @@ const FlatTable = ({
135
135
  }
136
136
  };
137
137
  (0, _react.useLayoutEffect)(() => {
138
- const focusableElements = tableRef.current?.querySelectorAll(FOCUSABLE_ROW_AND_CELL_QUERY);
138
+ const findSelectedId = () => {
139
+ const firstfocusableElement = tableRef.current?.querySelector(FOCUSABLE_ROW_AND_CELL_QUERY);
140
+
141
+ // if no other menu item is selected, we need to make the first row a tab stop
142
+ if (firstfocusableElement && !selectedId) {
143
+ const currentlySelectedId = firstfocusableElement?.getAttribute("id");
144
+
145
+ /* istanbul ignore else */
146
+ if (currentlySelectedId && selectedId !== currentlySelectedId) {
147
+ setSelectedId(currentlySelectedId);
148
+ }
149
+ }
150
+ };
151
+ const observer = new MutationObserver(findSelectedId);
139
152
 
140
- // if no other menu item is selected, we need to make the first row a tab stop
141
- if (focusableElements && !selectedId) {
142
- const focusableArray = Array.from(focusableElements).filter(el => el.getAttribute("tabindex") !== null);
143
- setSelectedId(focusableArray[0]?.getAttribute("id") || "");
153
+ /* istanbul ignore else */
154
+ if (wrapperRef.current) {
155
+ observer.observe(wrapperRef.current, {
156
+ subtree: true,
157
+ childList: true,
158
+ attributes: true
159
+ });
144
160
  }
161
+ return () => observer.disconnect();
145
162
  }, [selectedId]);
146
163
  return /*#__PURE__*/_react.default.createElement(_flatTable.StyledFlatTableWrapper, _extends({
147
164
  ref: wrapperRef,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "120.0.0",
3
+ "version": "120.1.0",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "files": [
6
6
  "lib",