Skip to content

Commit

Permalink
feat(core): support field inject/invoke actions api (#3389)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Sep 12, 2022
1 parent e4ba3ea commit 0759376
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 2 deletions.
28 changes: 28 additions & 0 deletions packages/core/docs/api/models/Field.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,34 @@ interface match {

FormPathPattern API Reference [FormPath](/api/entry/form-path#formpathpattern)

### inject

#### Description

Inject executable methods into field models

#### Signature

```ts
interface inject {
(actions: Record<string, (...args: any[]) => any>): void
}
```

### invoke

#### Description

Invoke an executable method injected by the field model via inject

#### Signature

```ts
interface invoke {
(name: string, ...args: any[]): any
}
```

## Types of

<Alert>
Expand Down
28 changes: 28 additions & 0 deletions packages/core/docs/api/models/Field.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,34 @@ interface match {

FormPathPattern API 参考 [FormPath](/api/entry/form-path#formpathpattern)

### inject

#### 描述

给字段模型注入可执行方法

#### 签名

```ts
interface inject {
(actions: Record<string, (...args: any[]) => any>): void
}
```

### invoke

#### 描述

调用字段模型通过 inject 注入的可执行方法

#### 签名

```ts
interface invoke {
(name: string, ...args: any[]): any
}
```

## 类型

<Alert>
Expand Down
28 changes: 28 additions & 0 deletions packages/core/docs/api/models/VoidField.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,34 @@ interface match {

FormPathPattern API Reference [FormPath](/api/entry/form-path#formpathpattern)

### inject

#### Description

Inject executable methods into field models

#### Signature

```ts
interface inject {
(actions: Record<string, (...args: any[]) => any>): void
}
```

### invoke

#### Description

Invoke an executable method injected by the field model via inject

#### Signature

```ts
interface invoke {
(name: string, ...args: any[]): any
}
```

## Types of

<Alert>
Expand Down
28 changes: 28 additions & 0 deletions packages/core/docs/api/models/VoidField.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,34 @@ interface match {

FormPathPattern API 参考 [FormPath](/api/entry/form-path#formpathpattern)

### inject

#### 描述

给字段模型注入可执行方法

#### 签名

```ts
interface inject {
(actions: Record<string, (...args: any[]) => any>): void
}
```

### invoke

#### 描述

调用字段模型通过 inject 注入的可执行方法

#### 签名

```ts
interface invoke {
(name: string, ...args: any[]): any
}
```

## 类型

<Alert>
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2277,3 +2277,21 @@ test('onFieldReact with field destroyed', () => {
obs.value = '111'
expect(fn).toBeCalledTimes(2)
})

test('field actions', () => {
const form = attach(createForm())
const aa = attach(
form.createField({
name: 'aa',
})
)
expect(aa.actions).toEqual({})
aa.inject({
test: () => 123,
})
expect(aa.invoke('test')).toEqual(123)
aa.inject({
test: () => 321,
})
expect(aa.invoke('test')).toEqual(321)
})
24 changes: 23 additions & 1 deletion packages/core/src/models/BaseField.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { FormPath, FormPathPattern, isValid, toArr } from '@formily/shared'
import {
FormPath,
FormPathPattern,
isValid,
toArr,
each,
isFn,
} from '@formily/shared'
import {
JSXComponent,
LifeCycleTypes,
FieldDisplayTypes,
FieldPatternTypes,
FieldDecorator,
FieldComponent,
IFieldActions,
} from '../types'
import { locateNode, destroy, initFieldUpdate } from '../shared/internals'
import { Form } from './Form'
Expand Down Expand Up @@ -37,6 +45,8 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {

disposers: (() => void)[] = []

actions: IFieldActions = {}

locate(address: FormPathPattern) {
this.form.fields[address.toString()] = this as any
locateNode(this as any, address)
Expand Down Expand Up @@ -309,4 +319,16 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {
match = (pattern: FormPathPattern) => {
return FormPath.parse(pattern).matchAliasGroup(this.address, this.path)
}

inject = (actions: IFieldActions) => {
each(actions, (action, key) => {
if (isFn(action)) {
this.actions[key] = action
}
})
}

invoke = (name: string, ...args: any[]) => {
return this.actions[name]?.(...args)
}
}
1 change: 0 additions & 1 deletion packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class Field<
feedbacks: IFieldFeedback[]
caches: IFieldCaches = {}
requests: IFieldRequests = {}

constructor(
address: FormPathPattern,
props: IFieldProps<Decorator, Component, TextType, ValueType>,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,7 @@ export interface IFieldStateGetter {
): ReturnType<Getter>
(pattern: FieldMatchPattern): IGeneralFieldState
}

export interface IFieldActions {
[key: string]: (...args: any[]) => any
}

0 comments on commit 0759376

Please sign in to comment.