2022-02-22 11:17:24 +08:00
|
|
|
import { ISchema, Schema, useFieldSchema } from '@formily/react';
|
|
|
|
import { uid } from '@formily/shared';
|
|
|
|
import { SchemaInitializerItemOptions } from '../';
|
|
|
|
import { useCollection } from '../../collection-manager';
|
|
|
|
import { useDesignable } from '../../schema-component';
|
|
|
|
|
|
|
|
export const gridRowColWrap = (schema: ISchema) => {
|
|
|
|
return {
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'Grid.Row',
|
|
|
|
properties: {
|
|
|
|
[uid()]: {
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'Grid.Col',
|
|
|
|
properties: {
|
|
|
|
[schema.name || uid()]: schema,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeTableColumn = (schema, cb) => {
|
|
|
|
cb(schema, {
|
|
|
|
removeParentsIfNoChildren: true,
|
2022-02-25 23:09:42 +08:00
|
|
|
// breakRemoveOn: (s) => {
|
|
|
|
// return s['x-component'].startsWith('Table.');
|
|
|
|
// },
|
2022-02-22 11:17:24 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeGridFormItem = (schema, cb) => {
|
|
|
|
cb(schema, {
|
|
|
|
removeParentsIfNoChildren: true,
|
|
|
|
breakRemoveOn: {
|
|
|
|
'x-component': 'Grid',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-25 23:09:42 +08:00
|
|
|
const findTableColumn = (schema: Schema, key: string, action: string, deepth: number = 0) => {
|
|
|
|
return schema.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
const c = s.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
if (c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-22 11:17:24 +08:00
|
|
|
export const useTableColumnInitializerFields = () => {
|
|
|
|
const { name, fields } = useCollection();
|
2022-03-02 13:22:30 +08:00
|
|
|
return fields
|
2022-03-02 20:08:11 +08:00
|
|
|
.filter((field) => field?.interface && field?.interface !== 'subTable')
|
2022-03-02 13:22:30 +08:00
|
|
|
.map((field) => {
|
2022-03-03 12:15:17 +08:00
|
|
|
if (field.target) {
|
2022-02-22 11:17:24 +08:00
|
|
|
return {
|
2022-03-02 13:22:30 +08:00
|
|
|
field,
|
2022-02-22 11:17:24 +08:00
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
2022-03-02 13:22:30 +08:00
|
|
|
find: findTableColumn,
|
|
|
|
remove: removeTableColumn,
|
|
|
|
component: 'LinkToFieldInitializer',
|
2022-02-22 11:17:24 +08:00
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
2022-03-02 13:22:30 +08:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
component: 'CollectionFieldInitializer',
|
|
|
|
find: findTableColumn,
|
|
|
|
remove: removeTableColumn,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
});
|
2022-02-22 11:17:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useFormItemInitializerFields = () => {
|
|
|
|
const { name, fields } = useCollection();
|
2022-03-02 20:08:11 +08:00
|
|
|
return fields
|
|
|
|
?.filter((field) => field?.interface)
|
|
|
|
?.map((field) => {
|
2022-03-03 12:15:17 +08:00
|
|
|
if (field.interface === 'subTable') {
|
2022-03-02 20:08:11 +08:00
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
2022-03-03 12:15:17 +08:00
|
|
|
component: 'SubTableFieldInitializer',
|
2022-03-02 20:08:11 +08:00
|
|
|
remove: removeGridFormItem,
|
|
|
|
field,
|
|
|
|
schema: {
|
2022-03-03 12:15:17 +08:00
|
|
|
type: 'void',
|
2022-03-02 20:08:11 +08:00
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
2022-03-03 12:15:17 +08:00
|
|
|
'x-component': 'div',
|
2022-03-02 20:08:11 +08:00
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
}
|
2022-03-03 12:15:17 +08:00
|
|
|
if (field.target) {
|
2022-03-02 20:08:11 +08:00
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
2022-03-03 12:15:17 +08:00
|
|
|
component: 'LinkToFieldInitializer',
|
2022-03-02 20:08:11 +08:00
|
|
|
remove: removeGridFormItem,
|
|
|
|
field,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
2022-03-03 12:15:17 +08:00
|
|
|
'x-component': 'CollectionField',
|
2022-03-02 20:08:11 +08:00
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
}
|
2022-03-02 13:22:30 +08:00
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
2022-03-02 20:08:11 +08:00
|
|
|
component: 'CollectionFieldInitializer',
|
2022-03-02 13:22:30 +08:00
|
|
|
remove: removeGridFormItem,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
2022-03-02 20:08:11 +08:00
|
|
|
});
|
2022-02-22 11:17:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const findSchema = (schema: Schema, key: string, action: string) => {
|
|
|
|
return schema.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
const c = findSchema(s, key, action);
|
|
|
|
if (c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeSchema = (schema, cb) => {
|
|
|
|
return cb(schema);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useCurrentSchema = (action: string, key: string, find = findSchema, rm = removeSchema) => {
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const { remove } = useDesignable();
|
|
|
|
const schema = find(fieldSchema, key, action);
|
|
|
|
return {
|
|
|
|
schema,
|
|
|
|
exists: !!schema,
|
|
|
|
remove() {
|
|
|
|
schema && rm(schema, remove);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|