-
-
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(vue): improve expression scope (#2875)
* feat(vue): improve expression scope * chore(test): improve funcs coverage
- Loading branch information
Showing
8 changed files
with
172 additions
and
2 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
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,22 @@ | ||
--- | ||
order: 8 | ||
--- | ||
|
||
# ExpressionScope | ||
|
||
## 描述 | ||
|
||
用于自定义组件内部给 json-schema 表达式传递局部作用域 | ||
|
||
## 签名 | ||
|
||
```ts | ||
interface IExpressionScopeProps { | ||
value?: any | ||
} | ||
type ExpressionScope = Vue.Component<any, any, any, IExpressionScopeProps> | ||
``` | ||
## 用例 | ||
<dumi-previewer demoPath="api/components/expression-scope" /> |
56 changes: 56 additions & 0 deletions
56
packages/vue/docs/demos/api/components/expression-scope.vue
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,56 @@ | ||
<template> | ||
<FormProvider :form="form"> | ||
<SchemaField :scope="{ $outerScope: 'outer scope value' }"> | ||
<SchemaVoidField x-component="Container"> | ||
<SchemaVoidField | ||
name="div" | ||
x-component="Text" | ||
:x-component-props="{ text: `{{$innerScope + ' ' + $outerScope}}` }" | ||
/> | ||
</SchemaVoidField> | ||
</SchemaField> | ||
</FormProvider> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent } from '@vue/composition-api' | ||
import { createForm } from '@formily/core' | ||
import { | ||
FormProvider, | ||
h, | ||
createSchemaField, | ||
ExpressionScope, | ||
} from '@formily/vue' | ||
const Container = defineComponent({ | ||
setup(_props, { slots }) { | ||
return () => | ||
h( | ||
ExpressionScope, | ||
{ | ||
props: { value: { $innerScope: 'inner scope value' } }, | ||
}, | ||
slots | ||
) | ||
}, | ||
}) | ||
const Text = defineComponent({ | ||
props: ['text'], | ||
setup(props) { | ||
return () => h('div', {}, { default: () => props.text }) | ||
}, | ||
}) | ||
const SchemaField = createSchemaField({ | ||
components: { Container, Text }, | ||
}) | ||
export default { | ||
components: { FormProvider, ...SchemaField }, | ||
data() { | ||
return { | ||
Text, | ||
form: createForm(), | ||
} | ||
}, | ||
} | ||
</script> |
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,55 @@ | ||
import { render } from '@testing-library/vue' | ||
import { createForm } from '@formily/core' | ||
import { FormProvider, ExpressionScope, createSchemaField, h } from '..' | ||
import { defineComponent } from '@vue/composition-api' | ||
|
||
test('expression scope', async () => { | ||
const Container = defineComponent({ | ||
setup(_props, { slots }) { | ||
return () => | ||
h( | ||
ExpressionScope, | ||
{ | ||
props: { value: { $innerScope: 'inner scope value' } }, | ||
}, | ||
slots | ||
) | ||
}, | ||
}) | ||
const Input = defineComponent({ | ||
props: ['text'], | ||
setup(props) { | ||
return () => | ||
h( | ||
'div', | ||
{ attrs: { 'data-testid': 'test-input' } }, | ||
{ default: () => props.text } | ||
) | ||
}, | ||
}) | ||
const SchemaField = createSchemaField({ | ||
components: { Container, Input }, | ||
}) | ||
const form = createForm() | ||
const { getByTestId } = render({ | ||
components: { ...SchemaField, FormProvider }, | ||
data() { | ||
return { form } | ||
}, | ||
template: `<FormProvider :form="form"> | ||
<SchemaField :scope="{ $outerScope: 'outer scope value' }"> | ||
<SchemaVoidField x-component="Container"> | ||
<SchemaVoidField | ||
name="div" | ||
x-component="Input" | ||
:x-component-props='{ text: "{{$innerScope + $outerScope}}"}' | ||
/> | ||
</SchemaVoidField> | ||
</SchemaField> | ||
</FormProvider>`, | ||
}) | ||
|
||
expect(getByTestId('test-input').textContent).toBe( | ||
'inner scope valueouter 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { computed, defineComponent, inject, provide, Ref } from 'vue-demi' | ||
import { SchemaExpressionScopeSymbol, Fragment, h } from '../shared' | ||
import { IExpressionScopeProps } from '../types' | ||
|
||
export const ExpressionScope = defineComponent({ | ||
name: 'ExpressionScope', | ||
props: ['value'], | ||
setup(props: IExpressionScopeProps, { slots }) { | ||
const scopeRef = inject<Ref>(SchemaExpressionScopeSymbol) | ||
const expressionScopeRef = computed(() => ({ | ||
...scopeRef.value, | ||
...props.value, | ||
})) | ||
|
||
provide(SchemaExpressionScopeSymbol, expressionScopeRef) | ||
|
||
return () => h(Fragment, {}, slots) | ||
}, | ||
}) |
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