Skip to content

Commit

Permalink
feat(core): add setData & setContent of field models (#2478)
Browse files Browse the repository at this point in the history
  • Loading branch information
DivXPro authored Nov 22, 2021
1 parent 024a74f commit f6d3103
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 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 @@ -422,6 +422,34 @@ interface getState<T> {

IFieldState Reference [IFieldState](#ifieldstate)

### setData

#### Description

set field data

#### Signature

```ts
interface setData {
(data: any): void
}
```

### setContent

#### Description

set field content

#### Signature

```ts
interface setContent {
(content: any): void
}
```

### onInit

#### Description
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 @@ -422,6 +422,34 @@ interface getState<T> {

IFieldState 参考 [IFieldState](#ifieldstate)

### setData

#### 描述

设置 Data 值

#### 签名

```ts
interface setData {
(data: any): void
}
```

### setContent

#### 描述

设置 Content 值

#### 签名

```ts
interface setContent {
(content: any): void
}
```

### onInit

#### 描述
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 @@ -201,6 +201,34 @@ interface getState<T> {

IVoidFieldState Reference [IVoidFieldState](#ifieldstate)

### setData

#### Description

set field data

#### Signature

```ts
interface setData {
(data: any): void
}
```

### setContent

#### Description

set field content

#### Signature

```ts
interface setContent {
(content: any): void
}
```

### onInit

#### Description
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 @@ -201,6 +201,34 @@ interface getState<T> {

IVoidFieldState 参考 [IVoidFieldState](#ifieldstate)

### setData

#### 描述

设置 Data 值

#### 签名

```ts
interface setData {
(data: any): void
}
```

### setContent

#### 描述

设置 Content 值

#### 签名

```ts
interface setContent {
(content: any): void
}
```

### onInit

#### 描述
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,33 @@ test('required/setRequired', () => {
expect(dd.required).toBeTruthy()
})

test('setData/setContent', () => {
const form = attach(createForm())
const aa = attach(
form.createField({
name: 'aa',
required: true,
})
)
aa.setData('This is data')
aa.setContent('This is Content')
expect(aa.data).toEqual('This is data')
expect(aa.content).toEqual('This is Content')
})

test('setData/setContent in void field', () => {
const form = attach(createForm())
const voidFeild = attach(
form.createVoidField({
name: 'voidFeild',
})
)
voidFeild.setData('This is data')
voidFeild.setContent('This is Content')
expect(voidFeild.data).toEqual('This is data')
expect(voidFeild.content).toEqual('This is Content')
})

test('setErrors/setWarnings/setSuccesses/setValidator', async () => {
const form = attach(createForm())
const aa = attach(
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/models/BaseField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ export class BaseField<Decorator = any, Component = any, TextType = any> {
}
}

setData = (data: any) => {
this.data = data
}

setContent = (content: any) => {
this.content = content
}

onInit = () => {
this.initialized = true
initFieldUpdate(this as any)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ export class Field<
setComponentProps: action,
setDecorator: action,
setDecoratorProps: action,
setData: action,
setContent: action,
validate: action,
reset: action,
onInit: batch,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/models/VoidField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class VoidField<
setComponentProps: action,
setDecorator: action,
setDecoratorProps: action,
setData: action,
setContent: action,
onInit: batch,
onMount: batch,
onUnmount: batch,
Expand Down

0 comments on commit f6d3103

Please sign in to comment.