Skip to content

Commit c8485c0

Browse files
authoredMay 24, 2022
feat(json-schema): support x-compile-omitted attribute (#3145)
* feat(json-schema): support x-compile-omitted attribute * feat(json-schema): support x-compile-omitted attribute
1 parent 4eee357 commit c8485c0

File tree

8 files changed

+64
-7
lines changed

8 files changed

+64
-7
lines changed
 

‎packages/json-schema/src/__tests__/traverse.spec.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ import { FormPath } from '@formily/shared'
33

44
test('traverseSchema', () => {
55
const visited = []
6+
const omitted = []
67
traverseSchema(
78
{
89
type: 'string',
10+
title: '{{aa}}',
911
required: true,
1012
'x-validator': 'phone',
13+
'x-compile-omitted': ['title'],
1114
default: {
1215
input: 123,
1316
},
1417
},
15-
(value, path) => {
16-
visited.push(path)
18+
(value, path, omitCompile) => {
19+
if (omitCompile) {
20+
omitted.push(value)
21+
} else {
22+
visited.push(path)
23+
}
1724
}
1825
)
1926
expect(visited).toEqual([
@@ -22,6 +29,7 @@ test('traverseSchema', () => {
2229
['required'],
2330
['default'],
2431
])
32+
expect(omitted).toEqual(['{{aa}}'])
2533
})
2634

2735
test('traverse circular reference', () => {

‎packages/json-schema/src/compiler.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ export const patchSchemaCompile = (
115115
scope: any,
116116
demand = false
117117
) => {
118-
traverseSchema(sourceSchema, (value, path) => {
118+
traverseSchema(sourceSchema, (value, path, omitCompile) => {
119119
let compiled = value
120120
let collected = hasCollected(() => {
121-
compiled = compile(value, scope)
121+
if (!omitCompile) {
122+
compiled = compile(value, scope)
123+
}
122124
})
123125
if (compiled === undefined) return
124126
if (demand) {

‎packages/json-schema/src/schema.ts

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ export class Schema<
178178

179179
['x-read-pretty']?: boolean;
180180

181+
['x-compile-omitted']?: string[];
182+
181183
[key: `x-${string | number}` | symbol]: any
182184

183185
_isJSONSchemaObject = true

‎packages/json-schema/src/shared.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const traverse = (
9898

9999
export const traverseSchema = (
100100
schema: ISchema,
101-
visitor: (value: any, path: any[]) => void
101+
visitor: (value: any, path: any[], omitCompile?: boolean) => void
102102
) => {
103103
if (schema['x-validator'] !== undefined) {
104104
visitor(schema['x-validator'], ['x-validator'])
@@ -107,13 +107,18 @@ export const traverseSchema = (
107107
const root = schema
108108
const traverse = (target: any, path = []) => {
109109
if (
110+
path[0] === 'x-compile-omitted' ||
110111
path[0] === 'x-validator' ||
111112
path[0] === 'version' ||
112113
path[0] === '_isJSONSchemaObject'
113114
)
114115
return
115116
if (String(path[0]).indexOf('x-') == -1 && isFn(target)) return
116117
if (SchemaNestedMap[path[0]]) return
118+
if (schema['x-compile-omitted']?.indexOf(path[0]) > -1) {
119+
visitor(target, path, true)
120+
return
121+
}
117122
if (isPlainObj(target)) {
118123
if (path[0] === 'default' || path[0] === 'x-value') {
119124
visitor(target, path)

‎packages/json-schema/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,7 @@ export type ISchema<
290290

291291
['x-read-pretty']?: boolean
292292

293+
['x-compile-omitted']?: string[]
294+
293295
[key: `x-${string | number}` | symbol]: any
294296
}>

‎packages/react/docs/api/shared/Schema.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Create a Schema Tree based on a piece of json schema data to ensure that each sc
7171
| definitions | Schema predefined | [SchemaProperties](#schemaproperties) | - |
7272
| $ref | Read the Schema from the Schema predefined and merge it into the current Schema | String | - |
7373
| x-data | Extends Data | Object | `data` |
74+
| x-compile-omitted | list of attributes to ignore compiled expressions | string[] | `[]` |
7475

7576
#### Detailed description
7677

‎packages/react/docs/api/shared/Schema.zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Schema {
7171
| definitions | Schema 预定义 | [SchemaProperties](#schemaproperties) | - |
7272
| $ref | 从 Schema 预定义中读取 Schema 并合并至当前 Schema | String | - |
7373
| x-data | 扩展属性 | Object | `data` |
74+
| x-compile-omitted | 忽略编译表达式的属性列表 | string[] | `[]` |
7475

7576
#### 详细说明
7677

‎packages/react/src/__tests__/expression.spec.tsx

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
2-
import { render } from '@testing-library/react'
2+
import { render, waitFor } from '@testing-library/react'
33
import { createForm } from '@formily/core'
4-
import { FormProvider, ExpressionScope, createSchemaField } from '..'
4+
import { FormProvider, ExpressionScope, createSchemaField, useField } from '..'
55

66
test('expression scope', async () => {
77
const Container = (props) => {
@@ -37,3 +37,39 @@ test('expression scope', async () => {
3737
'this is inner scope value this is outer scope value'
3838
)
3939
})
40+
41+
test('x-compile-omitted', async () => {
42+
const form = createForm()
43+
const SchemaField = createSchemaField({
44+
components: {
45+
Input: (props) => (
46+
<div data-testid="input">
47+
{props.aa}
48+
{useField().title}
49+
{props.extra}
50+
</div>
51+
),
52+
},
53+
})
54+
55+
const { queryByTestId } = render(
56+
<FormProvider form={form}>
57+
<SchemaField>
58+
<SchemaField.String
59+
name="target"
60+
x-compile-omitted={['x-component-props']}
61+
title="{{123 + '321'}}"
62+
x-component-props={{
63+
aa: '{{fake}}',
64+
extra: 'extra',
65+
}}
66+
x-component="Input"
67+
/>
68+
<SchemaField.String name="btn" x-component="Button" />
69+
</SchemaField>
70+
</FormProvider>
71+
)
72+
await waitFor(() => {
73+
expect(queryByTestId('input').textContent).toBe('{{fake}}123321extra')
74+
})
75+
})

0 commit comments

Comments
 (0)
Please sign in to comment.