Skip to content

Commit

Permalink
Merge pull request baidu#9517 from zhangtao07/master
Browse files Browse the repository at this point in the history
修复crud2 若干bug
  • Loading branch information
zhangtao07 committed Jan 24, 2024
2 parents c87f174 + 82bd6a3 commit 03175e1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
14 changes: 11 additions & 3 deletions packages/amis-editor/src/plugin/CRUD2/BaseCRUD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,11 @@ export class BaseCRUDPlugin extends BasePlugin {
}
}

async buildDataSchemas(node: EditorNodeType, region?: EditorNodeType) {
async buildDataSchemas(
node: EditorNodeType,
region?: EditorNodeType,
trigger?: EditorNodeType
) {
const child: EditorNodeType = node.children.find(
item => !!~['table2', 'cards', 'list'].indexOf(item.type)
);
Expand All @@ -1129,7 +1133,12 @@ export class BaseCRUDPlugin extends BasePlugin {
return;
}

const tmpSchema = await child.info.plugin.buildDataSchemas?.(child, region);
const tmpSchema = await child.info.plugin.buildDataSchemas?.(
child,
region,
trigger,
node
);

const childDataSchema = {
...tmpSchema,
Expand All @@ -1142,7 +1151,6 @@ export class BaseCRUDPlugin extends BasePlugin {
$id: 'crud2',
type: 'object',
properties: {
...items?.properties,
items: {
...items,
title: '全部数据'
Expand Down
40 changes: 35 additions & 5 deletions packages/amis-editor/src/plugin/Table2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ export class Table2Plugin extends BasePlugin {
async buildDataSchemas(
node: EditorNodeType,
region?: EditorNodeType,
trigger?: EditorNodeType
trigger?: EditorNodeType,
parent?: EditorNodeType
) {
const itemsSchema: any = {
$id: 'tableRow',
Expand All @@ -639,6 +640,32 @@ export class Table2Plugin extends BasePlugin {
item => item.isRegion && item.region === 'columns'
);

const parentScopeId = `${parent?.id}-${parent?.type}${
node.parent?.type === 'cell' ? '-currentRow' : ''
}`;

// 追加当前行scope
let isColumnChild = false;
if (trigger) {
isColumnChild = someTree(
columns?.children,
item => item.id === trigger.id
);

if (isColumnChild) {
const scopeId = `${node.id}-${node.type}-currentRow`;
if (this.manager.dataSchema.getScope(scopeId)) {
this.manager.dataSchema.removeScope(scopeId);
}
if (this.manager.dataSchema.getScope(parentScopeId)) {
this.manager.dataSchema.switchTo(parentScopeId);
}
this.manager.dataSchema.addScope([], scopeId);
this.manager.dataSchema.current.tag = '当前行记录';
this.manager.dataSchema.current.group = '组件上下文';
}
}

if (columns) {
for (let current of columns.children) {
const schema = current.schema;
Expand All @@ -657,21 +684,24 @@ export class Table2Plugin extends BasePlugin {
}
}

let cellProperties = {};
if (trigger) {
const isColumnChild = someTree(
columns?.children,
item => item.id === trigger.id
);

isColumnChild && (cellProperties = itemsSchema.properties);
// 追加当前行数据
if (isColumnChild) {
const scopeId = `${node.id}-${node.type}-currentRow`;
const scope = this.manager.dataSchema.getScope(scopeId);
scope?.addSchema(itemsSchema);
}
}

const result: any = {
$id: 'table2',
$id: `${node.id}-${node.type}`,
type: 'object',
properties: {
...cellProperties,
rows: {
type: 'array',
title: '数据列表',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
CRUDScaffoldConfig
} from '../../builder';
import type {EditorNodeType} from 'amis-editor-core';
import type {FormControlProps} from 'amis';
import type {FormControlProps, PlainObject} from 'amis';

interface Option {
label: string;
Expand Down Expand Up @@ -235,6 +235,12 @@ export class CRUDFiltersControl extends React.Component<
this.setState({loading: false});
}

setFilterVisible(schema: PlainObject) {
if (!('visibleOn' in schema) && schema.type) {
schema.visible = schema.body?.length > 0;
}
}

async updateSimpleQuery(enable: boolean) {
const {manager, nodeId, builder} = this.props;
const store = manager.store;
Expand Down Expand Up @@ -269,7 +275,7 @@ export class CRUDFiltersControl extends React.Component<
}
})) ?? [];

const newFilterSchema = traverseSchemaDeep(
let newFilterSchema = traverseSchemaDeep(
filterSchema,
(key: string, value: any, host: any) => {
/** 更新标识符 */
Expand All @@ -292,14 +298,15 @@ export class CRUDFiltersControl extends React.Component<
return [key, value];
}
);
this.setFilterVisible(newFilterSchema);

const targetNode = manager.store.getNodeById(filterSchema.$$id);

if (targetNode) {
targetNode.updateSchema(newFilterSchema);
}
} else {
const newFilterSchema = traverseSchemaDeep(
let newFilterSchema = traverseSchemaDeep(
filterSchema,
(key: string, value: any, host: any) => {
/** 更新标识符 */
Expand Down Expand Up @@ -330,6 +337,7 @@ export class CRUDFiltersControl extends React.Component<
}
);

this.setFilterVisible(newFilterSchema);
const targetNode = manager.store.getNodeById(filterSchema.$$id);

if (targetNode) {
Expand Down Expand Up @@ -402,7 +410,7 @@ export class CRUDFiltersControl extends React.Component<
}
});

const newFilterSchema = traverseSchemaDeep(
let newFilterSchema = traverseSchemaDeep(
filterSchema,
(key: string, value: any, host: any) => {
/** 更新标识符 */
Expand All @@ -425,14 +433,15 @@ export class CRUDFiltersControl extends React.Component<
return [key, value];
}
);
this.setFilterVisible(newFilterSchema);

const targetNode = manager.store.getNodeById(filterSchema.$$id);

if (targetNode) {
targetNode.updateSchema(newFilterSchema);
}
} else {
const newFilterSchema = traverseSchemaDeep(
let newFilterSchema = traverseSchemaDeep(
filterSchema,
(key: string, value: any, host: any) => {
/** 更新标识符 */
Expand Down Expand Up @@ -464,6 +473,7 @@ export class CRUDFiltersControl extends React.Component<
return [key, value];
}
);
this.setFilterVisible(newFilterSchema);

const targetNode = manager.store.getNodeById(filterSchema.$$id);

Expand Down
2 changes: 1 addition & 1 deletion packages/amis-ui/src/components/table/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export default class Head extends React.PureComponent<Props> {
colIndex === data.length - 1
})}
depth={item.depth}
col={cIndex > -1 ? cIndex.toString() : undefined}
col={String(colIndex)}
>
{typeof item.title === 'function'
? item.title(children)
Expand Down

0 comments on commit 03175e1

Please sign in to comment.