BodyTr.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {useSortable} from '@dnd-kit/sortable';
  2. import {Row, flexRender} from '@tanstack/react-table';
  3. import classNames from 'classnames';
  4. import {CSSProperties, FC, ReactNode} from 'react';
  5. type Props = {
  6. highlight?: boolean,
  7. enableDnd?: boolean,
  8. preview?: boolean,
  9. row: Row<Record<string, any>>,
  10. renderSubComponent?: (row: Row<Record<string, any>>) => ReactNode;
  11. enableEllipsis?: boolean;
  12. };
  13. const BodyTr: FC<Props> = function({
  14. row,
  15. highlight,
  16. enableDnd,
  17. preview,
  18. renderSubComponent,
  19. enableEllipsis,
  20. }) {
  21. const trList = row.getVisibleCells();
  22. const {setNodeRef, listeners, attributes} = useSortable({
  23. id: row.id,
  24. disabled: !enableDnd,
  25. data: {
  26. row,
  27. },
  28. });
  29. return (
  30. <>
  31. <tr
  32. ref={setNodeRef}
  33. {...listeners}
  34. {...attributes}
  35. style={{cursor: enableDnd || preview ? 'move' : 'auto'}}
  36. className={classNames('ld-table-body-tr ', {
  37. 'ld-table-tr-highlight': highlight,
  38. 'ld-table-tr-preview': preview,
  39. })}
  40. >
  41. {trList.map(function({
  42. id,
  43. column,
  44. getContext,
  45. }) {
  46. const {
  47. align,
  48. fixed,
  49. fixedStyle,
  50. ellipsis,
  51. } = column.columnDef.meta as {
  52. align?: 'left' | 'center' | 'right';
  53. fixed?: 'right' | 'left';
  54. fixedStyle: CSSProperties,
  55. ellipsis?: boolean,
  56. };
  57. return (
  58. <td
  59. title={getContext().getValue() as string}
  60. key={id}
  61. className={classNames({
  62. 'ld-table-fixed-right ld-table-right-fixed-shadow': fixed === 'right',
  63. 'ld-table-fixed-left ld-table-left-fixed-shadow': fixed === 'left',
  64. 'ld-table-ellipsis': !fixed && ((enableEllipsis && ellipsis !== false) || ellipsis),
  65. })}
  66. style={{
  67. width: column.getSize(),
  68. textAlign: align ?? 'left',
  69. ...fixedStyle,
  70. }}
  71. >
  72. {flexRender(
  73. column.columnDef.cell,
  74. getContext(),
  75. )}
  76. </td>
  77. );
  78. })}
  79. </tr>
  80. {row.getIsExpanded() && Boolean(renderSubComponent)
  81. ? (
  82. <tr className="ld-table-body-tr">
  83. <td colSpan={row.getVisibleCells().length}>
  84. {renderSubComponent?.(row)}
  85. </td>
  86. </tr>
  87. )
  88. : null}
  89. </>
  90. );
  91. };
  92. export default BodyTr;