Skip to content

Commit

Permalink
feat(react): improve formily scope standard specification (#3212)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jun 20, 2022
1 parent ef218b9 commit 0811c87
Show file tree
Hide file tree
Showing 32 changed files with 830 additions and 53 deletions.
8 changes: 2 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ export default () => (
<Section
title="High-Quality Community Group"
style={{ marginTop: 140 }}
titleStyle={{ paddingBottom: 140, fontWeight: 'bold' }}
titleStyle={{ paddingBottom: 20, fontWeight: 'bold' }}
>
<QrCodeGroup>
<QrCode link="//img.alicdn.com/imgextra/i2/O1CN01n7kuJW1nrXhBw3Nud_!!6000000005143-0-tps-1284-1644.jpg" />
<QrCode
title="Already Full"
link="//img.alicdn.com/imgextra/i3/O1CN018neaqX1HvbT6SUIbp_!!6000000000820-0-tps-1284-1644.jpg"
/>
<QrCode link="//img.alicdn.com/imgextra/i1/O1CN011zlc5b1uu1BDUpNg1_!!6000000006096-2-tps-978-1380.png" />
</QrCodeGroup>
</Section>
)
Expand Down
10 changes: 3 additions & 7 deletions docs/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,12 @@ import './site/styles.less'

export default () => (
<Section
title="高质量技术交流"
title="全球互助共建答疑大群"
style={{ marginTop: 140 }}
titleStyle={{ paddingBottom: 140 }}
titleStyle={{ paddingBottom: 20, fontWeight: 'bold' }}
>
<QrCodeGroup>
<QrCode link="//img.alicdn.com/imgextra/i2/O1CN01n7kuJW1nrXhBw3Nud_!!6000000005143-0-tps-1284-1644.jpg" />
<QrCode
title="该群已满"
link="//img.alicdn.com/imgextra/i3/O1CN018neaqX1HvbT6SUIbp_!!6000000000820-0-tps-1284-1644.jpg"
/>
<QrCode link="//img.alicdn.com/imgextra/i1/O1CN011zlc5b1uu1BDUpNg1_!!6000000006096-2-tps-978-1380.png" />
</QrCodeGroup>
</Section>
)
Expand Down
20 changes: 14 additions & 6 deletions packages/antd/src/array-base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
useFieldSchema,
Schema,
JSXComponent,
ExpressionScope,
RecordScope,
RecordsScope,
} from '@formily/react'
import { isValid, clone } from '@formily/shared'
import { SortableHandle } from 'react-sortable-hoc'
Expand Down Expand Up @@ -109,18 +110,25 @@ export const ArrayBase: ComposedArrayBase = (props) => {
const field = useField<ArrayField>()
const schema = useFieldSchema()
return (
<ArrayBaseContext.Provider value={{ field, schema, props }}>
{props.children}
</ArrayBaseContext.Provider>
<RecordsScope getRecords={() => field.value}>
<ArrayBaseContext.Provider value={{ field, schema, props }}>
{props.children}
</ArrayBaseContext.Provider>
</RecordsScope>
)
}

