From 792ff51387d02ba4ca9c923bfa4accc537dadec7 Mon Sep 17 00:00:00 2001 From: Aaron Panchal Date: Fri, 10 Feb 2017 11:00:54 -0800 Subject: [PATCH] ap - support feedback/error objects in FormRow --- src/components/FormRow.js | 18 +++++++++++++++--- test/components/FormRow.spec.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/components/FormRow.js b/src/components/FormRow.js index db682522f..436e0157f 100644 --- a/src/components/FormRow.js +++ b/src/components/FormRow.js @@ -17,6 +17,12 @@ function determineElement(type) { type; } +function parseFeedback(feedback) { + return typeof feedback === 'object' ? + [ null, { error: feedback } ] : + [ feedback, {} ]; +} + const FormRow = props => { const { id, @@ -34,7 +40,9 @@ const FormRow = props => { } = props; const InputElement = determineElement(type); - const rowColor = color || state || (feedback && 'danger'); + + const [ baseFeedback, childFeedback ]= parseFeedback(feedback); + const rowColor = color || state || (baseFeedback && 'danger'); return ( @@ -50,9 +58,10 @@ const FormRow = props => { type={type} children={React.Children.map(children, child => React.cloneElement(child, { type }))} {...attributes} + {...childFeedback} /> {hint ? : null} - {feedback ? : null} + {baseFeedback ? : null} ); @@ -61,7 +70,10 @@ const FormRow = props => { FormRow.propTypes = { label: React.PropTypes.string, hint: React.PropTypes.string, - feedback: React.PropTypes.string, + feedback: React.PropTypes.oneOfType([ + React.PropTypes.string, + React.PropTypes.object + ]), required: React.PropTypes.bool, type: React.PropTypes.oneOfType([ React.PropTypes.string, diff --git a/test/components/FormRow.spec.js b/test/components/FormRow.spec.js index 90f1e6e07..5b2a5b6c1 100644 --- a/test/components/FormRow.spec.js +++ b/test/components/FormRow.spec.js @@ -85,6 +85,26 @@ describe('', () => { }); }); + describe('with feedback object for children', () => { + const feedback = { childField: 'not good' }; + const component = shallow( + + ); + + it('should not be shown', () => { + assert.equal(component.find(FormFeedback).length, 0); + }); + + it('should not add danger color', () => { + assert.equal(component.prop('color'), null); + assert.equal(component.find(Input).prop('state'), null); + }); + + it('should forward feedback to input', () => { + assert.equal(component.find(Input).prop('error'), feedback); + }); + }); + describe('with hint', () => { const component = shallow(