Skip to content
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
12 changes: 5 additions & 7 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ export default function(WrappedComponent) {

componentDidMount() {
this.props.form.registerField(this);

this.validators = this.props.validators || [];
if (this.component && this.component.validators) {
this.validators = this.component.validators.concat(this.validators);
}
}

componentWillUnmount() {
Expand All @@ -38,7 +33,7 @@ export default function(WrappedComponent) {
}

validate() {
for (const validator of this.validators) {
for (const validator of this.props.validators) {
const result = validator(this.state.value);
if (result !== undefined) {
this.setState({valid: false, message: result});
Expand Down Expand Up @@ -116,7 +111,6 @@ export default function(WrappedComponent) {
valid={this.state.valid}
validate={this.validate}
value={this.state.value}
ref={component => { this.component = component;}}
/>
);
}
Expand All @@ -126,6 +120,10 @@ export default function(WrappedComponent) {
name: PropTypes.string.isRequired
};

Field.defaultProps = {
validators: []
};

return Field;
}

17 changes: 1 addition & 16 deletions test/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ const InputField = Field(
}
);

const RequiredInputField = Field(
class extends React.Component {
constructor(props) {
super(props);
this.validators = [required()];
}

render() {
return (
<input {...this.props.element}/>
);
}
}
);

const SelectField = Field(
class extends React.Component {
options() {
Expand All @@ -49,4 +34,4 @@ const SelectField = Field(
}
);

export {InputField, RequiredInputField, SelectField};
export {InputField, SelectField};
69 changes: 1 addition & 68 deletions test/unit/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Enzyme from 'enzyme';
import {mount} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Form from '../../src/form';
import {InputField, SelectField, RequiredInputField} from '../fields';
import {InputField, SelectField } from '../fields';
import {required, minLength} from '../validators';

Enzyme.configure({ adapter: new Adapter() });
Expand Down Expand Up @@ -112,73 +112,6 @@ describe('Field', function() {
});
});

describe('with a validator defined in the wrapped component', () => {
beforeEach(() => {
this.wrapper = mount(
<Form>
<RequiredInputField name="banana" type="text"/>
</Form>
);
this.field = this.wrapper.instance().getField('banana');
});

it('should be invalid without a value', () => {
expect(this.field.validate()).to.be.false;
});

it('should be valid with a value', () => {
const event = {
type: 'change',
target: {value: 'peel'}
};
return this.field.handleChange(event)
.then(() => {
expect(this.field.validate()).to.be.true;
});
});

describe('and on the component tag', () => {
beforeEach(() => {
this.wrapper = mount(
<Form>
<RequiredInputField name="banana" type="text" validators={[minLength(5)]}/>
</Form>
);
this.field = this.wrapper.instance().getField('banana');
});

it('should have two validators', () => {
expect(this.field.validators).to.have.length(2);
});

it('should be invalid without a value', () => {
expect(this.field.validate()).to.be.false;
});

it('should be invalid with a value under 5 characters', () => {
const event = {
type: 'change',
target: {value: 'peel'}
};
return this.field.handleChange(event)
.then(() => {
expect(this.field.validate()).to.be.false;
});
});

it('should be valid with a value with 5 characters', () => {
const event = {
type: 'change',
target: {value: 'puree'}
};
return this.field.handleChange(event)
.then(() => {
expect(this.field.validate()).to.be.true;
});
});
});
});

describe('with initial values', () => {
beforeEach(() => {
const initialValues = {
Expand Down