BaseRow.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. shouldComponentUpdate(nextProps: BaseRowProps) {
  149. /**
  150. * Shallow comparison of incoming props to simulate PureComponent
  151. * Deep comparison cellWidths
  152. *
  153. * 浅层对比传入的 props,模拟 PureComponent
  154. * 深比较 cellWidths
  155. */
  156. const omitProps = ['cellWidths'];
  157. const isPropsShallowEqual = shallowEqualObjects(omit(nextProps, omitProps), omit(this.props, omitProps));
  158. if (!isPropsShallowEqual || !isEqual(pick(nextProps, omitProps), pick(this.props, omitProps))) {
  159. return true;
  160. }
  161. return false;
  162. }
  163. _cacheNode = (node: any) => {
  164. this.ref.current = node;
  165. };
  166. // Pass true to render the tree-shaped expand button
  167. renderExpandIcon = (record: Record<string, any>) => {
  168. const { renderExpandIcon } = this.props;
  169. return renderExpandIcon(record, true);
  170. };
  171. renderCells() {
  172. const {
  173. columns,
  174. record,
  175. index,
  176. prefixCls,
  177. fixed,
  178. components,
  179. expandableRow,
  180. level,
  181. expandIcon,
  182. rowExpandable,
  183. isSection,
  184. expandedRow,
  185. virtualized,
  186. indentSize,
  187. hideExpandedColumn,
  188. cellWidths,
  189. selected,
  190. expanded,
  191. disabled,
  192. onDidUpdate,
  193. } = this.props;
  194. const BodyCell = get(components, 'body.cell', strings.DEFAULT_COMPONENTS.body.cell);
  195. const cells: ReactNode[] = [];
  196. const displayExpandedColumn = rowExpandable(record);
  197. let firstIndex = 0;
  198. // const dataColumns = getDataColumns(columns);
  199. each(columns, (column, columnIndex) => {
  200. const columnKey = get(column, 'key');
  201. const expandableProps: { renderExpandIcon?: (record: Record<string, any>) => ReactNode; expandIcon?: ExpandIcon; indent?: number } = {};
  202. if (fixed !== 'right') {
  203. if (isInnerColumnKey(columnKey)) {
  204. firstIndex++;
  205. }
  206. if (expandableRow && columnIndex === firstIndex) {
  207. expandableProps.renderExpandIcon = this.renderExpandIcon;
  208. if (hideExpandedColumn || isSection) {
  209. expandableProps.expandIcon = expandIcon != null ? expandIcon : true;
  210. }
  211. }
  212. // Only the first data row will be indented
  213. if (level != null && columnIndex === firstIndex) {
  214. expandableProps.indent = level;
  215. if (!expandableRow) {
  216. expandableProps.indent = level + 1;
  217. }
  218. }
  219. }
  220. if (isExpandedColumn(column) && !displayExpandedColumn) {
  221. cells.push(<TableCell key={columnIndex} colIndex={columnIndex} isSection={isSection} />);
  222. } else if (!isScrollbarColumn(column)) {
  223. const diyProps: { width?: number } = {};
  224. if (BodyCell !== strings.DEFAULT_COMPONENTS.body.cell && virtualized && !expandedRow) {
  225. diyProps.width = get(cellWidths, columnIndex);
  226. }
  227. cells.push(
  228. <TableCell
  229. colIndex={columnIndex}
  230. {...expandableProps}
  231. {...diyProps}
  232. hideExpandedColumn={hideExpandedColumn}
  233. indentSize={indentSize}
  234. isSection={isSection}
  235. prefixCls={`${prefixCls}`}
  236. column={column}
  237. key={columnIndex}
  238. index={index}
  239. record={record}
  240. component={BodyCell}
  241. fixedLeft={isFixedLeft(column) && arrayAdd(cellWidths, 0, columnIndex)}
  242. lastFixedLeft={isLastLeftFixed(columns, column)}
  243. fixedRight={isFixedRight(column) && arrayAdd(cellWidths, columnIndex + 1)}
  244. firstFixedRight={isFirstFixedRight(columns, column)}
  245. selected={selected}
  246. expanded={expanded}
  247. disabled={disabled}
  248. onDidUpdate={onDidUpdate}
  249. />
  250. );
  251. }
  252. });
  253. return cells;
  254. }
  255. handleMouseEnter = (e: React.MouseEvent) => {
  256. this.foundation.handleMouseEnter(e);
  257. const customRowProps = this.adapter.getCache('customRowProps');
  258. if (typeof customRowProps.onMouseEnter === 'function') {
  259. customRowProps.onMouseEnter(e);
  260. }
  261. };
  262. handleMouseLeave = (e: React.MouseEvent) => {
  263. this.foundation.handleMouseLeave(e);
  264. const customRowProps = this.adapter.getCache('customRowProps');
  265. if (typeof customRowProps.onMouseLeave === 'function') {
  266. customRowProps.onMouseLeave(e);
  267. }
  268. };
  269. handleClick = (e: React.MouseEvent) => {
  270. this.foundation.handleClick(e);
  271. const customRowProps = this.adapter.getCache('customRowProps');
  272. if (customRowProps && typeof customRowProps.onClick === 'function') {
  273. customRowProps.onClick(e);
  274. }
  275. };
  276. render() {
  277. const { style } = this.props;
  278. const {
  279. components,
  280. prefixCls,
  281. selected,
  282. onRow,
  283. index,
  284. className,
  285. replaceClassName,
  286. record,
  287. hovered,
  288. expanded,
  289. expandableRow,
  290. level,
  291. expandedRow,
  292. isSection
  293. } = this.props;
  294. const BodyRow: any = components.body.row;
  295. const { className: customClassName, style: customStyle, ...rowProps } = onRow(record, index) || {};
  296. this.adapter.setCache('customRowProps', { ...rowProps });
  297. const baseRowStyle = { ...style, ...customStyle };
  298. const rowCls =
  299. typeof replaceClassName === 'string' && replaceClassName.length ?
  300. replaceClassName :
  301. classnames(
  302. className,
  303. `${prefixCls}-row`,
  304. {
  305. [`${prefixCls}-row-selected`]: selected,
  306. [`${prefixCls}-row-expanded`]: expanded,
  307. [`${prefixCls}-row-hovered`]: hovered,
  308. },
  309. customClassName
  310. );
  311. const ariaProps = {};
  312. if (typeof index === 'number') {
  313. ariaProps['aria-rowindex'] = index + 1;
  314. }
  315. if (expandableRow) {
  316. ariaProps['aria-expanded'] = expanded;
  317. }
  318. // if row is expandedRow, set it's level to 2
  319. if (expanded || expandedRow) {
  320. ariaProps['aria-level'] = 2;
  321. }
  322. if (typeof level === 'number') {
  323. ariaProps['aria-level'] = level + 1;
  324. }
  325. if (isSection) {
  326. ariaProps['aria-level'] = 1;
  327. }
  328. return (
  329. <BodyRow
  330. role="row"
  331. {...ariaProps}
  332. {...rowProps}
  333. style={baseRowStyle}
  334. className={rowCls}
  335. ref={this._cacheNode}
  336. onMouseEnter={this.handleMouseEnter}
  337. onMouseLeave={this.handleMouseLeave}
  338. onClick={this.handleClick}
  339. >
  340. {this.renderCells()}
  341. </BodyRow>
  342. );
  343. }
  344. }
  345. export type RenderExpandIcon = (record: Record<string, any>, isNested: boolean) => ReactNode | null;