From ff5863ce6fcc3f5fe51af4b4bb1b9311fab4dada Mon Sep 17 00:00:00 2001 From: Matt Uttridge Date: Wed, 6 Apr 2016 16:06:38 -0400 Subject: [PATCH] fix clone bug --- src/helpers/__tests__/cloneChildren-test.js | 5 +++-- src/helpers/cloneChildren.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/helpers/__tests__/cloneChildren-test.js b/src/helpers/__tests__/cloneChildren-test.js index e8bb7a4..7c33ee1 100644 --- a/src/helpers/__tests__/cloneChildren-test.js +++ b/src/helpers/__tests__/cloneChildren-test.js @@ -116,16 +116,17 @@ describe('cloneChildren', () => { const fieldErrors = { color: ['form error'] } + const rule = require('../cloneChildren').createFormableRule([], fieldErrors); const children = [ + fieldErrors={['other error']} /> ]; const clonedInput = cloneChildren([rule], children); - expect(clonedInput.props.fieldErrors).toEqual(['form error', 'other error']); + expect(clonedInput.props.fieldErrors).toEqual(['other error', 'form error']); }); it('returns a single child (not an array of one) when cloning a single child', () => { diff --git a/src/helpers/cloneChildren.js b/src/helpers/cloneChildren.js index 408e7c0..123f5b6 100644 --- a/src/helpers/cloneChildren.js +++ b/src/helpers/cloneChildren.js @@ -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. */ @@ -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]) }; } }