useRowInfo.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { useContext } from '@rc-component/context';
  3. import TableContext from "../context/TableContext";
  4. import { getColumnsKey } from "../utils/valueUtil";
  5. import { useEvent } from 'rc-util';
  6. import classNames from 'classnames';
  7. export default function useRowInfo(record, rowKey, recordIndex, indent) {
  8. var context = useContext(TableContext, ['prefixCls', 'fixedInfoList', 'flattenColumns', 'expandableType', 'expandRowByClick', 'onTriggerExpand', 'rowClassName', 'expandedRowClassName', 'indentSize', 'expandIcon', 'expandedRowRender', 'expandIconColumnIndex', 'expandedKeys', 'childrenColumnName', 'rowExpandable', 'onRow']);
  9. var flattenColumns = context.flattenColumns,
  10. expandableType = context.expandableType,
  11. expandedKeys = context.expandedKeys,
  12. childrenColumnName = context.childrenColumnName,
  13. onTriggerExpand = context.onTriggerExpand,
  14. rowExpandable = context.rowExpandable,
  15. onRow = context.onRow,
  16. expandRowByClick = context.expandRowByClick,
  17. rowClassName = context.rowClassName;
  18. // ======================= Expandable =======================
  19. // Only when row is not expandable and `children` exist in record
  20. var nestExpandable = expandableType === 'nest';
  21. var rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
  22. var mergedExpandable = rowSupportExpand || nestExpandable;
  23. var expanded = expandedKeys && expandedKeys.has(rowKey);
  24. var hasNestChildren = childrenColumnName && record && record[childrenColumnName];
  25. var onInternalTriggerExpand = useEvent(onTriggerExpand);
  26. // ========================= onRow ==========================
  27. var rowProps = onRow === null || onRow === void 0 ? void 0 : onRow(record, recordIndex);
  28. var onRowClick = rowProps === null || rowProps === void 0 ? void 0 : rowProps.onClick;
  29. var onClick = function onClick(event) {
  30. if (expandRowByClick && mergedExpandable) {
  31. onTriggerExpand(record, event);
  32. }
  33. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  34. args[_key - 1] = arguments[_key];
  35. }
  36. onRowClick === null || onRowClick === void 0 || onRowClick.apply(void 0, [event].concat(args));
  37. };
  38. // ====================== RowClassName ======================
  39. var computeRowClassName;
  40. if (typeof rowClassName === 'string') {
  41. computeRowClassName = rowClassName;
  42. } else if (typeof rowClassName === 'function') {
  43. computeRowClassName = rowClassName(record, recordIndex, indent);
  44. }
  45. // ========================= Column =========================
  46. var columnsKey = getColumnsKey(flattenColumns);
  47. return _objectSpread(_objectSpread({}, context), {}, {
  48. columnsKey: columnsKey,
  49. nestExpandable: nestExpandable,
  50. expanded: expanded,
  51. hasNestChildren: hasNestChildren,
  52. record: record,
  53. onTriggerExpand: onInternalTriggerExpand,
  54. rowSupportExpand: rowSupportExpand,
  55. expandable: mergedExpandable,
  56. rowProps: _objectSpread(_objectSpread({}, rowProps), {}, {
  57. className: classNames(computeRowClassName, rowProps === null || rowProps === void 0 ? void 0 : rowProps.className),
  58. onClick: onClick
  59. })
  60. });
  61. }