123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- import React, { createRef, ReactNode } from 'react';
- import classnames from 'classnames';
- import PropTypes from 'prop-types';
- import { each, noop, get, stubTrue, omit, isEqual, pick } from 'lodash';
- import { strings, cssClasses } from '@douyinfe/semi-foundation/table/constants';
- import shallowEqualObjects from '@douyinfe/semi-foundation/utils/shallowEqualObjects';
- import TableRowFoundation, { TableRowAdapter } from '@douyinfe/semi-foundation/table/tableRowFoundation';
- import {
- isLastLeftFixed,
- arrayAdd,
- isFixedLeft,
- isFixedRight,
- isScrollbarColumn,
- isFirstFixedRight,
- isInnerColumnKey,
- isExpandedColumn
- } from '@douyinfe/semi-foundation/table/utils';
- import Store from '@douyinfe/semi-foundation/utils/Store';
- import { BaseRowKeyType } from '@douyinfe/semi-foundation/table/foundation';
- import BaseComponent from '../../_base/baseComponent';
- import TableCell from '../TableCell';
- import { ColumnProps, Fixed, TableComponents, Virtualized, ExpandIcon, OnRow, RowExpandable } from '../interface';
- export interface BaseRowProps {
- anyColumnFixed?: boolean;
- cellWidths?: number[];
- className?: string;
- columns: ColumnProps[]; // required
- components?: TableComponents; // required
- disabled?: boolean;
- expandIcon?: ExpandIcon;
- expandableRow?: boolean;
- expanded?: boolean;
- expandedRow?: boolean;
- fixed?: Fixed;
- height?: string | number;
- hideExpandedColumn?: boolean;
- hovered: boolean; // required
- indent?: number;
- indentSize?: number;
- index?: number;
- isSection?: boolean;
- level?: number;
- onDidUpdate?: (ref: React.MutableRefObject<any>) => void;
- onHover?: (mouseEnter: boolean, rowKey: string | number) => void;
- onRow?: OnRow<any>;
- onRowClick?: (rowKey: BaseRowKeyType, e: React.MouseEvent, expand: boolean) => void;
- onRowDoubleClick?: (record: Record<string, any>, e: React.MouseEvent) => void;
- onRowMouseEnter?: (record: Record<string, any>, e: React.MouseEvent) => void;
- onRowMouseLeave?: (record: Record<string, any>, e: React.MouseEvent) => void;
- prefixCls?: string;
- record?: Record<string, any>;
- renderExpandIcon?: RenderExpandIcon;
- replaceClassName?: string;
- rowExpandable?: RowExpandable<any>;
- rowKey?: string | number; // required, this place rowKey is a real key of the row
- selected?: boolean;
- store?: Store;
- style?: React.CSSProperties;
- virtualized?: Virtualized;
- visible: boolean; // required
- /** whether display none */
- displayNone?: boolean
- }
- /**
- * avoid affected by https://www.npmjs.com/package/babel-plugin-transform-react-remove-prop-types
- */
- export const baseRowPropTypes = {
- anyColumnFixed: PropTypes.bool,
- cellWidths: PropTypes.array.isRequired,
- className: PropTypes.string,
- columns: PropTypes.array.isRequired,
- components: PropTypes.object.isRequired,
- disabled: PropTypes.bool,
- expandIcon: PropTypes.oneOfType([PropTypes.bool, PropTypes.func, PropTypes.node]),
- expandableRow: PropTypes.bool,
- expanded: PropTypes.bool,
- displayNone: PropTypes.bool,
- expandedRow: PropTypes.bool,
- fixed: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
- height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
- hideExpandedColumn: PropTypes.bool,
- hovered: PropTypes.bool.isRequired,
- indent: PropTypes.number,
- indentSize: PropTypes.number,
- index: PropTypes.number,
- isSection: PropTypes.bool,
- level: PropTypes.number,
- onDidUpdate: PropTypes.func,
- onHover: PropTypes.func,
- onRow: PropTypes.func,
- onRowClick: PropTypes.func,
- onRowContextMenu: PropTypes.func,
- onRowDoubleClick: PropTypes.func,
- onRowMouseEnter: PropTypes.func,
- onRowMouseLeave: PropTypes.func,
- prefixCls: PropTypes.string,
- record: PropTypes.object,
- renderExpandIcon: PropTypes.func,
- replaceClassName: PropTypes.string,
- rowExpandable: PropTypes.func,
- rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, // real key of the row
- selected: PropTypes.bool,
- store: PropTypes.object,
- style: PropTypes.object,
- virtualized: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
- visible: PropTypes.bool.isRequired,
- };
- export default class TableRow extends BaseComponent<BaseRowProps, Record<string, any>> {
- static propTypes = baseRowPropTypes;
- static defaultProps = {
- columns: [] as [],
- rowExpandable: stubTrue,
- components: {
- body: {
- row: 'tr',
- cell: 'td',
- },
- },
- prefixCls: cssClasses.PREFIX,
- onRow: noop,
- onRowClick: noop,
- onRowDoubleClick: noop,
- onRowMouseEnter: noop,
- onRowMouseLeave: noop,
- onHover: noop,
- onDidUpdate: noop,
- visible: true,
- hovered: false,
- selected: false,
- disabled: false,
- };
- get adapter(): TableRowAdapter<BaseRowProps> {
- return {
- ...super.adapter,
- notifyClick: (...args) => this.props.onRowClick(...args),
- notifyDoubleClick: (...args) => this.props.onRowDoubleClick(...args),
- notifyMouseLeave: (...args) => {
- this.props.onHover(false, this.props.rowKey);
- this.props.onRowMouseEnter(...args);
- },
- notifyMouseEnter: (...args) => {
- this.props.onHover(true, this.props.rowKey);
- this.props.onRowMouseEnter(...args);
- },
- };
- }
- ref: React.MutableRefObject<any>;
- constructor(props: BaseRowProps) {
- super(props);
- this.ref = createRef();
- this.foundation = new TableRowFoundation(this.adapter);
- }
- componentDidMount() {
- // fix #745
- // didmount/willUnmount may be called twice when React.StrictMode is true in React 18, we need to ensure that this.cache.customRowProps is correct
- const {
- onRow,
- index,
- record,
- } = this.props;
- const customRowProps = this.adapter.getCache('customRowProps');
- if (typeof customRowProps === 'undefined') {
- const { className: customClassName, style: customStyle, ...rowProps } = onRow(record, index) || {};
- this.adapter.setCache('customRowProps', { ...rowProps });
- }
- }
- shouldComponentUpdate(nextProps: BaseRowProps) {
- /**
- * Shallow comparison of incoming props to simulate PureComponent
- * Deep comparison cellWidths
- *
- * 浅层对比传入的 props,模拟 PureComponent
- * 深比较 cellWidths
- */
- const omitProps = ['cellWidths'];
- const isPropsShallowEqual = shallowEqualObjects(omit(nextProps, omitProps), omit(this.props, omitProps));
- if (!isPropsShallowEqual || !isEqual(pick(nextProps, omitProps), pick(this.props, omitProps))) {
- return true;
- }
- return false;
- }
- _cacheNode = (node: any) => {
- this.ref.current = node;
- };
- // Pass true to render the tree-shaped expand button
- renderExpandIcon = (record: Record<string, any>) => {
- const { renderExpandIcon } = this.props;
- return renderExpandIcon(record, true);
- };
- renderCells() {
- const {
- columns,
- record,
- index,
- prefixCls,
- fixed,
- components,
- expandableRow,
- level,
- expandIcon,
- rowExpandable,
- isSection,
- expandedRow,
- virtualized,
- indentSize,
- hideExpandedColumn,
- cellWidths,
- selected,
- expanded,
- disabled,
- onDidUpdate,
- } = this.props;
- const BodyCell = get(components, 'body.cell', strings.DEFAULT_COMPONENTS.body.cell);
- const cells: ReactNode[] = [];
- const displayExpandedColumn = rowExpandable(record);
- let firstIndex = 0;
- // const dataColumns = getDataColumns(columns);
- each(columns, (column, columnIndex) => {
- const columnKey = get(column, 'key');
- const expandableProps: { renderExpandIcon?: (record: Record<string, any>) => ReactNode; expandIcon?: ExpandIcon; indent?: number } = {};
- if (fixed !== 'right') {
- if (isInnerColumnKey(columnKey)) {
- firstIndex++;
- }
- if (expandableRow && columnIndex === firstIndex) {
- expandableProps.renderExpandIcon = this.renderExpandIcon;
- if (hideExpandedColumn || isSection) {
- expandableProps.expandIcon = expandIcon != null ? expandIcon : true;
- }
- }
- // Only the first data row will be indented
- if (level != null && columnIndex === firstIndex) {
- expandableProps.indent = level;
- if (!expandableRow && hideExpandedColumn) {
- expandableProps.indent = level + 1;
- }
- }
- }
- if (isExpandedColumn(column) && !displayExpandedColumn) {
- cells.push(<TableCell key={columnIndex} colIndex={columnIndex} isSection={isSection} />);
- } else if (!isScrollbarColumn(column)) {
- const diyProps: { width?: number } = {};
- if (BodyCell !== strings.DEFAULT_COMPONENTS.body.cell && virtualized && !expandedRow) {
- diyProps.width = get(cellWidths, columnIndex);
- }
- cells.push(
- <TableCell
- colIndex={columnIndex}
- {...expandableProps}
- {...diyProps}
- hideExpandedColumn={hideExpandedColumn}
- indentSize={indentSize}
- isSection={isSection}
- prefixCls={`${prefixCls}`}
- column={column}
- key={columnIndex}
- index={index}
- record={record}
- component={BodyCell}
- fixedLeft={isFixedLeft(column) && arrayAdd(cellWidths, 0, columnIndex)}
- lastFixedLeft={isLastLeftFixed(columns, column)}
- fixedRight={isFixedRight(column) && arrayAdd(cellWidths, columnIndex + 1)}
- firstFixedRight={isFirstFixedRight(columns, column)}
- selected={selected}
- expanded={expanded}
- disabled={disabled}
- onDidUpdate={onDidUpdate}
- />
- );
- }
- });
- return cells;
- }
- handleMouseEnter = (e: React.MouseEvent) => {
- this.foundation.handleMouseEnter(e);
- const customRowProps = this.adapter.getCache('customRowProps');
- if (typeof customRowProps?.onMouseEnter === 'function') {
- customRowProps.onMouseEnter(e);
- }
- };
- handleMouseLeave = (e: React.MouseEvent) => {
- this.foundation.handleMouseLeave(e);
- const customRowProps = this.adapter.getCache('customRowProps');
- if (typeof customRowProps?.onMouseLeave === 'function') {
- customRowProps.onMouseLeave(e);
- }
- };
- handleClick = (e: React.MouseEvent) => {
- this.foundation.handleClick(e);
- const customRowProps = this.adapter.getCache('customRowProps');
- if (customRowProps && typeof customRowProps.onClick === 'function') {
- customRowProps.onClick(e);
- }
- };
- render() {
- const { style } = this.props;
- const {
- components,
- prefixCls,
- selected,
- onRow,
- index,
- className,
- replaceClassName,
- record,
- hovered,
- expanded,
- displayNone,
- expandableRow,
- level,
- expandedRow,
- isSection
- } = this.props;
- const BodyRow: any = components.body.row;
- const { className: customClassName, style: customStyle, ...rowProps } = onRow(record, index) || {};
- this.adapter.setCache('customRowProps', { ...rowProps });
- const baseRowStyle = { ...style, ...customStyle };
- const rowCls =
- typeof replaceClassName === 'string' && replaceClassName.length ?
- replaceClassName :
- classnames(
- className,
- `${prefixCls}-row`,
- {
- [`${prefixCls}-row-selected`]: selected,
- [`${prefixCls}-row-expanded`]: expanded,
- [`${prefixCls}-row-hovered`]: hovered,
- [`${prefixCls}-row-hidden`]: displayNone,
- },
- customClassName
- );
- const ariaProps = {};
- if (typeof index === 'number') {
- ariaProps['aria-rowindex'] = index + 1;
- }
- if (expandableRow) {
- ariaProps['aria-expanded'] = expanded;
- }
- // if row is expandedRow, set it's level to 2
- if (expanded || expandedRow) {
- ariaProps['aria-level'] = 2;
- }
- if (typeof level === 'number') {
- ariaProps['aria-level'] = level + 1;
- }
- if (isSection) {
- ariaProps['aria-level'] = 1;
- }
- return (
- <BodyRow
- role="row"
- {...ariaProps}
- {...rowProps}
- style={baseRowStyle}
- className={rowCls}
- ref={this._cacheNode}
- onMouseEnter={this.handleMouseEnter}
- onMouseLeave={this.handleMouseLeave}
- onClick={this.handleClick}
- >
- {this.renderCells()}
- </BodyRow>
- );
- }
- }
- export type RenderExpandIcon = (record: Record<string, any>, isNested: boolean) => ReactNode | null;
|