Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing errors to HasManyFields component #377

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/HasManyFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class HasManyFields extends React.Component {
blank: PropTypes.any,
defaultValue: PropTypes.array,
disabled: PropTypes.bool,
errors: PropTypes.array,
label: PropTypes.string.isRequired,
onAdd: PropTypes.func,
onRemove: PropTypes.func,
Expand All @@ -23,6 +24,7 @@ class HasManyFields extends React.Component {

static defaultProps = {
defaultValue: [],
errors: [],
onAdd: noop,
onRemove: noop,
onUpdate: noop,
Expand Down Expand Up @@ -96,7 +98,7 @@ class HasManyFields extends React.Component {
};

render() {
const { template: Template, label, disabled } = this.props;
const { template: Template, label, disabled, errors } = this.props;

return (
<div>
Expand All @@ -108,6 +110,7 @@ class HasManyFields extends React.Component {
>
<Template
value={item}
errors={errors[i]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just double-checking that if we don't use errors, this will not pass errors to child components that don't support errors prop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I misunderstood, but if the child components don't support errors prop, then this will just be ignored. Or do you mean we should only pass errors along to the child components if errors prop was actually passed in to the HasManyFields component?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of, otherwise you'll get prop errors in console.
But in this case, if no errors are specified, the errors prop will be passed undefined, which I think is okay

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that was my thinking. If no errors prop is specified, the prop will be passed as undefined, which I believe React treats as not having the prop passed at all. If errors are specified, then the prop will get passed with an actual value, but I imagine people would only pass in errors if their template supports that prop

onChange={this.updateItem(i)}
ref={this.setRowReference(i)}
disabled={disabled}
Expand Down
15 changes: 15 additions & 0 deletions test/components/HasManyFields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../src';

const items = [ 'monkey', 'cat', 'mouse' ];
const errors = [{ name: "can't be blank" }, {}, { foo: "can't be bar" }];

describe('<HasManyFields />', () => {
describe('uncontrolled', () => {
Expand All @@ -22,6 +23,7 @@ describe('<HasManyFields />', () => {
const component = shallow(
<HasManyFields
defaultValue={items}
errors={errors}
template={Input}
blank={values => values.length.toString()}
onAdd={onAdd}
Expand Down Expand Up @@ -53,6 +55,12 @@ describe('<HasManyFields />', () => {
});
});

it('should pass the errors to each template', () => {
component.find(Input).forEach((input, i) => {
assert.deepEqual(input.prop('errors'), errors[i]);
});
});

it('should add a row when add button is clicked', () => {
const expectedItems = [ 'monkey', 'cat', 'mouse', '3' ];
addItem.simulate('click');
Expand Down Expand Up @@ -94,6 +102,7 @@ describe('<HasManyFields />', () => {
const component = shallow(
<HasManyFields
value={items}
errors={errors}
template={Input}
blank={'foo'}
onAdd={onAdd}
Expand Down Expand Up @@ -121,6 +130,12 @@ describe('<HasManyFields />', () => {
});
});

it('should pass the errors to each template', () => {
component.find(Input).forEach((input, i) => {
assert.deepEqual(input.prop('errors'), errors[i]);
});
});

it('should add a row when add button is clicked', () => {
const expectedItems = [ 'monkey', 'cat', 'mouse', 'foo' ];
addItem.simulate('click');
Expand Down