From 622a6d5a27e1ad3b4e27c67a3f24cec56d4ad005 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 27 Aug 2021 10:31:27 +0800 Subject: [PATCH] feat: improve code --- .../src/components/schema-renderer/index.tsx | 6 ++++-- .../database-field/interfaces/attachment.ts | 19 +++++++++++++++++++ .../database-field/interfaces/chinaRegion.ts | 19 +++++++++++++++++++ .../database-field/interfaces/createdBy.ts | 1 + .../database-field/interfaces/index.ts | 5 +++++ .../database-field/interfaces/linkTo.ts | 1 + .../database-field/interfaces/subTable.ts | 1 + .../database-field/interfaces/updatedBy.ts | 1 + packages/client/src/schemas/table/index.tsx | 18 ++++++++++++++++-- 9 files changed, 67 insertions(+), 4 deletions(-) diff --git a/packages/client/src/components/schema-renderer/index.tsx b/packages/client/src/components/schema-renderer/index.tsx index 5eb2fc796..e9e1a95cb 100644 --- a/packages/client/src/components/schema-renderer/index.tsx +++ b/packages/client/src/components/schema-renderer/index.tsx @@ -145,8 +145,10 @@ export const loadChinaRegionData = ( }; const useChinaRegionFieldValue = (field: ArrayField) => { - field.value = field?.value?.sort((a, b) => a.level - b.level); - console.log('useChinaRegionFieldValue', field.value); + if (field.readPretty) { + field.value = field?.value?.sort((a, b) => a.level - b.level); + } + console.log('useChinaRegionFieldValue', field); } export const SchemaField = createSchemaField({ diff --git a/packages/client/src/schemas/database-field/interfaces/attachment.ts b/packages/client/src/schemas/database-field/interfaces/attachment.ts index dabe7c963..cc8ef0d1b 100644 --- a/packages/client/src/schemas/database-field/interfaces/attachment.ts +++ b/packages/client/src/schemas/database-field/interfaces/attachment.ts @@ -1,11 +1,13 @@ import { FieldOptions } from '.'; import { defaultProps } from './properties'; +import { uid } from '@formily/shared'; export const attachment: FieldOptions = { name: 'attachment', type: 'object', group: 'media', title: '附件', + isAssociation: true, default: { dataType: 'belongsToMany', target: 'attachments', @@ -18,6 +20,23 @@ export const attachment: FieldOptions = { 'x-designable-bar': 'Upload.DesignableBar', }, }, + initialize: (values: any) => { + if (!values.through) { + values.through = `t_${uid()}`; + } + if (!values.foreignKey) { + values.foreignKey = `f_${uid()}`; + } + if (!values.otherKey) { + values.otherKey = `f_${uid()}`; + } + if (!values.sourceKey) { + values.sourceKey = 'id'; + } + if (!values.targetKey) { + values.targetKey = 'id'; + } + }, properties: { ...defaultProps, 'uiSchema.x-component-props.multiple': { diff --git a/packages/client/src/schemas/database-field/interfaces/chinaRegion.ts b/packages/client/src/schemas/database-field/interfaces/chinaRegion.ts index 558f48795..d265794e3 100644 --- a/packages/client/src/schemas/database-field/interfaces/chinaRegion.ts +++ b/packages/client/src/schemas/database-field/interfaces/chinaRegion.ts @@ -1,5 +1,6 @@ import { defaultProps } from './properties'; import { FieldOptions } from '.'; +import { uid } from '@formily/shared'; export const chinaRegion: FieldOptions = { name: 'chinaRegion', @@ -7,6 +8,7 @@ export const chinaRegion: FieldOptions = { group: 'choices', order: 7, title: '中国行政区划', + isAssociation: true, default: { dataType: 'belongsToMany', target: 'china_regions', @@ -35,6 +37,23 @@ export const chinaRegion: FieldOptions = { 'x-designable-bar': 'Cascader.DesignableBar', }, }, + initialize: (values: any) => { + if (!values.through) { + values.through = `t_${uid()}`; + } + if (!values.foreignKey) { + values.foreignKey = `f_${uid()}`; + } + if (!values.otherKey) { + values.otherKey = `f_${uid()}`; + } + if (!values.sourceKey) { + values.sourceKey = 'id'; + } + if (!values.targetKey) { + values.targetKey = 'id'; + } + }, properties: { ...defaultProps, 'uiSchema.x-component-props.maxLevel': { diff --git a/packages/client/src/schemas/database-field/interfaces/createdBy.ts b/packages/client/src/schemas/database-field/interfaces/createdBy.ts index b9355980b..9d0777798 100644 --- a/packages/client/src/schemas/database-field/interfaces/createdBy.ts +++ b/packages/client/src/schemas/database-field/interfaces/createdBy.ts @@ -8,6 +8,7 @@ export const createdBy: FieldOptions = { group: 'systemInfo', order: 3, title: '创建人', + isAssociation: true, default: { dataType: 'belongsTo', target: 'users', diff --git a/packages/client/src/schemas/database-field/interfaces/index.ts b/packages/client/src/schemas/database-field/interfaces/index.ts index a4f7a7654..89f247b30 100644 --- a/packages/client/src/schemas/database-field/interfaces/index.ts +++ b/packages/client/src/schemas/database-field/interfaces/index.ts @@ -66,3 +66,8 @@ export const options = Object.keys(groupLabels).map(groupName => { }).sort((a, b) => a.order - b.order), } }); + +export const isAssociation = (field) => { + const options = interfaces.get(field.interface); + return options?.isAssociation; +} diff --git a/packages/client/src/schemas/database-field/interfaces/linkTo.ts b/packages/client/src/schemas/database-field/interfaces/linkTo.ts index ca2d831b1..008fc50e8 100644 --- a/packages/client/src/schemas/database-field/interfaces/linkTo.ts +++ b/packages/client/src/schemas/database-field/interfaces/linkTo.ts @@ -8,6 +8,7 @@ export const linkTo: FieldOptions = { group: 'relation', order: 1, title: '关联字段', + isAssociation: true, default: { dataType: 'belongsToMany', // name, diff --git a/packages/client/src/schemas/database-field/interfaces/subTable.ts b/packages/client/src/schemas/database-field/interfaces/subTable.ts index 6bd9af0a5..af2d096ab 100644 --- a/packages/client/src/schemas/database-field/interfaces/subTable.ts +++ b/packages/client/src/schemas/database-field/interfaces/subTable.ts @@ -8,6 +8,7 @@ export const subTable: FieldOptions = { group: 'relation', order: 2, title: '子表格', + isAssociation: true, default: { dataType: 'hasMany', // name, diff --git a/packages/client/src/schemas/database-field/interfaces/updatedBy.ts b/packages/client/src/schemas/database-field/interfaces/updatedBy.ts index d65db82b4..8e0c59bd7 100644 --- a/packages/client/src/schemas/database-field/interfaces/updatedBy.ts +++ b/packages/client/src/schemas/database-field/interfaces/updatedBy.ts @@ -7,6 +7,7 @@ export const updatedBy: FieldOptions = { group: 'systemInfo', order: 4, title: '最后修改人', + isAssociation: true, default: { dataType: 'belongsTo', target: 'users', diff --git a/packages/client/src/schemas/table/index.tsx b/packages/client/src/schemas/table/index.tsx index b9e9b481e..aef1bece1 100644 --- a/packages/client/src/schemas/table/index.tsx +++ b/packages/client/src/schemas/table/index.tsx @@ -40,7 +40,7 @@ import { SchemaField, SchemaRenderer, } from '../../components/schema-renderer'; -import { interfaces, options } from '../database-field/interfaces'; +import { interfaces, isAssociation, options } from '../database-field/interfaces'; import { DraggableBlockContext } from '../../components/drag-and-drop'; import AddNew from '../add-new'; import { isGridRowOrCol } from '../grid'; @@ -402,7 +402,7 @@ function AddColumn() { const { appendChild, remove } = useDesignable(); const { collection, fields, refresh } = useCollectionContext(); const displayed = useDisplayedMapContext(); - // const { service } = useTable(); + const { service } = useTable(); return ( { if (checked) { + console.log('SwitchMenuItem.field.name', field.dataType, service.params[0]) const data = appendChild({ type: 'void', 'x-component': 'Table.Column', @@ -426,12 +427,25 @@ function AddColumn() { 'x-designable-bar': 'Table.Column.DesignableBar', }); await createSchema(data); + if (isAssociation(field)) { + const defaultAppends = service.params[0]?.defaultAppends || []; + defaultAppends.push(field.name); + await service.run({...service.params[0], defaultAppends}); + } } else { const s: any = displayed.get(field.name); const p = getSchemaPath(s); const removed = remove(p); await removeSchema(removed); displayed.remove(field.name); + if (isAssociation(field)) { + const defaultAppends = service.params[0]?.defaultAppends || []; + const index = defaultAppends.indexOf(field.name); + if (index > -1) { + defaultAppends.splice(index, 1); + } + await service.run({...service.params[0], defaultAppends}); + } } // service.refresh(); }}