Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add APIs to know whether a form is ready to submit #56

Merged
merged 2 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ And add `force` and `scroll`. `scroll` is the same as [dom-scroll-into-view's fu

Defaults to false. Whether to validate fields which have been validated(caused by validateTrigger).

### getFieldsError(names): Object{ [name]: String[] }

Get inputs' validate errors.

### getFieldError(name): String[]

Get input's validate errors.
Expand All @@ -297,6 +301,14 @@ Whether this input is validating.

Whether one of the inputs is validating.

### isFieldTouched(name: String): Bool

Whether this input's value had been change.

### isFieldsTouched(names: String[]): Bool

Whether one of the inputs' values had been change.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether any of the inputs


### isSubmitting(): Bool

Whether the form is submitting.
Expand Down
28 changes: 23 additions & 5 deletions src/createBaseForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ function createBaseForm(option = {}, mixins = []) {
name = nameKeyObj.name;
}
const field = this.getField(name);
callback({ name, field, fieldMeta, value });
callback({ name, field: { ...field, value, touched: true }, fieldMeta });
},

onCollect(name_, action, ...args) {
this.onCollectCommon(name_, action, args, ({ name, field, fieldMeta, value }) => {
this.onCollectCommon(name_, action, args, ({ name, field, fieldMeta }) => {
const { validate } = fieldMeta;
const fieldContent = {
...field,
value,
dirty: hasRules(validate),
};
this.setFields({
Expand All @@ -88,10 +87,9 @@ function createBaseForm(option = {}, mixins = []) {
},

onCollectValidate(name_, action, ...args) {
this.onCollectCommon(name_, action, args, ({ field, fieldMeta, value }) => {
this.onCollectCommon(name_, action, args, ({ field, fieldMeta }) => {
const fieldContent = {
...field,
value,
dirty: true,
};
this.validateFieldsInternal([fieldContent], {
Expand Down Expand Up @@ -254,6 +252,15 @@ function createBaseForm(option = {}, mixins = []) {
return field && field[member];
},

getFieldsError(names) {
const fields = names || flatFieldNames(this.getValidFieldsName());
const allErrors = {};
fields.forEach((f) => {
set(allErrors, f, this.getFieldError(f));
});
return allErrors;
},

getFieldError(name) {
return getErrorStrs(this.getFieldMember(name, 'errors'));
},
Expand Down Expand Up @@ -564,6 +571,17 @@ function createBaseForm(option = {}, mixins = []) {
});
},

isFieldTouched(name) {
return this.getFieldMember(name, 'touched');
},

isFieldsTouched(ns) {
const names = ns || this.getValidFieldsName();
return names.some((n) => {
return this.isFieldTouched(n);
});
},

isSubmitting() {
return this.state.submitting;
},
Expand Down
3 changes: 3 additions & 0 deletions src/createForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export const mixin = {
setFieldsInitialValue: this.setFieldsInitialValue,
getFieldDecorator: this.getFieldDecorator,
getFieldProps: this.getFieldProps,
getFieldsError: this.getFieldsError,
getFieldError: this.getFieldError,
isFieldValidating: this.isFieldValidating,
isFieldsValidating: this.isFieldsValidating,
isFieldsTouched: this.isFieldsTouched,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 API 名字有点晦涩,但是 dirty 已经被使用了 cc @afc163 @yiminghe

isFieldTouched: this.isFieldTouched,
isSubmitting: this.isSubmitting,
submit: this.submit,
validateFields: this.validateFields,
Expand Down