Skip to content

Commit fd0972e

Browse files
committed
DRYing ButtonInput
1 parent 2749cfd commit fd0972e

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/ButtonInput.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import React from 'react';
22
import Button from './Button';
33
import FormGroup from './FormGroup';
44
import InputBase from './InputBase';
5-
6-
function valueValidation({children, value}, propName, componentName) {
7-
if (children && value) {
8-
return new Error('Both value and children cannot be passed to ButtonInput');
9-
}
10-
return React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.number]).call(null, {children, value}, propName, componentName);
11-
}
5+
import childrenValueValidation from './utils/childrenValueInputValidation';
126

137
class ButtonInput extends InputBase {
148
renderFormGroup(children) {
@@ -23,18 +17,20 @@ class ButtonInput extends InputBase {
2317
}
2418
}
2519

20+
ButtonInput.types = ['button', 'reset', 'submit'];
21+
2622
ButtonInput.defaultProps = {
2723
type: 'button'
2824
};
2925

3026
ButtonInput.propTypes = {
31-
type: React.PropTypes.oneOf(['button', 'reset', 'submit']),
27+
type: React.PropTypes.oneOf(ButtonInput.types),
3228
bsStyle(props) {
3329
//defer to Button propTypes of bsStyle
3430
return null;
3531
},
36-
children: valueValidation,
37-
value: valueValidation
32+
children: childrenValueValidation,
33+
value: childrenValueValidation
3834
};
3935

4036
export default ButtonInput;

src/Input.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import InputBase from './InputBase';
33
import ButtonInput from './ButtonInput';
44
import deprecationWarning from './utils/deprecationWarning';
55

6-
const buttonTypes = ['button', 'reset', 'submit'];
7-
86
class Input extends InputBase {
97
render() {
10-
if (buttonTypes.indexOf(this.props.type) > -1) {
8+
if (ButtonInput.types.indexOf(this.props.type) > -1) {
119
deprecationWarning(`Input type=${this.props.type}`, 'ButtonInput');
1210
return <ButtonInput {...this.props} />;
1311
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { singlePropFrom } from './CustomPropTypes';
3+
4+
const propList = ['children', 'value'];
5+
const typeList = [React.PropTypes.number, React.PropTypes.string];
6+
7+
export default function valueValidation(props, propName, componentName) {
8+
let error = singlePropFrom(propList)(props, propName, componentName);
9+
if (!error) {
10+
const oneOfType = React.PropTypes.oneOfType(typeList);
11+
error = oneOfType(props, propName, componentName);
12+
}
13+
return error;
14+
}

test/ButtonInputSpec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ describe('ButtonInput', () =>{
5555
});
5656

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

62-
shouldWarn('Both value and children');
61+
result.should.be.instanceOf(Error);
62+
result.message.should.have.string('value and children');
6363
});
6464

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

0 commit comments

Comments
 (0)