Skip to content

Commit

Permalink
move instance for serialization. Fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed May 19, 2016
1 parent 80b3e25 commit 413dc0a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 46 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# History
----

## 0.16.0 / 2016-05-19

- move instance to this.instances

## 0.15.0 / 2016-03-28

- add getValueFromEvent/getValueProps
Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-form",
"version": "0.15.4",
"version": "0.16.0",
"description": "React High Order Form Component",
"keywords": [
"react",
Expand All @@ -24,10 +24,7 @@
"license": "MIT",
"main": "./lib/index",
"config": {
"port": 8000,
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
"port": 8000
},
"scripts": {
"build": "rc-tools run build",
Expand All @@ -44,7 +41,6 @@
"devDependencies": {
"antd": "^0.12.1",
"async": "^1.5.2",
"cz-conventional-changelog": "^1.1.5",
"expect.js": "0.3.x",
"history": "^1.16.0",
"pre-commit": "1.x",
Expand Down
38 changes: 6 additions & 32 deletions src/createBaseForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function createBaseForm(option = {}, mixins = []) {
fields = mapPropsToFields(this.props);
}
this.fields = fields || {};
this.instances = {};
this.fieldsMeta = {};
this.cachedBind = {};
return {
Expand All @@ -37,21 +38,7 @@ function createBaseForm(option = {}, mixins = []) {

componentWillReceiveProps(nextProps) {
if (mapPropsToFields) {
const fields = mapPropsToFields(nextProps);
if (fields) {
const instanceFields = this.fields = {
...this.fields,
};
for (const fieldName in fields) {
if (fields.hasOwnProperty(fieldName)) {
instanceFields[fieldName] = {
...fields[fieldName],
// keep instance
instance: instanceFields[fieldName] && instanceFields[fieldName].instance,
};
}
}
}
this.fields = mapPropsToFields(nextProps);
}
},

Expand Down Expand Up @@ -246,8 +233,7 @@ function createBaseForm(option = {}, mixins = []) {
},

getFieldInstance(name) {
const { fields } = this;
return fields[name] && fields[name].instance;
return this.instances[name];
},

getValueFromFields(name, fields) {
Expand All @@ -268,12 +254,6 @@ function createBaseForm(option = {}, mixins = []) {
},
setFields(fields) {
const originalFields = this.fields;
// reserve `instance`
Object.keys(fields).forEach((key) => {
if (!originalFields[key]) return;
fields[key].instance = originalFields[key].instance;
});

const nowFields = {
...originalFields,
...fields,
Expand Down Expand Up @@ -343,6 +323,7 @@ function createBaseForm(option = {}, mixins = []) {
// after destroy, delete data
delete this.fieldsMeta[name];
delete this.fields[name];
delete this.instances[name];
return;
}
const fieldMeta = this.getFieldMeta(name);
Expand All @@ -352,8 +333,7 @@ function createBaseForm(option = {}, mixins = []) {
}
fieldMeta.ref(component);
}
this.fields[name] = this.fields[name] || {};
this.fields[name].instance = component;
this.instances[name] = component;
},

validateFieldsInternal(fields, {
Expand All @@ -371,7 +351,6 @@ function createBaseForm(option = {}, mixins = []) {
if (field.errors) {
alreadyErrors[name] = {
errors: field.errors,
instance: field.instance,
};
}
return;
Expand Down Expand Up @@ -427,7 +406,6 @@ function createBaseForm(option = {}, mixins = []) {
if (nowField.value !== allValues[name]) {
expired.push({
name,
instance: nowField.instance,
});
} else {
nowField.errors = fieldErrors && fieldErrors.errors;
Expand All @@ -436,21 +414,17 @@ function createBaseForm(option = {}, mixins = []) {
nowField.dirty = false;
nowAllFields[name] = nowField;
}
if (fieldErrors) {
fieldErrors.instance = nowField.instance;
}
});
this.setFields(nowAllFields);
if (callback) {
if (expired.length) {
expired.forEach(({ name, instance }) => {
expired.forEach(({ name }) => {
const fieldErrors = [{
message: `${name} need to revalidate`,
field: name,
}];
errorsGroup[name] = {
expired: true,
instance,
errors: fieldErrors,
};
});
Expand Down
19 changes: 11 additions & 8 deletions src/createDOMForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ const mixin = {
validateFieldsAndScroll(ns, opt, cb) {
const { names, callback, options } = getParams(ns, opt, cb);

function newCb(error, values) {
const newCb = (error, values) => {
if (error) {
let firstNode;
let firstTop;
for (const name in error) {
if (error.hasOwnProperty(name) && error[name].instance) {
const node = ReactDOM.findDOMNode(error[name].instance);
const top = node.getBoundingClientRect().top;
if (firstTop === undefined || firstTop > top) {
firstTop = top;
firstNode = node;
if (error.hasOwnProperty(name)) {
const instance = this.getFieldInstance(name);
if (instance) {
const node = ReactDOM.findDOMNode(instance);
const top = node.getBoundingClientRect().top;
if (firstTop === undefined || firstTop > top) {
firstTop = top;
firstNode = node;
}
}
}
}
Expand All @@ -81,7 +84,7 @@ const mixin = {
if (typeof callback === 'function') {
callback(error, values);
}
}
};

return this.validateFields(names, options, newCb);
},
Expand Down

0 comments on commit 413dc0a

Please sign in to comment.