ArrayBase.Item = ({ children, ...props }) => {
return (
<ItemContext.Provider value={props}>
<ExpressionScope value={{ $record: props.record, $index: props.index }}>
<RecordScope
getIndex={() => props.index}
getRecord={() =>
typeof props.record === 'function' ? props.record() : props.record
}
>
{children}
</ExpressionScope>
</RecordScope>
</ItemContext.Provider>
)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/antd/src/array-cards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ export const ArrayCards: ComposedArrayCards = observer((props) => {
/>
)
return (
<ArrayBase.Item key={index} index={index} record={item}>
<ArrayBase.Item
key={index}
index={index}
record={() => dataSource[index]}
>
<Card
{...props}
onChange={() => {}}
Expand Down
2 changes: 1 addition & 1 deletion packages/antd/src/array-collapse/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const ArrayCollapse: ComposedArrayCollapse = observer(
address: `${path}.**`,
})
return (
<ArrayBase.Item index={index} record={item}>
<ArrayBase.Item index={index} record={() => dataSource[index]}>
<RecursionField
schema={items}
name={index}
Expand Down
6 changes: 5 additions & 1 deletion packages/antd/src/array-items/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export const ArrayItems: ComposedArrayItems = observer((props) => {
? schema.items[index] || schema.items[0]
: schema.items
return (
<ArrayBase.Item key={index} index={index} record={item}>
<ArrayBase.Item
key={index}
index={index}
record={() => dataSource[index]}
>
<SortableItem key={`item-${index}`} index={index}>
<div className={`${prefixCls}-item-inner`}>
<RecursionField schema={items} name={index} />
Expand Down
2 changes: 1 addition & 1 deletion packages/antd/src/array-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const useArrayTableColumns = (
render: (value: any, record: any) => {
const index = dataSource.indexOf(record)
const children = (
<ArrayBase.Item index={index} record={record}>
<ArrayBase.Item index={index} record={() => dataSource[index]}>
<RecursionField schema={schema} name={index} onlyRenderProperties />
</ArrayBase.Item>
)
Expand Down
13 changes: 9 additions & 4 deletions packages/antd/src/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import React from 'react'
import { Form as FormType, ObjectField, IFormFeedback } from '@formily/core'
import {
Form as FormType,
ObjectField,
IFormFeedback,
isForm,
} from '@formily/core'
import {
useParentForm,
FormProvider,
ExpressionScope,
JSXComponent,
RecordScope,
} from '@formily/react'
import { FormLayout, IFormLayoutProps } from '../form-layout'
import { PreviewText } from '../preview-text'
Expand All @@ -26,7 +31,7 @@ export const Form: React.FC<React.PropsWithChildren<FormProps>> = ({
}) => {
const top = useParentForm()
const renderContent = (form: FormType | ObjectField) => (
<ExpressionScope value={{ $$form: form }}>
<RecordScope getRecord={() => (isForm(form) ? form.values : form.value)}>
<PreviewText.Placeholder value={previewTextPlaceholder}>
<FormLayout {...props}>
{React.createElement(
Expand All @@ -42,7 +47,7 @@ export const Form: React.FC<React.PropsWithChildren<FormProps>> = ({
)}
</FormLayout>
</PreviewText.Placeholder>
</ExpressionScope>
</RecordScope>
)
if (form)
return <FormProvider form={form}>{renderContent(form)}</FormProvider>
Expand Down
20 changes: 14 additions & 6 deletions packages/next/src/array-base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
useFieldSchema,
Schema,
JSXComponent,
ExpressionScope,
RecordScope,
RecordsScope,
} from '@formily/react'
import { SortableHandle } from 'react-sortable-hoc'
import {
Expand Down Expand Up @@ -105,18 +106,25 @@ export const ArrayBase: ComposedArrayBase = (props) => {
const field = useField<ArrayField>()
const schema = useFieldSchema()
return (
<ArrayBaseContext.Provider value={{ field, schema, props }}>
{props.children}
</ArrayBaseContext.Provider>
<RecordsScope getRecords={() => field.value}>
<ArrayBaseContext.Provider value={{ field, schema, props }}>
{props.children}
</ArrayBaseContext.Provider>
</RecordsScope>
)
}

ArrayBase.Item = ({ children, ...props }) => {
return (
<ItemContext.Provider value={props}>
<ExpressionScope value={{ $record: props.record, $index: props.index }}>
<RecordScope
getIndex={() => props.index}
getRecord={() =>
typeof props.record === 'function' ? props.record() : props.record
}
>
{children}
</ExpressionScope>
</RecordScope>
</ItemContext.Provider>
)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/array-cards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ export const ArrayCards: ComposedArrayCards = observer((props) => {
/>
)
return (
<ArrayBase.Item key={index} index={index} record={item}>
<ArrayBase.Item
key={index}
index={index}
record={() => dataSource[index]}
>
<Card
contentHeight="auto"
{...props}
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/array-collapse/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ export const ArrayCollapse: ComposedArrayCollapse = observer(
key={index}
title={title()}
>
<ArrayBase.Item index={index} key={index} record={item}>
<ArrayBase.Item
index={index}
key={index}
record={() => dataSource[index]}
>
{content}
</ArrayBase.Item>
</Collapse.Panel>
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/array-items/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export const ArrayItems: ComposedArrayItems = observer((props) => {
? schema.items[index] || schema.items[0]
: schema.items
return (
<ArrayBase.Item key={index} index={index} record={item}>
<ArrayBase.Item
key={index}
index={index}
record={() => dataSource[index]}
>
<SortableItem key={`item-${index}`} index={index}>
<div className={`${prefixCls}-item-inner`}>
<RecursionField schema={items} name={index} />
Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/array-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ const useArrayTableColumns = (
cell: (value: any, _: number, record: any) => {
const index = dataSource.indexOf(record)
const children = (
<ArrayBase.Item key={index} index={index} record={record}>
<ArrayBase.Item
key={index}
index={index}
record={() => dataSource[index]}
>
<RecursionField schema={schema} name={index} onlyRenderProperties />
</ArrayBase.Item>
)
Expand Down
7 changes: 4 additions & 3 deletions packages/next/src/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo } from 'react'
import {
FormProvider,
ExpressionScope,
RecordScope,
JSXComponent,
useParentForm,
} from '@formily/react'
Expand All @@ -13,6 +13,7 @@ import {
Form as FormType,
ObjectField,
IFormFeedback,
isForm,
} from '@formily/core'
import { PreviewText } from '../preview-text'
export interface FormProps extends IFormLayoutProps {
Expand Down Expand Up @@ -40,7 +41,7 @@ export const Form: React.FC<React.PropsWithChildren<FormProps>> = ({
}, [lang])

const renderContent = (form: FormType | ObjectField) => (
<ExpressionScope value={{ $$form: form }}>
<RecordScope getRecord={() => (isForm(form) ? form.values : form.value)}>
<PreviewText.Placeholder value={previewTextPlaceholder}>
<FormLayout {...props}>
{React.createElement(
Expand All @@ -56,7 +57,7 @@ export const Form: React.FC<React.PropsWithChildren<FormProps>> = ({
)}
</FormLayout>
</PreviewText.Placeholder>
</ExpressionScope>
</RecordScope>
)

if (form)
Expand Down
62 changes: 62 additions & 0 deletions packages/react/docs/api/components/ExpressionScope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
order: 8
---

# ExpressionScope

## Description

Used to pass local scopes to json-schema expressions inside custom components

## Signature

```ts
interface IExpressionScopeProps {
value?: any
}
type ExpressionScope = React.FC<React.PropsWithChildren<IExpressionScopeProps>>
```
## Example
```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>
)
```
Loading

0 comments on commit 0811c87

Please sign in to comment.