RecordsScope
描述
标准作用域注入组件,向子树的 Schema 表达式注入以下内置变量:
$records当前记录列表数据
通常与 RecordScope 搭配使用:RecordsScope 为整张列表提供数据源,RecordScope 在渲染每一行时注入当前行记录。
使用约定
任何自增列表扩展组件,内部都应该使用 RecordsScope,用于传递记录作用域变量。
用例
tsx
import { createForm } from '@silver-formily/core'
import { createSchemaField, FormProvider, RecordsScope } from '@silver-formily/react'
import { Input } from 'antd'
const form = createForm()
function MyCustomComponent(props) {
return (
<RecordsScope getRecords={() => props.records}>
{props.children}
</RecordsScope>
)
}
const SchemaField = createSchemaField({
components: {
Input,
MyCustomComponent,
},
})
export default () => (
<FormProvider form={form}>
<SchemaField
schema={{
type: 'object',
properties: {
records: {
'type': 'void',
'x-component': 'MyCustomComponent',
'x-component-props': {
records: [
{
name: 'Name',
code: 'Code',
},
],
},
'properties': {
input: {
'type': 'string',
'x-component': 'Input',
'x-value': `{{\`\${$records[0].name} \${$records[0].code} \`}}`,
},
},
},
},
}}
/>
</FormProvider>
)tsx
import { createForm } from '@silver-formily/core'
import { createSchemaField, FormProvider, RecordsScope } from '@silver-formily/react'
import { Input } from 'antd'
const form = createForm()
function MyCustomComponent(props) {
return (
<RecordsScope getRecords={() => props.records}>
{props.children}
</RecordsScope>
)
}
const SchemaField = createSchemaField({
components: {
Input,
MyCustomComponent,
},
})
export default () => (
<FormProvider form={form}>
<SchemaField
schema={{
type: 'object',
properties: {
records: {
'type': 'void',
'x-component': 'MyCustomComponent',
'x-component-props': {
records: [
{
name: 'Name',
code: 'Code',
},
],
},
'properties': {
input: {
'type': 'string',
'x-component': 'Input',
'x-value': `{{\`\${$records[0].name} \${$records[0].code} \`}}`,
},
},
},
},
}}
/>
</FormProvider>
)
查看源码
API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| getRecords | 返回当前记录集合的函数 | Function | - |
| children | 子节点 | React.ReactNode | - |
公开类型要求 getRecords 必填,children 可选。运行时回调返回空值时,$records getter 回退为 []。