Skip to content

Commit

Permalink
fix(json-schema): fix findComponent return unexpected result (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweiGL authored Jun 22, 2021
1 parent b549390 commit 3453c69
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
"build:docs": "dumi build",
"start": "dumi dev",
"test": "jest --coverage",
"test:sc": "jest packages/react-shared-components/src/__tests__/*.spec.tsx",
"test:sc": "jest packages/react-shared-components",
"test:sc:watch": "npm run test:sc -- --watch",
"test:reactive": "jest packages/reactive/src/__tests__/*.spec.ts",
"test:core": "jest packages/core/src/__tests__/*.spec.ts",
"test:reactive": "jest packages/reactive",
"test:core": "jest packages/core",
"test:core:watch": "npm run test:core --- --watch",
"test:schema": "jest packages/json-schema/src/__tests__/*.spec.ts",
"test:schema": "jest packages/json-schema",
"test:schema:watch": "npm run test:schema --- --watch",
"test:react": "jest packages/react/src/__tests__/*.spec.tsx",
"test:shared": "jest packages/shared/src/__tests__/*.spec.ts",
"test:path": "jest packages/path/src/__tests__/*.spec.ts",
"test:react": "jest packages/react",
"test:shared": "jest packages/shared",
"test:path": "jest packages/path",
"test:react:watch": "npm run test:react --- --watch",
"test:react-schema": "jest packages/react-schema-field/src/__tests__/*.spec.tsx",
"test:react-schema": "jest packages/react-schema-field",
"test:react-schema:watch": "npm run test:react-schema --- --watch",
"test:vue": "jest packages/vue/src/__tests__/*.spec.ts",
"test:vue": "jest packages/vue",
"test:vue:watch": "npm run test:vue --- --watch",
"test:reactive-vue": "jest packages/reactive-vue/src/__tests__/*.spec.ts",
"test:reactive-vue": "jest packages/reactive-vue",
"test:reactive-vue:watch": "npm run test:reactive-vue --- --watch",
"test:watch": "jest --watch",
"test:prod": "jest --coverage --silent",
Expand Down
46 changes: 46 additions & 0 deletions packages/json-schema/src/__tests__/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,49 @@ describe('transform validator', () => {
expect(props.validator.length).toEqual(1)
})
})

describe('transform component', () => {
test('findComponent', () => {
const Input = () => null
const Number = () => null

Input.Number = Number

const options = {
components: {
Input,
},
}

const schema = new Schema({
type: 'object',
properties: {
a: {
type: 'string',
'x-component': '',
},
b: {
type: 'string',
'x-component': 'Input',
},
c: {
type: 'string',
'x-component': 'Input.Number',
},
e: {
type: 'string',
'x-component': 'Test',
},
},
})

expect(schema.properties.a.toFieldProps(options).component).toBeUndefined()
expect(schema.properties.b.toFieldProps(options).component[0]).toEqual(
Input
)
expect(schema.properties.c.toFieldProps(options).component[0]).toEqual(
Number
)
expect(schema.properties.e.toFieldProps(options).component).toBeUndefined()
})
})
18 changes: 10 additions & 8 deletions packages/json-schema/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,23 @@ const findComponent = (
options: ISchemaFieldFactoryOptions,
state: Formily.Core.Types.IFieldState
) => {
if (FormPath.isPathPattern(path)) {
const component =
FormPath.getIn(options?.components, path) || state?.[type]?.[0]
if (component) {
return component
let component: any = state?.[type]?.[0]

if (path && options?.components) {
if (FormPath.isPathPattern(path)) {
component = FormPath.getIn(options.components, path)
}
if (options?.components) {
if (!component) {
//Todo: need to use __DEV__ keyword
console.error(
`[Formily JSON Schema]: Cannot find the '${path}' component mapped by Schema.x-${type}`
)
}
}
if (isFn(path)) return path
return state?.[type]?.[0]
if (isFn(path)) {
return path
}
return component
}

const getBaseProps = (
Expand Down

0 comments on commit 3453c69

Please sign in to comment.