fix: block model

This commit is contained in:
chenos 2025-06-30 16:39:48 +08:00
parent 881fcd86a3
commit 0f5952a6e1
3 changed files with 81 additions and 73 deletions

View File

@ -8,8 +8,21 @@
*/ */
import { APIResource, BaseRecordResource, Collection, DefaultStructure, FlowModel } from '@nocobase/flow-engine'; import { APIResource, BaseRecordResource, Collection, DefaultStructure, FlowModel } from '@nocobase/flow-engine';
import { Card } from 'antd';
import React from 'react';
export class BlockModel<T = DefaultStructure> extends FlowModel<T> {} export class BlockModel<T = DefaultStructure> extends FlowModel<T> {
decoratorProps: Record<string, any> = {};
renderComponent() {
throw new Error('renderComponent method must be implemented in subclasses of BlockModel');
return null;
}
render() {
return <Card {...this.decoratorProps}>{this.renderComponent()}</Card>;
}
}
export class DataBlockModel<T = DefaultStructure> extends BlockModel<T> { export class DataBlockModel<T = DefaultStructure> extends BlockModel<T> {
resource: APIResource; resource: APIResource;

View File

@ -216,78 +216,70 @@ export class TableModel extends DataBlockModel<TableModelStructure> {
}, },
}; };
render() { renderComponent() {
return ( return (
<Card> <Spin spinning={this.resource.loading}>
<Spin spinning={this.resource.loading}> <DndProvider>
<DndProvider> <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }}> <Space>
<Space> {this.mapSubModels('actions', (action) => {
{this.mapSubModels('actions', (action) => { // @ts-ignore
// @ts-ignore if (action.props.position === 'left') {
if (action.props.position === 'left') { return (
return ( <FlowModelRenderer model={action} showFlowSettings={{ showBackground: false, showBorder: false }} />
<FlowModelRenderer );
model={action} }
showFlowSettings={{ showBackground: false, showBorder: false }}
/>
);
}
return null; return null;
})} })}
{/* 占位 */} {/* 占位 */}
<span></span> <span></span>
</Space> </Space>
<Space> <Space>
{this.mapSubModels('actions', (action) => { {this.mapSubModels('actions', (action) => {
// @ts-ignore // @ts-ignore
if (action.props.position !== 'left') { if (action.props.position !== 'left') {
return ( return (
<FlowModelRenderer <FlowModelRenderer model={action} showFlowSettings={{ showBackground: false, showBorder: false }} />
model={action} );
showFlowSettings={{ showBackground: false, showBorder: false }} }
/>
);
}
return null; return null;
})} })}
<AddActionButton model={this} subModelBaseClass="GlobalActionModel" subModelKey="actions" /> <AddActionButton model={this} subModelBaseClass="GlobalActionModel" subModelKey="actions" />
</Space> </Space>
</div> </div>
</DndProvider> </DndProvider>
<Table <Table
components={this.components} components={this.components}
tableLayout="fixed" tableLayout="fixed"
rowKey={this.collection.filterTargetKey} rowKey={this.collection.filterTargetKey}
rowSelection={{ rowSelection={{
columnWidth: 50, columnWidth: 50,
type: 'checkbox', type: 'checkbox',
onChange: (_, selectedRows) => { onChange: (_, selectedRows) => {
this.resource.setSelectedRows(selectedRows); this.resource.setSelectedRows(selectedRows);
}, },
selectedRowKeys: this.resource.getSelectedRows().map((row) => row.id), selectedRowKeys: this.resource.getSelectedRows().map((row) => row.id),
}} }}
virtual={this.props.virtual} virtual={this.props.virtual}
scroll={{ x: 'max-content', y: 600 }} scroll={{ x: 'max-content', y: 600 }}
dataSource={this.resource.getData()} dataSource={this.resource.getData()}
columns={this.getColumns()} columns={this.getColumns()}
pagination={{ pagination={{
current: this.resource.getPage(), current: this.resource.getPage(),
pageSize: this.resource.getPageSize(), pageSize: this.resource.getPageSize(),
total: this.resource.getMeta('count'), total: this.resource.getMeta('count'),
}} }}
onChange={(pagination) => { onChange={(pagination) => {
console.log('onChange pagination:', pagination); console.log('onChange pagination:', pagination);
this.resource.loading = true; this.resource.loading = true;
this.resource.setPage(pagination.current); this.resource.setPage(pagination.current);
this.resource.setPageSize(pagination.pageSize); this.resource.setPageSize(pagination.pageSize);
this.resource.refresh(); this.resource.refresh();
}} }}
/> />
</Spin> </Spin>
</Card>
); );
} }
} }

View File

@ -436,6 +436,10 @@ export class CollectionField {
return this.options.uiSchema || {}; return this.options.uiSchema || {};
} }
get targetCollection() {
return this.collection.collectionManager.getCollection(this.options.target);
}
getComponentProps() { getComponentProps() {
return this.options.uiSchema?.['x-component-props'] || {}; return this.options.uiSchema?.['x-component-props'] || {};
} }
@ -444,11 +448,10 @@ export class CollectionField {
if (!this.options.target) { if (!this.options.target) {
return []; return [];
} }
const targetCollection = this.collection.collectionManager.getCollection(this.options.target); if (!this.targetCollection) {
if (!targetCollection) {
throw new Error(`Target collection ${this.options.target} not found for field ${this.name}`); throw new Error(`Target collection ${this.options.target} not found for field ${this.name}`);
} }
return targetCollection.getFields(); return this.targetCollection.getFields();
} }
getInterfaceOptions() { getInterfaceOptions() {