feat: 补充CRUD2 testid

This commit is contained in:
yupeng12 2024-04-23 16:26:58 +08:00
parent c3bcbf2a70
commit 9b80305323
6 changed files with 38 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import React from 'react';
import {ThemeProps, ClassNamesFn} from 'amis-core';
import {ColumnProps} from './index';
import type {TestIdBuilder} from 'amis-core';
const zIndex = 1;
@ -26,6 +27,7 @@ export interface Props extends ThemeProps {
col?: string;
index?: number;
classnames: ClassNamesFn;
testIdBuilder?: TestIdBuilder;
}
export default class BodyCell extends React.PureComponent<Props> {
@ -48,7 +50,8 @@ export default class BodyCell extends React.PureComponent<Props> {
depth,
col,
wrapperComponent: Component,
classnames: cx
classnames: cx,
testIdBuilder
} = this.props;
return (
@ -62,6 +65,7 @@ export default class BodyCell extends React.PureComponent<Props> {
style={fixed ? {position: 'sticky', zIndex, ...style} : {...style}}
data-depth={depth || null}
data-col={col}
{...testIdBuilder?.getTestId()}
>
{children}
</Component>

View File

@ -18,6 +18,7 @@ import {
hasFixedColumn,
levelsSplit
} from './util';
import type {TestIdBuilder} from 'amis-core';
export interface Props extends ThemeProps {
data: any;
@ -53,6 +54,7 @@ export interface Props extends ThemeProps {
expandedRowClassName: string;
expandedRowRender?: Function;
isExpanded: boolean;
testIdBuilder?: TestIdBuilder;
[propName: string]: any; // 对应checkbox属性
}
@ -204,6 +206,7 @@ class BodyRow extends React.PureComponent<Props> {
onMouseLeave,
onClick,
onChange,
testIdBuilder,
...rest
} = this.props;
@ -223,6 +226,7 @@ class BodyRow extends React.PureComponent<Props> {
const cells = tdColumns.map((item, i) => {
// 为了支持灵活合并单元格renderers层的Table2传递的render方法返回{children: <ReactElement>, props: {rowSpan, colSpan}}
// 但直接使用amis-ui的tablerender方法一般直接返回ReactElement
const cellIDBuilder = testIdBuilder?.getChild(`cell-${i}`);
const render =
item.render && typeof item.render === 'function'
? item.render(data[item.name], data, rowIndex, i, levelsSplit(levels))
@ -256,6 +260,7 @@ class BodyRow extends React.PureComponent<Props> {
[`${className}`]: !!className
})}
col={i > -1 ? i.toString() : undefined}
testIdBuilder={cellIDBuilder}
>
<div
className={cx('Table-cell-wrapper', {
@ -272,7 +277,6 @@ class BodyRow extends React.PureComponent<Props> {
</Cell>
);
});
const rowClassNameClass =
rowClassName && typeof rowClassName === 'function'
? rowClassName(data, rowIndex)

View File

@ -43,6 +43,8 @@ import {
getSortData
} from './util';
import type {TestIdBuilder} from 'amis-core';
export interface ColumnProps {
title: string | React.ReactNode | Function;
name: string;
@ -176,6 +178,7 @@ export interface TableProps extends ThemeProps, LocaleProps, SpinnerExtraProps {
*/
autoFillHeight?: boolean | AutoFillHeightObject;
lazyRenderAfter?: boolean;
testIdBuilder?: TestIdBuilder;
}
export interface ScrollProps {
@ -995,7 +998,8 @@ export class Table extends React.PureComponent<TableProps, TableState> {
columns,
lazyRenderAfter,
classPrefix,
classnames: cx
classnames: cx,
testIdBuilder
} = this.props;
const rowSelectionKeyField = this.getRowSelectionKeyField();
@ -1003,6 +1007,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
this.state.selectedRowKeys,
key => key === data[rowSelectionKeyField]
);
const rowTIDBuilder = testIdBuilder?.getChild(`row-${rowIndex}`);
const childrenColumnName = this.getChildrenColumnName();
// 当前行是否可展开
@ -1070,6 +1075,7 @@ export class Table extends React.PureComponent<TableProps, TableState> {
lazyRenderAfter={lazyRenderAfter}
classnames={cx}
classPrefix={classPrefix}
testIdBuilder={rowTIDBuilder}
{...checkboxProps}
/>,
children

View File

@ -1313,7 +1313,6 @@ export default class CRUD2 extends React.Component<CRUD2Props, any> {
columnsTogglable,
headerToolbarClassName,
footerToolbarClassName,
testid,
...rest
} = this.props;

View File

@ -36,7 +36,14 @@ export class OperationField extends React.Component<OperationProps, object> {
static defaultProps: Partial<OperationProps> = {};
render() {
const {className, style, buttons, render, classnames: cx} = this.props;
const {
className,
style,
buttons,
render,
classnames: cx,
testIdBuilder
} = this.props;
return (
<div className={cx('OperationField', className)} style={style}>
@ -53,7 +60,10 @@ export class OperationField extends React.Component<OperationProps, object> {
...(button as any)
},
{
key: index
key: index,
testIdBuilder: testIdBuilder?.getChild(
`button-${button.testid || button.id || index}`
)
}
)
)

View File

@ -57,6 +57,8 @@ import './TableCell';
import './ColumnToggler';
import {SchemaQuickEdit} from '../QuickEdit';
import type {TestIdBuilder} from 'amis-core';
/**
* Table 2
* https://aisuda.bce.baidu.com/amis/zh-CN/components/table2
@ -877,7 +879,8 @@ export default class Table2 extends React.Component<Table2Props, object> {
itemBadge,
data,
classnames: cx,
env
env,
testIdBuilder
} = this.props;
const cols: Array<any> = [];
@ -950,6 +953,9 @@ export default class Table2 extends React.Component<Table2Props, object> {
const item =
store.getRowByIndex(rowIndex, [...(levels || [])]) || {};
const itemIDBuilder = testIdBuilder?.getChild(
`row-${rowIndex}-cell-${colIndex}`
);
const obj = {
children: this.renderCellSchema(column, {
@ -982,7 +988,8 @@ export default class Table2 extends React.Component<Table2Props, object> {
},
row: item,
showBadge,
itemBadge
itemBadge,
testIdBuilder: itemIDBuilder
}),
props
};