Skip to content

Commit 48fbcf0

Browse files
authored
feat: compile expression for array-table column title (#868)
* feat: compile expression for array-table column title * feat: named export SchemaContext instead of default * feat: rename SchemaContext to FormSchemaForm
1 parent 1d7d94e commit 48fbcf0

File tree

8 files changed

+38
-21
lines changed

8 files changed

+38
-21
lines changed

packages/antd-components/src/array-table/index.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React from 'react'
1+
import React, { useContext } from 'react'
22
import {
33
ISchemaFieldComponentProps,
44
SchemaField,
5-
Schema
5+
Schema,
6+
complieExpression,
7+
FormExpressionScopeContext
68
} from '@formily/react-schema-renderer'
79
import { toArr, isFn, isArr, FormPath } from '@formily/shared'
810
import { ArrayList, DragListView } from '@formily/react-shared-components'
@@ -40,6 +42,7 @@ const DragHandler = styled.span`
4042

4143
export const ArrayTable: any = styled(
4244
(props: ISchemaFieldComponentProps & { className: string }) => {
45+
const expressionScope = useContext(FormExpressionScopeContext)
4346
const { value, schema, className, editable, path, mutators } = props
4447
const {
4548
renderAddition,
@@ -71,7 +74,7 @@ export const ArrayTable: any = styled(
7174
...props.getExtendsProps()
7275
}
7376
return {
74-
title: props.title,
77+
title: complieExpression(props.title, expressionScope),
7578
...itemProps,
7679
key,
7780
dataIndex: key,
@@ -178,10 +181,12 @@ export const ArrayTable: any = styled(
178181
)}
179182
<ArrayList.Addition>
180183
{({ children }) => {
181-
return children && (
182-
<div className="array-table-addition" onClick={onAdd}>
183-
{children}
184-
</div>
184+
return (
185+
children && (
186+
<div className="array-table-addition" onClick={onAdd}>
187+
{children}
188+
</div>
189+
)
185190
)
186191
}}
187192
</ArrayList.Addition>

packages/antd/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
FormProvider,
1515
JSONCondition,
1616
complieExpression,
17+
FormExpressionScopeContext,
1718
FormEffectHooks,
1819
createEffectHook,
1920
setValidationLanguage,
@@ -79,6 +80,7 @@ export {
7980
JSONCondition,
8081
FormEffectHooks,
8182
complieExpression,
83+
FormExpressionScopeContext,
8284
createEffectHook,
8385
setValidationLanguage,
8486
setValidationLocale,

packages/next-components/src/array-table/index.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import React, { forwardRef } from 'react'
1+
import React, { forwardRef, useContext } from 'react'
22
import {
33
ISchemaFieldComponentProps,
44
SchemaField,
5-
Schema
5+
Schema,
6+
FormExpressionScopeContext,
7+
complieExpression
68
} from '@formily/react-schema-renderer'
79
import { toArr, isFn, isArr, FormPath } from '@formily/shared'
810
import { ArrayList, DragListView } from '@formily/react-shared-components'
@@ -124,6 +126,7 @@ const DragableTable = styled(props => {
124126

125127
export const ArrayTable = styled(
126128
(props: ISchemaFieldComponentProps & { className: string }) => {
129+
const expressionScope = useContext(FormExpressionScopeContext)
127130
const { value, schema, className, editable, path, mutators } = props
128131
const {
129132
renderAddition,
@@ -158,7 +161,7 @@ export const ArrayTable = styled(
158161
<ArrayList.Item
159162
width={200}
160163
{...itemProps}
161-
title={props.title}
164+
title={complieExpression(props.title, expressionScope)}
162165
key={key}
163166
dataIndex={key}
164167
cell={(value: any, index: number) => {
@@ -255,10 +258,12 @@ export const ArrayTable = styled(
255258
)}
256259
<ArrayList.Addition>
257260
{({ children }) => {
258-
return children && (
259-
<div className="array-table-addition" onClick={onAdd}>
260-
{children}
261-
</div>
261+
return (
262+
children && (
263+
<div className="array-table-addition" onClick={onAdd}>
264+
{children}
265+
</div>
266+
)
262267
)
263268
}}
264269
</ArrayList.Addition>
@@ -270,7 +275,7 @@ export const ArrayTable = styled(
270275
display: inline-block;
271276
width: 100%;
272277
max-width: 100%;
273-
overflow: scroll;
278+
overflow: auto;
274279
table {
275280
margin-bottom: 0 !important;
276281
th,

packages/next/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
FormProvider,
1515
JSONCondition,
1616
complieExpression,
17+
FormExpressionScopeContext,
1718
FormEffectHooks,
1819
createEffectHook,
1920
setValidationLanguage,
@@ -80,6 +81,7 @@ export {
8081
JSONCondition,
8182
FormEffectHooks,
8283
complieExpression,
84+
FormExpressionScopeContext,
8385
createEffectHook,
8486
setValidationLanguage,
8587
setValidationLocale,

packages/react-schema-renderer/src/components/SchemaField.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
ISchemaVirtualFieldComponentProps
1616
} from '../types'
1717
import { Schema } from '../shared/schema'
18-
import SchemaContext, {
18+
import {
19+
FormSchemaContext,
1920
FormComponentsContext,
2021
FormExpressionScopeContext
2122
} from '../shared/context'
@@ -45,7 +46,7 @@ export const SchemaField: React.FunctionComponent<ISchemaFieldProps> = (
4546
props: ISchemaFieldProps
4647
) => {
4748
const path = FormPath.parse(props.path)
48-
const formSchema = useContext(SchemaContext)
49+
const formSchema = useContext(FormSchemaContext)
4950
const fieldSchema = new Schema(props.schema || formSchema.get(path))
5051
const formRegistry = useContext(FormComponentsContext)
5152
const expressionScope = useContext(FormExpressionScopeContext)

packages/react-schema-renderer/src/components/SchemaForm.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ISchemaFormProps } from '../types'
33
import { Form } from '@formily/react'
44
import { SchemaField } from './SchemaField'
55
import { useSchemaForm } from '../hooks/useSchemaForm'
6-
import SchemaContext, {
6+
import {
7+
FormSchemaContext,
78
FormComponentsContext,
89
FormExpressionScopeContext
910
} from '../shared/context'
@@ -25,7 +26,7 @@ export const SchemaForm: React.FC<ISchemaFormProps> = props => {
2526
value={{ fields, virtualFields, formComponent, formItemComponent }}
2627
>
2728
<FormExpressionScopeContext.Provider value={props.expressionScope}>
28-
<SchemaContext.Provider value={schema}>
29+
<FormSchemaContext.Provider value={schema}>
2930
<Form form={form}>
3031
{React.createElement(
3132
formComponent,
@@ -43,7 +44,7 @@ export const SchemaForm: React.FC<ISchemaFormProps> = props => {
4344
children
4445
)}
4546
</Form>
46-
</SchemaContext.Provider>
47+
</FormSchemaContext.Provider>
4748
</FormExpressionScopeContext.Provider>
4849
</FormComponentsContext.Provider>
4950
)

packages/react-schema-renderer/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
export * from './components/SchemaField'
3333
export * from './components/SchemaForm'
3434
export * from './components/SchemaMarkup'
35+
export * from './shared/context'
3536
export * from './shared/connect'
3637
export * from './shared/registry'
3738
export * from './shared/schema'

packages/react-schema-renderer/src/shared/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export const FormComponentsContext = createContext<ISchemaFormRegistry>(null)
66

77
export const FormExpressionScopeContext = createContext<any>({})
88

9-
export default createContext<Schema>(null)
9+
export const FormSchemaContext = createContext<Schema>(null)

0 commit comments

Comments
 (0)