import {observer} from 'mobx-react'; import React from 'react'; import {IRow, IColumn} from '../../store/table'; import {RendererProps} from '../../factory'; import {Action} from '../Action'; import {reaction} from 'mobx'; import {isClickOnInput} from '../../utils/helper'; interface TableRowProps extends Pick { onCheck: (item: IRow) => void; classPrefix: string; renderCell: ( region: string, column: IColumn, item: IRow, props: any ) => React.ReactNode; columns: Array; item: IRow; parent?: IRow; itemClassName?: string; itemIndex: number; regionPrefix?: string; checkOnItemClick?: boolean; ignoreFootableContent?: boolean; [propName: string]: any; } @observer export class TableRow extends React.Component { // reaction?: () => void; constructor(props: TableRowProps) { super(props); this.handleAction = this.handleAction.bind(this); this.handleQuickChange = this.handleQuickChange.bind(this); this.handleChange = this.handleChange.bind(this); this.handleItemClick = this.handleItemClick.bind(this); } // 定义点击一行的行为,通过 itemAction配置 handleItemClick(e: React.MouseEvent) { if (isClickOnInput(e)) { return; } const {itemAction, onAction, item} = this.props; if (itemAction) { onAction && onAction(e, itemAction, item?.data); } this.props.onCheck(this.props.item); } handleAction(e: React.UIEvent, action: Action, ctx: any) { const {onAction, item} = this.props; onAction && onAction(e, action, ctx || item.data); } handleQuickChange( values: object, saveImmediately?: boolean, savePristine?: boolean, resetOnFailed?: boolean ) { const {onQuickChange, item} = this.props; onQuickChange && onQuickChange(item, values, saveImmediately, savePristine, resetOnFailed); } handleChange( value: any, name: string, submit?: boolean, changePristine?: boolean ) { if (!name || typeof name !== 'string') { return; } const {item, onQuickChange} = this.props; onQuickChange?.( item, { [name]: value }, submit, changePristine ); } render() { const { itemClassName, itemIndex, item, columns, renderCell, children, footableMode, ignoreFootableContent, footableColSpan, regionPrefix, checkOnItemClick, classPrefix: ns, render, classnames: cx, parent, itemAction, ...rest } = this.props; if (footableMode) { if (!item.expanded) { return null; } return ( {ignoreFootableContent ? columns.map(column => ( {column.label !== false ? : null} )) : columns.map(column => ( {column.label !== false ? ( ) : null} {renderCell( `${regionPrefix}${itemIndex}/${column.index}`, column, item, { ...rest, width: null, rowIndex: itemIndex, colIndex: column.index, key: column.index, onAction: this.handleAction, onQuickChange: this.handleQuickChange, onChange: this.handleChange } )} ))}
{render( `${regionPrefix}${itemIndex}/${column.index}/tpl`, column.label )}
); } if (parent && !parent.expanded) { return null; } return ( {columns.map(column => renderCell(`${itemIndex}/${column.index}`, column, item, { ...rest, rowIndex: itemIndex, colIndex: column.index, key: column.index, onAction: this.handleAction, onQuickChange: this.handleQuickChange, onChange: this.handleChange }) )} ); } }