Skip to content

Commit

Permalink
fix(core): add lifecycle buffer gc (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 2, 2020
1 parent cb7f095 commit 360c211
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/zh-cn/schema-develop/complex-linkage.md
Expand Up @@ -86,6 +86,60 @@ ReactDOM.render(<App />, document.getElementById('root'))
- 使用 FormEffectHooks 可以很方便的将联动逻辑拆分出去,方便我们进行物理分离
- 借助路径系统的批量匹配能力实现一对多联动

## 自己联动自己(副作用校验)

```jsx
import React, { useEffect } from 'react'
import ReactDOM from 'react-dom'
import {
SchemaForm,
SchemaMarkupField as Field,
FormButtonGroup,
createFormActions,
FormEffectHooks,
Submit,
Reset
} from '@formily/antd' // 或者 @formily/next
import { Input, Select } from '@formily/antd-components'
import Printer from '@formily/printer'
import { merge } from 'rxjs'
import 'antd/dist/antd.css'

const { onFieldValueChange$, onFormSubmitStart$ } = FormEffectHooks

const useValidator = (name, validator) => {
const { setFieldState } = createFormActions()
merge(onFieldValueChange$(name), onFormSubmitStart$()).subscribe(() => {
setFieldState(name, state => {
state.errors = validator(state.value, state)
})
})
}

const App = () => {
return (
<Printer>
<SchemaForm
components={{ Input, Select }}
onSubmit={values => {
console.log(values)
}}
effects={() => {
useValidator('aa', value => (!value ? '必填字段' : ''))
}}
>
<Field type="string" name="aa" title="AA" x-component="Input" />
<FormButtonGroup>
<Submit />
</FormButtonGroup>
</SchemaForm>
</Printer>
)
}

ReactDOM.render(<App />, document.getElementById('root'))
```

## 多对一联动

```jsx
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/shared/lifecycle.ts
Expand Up @@ -99,6 +99,7 @@ export class FormHeart<Payload = any, Context = any> extends Subscribable {
this.buffer.forEach(({ type, payload, context }) => {
this.publish(type, payload, context)
})
this.buffer = []
}
}

Expand Down

0 comments on commit 360c211

Please sign in to comment.