From 154df875b5f2cd65b04a26584932d6297bc5b89d Mon Sep 17 00:00:00 2001 From: Eric Valadas Date: Sun, 25 Feb 2018 01:29:24 -0500 Subject: [PATCH 1/2] Remove ability to set validators on the component --- src/field.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/field.js b/src/field.js index 85c42a8..264a24b 100644 --- a/src/field.js +++ b/src/field.js @@ -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() { @@ -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}); @@ -116,7 +111,6 @@ export default function(WrappedComponent) { valid={this.state.valid} validate={this.validate} value={this.state.value} - ref={component => { this.component = component;}} /> ); } @@ -126,6 +120,10 @@ export default function(WrappedComponent) { name: PropTypes.string.isRequired }; + Field.defaultProps = { + validators: [] + }; + return Field; } From 878e1910b340df67fde725a5db7d65fd57e57cda Mon Sep 17 00:00:00 2001 From: Eric Valadas Date: Sun, 25 Feb 2018 01:37:18 -0500 Subject: [PATCH 2/2] Remove tests for component level validators --- test/fields.js | 17 +----------- test/unit/field.js | 69 +--------------------------------------------- 2 files changed, 2 insertions(+), 84 deletions(-) diff --git a/test/fields.js b/test/fields.js index 6dc55c8..f6a780b 100644 --- a/test/fields.js +++ b/test/fields.js @@ -13,21 +13,6 @@ const InputField = Field( } ); -const RequiredInputField = Field( - class extends React.Component { - constructor(props) { - super(props); - this.validators = [required()]; - } - - render() { - return ( - - ); - } - } -); - const SelectField = Field( class extends React.Component { options() { @@ -49,4 +34,4 @@ const SelectField = Field( } ); -export {InputField, RequiredInputField, SelectField}; +export {InputField, SelectField}; diff --git a/test/unit/field.js b/test/unit/field.js index 234727f..844b0c4 100644 --- a/test/unit/field.js +++ b/test/unit/field.js @@ -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() }); @@ -112,73 +112,6 @@ describe('Field', function() { }); }); - describe('with a validator defined in the wrapped component', () => { - beforeEach(() => { - this.wrapper = mount( -
- - - ); - 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( -
- - - ); - 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 = {