From 8bc884b3af3704e47c904f1161d146808229cb20 Mon Sep 17 00:00:00 2001 From: janrywang Date: Fri, 23 Apr 2021 10:32:11 +0800 Subject: [PATCH] feat(json-schema): add error when x-component can not found --- packages/json-schema/src/transformer.ts | 28 ++++++-- packages/shared/src/__tests__/index.spec.ts | 71 --------------------- packages/shared/src/index.ts | 1 - packages/shared/src/log.ts | 69 -------------------- 4 files changed, 22 insertions(+), 147 deletions(-) delete mode 100644 packages/shared/src/log.ts diff --git a/packages/json-schema/src/transformer.ts b/packages/json-schema/src/transformer.ts index 68be03179c8..36b36091561 100644 --- a/packages/json-schema/src/transformer.ts +++ b/packages/json-schema/src/transformer.ts @@ -199,6 +199,26 @@ const omitInvalid = (target: any) => { ) } +const findComponent = ( + type: 'component' | 'decorator', + path: Formily.Core.Types.FormPathPattern, + options: ISchemaFieldFactoryOptions, + state: IFieldState +) => { + if (path) { + const component = + FormPath.getIn(options?.components, path) || state?.[type]?.[0] + if (component) { + return component + } + //Todo: need to use __DEV__ keyword + console.error( + `[Formily JSON Schema]: Cannot find the '${path}' component mapped by Schema.x-${type}` + ) + } + return state?.[type]?.[0] +} + const getStateFromSchema = ( schema: Schema, options: ISchemaFieldFactoryOptions, @@ -224,15 +244,11 @@ const getStateFromSchema = ( display: schema['x-display'], pattern: schema['x-pattern'], decorator: [ - (schema['x-decorator'] && - FormPath.getIn(options?.components, schema['x-decorator'])) || - state?.decorator?.[0], + findComponent('decorator', schema['x-decorator'], options, state), schema['x-decorator-props'] || state?.decorator?.[1], ], component: [ - (schema['x-component'] && - FormPath.getIn(options?.components, schema['x-component'])) || - state?.component?.[0], + findComponent('component', schema['x-component'], options, state), schema['x-component-props'] || state?.component?.[1], ], }) diff --git a/packages/shared/src/__tests__/index.spec.ts b/packages/shared/src/__tests__/index.spec.ts index db9536bdf55..27fc0a75328 100644 --- a/packages/shared/src/__tests__/index.spec.ts +++ b/packages/shared/src/__tests__/index.spec.ts @@ -21,7 +21,6 @@ import { Subscribable } from '../subscribable' import { merge } from '../merge' import { instOf } from '../instanceof' import { isFn, isHTMLElement, isNumberLike, isReactElement } from '../checkers' -import { log } from '../log' import { defaults } from '../defaults' describe('array', () => { @@ -525,76 +524,6 @@ describe('types', () => { }) }) -describe('log', () => { - const SomeString = Date.now().toString(32) - const SomeObject = { v: SomeString } - const Keyword = 'Formily' - const Tips = 'you should do something' - const FormilyLog = log - test('log api', () => { - expect(FormilyLog.log(SomeString)).toEqual({ - content: SomeString, - keyword: Keyword, - }) - expect(FormilyLog.log(SomeObject)).toEqual({ - content: SomeObject, - keyword: Keyword, - }) - }) - test('info api', () => { - expect(FormilyLog.info(SomeString)).toEqual({ - content: SomeString, - keyword: Keyword, - }) - expect(FormilyLog.info(SomeObject)).toEqual({ - content: SomeObject, - keyword: Keyword, - }) - }) - test('warn api', () => { - expect(FormilyLog.warn(SomeString)).toEqual({ - content: SomeString, - keyword: Keyword, - }) - expect(FormilyLog.warn(SomeObject)).toEqual({ - content: SomeObject, - keyword: Keyword, - }) - - expect(FormilyLog.warn(SomeString, Tips)).toEqual({ - content: SomeString, - keyword: Keyword, - tips: Tips, - }) - expect(FormilyLog.warn(SomeObject, Tips)).toEqual({ - content: SomeObject, - keyword: Keyword, - tips: Tips, - }) - }) - test('error api', () => { - expect(FormilyLog.error(SomeString)).toEqual({ - content: SomeString, - keyword: Keyword, - }) - expect(FormilyLog.error(SomeObject)).toEqual({ - content: SomeObject, - keyword: Keyword, - }) - - expect(FormilyLog.error(SomeString, Tips)).toEqual({ - content: SomeString, - keyword: Keyword, - tips: Tips, - }) - expect(FormilyLog.error(SomeObject, Tips)).toEqual({ - content: SomeObject, - keyword: Keyword, - tips: Tips, - }) - }) -}) - describe('merge', () => { test('assign', () => { const target = { diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 945ae8c1d76..301044bad89 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -12,5 +12,4 @@ export * from './subscribable' export * from './merge' export * from './instanceof' export * from './defaults' -export * from './log' export * from './uid' diff --git a/packages/shared/src/log.ts b/packages/shared/src/log.ts deleted file mode 100644 index 43dadd06f0e..00000000000 --- a/packages/shared/src/log.ts +++ /dev/null @@ -1,69 +0,0 @@ -enum CONSOLE_METHODS { - DEBUG = 'debug', - ERROR = 'error', - INFO = 'info', - LOG = 'log', - WARN = 'warn', - DIR = 'dir', - DIRXML = 'dirxml', - TABLE = 'table', - TRACE = 'trace', - GROUP = 'group', - GROUPCOLLAPSED = 'groupCollapsed', - GROUPEND = 'groupEnd', - CLEAR = 'clear', - COUNT = 'count', - COUNTRESET = 'countReset', - ASSERT = 'assert', - PROFILE = 'profile', - PROFILEEND = 'profileEnd', - TIME = 'time', - TIMELOG = 'timeLog', - TIMEEND = 'timeEnd', - TIMESTAMP = 'timeStamp', - CONTEXT = 'context', - MEMORY = 'memory', - // custom name - TIPS = 'tips', -} -type ILogData = { - content: C - tips?: T - keyword: string -} - -class Log { - private keyword = 'APP' - constructor(keyword: string) { - this.keyword = keyword - } - - private getKeyWordStyle(name: CONSOLE_METHODS): string { - return `[ ${this.keyword} ${name} ]: ` - } - private callConsole(name: CONSOLE_METHODS, content: C, tips?: T) { - const logData: ILogData = { content, keyword: this.keyword } - const Console = console - const keyword = this.getKeyWordStyle(name) - Console[name] && Console[name](keyword, content) - if (tips) { - logData.tips = tips - Console.info(this.getKeyWordStyle(CONSOLE_METHODS.TIPS), tips) - } - return logData - } - public log(content: C) { - return this.callConsole(CONSOLE_METHODS.LOG, content) - } - public warn(content: C, tips?: T) { - return this.callConsole(CONSOLE_METHODS.WARN, content, tips) - } - public error(content: C, tips?: T) { - return this.callConsole(CONSOLE_METHODS.ERROR, content, tips) - } - public info(content: C) { - return this.callConsole(CONSOLE_METHODS.INFO, content) - } -} - -export const log = new Log('Formily')