mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-01 19:38:16 +08:00
schema ts 类型调整
This commit is contained in:
parent
c94a2fab77
commit
238f055ec6
@ -1,16 +1,41 @@
|
||||
import {
|
||||
BaseType,
|
||||
Context,
|
||||
IntersectionNodeParser as BaseIntersectionNodeParser
|
||||
IntersectionNodeParser as BaseIntersectionNodeParser,
|
||||
IntersectionType,
|
||||
NodeParser,
|
||||
ReferenceType
|
||||
} from 'ts-json-schema-generator';
|
||||
import ts from 'typescript';
|
||||
|
||||
export class IntersectionNodeParser extends BaseIntersectionNodeParser {
|
||||
protected readonly childParser: NodeParser;
|
||||
|
||||
public constructor(typeChecker: ts.TypeChecker, childNodeParser: NodeParser) {
|
||||
super(typeChecker, childNodeParser);
|
||||
this.childParser = childNodeParser;
|
||||
}
|
||||
|
||||
public createType(
|
||||
node: ts.IntersectionTypeNode,
|
||||
context: Context
|
||||
): BaseType | undefined {
|
||||
console.log('here');
|
||||
// 这两个只能用 allOf 来了,提取不了。
|
||||
const shouldBeReference = (type: any) =>
|
||||
type.typeName?.escapedText === 'SchemaObject' ||
|
||||
type.typeName?.escapedText === 'FormControlSchema';
|
||||
|
||||
const matched = node.types.some(shouldBeReference);
|
||||
|
||||
// 跟 SchemaObject and 一般都有问题,改成 allOf 不要支持 additional props false 了
|
||||
if (matched) {
|
||||
const types: BaseType[] = node.types
|
||||
.map(type => this.childParser.createType(type, context)!)
|
||||
.filter(item => item);
|
||||
|
||||
return new IntersectionType(types);
|
||||
}
|
||||
|
||||
return super.createType(node, context);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,48 @@
|
||||
import {
|
||||
BaseType,
|
||||
Definition,
|
||||
IntersectionType,
|
||||
IntersectionTypeFormatter as BaseIntersectionTypeFormatter
|
||||
IntersectionTypeFormatter as BaseIntersectionTypeFormatter,
|
||||
ReferenceType,
|
||||
TypeFormatter
|
||||
} from 'ts-json-schema-generator';
|
||||
import {getAllOfDefinitionReducer} from 'ts-json-schema-generator/dist/src/Utils/allOfDefinition';
|
||||
|
||||
export class IntersectionTypeFormatter extends BaseIntersectionTypeFormatter {
|
||||
protected readonly childFormatter: TypeFormatter;
|
||||
public constructor(childTypeFormatter: TypeFormatter) {
|
||||
super(childTypeFormatter);
|
||||
this.childFormatter = childTypeFormatter;
|
||||
}
|
||||
|
||||
getDefinition(type: any): Definition {
|
||||
if (type.types[1]?.name === 'SchemaObject') {
|
||||
console.log(type);
|
||||
process.exit(1);
|
||||
const hasReferenceType = type.types.some(
|
||||
(t: BaseType) => t instanceof ReferenceType
|
||||
);
|
||||
|
||||
if (hasReferenceType) {
|
||||
const references: Array<any> = [];
|
||||
const rest: Array<any> = [];
|
||||
|
||||
type.types.forEach((t: any) => {
|
||||
if (t instanceof ReferenceType) {
|
||||
references.push(t);
|
||||
} else {
|
||||
rest.push(t);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
allOf: references
|
||||
.map((type: any) => this.childFormatter.getDefinition(type))
|
||||
.concat(
|
||||
rest.reduce(getAllOfDefinitionReducer(this.childFormatter), {
|
||||
type: 'object',
|
||||
additionalProperties: true // 这里只能是 true 了
|
||||
} as Definition)
|
||||
)
|
||||
.filter((item: any) => item)
|
||||
} as any;
|
||||
}
|
||||
|
||||
return super.getDefinition(type);
|
||||
|
@ -1,12 +0,0 @@
|
||||
import {
|
||||
Definition,
|
||||
IntersectionType,
|
||||
ObjectType,
|
||||
ObjectTypeFormatter as BaseObjectTypeFormatter
|
||||
} from 'ts-json-schema-generator';
|
||||
|
||||
export class ObjectTypeFormatter extends BaseObjectTypeFormatter {
|
||||
getDefinition(type: ObjectType): Definition {
|
||||
return super.getDefinition(type);
|
||||
}
|
||||
}
|
@ -17,7 +17,6 @@ import {
|
||||
SubNodeParser
|
||||
} from 'ts-json-schema-generator';
|
||||
import {IntersectionTypeFormatter as MyIntersectionTypeFormatter} from './TypeFormatter/IntersectionTypeFormatter';
|
||||
import {ObjectTypeFormatter as MyObjectTypeFormatter} from './TypeFormatter/ObjectTypeFormatter';
|
||||
import {IntersectionNodeParser as MyIntersectionNodeParser} from './NodeParser/IntersectionNodeParser';
|
||||
|
||||
/**
|
||||
@ -54,12 +53,6 @@ function hackIt(generator: any) {
|
||||
new MyIntersectionTypeFormatter(circularReferenceTypeFormatter)
|
||||
);
|
||||
|
||||
replaceTypeFormatter(
|
||||
typeFormatters,
|
||||
ObjectTypeFormatter,
|
||||
new MyObjectTypeFormatter(circularReferenceTypeFormatter)
|
||||
);
|
||||
|
||||
const chainNodeParser = generator.nodeParser.childNodeParser;
|
||||
const typeChecker = generator.program.getTypeChecker();
|
||||
|
||||
|
@ -18,12 +18,13 @@ import {
|
||||
SchemaClassName,
|
||||
SchemaCollection,
|
||||
SchemaExpression,
|
||||
SchemaObject,
|
||||
SchemaTpl,
|
||||
SchemaUrlPath
|
||||
} from '../Schema';
|
||||
import {ActionSchema} from './Action';
|
||||
|
||||
export type CardBodyField = BaseSchema & {
|
||||
export type CardBodyField = SchemaObject & {
|
||||
/**
|
||||
* 列标题
|
||||
*/
|
||||
|
@ -40,6 +40,13 @@ export type ComboCondition = {
|
||||
mode?: string;
|
||||
};
|
||||
|
||||
export type ComboSubControl = FormControlSchema & {
|
||||
/**
|
||||
* 是否唯一, 只有在 combo 里面才有用
|
||||
*/
|
||||
unique?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Combo 组合输入框类型
|
||||
* 文档:https://baidu.gitee.io/amis/docs/components/form/combo
|
||||
@ -106,7 +113,7 @@ export interface ComboControlSchema extends FormBaseControl {
|
||||
/**
|
||||
* 数组输入框的子项
|
||||
*/
|
||||
controls?: Array<FormControlSchema>;
|
||||
controls?: Array<ComboSubControl>;
|
||||
|
||||
/**
|
||||
* 是否可拖拽排序
|
||||
|
@ -11,6 +11,18 @@ import {FormBaseControl, FormControlSchema, FormItemWrap} from './Item';
|
||||
import getExprProperties from '../../utils/filter-schema';
|
||||
import {SchemaClassName} from '../../Schema';
|
||||
|
||||
export type GroupSubControl = FormControlSchema & {
|
||||
/**
|
||||
* 列类名, 在某些容器里面有用比如 group
|
||||
*/
|
||||
columnClassName?: SchemaClassName;
|
||||
|
||||
/**
|
||||
* 宽度占用比率。在某些容器里面有用比如 group
|
||||
*/
|
||||
columnRatio?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Group 表单集合渲染器,能让多个表单在一行显示
|
||||
* 文档:https://baidu.gitee.io/amis/docs/components/form/group
|
||||
@ -21,7 +33,7 @@ export interface GroupControlSchema extends FormBaseControl {
|
||||
/**
|
||||
* FormItem 集合
|
||||
*/
|
||||
controls: Array<FormControlSchema>;
|
||||
controls: Array<GroupSubControl>;
|
||||
|
||||
/**
|
||||
* 子表单项默认的展示模式
|
||||
|
@ -483,21 +483,6 @@ export interface FormBaseControl extends Omit<BaseSchema, 'type'> {
|
||||
* 默认值,切记只能是静态值,不支持取变量,跟数据关联是通过设置 name 属性来实现的。
|
||||
*/
|
||||
value?: any;
|
||||
|
||||
/**
|
||||
* 是否唯一, 只有在 combo 里面才有用
|
||||
*/
|
||||
unique?: boolean;
|
||||
|
||||
/**
|
||||
* 列类名, 在某些容器里面有用比如 group
|
||||
*/
|
||||
columnClassName?: SchemaClassName;
|
||||
|
||||
/**
|
||||
* 宽度占用比率。在某些容器里面有用比如 group
|
||||
*/
|
||||
columnRatio?: number;
|
||||
}
|
||||
|
||||
export interface FormItemBasicConfig extends Partial<RendererConfig> {
|
||||
|
@ -3,7 +3,7 @@ import {Renderer, RendererProps} from '../factory';
|
||||
import {Api, SchemaNode, Schema, Action} from '../types';
|
||||
import cx from 'classnames';
|
||||
import {isVisible} from '../utils/helper';
|
||||
import {BaseSchema} from '../Schema';
|
||||
import {BaseSchema, SchemaObject} from '../Schema';
|
||||
|
||||
export type HBoxColumnObject = {
|
||||
columnClassName?: string;
|
||||
@ -16,7 +16,7 @@ export type HBoxColumnObject = {
|
||||
};
|
||||
};
|
||||
|
||||
export type HBoxColumn = HBoxColumnObject & BaseSchema; // 不能用 SchemaObject 呢,会报错
|
||||
export type HBoxColumn = HBoxColumnObject & SchemaObject; // 不能用 SchemaObject 呢,会报错
|
||||
|
||||
/**
|
||||
* Hbox 水平布局渲染器。
|
||||
|
@ -28,6 +28,7 @@ import {
|
||||
SchemaClassName,
|
||||
SchemaCollection,
|
||||
SchemaExpression,
|
||||
SchemaObject,
|
||||
SchemaTokenizeableString,
|
||||
SchemaTpl,
|
||||
SchemaUrlPath
|
||||
@ -35,7 +36,7 @@ import {
|
||||
import {ActionSchema} from './Action';
|
||||
import {SchemaRemark} from './Remark';
|
||||
|
||||
export type ListBodyField = BaseSchema & {
|
||||
export type ListBodyField = SchemaObject & {
|
||||
/**
|
||||
* 列标题
|
||||
*/
|
||||
|
@ -30,6 +30,7 @@ import {TableContent} from './TableContent';
|
||||
import {
|
||||
BaseSchema,
|
||||
SchemaClassName,
|
||||
SchemaObject,
|
||||
SchemaTokenizeableString
|
||||
} from '../../Schema';
|
||||
import {FormControlSchema, FormControlType} from '../Form/Item';
|
||||
@ -37,7 +38,7 @@ import {SchemaPopOver} from '../PopOver';
|
||||
import {SchemaQuickEdit} from '../QuickEdit';
|
||||
import {SchemaCopyable} from '../Copyable';
|
||||
|
||||
export type TableColumn = BaseSchema & {
|
||||
export type TableColumn = SchemaObject & {
|
||||
/**
|
||||
* 列标题
|
||||
*/
|
||||
|
@ -2,9 +2,9 @@ import React from 'react';
|
||||
import {Renderer, RendererProps} from '../factory';
|
||||
import {Api, SchemaNode, Schema, Action} from '../types';
|
||||
import cx from 'classnames';
|
||||
import {BaseSchema} from '../Schema';
|
||||
import {BaseSchema, SchemaObject} from '../Schema';
|
||||
|
||||
export type HboxRow = BaseSchema & {
|
||||
export type HboxRow = SchemaObject & {
|
||||
rowClassName?: string;
|
||||
cellClassName?: string;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user