Skip to content

Commit

Permalink
fix: log different error message, close: react-component#124
Browse files Browse the repository at this point in the history
  • Loading branch information
benjycui committed Dec 8, 2017
1 parent 3594c92 commit 98792e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-form",
"version": "2.1.4",
"version": "2.1.5",
"description": "React High Order Form Component",
"keywords": [
"react",
Expand Down
12 changes: 10 additions & 2 deletions src/createFieldsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ class FieldsStore {
}

flattenFields(fields) {
return flattenFields(fields, (_, node) => isFormField(node));
return flattenFields(
fields,
(_, node) => isFormField(node),
'You must wrap field data with `createFormField`.'
);
}

flattenRegisteredFields(fields) {
const validFieldsName = this.getValidFieldsName();
return flattenFields(fields, path => validFieldsName.indexOf(path) >= 0);
return flattenFields(
fields,
path => validFieldsName.indexOf(path) >= 0,
'You cannot set field before registering it.'
);
}

setFieldsInitialValue = (initialValues) => {
Expand Down
10 changes: 6 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function flattenArray(arr) {
return Array.prototype.concat.apply([], arr);
}

export function treeTraverse(path = '', tree, isLeafNode, callback) {
export function treeTraverse(path = '', tree, isLeafNode, errorMessage, callback) {
if (isLeafNode(path, tree)) {
callback(path, tree);
} else if (tree === undefined) {
Expand All @@ -29,11 +29,12 @@ export function treeTraverse(path = '', tree, isLeafNode, callback) {
`${path}[${index}]`,
subTree,
isLeafNode,
errorMessage,
callback
));
} else { // It's object and not a leaf node
if (typeof tree !== 'object') {
console.error('You must wrap field data with `createFormField`.');
console.error(errorMessage);
return;
}
Object.keys(tree).forEach(subTreeKey => {
Expand All @@ -42,15 +43,16 @@ export function treeTraverse(path = '', tree, isLeafNode, callback) {
`${path}${path ? '.' : ''}${subTreeKey}`,
subTree,
isLeafNode,
errorMessage,
callback
);
});
}
}

export function flattenFields(maybeNestedFields, isLeafNode) {
export function flattenFields(maybeNestedFields, isLeafNode, errorMessage) {
const fields = {};
treeTraverse(undefined, maybeNestedFields, isLeafNode, (path, node) => {
treeTraverse(undefined, maybeNestedFields, isLeafNode, errorMessage, (path, node) => {
fields[path] = node;
});
return fields;
Expand Down

0 comments on commit 98792e2

Please sign in to comment.