-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): improve expression scopes (#2778)
- Loading branch information
Showing
9 changed files
with
164 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
62 changes: 62 additions & 0 deletions
62
packages/react/docs/api/components/ExpressionScope.zh-CN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
order: 8 | ||
--- | ||
|
||
# ExpressionScope | ||
|
||
## 描述 | ||
|
||
用于自定义组件内部给 json-schema 表达式传递局部作用域 | ||
|
||
## 签名 | ||
|
||
```ts | ||
interface IExpressionScopeProps { | ||
value?: any | ||
} | ||
type ExpressionScope = React.FC<IExpressionScopeProps> | ||
``` | ||
## 用例 | ||
```tsx | ||
import React from 'react' | ||
import { createForm } from '@formily/core' | ||
import { | ||
FormProvider, | ||
createSchemaField, | ||
ExpressionScope, | ||
} from '@formily/react' | ||
import { Input } from 'antd' | ||
|
||
const form = createForm() | ||
|
||
const Container = (props) => { | ||
return ( | ||
<ExpressionScope value={{ $innerScope: 'this inner scope value' }}> | ||
{props.children} | ||
</ExpressionScope> | ||
) | ||
} | ||
|
||
const SchemaField = createSchemaField({ | ||
components: { | ||
Container, | ||
Input, | ||
}, | ||
}) | ||
|
||
export default () => ( | ||
<FormProvider form={form}> | ||
<SchemaField> | ||
<SchemaField.Void x-component="Container"> | ||
<SchemaField.String | ||
name="input" | ||
x-component="Input" | ||
x-value="{{$innerScope}}" | ||
/> | ||
</SchemaField.Void> | ||
</SchemaField> | ||
</FormProvider> | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { createForm } from '@formily/core' | ||
import { FormProvider, ExpressionScope, createSchemaField } from '..' | ||
|
||
test('expression scope', async () => { | ||
const Container = (props) => { | ||
return ( | ||
<ExpressionScope value={{ $innerScope: 'this is inner scope value' }}> | ||
{props.children} | ||
</ExpressionScope> | ||
) | ||
} | ||
const Input = (props) => <div data-testid="test-input">{props.value}</div> | ||
const SchemaField = createSchemaField({ | ||
components: { | ||
Container, | ||
Input, | ||
}, | ||
}) | ||
const form = createForm() | ||
const { getByTestId } = render( | ||
<FormProvider form={form}> | ||
<SchemaField scope={{ $outerScope: 'this is outer scope value' }}> | ||
<SchemaField.Void x-component="Container"> | ||
<SchemaField.String | ||
name="input" | ||
x-component="Input" | ||
x-value="{{$innerScope + ' ' + $outerScope}}" | ||
/> | ||
</SchemaField.Void> | ||
</SchemaField> | ||
</FormProvider> | ||
) | ||
|
||
expect(getByTestId('test-input').textContent).toBe( | ||
'this is inner scope value this is outer scope value' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { useContext } from 'react' | ||
import { SchemaExpressionScopeContext } from '../shared' | ||
import { IExpressionScopeProps } from '../types' | ||
|
||
export const ExpressionScope: React.FC<IExpressionScopeProps> = (props) => { | ||
const scope = useContext(SchemaExpressionScopeContext) | ||
return ( | ||
<SchemaExpressionScopeContext.Provider value={{ ...scope, ...props.value }}> | ||
{props.children} | ||
</SchemaExpressionScopeContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters