Skip to content

Commit

Permalink
DRYing ButtonInput
Browse files Browse the repository at this point in the history
  • Loading branch information
aabenoja committed May 26, 2015
1 parent 2749cfd commit fd0972e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
16 changes: 6 additions & 10 deletions src/ButtonInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import React from 'react';
import Button from './Button';
import FormGroup from './FormGroup';
import InputBase from './InputBase';

function valueValidation({children, value}, propName, componentName) {
if (children && value) {
return new Error('Both value and children cannot be passed to ButtonInput');
}
return React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.number]).call(null, {children, value}, propName, componentName);
}
import childrenValueValidation from './utils/childrenValueInputValidation';

class ButtonInput extends InputBase {
renderFormGroup(children) {
Expand All @@ -23,18 +17,20 @@ class ButtonInput extends InputBase {
}
}

ButtonInput.types = ['button', 'reset', 'submit'];

ButtonInput.defaultProps = {
type: 'button'
};

ButtonInput.propTypes = {
type: React.PropTypes.oneOf(['button', 'reset', 'submit']),
type: React.PropTypes.oneOf(ButtonInput.types),
bsStyle(props) {
//defer to Button propTypes of bsStyle
return null;
},
children: valueValidation,
value: valueValidation
children: childrenValueValidation,
value: childrenValueValidation
};

export default ButtonInput;
4 changes: 1 addition & 3 deletions src/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import InputBase from './InputBase';
import ButtonInput from './ButtonInput';
import deprecationWarning from './utils/deprecationWarning';

const buttonTypes = ['button', 'reset', 'submit'];

class Input extends InputBase {
render() {
if (buttonTypes.indexOf(this.props.type) > -1) {
if (ButtonInput.types.indexOf(this.props.type) > -1) {
deprecationWarning(`Input type=${this.props.type}`, 'ButtonInput');
return <ButtonInput {...this.props} />;
}
Expand Down
14 changes: 14 additions & 0 deletions src/utils/childrenValueInputValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { singlePropFrom } from './CustomPropTypes';

const propList = ['children', 'value'];
const typeList = [React.PropTypes.number, React.PropTypes.string];

export default function valueValidation(props, propName, componentName) {
let error = singlePropFrom(propList)(props, propName, componentName);
if (!error) {
const oneOfType = React.PropTypes.oneOfType(typeList);
error = oneOfType(props, propName, componentName);
}
return error;
}
8 changes: 4 additions & 4 deletions test/ButtonInputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ describe('ButtonInput', () =>{
});

it('throws a warning if given both children and a value property', function () {
ReactTestUtils.renderIntoDocument(
<ButtonInput value="button">button</ButtonInput>
);
const testData = { value: 5, children: 'button' };
const result = ButtonInput.propTypes.value(testData, 'value', 'ButtonInput');

shouldWarn('Both value and children');
result.should.be.instanceOf(Error);
result.message.should.have.string('value and children');
});

it('does not throw an error for strings and numbers', function () {
Expand Down

0 comments on commit fd0972e

Please sign in to comment.