diff --git a/packages/core/client/src/collection-manager/demos/demo5.tsx b/packages/core/client/src/collection-manager/demos/demo5.tsx new file mode 100644 index 0000000000..6dafe56fbe --- /dev/null +++ b/packages/core/client/src/collection-manager/demos/demo5.tsx @@ -0,0 +1,126 @@ +import { FormLayout } from '@formily/antd'; +import { ISchema } from '@formily/react'; +import { uid } from '@formily/shared'; +import { + CardItem, + CollectionManagerProvider, + CollectionProvider, + FormItem, + Grid, + Input, + InputNumber, + Markdown, + SchemaComponent, + SchemaComponentProvider, + SchemaInitializer, + SchemaInitializerProvider, + useCollectionManager +} from '@nocobase/client'; +import cloneDeep from 'lodash/cloneDeep'; +import React from 'react'; + +const collection: any = { + name: 'posts', + fields: [], +}; + +const schema: ISchema = { + type: 'object', + properties: { + grid: { + type: 'void', + 'x-component': 'Grid', + 'x-initializer': 'AddFieldButton', + 'x-uid': uid(), + properties: {}, + }, + }, +}; + +const gridRowColWrap = (schema: ISchema) => { + return { + type: 'void', + 'x-component': 'Grid.Row', + properties: { + [uid()]: { + type: 'void', + 'x-component': 'Grid.Col', + properties: { + [schema.name || uid()]: schema, + }, + }, + }, + }; +}; + +const FormItemInitializer = (props) => { + const { item, insert } = props; + const { getInterface } = useCollectionManager(); + return ( + { + const interfaceOptions = getInterface(item.fieldInterface); + if (!interfaceOptions) { + return; + } + const name = `f_${uid()}`; + const options = cloneDeep(interfaceOptions.default); + options.name = name; + options.uiSchema.title = name; + collection.fields.push(options); + insert({ + name, + 'x-component': 'CollectionField', + 'x-collection-field': `posts.${name}`, + 'x-component-props': {}, + 'x-decorator': 'FormItem', + 'x-designer': 'FormItem.Designer', + }); + }} + /> + ); +}; + +export const AddFieldButton = (props: any) => { + const { insertPosition = 'beforeEnd', component } = props; + return ( + + ); +}; + +export default function App() { + return ( + + + + + + + + + + + + ); +} diff --git a/packages/core/client/src/collection-manager/index.md b/packages/core/client/src/collection-manager/index.md index c85dcad725..9881147a55 100644 --- a/packages/core/client/src/collection-manager/index.md +++ b/packages/core/client/src/collection-manager/index.md @@ -143,3 +143,7 @@ resource 需要与 `` 搭配使用,用于提供当前数据 + +## Initializer + + diff --git a/packages/core/client/src/schema-component/antd/grid/demos/demo3.tsx b/packages/core/client/src/schema-component/antd/grid/demos/demo3.tsx index a1311fe258..055c1bd98a 100644 --- a/packages/core/client/src/schema-component/antd/grid/demos/demo3.tsx +++ b/packages/core/client/src/schema-component/antd/grid/demos/demo3.tsx @@ -1,9 +1,7 @@ -import { ISchema, observer, useFieldSchema } from '@formily/react'; +import { ISchema } from '@formily/react'; import { uid } from '@formily/shared'; import { - CardItem, - DragHandler, - Grid, + CardItem, Grid, Markdown, SchemaComponent, SchemaComponentProvider, @@ -55,16 +53,6 @@ export const AddBlockButton = (props: any) => { ); }; -const Block = observer((props) => { - const fieldSchema = useFieldSchema(); - return ( -
- Block {fieldSchema.title} - -
- ); -}); - const schema: ISchema = { type: 'object', properties: {