Skip to content

Commit

Permalink
feat(core): add selfModified to field model (#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Nov 18, 2021
1 parent e84a476 commit cd6fadf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 53 deletions.
93 changes: 47 additions & 46 deletions packages/core/docs/api/models/Field.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,53 @@ All model attributes are listed below. If the attribute is writable, then we can

## Attributes

| Property | Description | Type | Read-only or not | Default value |
| -------------- | -------------------------------------------- | -------------------------------------------------- | ---------------- | ------------- |
| initialized | Has the field been initialized | Boolean | No | `false` |
| mounted | Is the field mounted | Boolean | No | `false` |
| unmounted | Is the field unmounted | Boolean | No | `false` |
| address | Field node path | [FormPath](/api/entry/form-path) | Yes | |
| path | Field data path | [FormPath](/api/entry/form-path) | Yes | |
| title | Field Title | [FieldMessage](#fieldmessage) | No | `""` |
| description | Field description | [FieldMessage](#fieldmessage) | No | `""` |
| loading | Field loading status | Boolean | No | `false` |
| validating | Is the field being validated | Boolean | No | `false` |
| modified | Whether the field has been manually modified | Boolean | No | `false` |
| active | Is the field active | Boolean | No | `false` |
| visited | Whether the field has been visited | Boolean | No | `false` |
| inputValue | Field input value | Any | No | `null` |
| inputValues | Field input value collection | Array | No | `[]` |
| dataSource | Field data source | Array | No | `[]` |
| validator | Field validator | [FieldValidator](#fieldvalidator) | No | `null` |
| decorator | field decorator | Any[] | No | `null` |
| component | Field component | Any[] | No | `null` |
| feedbacks | Field feedback information | [IFieldFeedback](#ifieldfeedback)[] | No | `[]` |
| parent | Parent field | [GeneralField](#generalfield) | yes | `null` |
| errors | Field all error message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| warnings | Field all warning message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| successes | Field all success message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| valid | Is the all field valid(include children) | Boolean | Yes | `true` |
| invalid | Is the all field illegal(include children) | Boolean | Yes | `false` |
| value | Field value | Any | No | |
| initialValue | Field default value | Any | No | |
| display | Field display status | [FieldDisplayTypes](#fielddisplaytypes) | No | `"visible"` |
| pattern | Field interaction mode | [FieldPatternTypes](#fieldpatterntypes) | No | `"editable"` |
| required | Is the field required | Boolean | No | `false` |
| hidden | Whether the field is hidden | Boolean | No | `false` |
| visible | Whether the field is displayed | Boolean | No | `true` |
| disabled | Whether the field is disabled | Boolean | No | `false` |
| readOnly | Is the field read-only | Boolean | No | `false` |
| readPretty | Whether the field is in the reading state | Boolean | No | `false` |
| editable | Field is editable | Boolean | No | `true` |
| validateStatus | Field validation status | [FieldValidateStatus](#fieldvalidatestatus) | yes | `null` |
| content | Field content, usually as a child node | any | No | `null` |
| data | Field extends properties | Object | No | `null` |
| selfErrors | Field own error message | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfWarnings | Field own warning message | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfSuccesses | Success message of the field itself | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfValid | Is the field valid | Boolean | Yes | `true` |
| selfInvalid | Is the field itself illegal | Boolean | Yes | `false` |
| Property | Description | Type | Read-only or not | Default value |
| -------------- | ------------------------------------------------- | -------------------------------------------------- | ---------------- | ------------- |
| initialized | Has the field been initialized | Boolean | No | `false` |
| mounted | Is the field mounted | Boolean | No | `false` |
| unmounted | Is the field unmounted | Boolean | No | `false` |
| address | Field node path | [FormPath](/api/entry/form-path) | Yes | |
| path | Field data path | [FormPath](/api/entry/form-path) | Yes | |
| title | Field Title | [FieldMessage](#fieldmessage) | No | `""` |
| description | Field description | [FieldMessage](#fieldmessage) | No | `""` |
| loading | Field loading status | Boolean | No | `false` |
| validating | Is the field being validated | Boolean | No | `false` |
| modified | Whether the field tree has been manually modified | Boolean | No | `false` |
| selfModified | Whether the field has been manually modified | Boolean | No | `false` |
| active | Is the field active | Boolean | No | `false` |
| visited | Whether the field has been visited | Boolean | No | `false` |
| inputValue | Field input value | Any | No | `null` |
| inputValues | Field input value collection | Array | No | `[]` |
| dataSource | Field data source | Array | No | `[]` |
| validator | Field validator | [FieldValidator](#fieldvalidator) | No | `null` |
| decorator | field decorator | Any[] | No | `null` |
| component | Field component | Any[] | No | `null` |
| feedbacks | Field feedback information | [IFieldFeedback](#ifieldfeedback)[] | No | `[]` |
| parent | Parent field | [GeneralField](#generalfield) | yes | `null` |
| errors | Field all error message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| warnings | Field all warning message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| successes | Field all success message(include children) | [IFormFeedback](/api/models/form/#iformfeedback)[] | Yes | `[]` |
| valid | Is the all field valid(include children) | Boolean | Yes | `true` |
| invalid | Is the all field illegal(include children) | Boolean | Yes | `false` |
| value | Field value | Any | No | |
| initialValue | Field default value | Any | No | |
| display | Field display status | [FieldDisplayTypes](#fielddisplaytypes) | No | `"visible"` |
| pattern | Field interaction mode | [FieldPatternTypes](#fieldpatterntypes) | No | `"editable"` |
| required | Is the field required | Boolean | No | `false` |
| hidden | Whether the field is hidden | Boolean | No | `false` |
| visible | Whether the field is displayed | Boolean | No | `true` |
| disabled | Whether the field is disabled | Boolean | No | `false` |
| readOnly | Is the field read-only | Boolean | No | `false` |
| readPretty | Whether the field is in the reading state | Boolean | No | `false` |
| editable | Field is editable | Boolean | No | `true` |
| validateStatus | Field validation status | [FieldValidateStatus](#fieldvalidatestatus) | yes | `null` |
| content | Field content, usually as a child node | any | No | `null` |
| data | Field extends properties | Object | No | `null` |
| selfErrors | Field own error message | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfWarnings | Field own warning message | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfSuccesses | Success message of the field itself | [FieldMessage](#fieldmessage)[] | No | `[]` |
| selfValid | Is the field valid | Boolean | Yes | `true` |
| selfInvalid | Is the field itself illegal | Boolean | Yes | `false` |

#### explain in detail

Expand Down
3 changes: 2 additions & 1 deletion packages/core/docs/api/models/Field.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ order: 1
| description | 字段描述 | [FieldMessage](#fieldmessage) || `""` |
| loading | 字段加载状态 | Boolean || `false` |
| validating | 字段是否正在校验 | Boolean || `false` |
| modified | 字段是否被手动修改过 | Boolean || `false` |
| modified | 字段子树是否被手动修改过 | Boolean || `false` |
| selfModified | 字段自身是否被手动修改过 | Boolean || `false` |
| active | 字段是否处于激活态 | Boolean || `false` |
| visited | 字段是否被浏览过 | Boolean || `false` |
| inputValue | 字段输入值 | Any || `null` |
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,3 +1660,32 @@ test('field name is length in dynamic assign', () => {
field.initialValue = 123
expect(field.value).toEqual(123)
})

test('nested field modified', async () => {
const form = attach(createForm())
const obj = attach(
form.createObjectField({
name: 'object',
})
)
const child = attach(
form.createField({
name: 'child',
basePath: 'object',
})
)
await child.onInput()
expect(child.modified).toBeTruthy()
expect(child.selfModified).toBeTruthy()
expect(obj.modified).toBeTruthy()
expect(obj.selfModified).toBeFalsy()
expect(form.modified).toBeTruthy()
await obj.reset()
expect(child.modified).toBeFalsy()
expect(child.selfModified).toBeFalsy()
expect(obj.modified).toBeFalsy()
expect(obj.selfModified).toBeFalsy()
expect(form.modified).toBeTruthy()
await form.reset()
expect(form.modified).toBeFalsy()
})
14 changes: 9 additions & 5 deletions packages/core/src/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
setSubmitting,
setLoading,
validateSelf,
modifySelf,
getValidFieldDefaultValue,
initializeStart,
initializeEnd,
Expand All @@ -65,9 +66,10 @@ export class Field<
loading: boolean
validating: boolean
submitting: boolean
modified: boolean
active: boolean
visited: boolean
selfModified: boolean
modified: boolean
inputValue: ValueType
inputValues: any[]
dataSource: FieldDataSource
Expand Down Expand Up @@ -100,7 +102,7 @@ export class Field<
this.loading = false
this.validating = false
this.submitting = false
this.modified = false
this.selfModified = false
this.active = false
this.visited = false
this.mounted = false
Expand Down Expand Up @@ -143,6 +145,7 @@ export class Field<
loading: observable.ref,
validating: observable.ref,
submitting: observable.ref,
selfModified: observable.ref,
modified: observable.ref,
active: observable.ref,
visited: observable.ref,
Expand Down Expand Up @@ -220,7 +223,7 @@ export class Field<
() => this.value,
(value) => {
this.notify(LifeCycleTypes.ON_FIELD_VALUE_CHANGE)
if (isValid(value) && this.modified && !this.caches.inputting) {
if (isValid(value) && this.selfModified && !this.caches.inputting) {
validateSelf(this)
}
}
Expand Down Expand Up @@ -456,8 +459,7 @@ export class Field<
this.inputValue = value
this.inputValues = values
this.value = value
this.modified = true
this.form.modified = true
this.modify()
this.notify(LifeCycleTypes.ON_FIELD_INPUT_VALUE_CHANGE)
this.notify(LifeCycleTypes.ON_FORM_INPUT_CHANGE, this.form)
await validateSelf(this, 'onInput')
Expand Down Expand Up @@ -496,4 +498,6 @@ export class Field<
queryFeedbacks = (search?: ISearchFeedback): IFieldFeedback[] => {
return queryFeedbacks(this, search)
}

modify = () => modifySelf(this)
}
Loading

0 comments on commit cd6fadf

Please sign in to comment.