Skip to content

Commit

Permalink
[changed] Moving type=static out of Input
Browse files Browse the repository at this point in the history
Introducing the Static component. Usage of type=static is now
deprecated. Please use Static instead.
  • Loading branch information
aabenoja committed May 26, 2015
1 parent fd0972e commit 0c61f46
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/FormControls/Static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import classNames from 'classnames';
import InputBase from '../InputBase';
import childrenValueValidation from '../utils/childrenValueInputValidation';

class Static extends InputBase {
getValue() {
const {children, value} = this.props;
return children ? children : value;
}

renderInput() {
return (
<p {...this.props} className={classNames(this.props.className, 'form-control-static')} ref="input" key="input">
{this.getValue()}
</p>
);
}
}

Static.propTypes = {
value: childrenValueValidation,
children: childrenValueValidation
};

export default Static;
5 changes: 5 additions & 0 deletions src/FormControls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Static from './Static';

export default {
Static
};
4 changes: 4 additions & 0 deletions src/Input.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';
import InputBase from './InputBase';
import ButtonInput from './ButtonInput';
import FormControls from './FormControls';
import deprecationWarning from './utils/deprecationWarning';

class Input extends InputBase {
render() {
if (ButtonInput.types.indexOf(this.props.type) > -1) {
deprecationWarning(`Input type=${this.props.type}`, 'ButtonInput');
return <ButtonInput {...this.props} />;
} else if (this.props.type === 'static') {
deprecationWarning('Input type=static', 'StaticText');
return <FormControls.Static {...this.props} />;
}

return super.render();
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import DropdownButton from './DropdownButton';
import DropdownMenu from './DropdownMenu';
import DropdownStateMixin from './DropdownStateMixin';
import FadeMixin from './FadeMixin';
import FormControls from './FormControls';
import Glyphicon from './Glyphicon';
import Grid from './Grid';
import Input from './Input';
Expand Down Expand Up @@ -75,6 +76,7 @@ export default {
DropdownMenu,
DropdownStateMixin,
FadeMixin,
FormControls,
Glyphicon,
Grid,
Input,
Expand Down
37 changes: 37 additions & 0 deletions test/FormControlsSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import FormControls from '../src/FormControls';

describe('Form Controls', function () {
describe('Static', function () {
it('renders a p element wrapped around the given value', function () {
const instance = ReactTestUtils.renderIntoDocument(
<FormControls.Static value='v' />
);

const result = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'p');
result.props.children.should.equal('v');
});

it('getValue() pulls from either value or children', function () {
let instance = ReactTestUtils.renderIntoDocument(
<FormControls.Static value='v' />
);

instance.getValue().should.equal('v');

instance = ReactTestUtils.renderIntoDocument(
<FormControls.Static>5</FormControls.Static>
);

instance.getValue().should.equal('5');
});

it('throws an error if both value and children are provided', function () {
const testData = { value: 'blah', children: 'meh' };
const result = FormControls.Static.propTypes.children(testData, 'children', 'Static');

result.should.be.instanceOf(Error);
});
});
});
7 changes: 3 additions & 4 deletions test/InputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ describe('Input', function () {
shouldWarn('deprecated');
});

it('renders a p element when type=static', function () {
let instance = ReactTestUtils.renderIntoDocument(
it('throws a warning when type=static', function () {
ReactTestUtils.renderIntoDocument(
<Input type="static" value="v" />
);

assert.ok(ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'p'));
assert.equal(instance.getValue(), 'v');
shouldWarn('deprecated');
});

it('renders an input element of given type when type is anything else', function () {
Expand Down

0 comments on commit 0c61f46

Please sign in to comment.