feat: improve code

This commit is contained in:
chenos 2021-08-27 10:31:27 +08:00
parent 5aad2bae76
commit 622a6d5a27
9 changed files with 67 additions and 4 deletions

View File

@ -145,8 +145,10 @@ export const loadChinaRegionData = (
};
const useChinaRegionFieldValue = (field: ArrayField) => {
if (field.readPretty) {
field.value = field?.value?.sort((a, b) => a.level - b.level);
console.log('useChinaRegionFieldValue', field.value);
}
console.log('useChinaRegionFieldValue', field);
}
export const SchemaField = createSchemaField({

View File

@ -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': {

View File

@ -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': {

View File

@ -8,6 +8,7 @@ export const createdBy: FieldOptions = {
group: 'systemInfo',
order: 3,
title: '创建人',
isAssociation: true,
default: {
dataType: 'belongsTo',
target: 'users',

View File

@ -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;
}

View File

@ -8,6 +8,7 @@ export const linkTo: FieldOptions = {
group: 'relation',
order: 1,
title: '关联字段',
isAssociation: true,
default: {
dataType: 'belongsToMany',
// name,

View File

@ -8,6 +8,7 @@ export const subTable: FieldOptions = {
group: 'relation',
order: 2,
title: '子表格',
isAssociation: true,
default: {
dataType: 'hasMany',
// name,

View File

@ -7,6 +7,7 @@ export const updatedBy: FieldOptions = {
group: 'systemInfo',
order: 4,
title: '最后修改人',
isAssociation: true,
default: {
dataType: 'belongsTo',
target: 'users',

View File

@ -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 (
<Dropdown
trigger={['hover']}
@ -417,6 +417,7 @@ function AddColumn() {
checked={displayed.has(field.name)}
onChange={async (checked) => {
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();
}}