BaseRow.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* eslint-disable max-len */
  2. /* eslint-disable eqeqeq */
  3. import React, { createRef, ReactNode } from 'react';
  4. import classnames from 'classnames';
  5. import PropTypes from 'prop-types';
  6. import { each, noop, get, stubTrue, omit, isEqual, pick } from 'lodash';
  7. import { strings, cssClasses } from '@douyinfe/semi-foundation/table/constants';
  8. import shallowEqualObjects from '@douyinfe/semi-foundation/utils/shallowEqualObjects';
  9. import TableRowFoundation, { TableRowAdapter } from '@douyinfe/semi-foundation/table/tableRowFoundation';
  10. import {
  11. isLastLeftFixed,
  12. arrayAdd,
  13. isFixedLeft,
  14. isFixedRight,
  15. isScrollbarColumn,
  16. isFirstFixedRight,
  17. isInnerColumnKey,
  18. isExpandedColumn
  19. } from '@douyinfe/semi-foundation/table/utils';
  20. import Store from '@douyinfe/semi-foundation/utils/Store';
  21. import { BaseRowKeyType } from '@douyinfe/semi-foundation/table/foundation';
  22. import BaseComponent from '../../_base/baseComponent';
  23. import TableCell from '../TableCell';
  24. import { ColumnProps, Fixed, TableComponents, Virtualized, ExpandIcon, OnRow, RowExpandable } from '../interface';
  25. export interface BaseRowProps {
  26. anyColumnFixed?: boolean;
  27. cellWidths?: number[];
  28. className?: string;
  29. columns: ColumnProps[]; // required
  30. components?: TableComponents; // required
  31. disabled?: boolean;
  32. expandIcon?: ExpandIcon;
  33. expandableRow?: boolean;
  34. expanded?: boolean;
  35. expandedRow?: boolean;
  36. fixed?: Fixed;
  37. height?: string | number;
  38. hideExpandedColumn?: boolean;
  39. hovered: boolean; // required
  40. indent?: number;
  41. indentSize?: number;
  42. index?: number;
  43. isSection?: boolean;
  44. level?: number;
  45. onDidUpdate?: (ref: React.MutableRefObject<any>) => void;
  46. onHover?: (mouseEnter: boolean, rowKey: string | number) => void;
  47. onRow?: OnRow<any>;
  48. onRowClick?: (rowKey: BaseRowKeyType, e: React.MouseEvent, expand: boolean) => void;
  49. onRowDoubleClick?: (record: Record<string, any>, e: React.MouseEvent) => void;
  50. onRowMouseEnter?: (record: Record<string, any>, e: React.MouseEvent) => void;
  51. onRowMouseLeave?: (record: Record<string, any>, e: React.MouseEvent) => void;
  52. prefixCls?: string;
  53. record?: Record<string, any>;
  54. renderExpandIcon?: RenderExpandIcon;
  55. replaceClassName?: string;
  56. rowExpandable?: RowExpandable<any>;
  57. rowKey?: string | number; // required, this place rowKey is a real key of the row
  58. selected?: boolean;
  59. store?: Store;
  60. style?: React.CSSProperties;
  61. virtualized?: Virtualized;
  62. visible: boolean; // required
  63. }
  64. export default class TableRow extends BaseComponent<BaseRowProps, Record<string, any>> {
  65. static propTypes = {
  66. anyColumnFixed: PropTypes.bool,
  67. cellWidths: PropTypes.array.isRequired,
  68. className: PropTypes.string,
  69. columns: PropTypes.array.isRequired,
  70. components: PropTypes.object.isRequired,
  71. disabled: PropTypes.bool,
  72. expandIcon: PropTypes.oneOfType([PropTypes.bool, PropTypes.func, PropTypes.node]),
  73. expandableRow: PropTypes.bool,
  74. expanded: PropTypes.bool,
  75. expandedRow: PropTypes.bool,
  76. fixed: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  77. height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  78. hideExpandedColumn: PropTypes.bool,
  79. hovered: PropTypes.bool.isRequired,
  80. indent: PropTypes.number,
  81. indentSize: PropTypes.number,
  82. index: PropTypes.number,
  83. isSection: PropTypes.bool,
  84. level: PropTypes.number,
  85. onDidUpdate: PropTypes.func,
  86. onHover: PropTypes.func,
  87. onRow: PropTypes.func,
  88. onRowClick: PropTypes.func,
  89. onRowContextMenu: PropTypes.func,
  90. onRowDoubleClick: PropTypes.func,
  91. onRowMouseEnter: PropTypes.func,
  92. onRowMouseLeave: PropTypes.func,
  93. prefixCls: PropTypes.string,
  94. record: PropTypes.object,
  95. renderExpandIcon: PropTypes.func,
  96. replaceClassName: PropTypes.string,
  97. rowExpandable: PropTypes.func,
  98. rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, // real key of the row
  99. selected: PropTypes.bool,
  100. store: PropTypes.object,
  101. style: PropTypes.object,
  102. virtualized: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
  103. visible: PropTypes.bool.isRequired,
  104. };
  105. static defaultProps = {
  106. columns: [] as [],
  107. rowExpandable: stubTrue,
  108. components: {
  109. body: {
  110. row: 'tr',
  111. cell: 'td',
  112. },
  113. },
  114. prefixCls: cssClasses.PREFIX,
  115. onRow: noop,
  116. onRowClick: noop,
  117. onRowDoubleClick: noop,
  118. onRowMouseEnter: noop,
  119. onRowMouseLeave: noop,
  120. onHover: noop,
  121. onDidUpdate: noop,
  122. visible: true,
  123. hovered: false,
  124. selected: false,
  125. disabled: false,
  126. };
  127. get adapter(): TableRowAdapter<BaseRowProps> {
  128. return {
  129. ...super.adapter,
  130. notifyClick: (...args) => this.props.onRowClick(...args),
  131. notifyDoubleClick: (...args) => this.props.onRowDoubleClick(...args),
  132. notifyMouseLeave: (...args) => {
  133. this.props.onHover(false, this.props.rowKey);
  134. this.props.onRowMouseEnter(...args);
  135. },
  136. notifyMouseEnter: (...args) => {
  137. this.props.onHover(true, this.props.rowKey);
  138. this.props.onRowMouseEnter(...args);
  139. },
  140. };
  141. }
  142. ref: React.MutableRefObject<any>;
  143. constructor(props: BaseRowProps) {
  144. super(props);
  145. this.ref = createRef();
  146. this.foundation = new TableRowFoundation(this.adapter);
  147. }
  148. componentDidMount() {
  149. // fix #745
  150. // didmount/willUnmount may be called twice when React.StrictMode is true in React 18, we need to ensure that this.cache.customRowProps is correct
  151. const {
  152. onRow,
  153. index,
  154. record,
  155. } = this.props;
  156. const customRowProps = this.adapter.getCache('customRowProps');
  157. if (typeof customRowProps === 'undefined') {
  158. const { className: customClassName, style: customStyle, ...rowProps } = onRow(record, index) || {};
  159. this.adapter.setCache('customRowProps', { ...rowProps });
  160. }
  161. }
  162. shouldComponentUpdate(nextProps: BaseRowProps) {
  163. /**
  164. * Shallow comparison of incoming props to simulate PureComponent
  165. * Deep comparison cellWidths
  166. *
  167. * 浅层对比传入的 props,模拟 PureComponent
  168. * 深比较 cellWidths
  169. */
  170. const omitProps = ['cellWidths'];
  171. const isPropsShallowEqual = shallowEqualObjects(omit(nextProps, omitProps), omit(this.props, omitProps));
  172. if (!isPropsShallowEqual || !isEqual(pick(nextProps, omitProps), pick(this.props, omitProps))) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. _cacheNode = (node: any) => {
  178. this.ref.current = node;
  179. };
  180. // Pass true to render the tree-shaped expand button
  181. renderExpandIcon = (record: Record<string, any>) => {
  182. const { renderExpandIcon } = this.props;
  183. return renderExpandIcon(record, true);
  184. };
  185. renderCells() {
  186. const {
  187. columns,
  188. record,
  189. index,
  190. prefixCls,
  191. fixed,
  192. components,
  193. expandableRow,
  194. level,
  195. expandIcon,
  196. rowExpandable,
  197. isSection,
  198. expandedRow,
  199. virtualized,
  200. indentSize,
  201. hideExpandedColumn,
  202. cellWidths,
  203. selected,
  204. expanded,
  205. disabled,
  206. onDidUpdate,
  207. } = this.props;
  208. const BodyCell = get(components, 'body.cell', strings.DEFAULT_COMPONENTS.body.cell);
  209. const cells: ReactNode[] = [];
  210. const displayExpandedColumn = rowExpandable(record);
  211. let firstIndex = 0;
  212. // const dataColumns = getDataColumns(columns);
  213. each(columns, (column, columnIndex) => {
  214. const columnKey = get(column, 'key');
  215. const expandableProps: { renderExpandIcon?: (record: Record<string, any>) => ReactNode; expandIcon?: ExpandIcon; indent?: number } = {};
  216. if (fixed !== 'right') {
  217. if (isInnerColumnKey(columnKey)) {
  218. firstIndex++;
  219. }
  220. if (expandableRow && columnIndex === firstIndex) {
  221. expandableProps.renderExpandIcon = this.renderExpandIcon;
  222. if (hideExpandedColumn || isSection) {
  223. expandableProps.expandIcon = expandIcon != null ? expandIcon : true;
  224. }
  225. }
  226. // Only the first data row will be indented
  227. if (level != null && columnIndex === firstIndex) {
  228. expandableProps.indent = level;
  229. if (!expandableRow) {
  230. expandableProps.indent = level + 1;
  231. }
  232. }
  233. }
  234. if (isExpandedColumn(column) && !displayExpandedColumn) {
  235. cells.push(<TableCell key={columnIndex} colIndex={columnIndex} isSection={isSection} />);
  236. } else if (!isScrollbarColumn(column)) {
  237. const diyProps: { width?: number } = {};
  238. if (BodyCell !== strings.DEFAULT_COMPONENTS.body.cell && virtualized && !expandedRow) {
  239. diyProps.width = get(cellWidths, columnIndex);
  240. }
  241. cells.push(
  242. <TableCell
  243. colIndex={columnIndex}
  244. {...expandableProps}
  245. {...diyProps}
  246. hideExpandedColumn={hideExpandedColumn}
  247. indentSize={indentSize}
  248. isSection={isSection}
  249. prefixCls={`${prefixCls}`}
  250. column={column}
  251. key={columnIndex}
  252. index={index}
  253. record={record}
  254. component={BodyCell}
  255. fixedLeft={isFixedLeft(column) && arrayAdd(cellWidths, 0, columnIndex)}
  256. lastFixedLeft={isLastLeftFixed(columns, column)}
  257. fixedRight={isFixedRight(column) && arrayAdd(cellWidths, columnIndex + 1)}
  258. firstFixedRight={isFirstFixedRight(columns, column)}
  259. selected={selected}
  260. expanded={expanded}
  261. disabled={disabled}
  262. onDidUpdate={onDidUpdate}
  263. />
  264. );
  265. }
  266. });
  267. return cells;
  268. }
  269. handleMouseEnter = (e: React.MouseEvent) => {
  270. this.foundation.handleMouseEnter(e);
  271. const customRowProps = this.adapter.getCache('customRowProps');
  272. if (typeof customRowProps.onMouseEnter === 'function') {
  273. customRowProps.onMouseEnter(e);
  274. }
  275. };
  276. handleMouseLeave = (e: React.MouseEvent) => {
  277. this.foundation.handleMouseLeave(e);
  278. const customRowProps = this.adapter.getCache('customRowProps');
  279. if (typeof customRowProps.onMouseLeave === 'function') {
  280. customRowProps.onMouseLeave(e);
  281. }
  282. };
  283. handleClick = (e: React.MouseEvent) => {
  284. this.foundation.handleClick(e);
  285. const customRowProps = this.adapter.getCache('customRowProps');
  286. if (customRowProps && typeof customRowProps.onClick === 'function') {
  287. customRowProps.onClick(e);
  288. }
  289. };
  290. render() {
  291. const { style } = this.props;
  292. const {
  293. components,
  294. prefixCls,
  295. selected,
  296. onRow,
  297. index,
  298. className,
  299. replaceClassName,
  300. record,
  301. hovered,
  302. expanded,
  303. expandableRow,
  304. level,
  305. expandedRow,
  306. isSection
  307. } = this.props;
  308. const BodyRow: any = components.body.row;
  309. const { className: customClassName, style: customStyle, ...rowProps } = onRow(record, index) || {};
  310. this.adapter.setCache('customRowProps', { ...rowProps });
  311. const baseRowStyle = { ...style, ...customStyle };
  312. const rowCls =
  313. typeof replaceClassName === 'string' && replaceClassName.length ?
  314. replaceClassName :
  315. classnames(
  316. className,
  317. `${prefixCls}-row`,
  318. {
  319. [`${prefixCls}-row-selected`]: selected,
  320. [`${prefixCls}-row-expanded`]: expanded,
  321. [`${prefixCls}-row-hovered`]: hovered,
  322. },
  323. customClassName
  324. );
  325. const ariaProps = {};
  326. if (typeof index === 'number') {
  327. ariaProps['aria-rowindex'] = index + 1;
  328. }
  329. if (expandableRow) {
  330. ariaProps['aria-expanded'] = expanded;
  331. }
  332. // if row is expandedRow, set it's level to 2
  333. if (expanded || expandedRow) {
  334. ariaProps['aria-level'] = 2;
  335. }
  336. if (typeof level === 'number') {
  337. ariaProps['aria-level'] = level + 1;
  338. }
  339. if (isSection) {
  340. ariaProps['aria-level'] = 1;
  341. }
  342. return (
  343. <BodyRow
  344. role="row"
  345. {...ariaProps}
  346. {...rowProps}
  347. style={baseRowStyle}
  348. className={rowCls}
  349. ref={this._cacheNode}
  350. onMouseEnter={this.handleMouseEnter}
  351. onMouseLeave={this.handleMouseLeave}
  352. onClick={this.handleClick}
  353. >
  354. {this.renderCells()}
  355. </BodyRow>
  356. );
  357. }
  358. }
  359. export type RenderExpandIcon = (record: Record<string, any>, isNested: boolean) => ReactNode | null;