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) => { const useChinaRegionFieldValue = (field: ArrayField) => {
if (field.readPretty) {
field.value = field?.value?.sort((a, b) => a.level - b.level); field.value = field?.value?.sort((a, b) => a.level - b.level);
console.log('useChinaRegionFieldValue', field.value); }
console.log('useChinaRegionFieldValue', field);
} }
export const SchemaField = createSchemaField({ export const SchemaField = createSchemaField({

View File

@ -1,11 +1,13 @@
import { FieldOptions } from '.'; import { FieldOptions } from '.';
import { defaultProps } from './properties'; import { defaultProps } from './properties';
import { uid } from '@formily/shared';
export const attachment: FieldOptions = { export const attachment: FieldOptions = {
name: 'attachment', name: 'attachment',
type: 'object', type: 'object',
group: 'media', group: 'media',
title: '附件', title: '附件',
isAssociation: true,
default: { default: {
dataType: 'belongsToMany', dataType: 'belongsToMany',
target: 'attachments', target: 'attachments',
@ -18,6 +20,23 @@ export const attachment: FieldOptions = {
'x-designable-bar': 'Upload.DesignableBar', '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: { properties: {
...defaultProps, ...defaultProps,
'uiSchema.x-component-props.multiple': { 'uiSchema.x-component-props.multiple': {

View File

@ -1,5 +1,6 @@
import { defaultProps } from './properties'; import { defaultProps } from './properties';
import { FieldOptions } from '.'; import { FieldOptions } from '.';
import { uid } from '@formily/shared';
export const chinaRegion: FieldOptions = { export const chinaRegion: FieldOptions = {
name: 'chinaRegion', name: 'chinaRegion',
@ -7,6 +8,7 @@ export const chinaRegion: FieldOptions = {
group: 'choices', group: 'choices',
order: 7, order: 7,
title: '中国行政区划', title: '中国行政区划',
isAssociation: true,
default: { default: {
dataType: 'belongsToMany', dataType: 'belongsToMany',
target: 'china_regions', target: 'china_regions',
@ -35,6 +37,23 @@ export const chinaRegion: FieldOptions = {
'x-designable-bar': 'Cascader.DesignableBar', '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: { properties: {
...defaultProps, ...defaultProps,
'uiSchema.x-component-props.maxLevel': { 'uiSchema.x-component-props.maxLevel': {

View File

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

View File

@ -66,3 +66,8 @@ export const options = Object.keys(groupLabels).map(groupName => {
}).sort((a, b) => a.order - b.order), }).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', group: 'relation',
order: 1, order: 1,
title: '关联字段', title: '关联字段',
isAssociation: true,
default: { default: {
dataType: 'belongsToMany', dataType: 'belongsToMany',
// name, // name,

View File

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

View File

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

View File

@ -40,7 +40,7 @@ import {
SchemaField, SchemaField,
SchemaRenderer, SchemaRenderer,
} from '../../components/schema-renderer'; } 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 { DraggableBlockContext } from '../../components/drag-and-drop';
import AddNew from '../add-new'; import AddNew from '../add-new';
import { isGridRowOrCol } from '../grid'; import { isGridRowOrCol } from '../grid';
@ -402,7 +402,7 @@ function AddColumn() {
const { appendChild, remove } = useDesignable(); const { appendChild, remove } = useDesignable();
const { collection, fields, refresh } = useCollectionContext(); const { collection, fields, refresh } = useCollectionContext();
const displayed = useDisplayedMapContext(); const displayed = useDisplayedMapContext();
// const { service } = useTable(); const { service } = useTable();
return ( return (
<Dropdown <Dropdown
trigger={['hover']} trigger={['hover']}
@ -417,6 +417,7 @@ function AddColumn() {
checked={displayed.has(field.name)} checked={displayed.has(field.name)}
onChange={async (checked) => { onChange={async (checked) => {
if (checked) { if (checked) {
console.log('SwitchMenuItem.field.name', field.dataType, service.params[0])
const data = appendChild({ const data = appendChild({
type: 'void', type: 'void',
'x-component': 'Table.Column', 'x-component': 'Table.Column',
@ -426,12 +427,25 @@ function AddColumn() {
'x-designable-bar': 'Table.Column.DesignableBar', 'x-designable-bar': 'Table.Column.DesignableBar',
}); });
await createSchema(data); await createSchema(data);
if (isAssociation(field)) {
const defaultAppends = service.params[0]?.defaultAppends || [];
defaultAppends.push(field.name);
await service.run({...service.params[0], defaultAppends});
}
} else { } else {
const s: any = displayed.get(field.name); const s: any = displayed.get(field.name);
const p = getSchemaPath(s); const p = getSchemaPath(s);
const removed = remove(p); const removed = remove(p);
await removeSchema(removed); await removeSchema(removed);
displayed.remove(field.name); 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(); // service.refresh();
}} }}