tachybase_todo/packages/client/src/schema-initializer/Initializers/utils.ts

146 lines
3.7 KiB
TypeScript
Raw Normal View History

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();
return (
fields
// .filter((field) => field?.uiSchema?.title)
.map((field) => {
return {
type: 'item',
title: field?.uiSchema?.title || field.name,
schema: {
name: field.name,
'x-collection-field': `${name}.${field.name}`,
'x-component': 'CollectionField',
},
component: 'CollectionFieldInitializer',
2022-02-25 23:09:42 +08:00
find: findTableColumn,
2022-02-22 11:17:24 +08:00
remove: removeTableColumn,
} as SchemaInitializerItemOptions;
})
);
};
export const useFormItemInitializerFields = () => {
const { name, fields } = useCollection();
return fields?.map((field) => {
2022-02-25 23:09:42 +08:00
if (field.interface === 'subTable') {
return {
type: 'item',
title: field?.uiSchema?.title || field.name,
component: 'SubTableFieldInitializer',
remove: removeGridFormItem,
field,
schema: {
type: 'void',
name: field.name,
'x-designer': 'FormItem.Designer',
'x-component': 'div',
'x-decorator': 'FormItem',
'x-collection-field': `${name}.${field.name}`,
},
} as SchemaInitializerItemOptions;
}
2022-02-22 11:17:24 +08:00
return {
type: 'item',
title: field?.uiSchema?.title || field.name,
component: 'CollectionFieldInitializer',
remove: removeGridFormItem,
schema: {
name: field.name,
'x-designer': 'FormItem.Designer',
'x-component': 'CollectionField',
'x-decorator': 'FormItem',
'x-collection-field': `${name}.${field.name}`,
},
} as SchemaInitializerItemOptions;
});
};
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);
},
};
};