Skip to content

Commit

Permalink
allow for infinite field nesting on return object
Browse files Browse the repository at this point in the history
  • Loading branch information
alexggordon committed Dec 14, 2016
1 parent 616ddd3 commit 90109f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/createBaseForm.js
Expand Up @@ -6,6 +6,7 @@ import {
isEmptyObject, flattenArray,
getNameKeyObj, getNameKeyStr,
flatFieldNames, clearVirtualField,
setObject,
} from './utils';
import AsyncValidator from 'async-validator';
import warning from 'warning';
Expand Down Expand Up @@ -333,8 +334,7 @@ function createBaseForm(option = {}, mixins = []) {
if (fieldsMeta.hasOwnProperty(fieldKey)) {
const nameKeyObj = getNameKeyObj(fieldKey);
if (nameKeyObj.name === name && nameKeyObj.key) {
ret[nameKeyObj.key] =
this.getValueFromFieldsInternal(fieldKey, fields);
setObject(nameKeyObj.key, this.getValueFromFieldsInternal(fieldKey, fields), ret)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Expand Up @@ -110,6 +110,16 @@ export function getNameKeyObj(str) {
};
}

export function setObject(str, value, context) {
var parts = str.split(NAME_KEY_SEP),
p = parts.pop();
// if object && string has another part && we've looped less than 100 times
for(var i=0, j; context && (j=parts[i]) && i<100; i++){
context = (j in context ? context[j] : context[j]={});
}
return context && p ? (context[p]=value) : undefined; // Object
}

export function flatFields(fields_, fieldsMeta) {
const fields = { ...fields_ };
Object.keys(fields).forEach((k) => {
Expand Down

0 comments on commit 90109f8

Please sign in to comment.