Skip to content

Commit

Permalink
fix(react): fix formSpy conflict with parent SchemaForm (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored May 18, 2020
1 parent 7be5b3e commit e122c9d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 85 deletions.
79 changes: 0 additions & 79 deletions docs/zh-cn/jsx-develop/faq.md
Original file line number Diff line number Diff line change
@@ -1,79 +0,0 @@
# FAQ

```jsx
import React, { useEffect, useState } from 'react'
import ReactDOM from 'react-dom'
import { SchemaForm } from '@formily/antd' // 或者 @formily/next
import { Input, Select, Radio } from '@formily/antd-components'
import Printer from '@formily/printer'
import 'antd/dist/antd.css'

const mockSchema = {
type: 'object',
properties: {
aa: {
type: 'boolean',
enum: [
{ label: '设置bb的枚举列表', value: true },
{ label: '还原bb的枚举列表', value: false }
],
default: false,
title: 'AA',
'x-component': 'RadioGroup',
'x-linkages': [
{
type: 'value:schema',
target: 'bb',
condition: '{{!!$value}}',
schema: {
enum: ['xx1', 'xx2', 'xx3']
},
otherwise: {
enum: ['zz']
}
}
]
},
bb: {
type: 'string',
title: 'BB',
'x-component': 'Input',
enum: ['yy']
},
cc: {
type: 'string',
title: '{{customCCTitle}}',
'x-component': 'Input'
}
}
}

const App = () => {
const [schema, setSchema] = useState({
type: 'object'
})
useEffect(() => {
setTimeout(() => {
setSchema(mockSchema)
}, 1000)
}, [])

return (
<Printer>
<SchemaForm
schema={schema}
components={{ Input, Select, RadioGroup: Radio.Group }}
onSubmit={values => {
console.log(values)
}}
expressionScope={{
customTitle: 'this is custom title',
customCCTitle: 'CC'
}}
/>
</Printer>
)
}

ReactDOM.render(<App />, document.getElementById('root'))
```
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useFormSpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useReducer
} from 'react'
import { FormHeartSubscriber, LifeCycleTypes, IForm } from '@formily/core'
import { isStr, FormPath, isArr,isFn } from '@formily/shared'
import { isStr, FormPath, isArr, isFn } from '@formily/shared'
import { IFormSpyProps, ISpyHook } from '../types'
import FormContext, { BroadcastContext } from '../context'

Expand Down Expand Up @@ -67,10 +67,10 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => {
}, [])
useMemo(() => {
initializedRef.current = true
if (form) {
subscriberId.current = form.subscribe(subscriber)
} else if (broadcast) {
if (broadcast) {
subscriberId.current = broadcast.subscribe(subscriber)
} else if (form) {
subscriberId.current = form.subscribe(subscriber)
}
initializedRef.current = false
}, [])
Expand All @@ -84,7 +84,7 @@ export const useFormSpy = (props: IFormSpyProps): ISpyHook => {
unmountRef.current = true
}
}, [])
const formApi: IForm = form ? form : broadcast && broadcast.getContext()
const formApi: IForm = broadcast ? broadcast && broadcast.getContext() : form
return {
form: formApi,
type,
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export class Broadcast extends Subscribable {
context: any

setContext(context: any) {
this.context = context
if (!this.context) {
this.context = context
}
}
getContext() {
return this.context
Expand Down

0 comments on commit e122c9d

Please sign in to comment.