Skip to content

Commit

Permalink
Merge pull request #70 from uttrasey/combine-field-errors-during-clone
Browse files Browse the repository at this point in the history
Fix clone bug
  • Loading branch information
kaw2k committed Apr 14, 2016
2 parents f929ccf + 6394ef4 commit c50eda9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/helpers/__tests__/cloneChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ describe('cloneChildren', () => {
expect(warning).toBeCalledWith(false, 'Duplicate name "color" found. Duplicate fields will be ignored');
});

it('combines the forms field errors with user specified field errors', () => {
const fieldErrors = {
color: ['form error']
}

const rule = require('../cloneChildren').createFormableRule([], fieldErrors);

const children = [
<Input name="color" type="text"
fieldErrors={['other error']} />
];

const clonedInput = cloneChildren([rule], children);

expect(clonedInput.props.fieldErrors).toEqual(['other error', 'form error']);
});

it('returns a single child (not an array of one) when cloning a single child', () => {
const createFormableRule = require('../cloneChildren').createFormableRule;
const rule = createFormableRule({})
Expand Down
13 changes: 12 additions & 1 deletion src/helpers/cloneChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ export function createErrorsRule(errors = [], fieldErrors = {}) {
}
}

function combineListsIfLists(...lists) {
const combined = [];

lists.forEach(list => {
if (list && list.length) {
combined.push(...list);
}
});
return combined;
}

/*
* Get extra properties for something we are going to weave our formable magic into.
*/
Expand All @@ -66,7 +77,7 @@ function getFormableComponentProperties(errors, fieldErrors, onSubmit, onChange)
onChange: compose(onChange, child.props.onChange || identity),
onSubmit: compose(onSubmit, child.props.onSubmit || identity),
errors: errors,
fieldErrors: child.props.fieldErrors || fieldErrors[child.props.name]
fieldErrors: combineListsIfLists(child.props.fieldErrors, fieldErrors[child.props.name])
};
}
}
Expand Down

0 comments on commit c50eda9

Please sign in to comment.