useRowInfo.js 3.5 KB

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