diff --git a/HISTORY.md b/HISTORY.md index e261876c..ea1bd4b0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/package.json b/package.json index 9ac4e9a9..5cb07095 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-form", - "version": "0.15.4", + "version": "0.16.0", "description": "React High Order Form Component", "keywords": [ "react", @@ -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", @@ -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", diff --git a/src/createBaseForm.jsx b/src/createBaseForm.jsx index ebc3a6bb..f9e768bb 100644 --- a/src/createBaseForm.jsx +++ b/src/createBaseForm.jsx @@ -28,6 +28,7 @@ function createBaseForm(option = {}, mixins = []) { fields = mapPropsToFields(this.props); } this.fields = fields || {}; + this.instances = {}; this.fieldsMeta = {}; this.cachedBind = {}; return { @@ -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); } }, @@ -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) { @@ -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, @@ -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); @@ -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, { @@ -371,7 +351,6 @@ function createBaseForm(option = {}, mixins = []) { if (field.errors) { alreadyErrors[name] = { errors: field.errors, - instance: field.instance, }; } return; @@ -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; @@ -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, }; }); diff --git a/src/createDOMForm.jsx b/src/createDOMForm.jsx index c2f0075f..08b03b50 100644 --- a/src/createDOMForm.jsx +++ b/src/createDOMForm.jsx @@ -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; + } } } } @@ -81,7 +84,7 @@ const mixin = { if (typeof callback === 'function') { callback(error, values); } - } + }; return this.validateFields(names, options, newCb); },