Skip to content

Commit

Permalink
ap - support feedback/error objects in FormRow
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Panchal committed Feb 10, 2017
1 parent 725d4e1 commit 792ff51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/components/FormRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ function determineElement(type) {
type;
}

function parseFeedback(feedback) {
return typeof feedback === 'object' ?
[ null, { error: feedback } ] :
[ feedback, {} ];
}

const FormRow = props => {
const {
id,
Expand All @@ -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 (
<FormGroup row color={rowColor}>
Expand All @@ -50,9 +58,10 @@ const FormRow = props => {
type={type}
children={React.Children.map(children, child => React.cloneElement(child, { type }))}
{...attributes}
{...childFeedback}
/>
{hint ? <FormText color="muted" children={hint} /> : null}
{feedback ? <FormFeedback children={feedback} /> : null}
{baseFeedback ? <FormFeedback children={baseFeedback} /> : null}
</Col>
</FormGroup>
);
Expand All @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions test/components/FormRow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ describe('<FormRow />', () => {
});
});

describe('with feedback object for children', () => {
const feedback = { childField: 'not good' };
const component = shallow(
<FormRow label="First Name" feedback={feedback} />
);

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(
<FormRow label="First Name" hint="hint" />
Expand Down

0 comments on commit 792ff51

Please sign in to comment.