Skip to content

Commit 0c61f46

Browse files
committed
[changed] Moving type=static out of Input
Introducing the Static component. Usage of type=static is now deprecated. Please use Static instead.
1 parent fd0972e commit 0c61f46

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

src/FormControls/Static.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import classNames from 'classnames';
3+
import InputBase from '../InputBase';
4+
import childrenValueValidation from '../utils/childrenValueInputValidation';
5+
6+
class Static extends InputBase {
7+
getValue() {
8+
const {children, value} = this.props;
9+
return children ? children : value;
10+
}
11+
12+
renderInput() {
13+
return (
14+
<p {...this.props} className={classNames(this.props.className, 'form-control-static')} ref="input" key="input">
15+
{this.getValue()}
16+
</p>
17+
);
18+
}
19+
}
20+
21+
Static.propTypes = {
22+
value: childrenValueValidation,
23+
children: childrenValueValidation
24+
};
25+
26+
export default Static;

src/FormControls/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Static from './Static';
2+
3+
export default {
4+
Static
5+
};

src/Input.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import React from 'react';
22
import InputBase from './InputBase';
33
import ButtonInput from './ButtonInput';
4+
import FormControls from './FormControls';
45
import deprecationWarning from './utils/deprecationWarning';
56

67
class Input extends InputBase {
78
render() {
89
if (ButtonInput.types.indexOf(this.props.type) > -1) {
910
deprecationWarning(`Input type=${this.props.type}`, 'ButtonInput');
1011
return <ButtonInput {...this.props} />;
12+
} else if (this.props.type === 'static') {
13+
deprecationWarning('Input type=static', 'StaticText');
14+
return <FormControls.Static {...this.props} />;
1115
}
1216

1317
return super.render();

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import DropdownButton from './DropdownButton';
1919
import DropdownMenu from './DropdownMenu';
2020
import DropdownStateMixin from './DropdownStateMixin';
2121
import FadeMixin from './FadeMixin';
22+
import FormControls from './FormControls';
2223
import Glyphicon from './Glyphicon';
2324
import Grid from './Grid';
2425
import Input from './Input';
@@ -75,6 +76,7 @@ export default {
7576
DropdownMenu,
7677
DropdownStateMixin,
7778
FadeMixin,
79+
FormControls,
7880
Glyphicon,
7981
Grid,
8082
Input,

test/FormControlsSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react';
2+
import ReactTestUtils from 'react/lib/ReactTestUtils';
3+
import FormControls from '../src/FormControls';
4+
5+
describe('Form Controls', function () {
6+
describe('Static', function () {
7+
it('renders a p element wrapped around the given value', function () {
8+
const instance = ReactTestUtils.renderIntoDocument(
9+
<FormControls.Static value='v' />
10+
);
11+
12+
const result = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'p');
13+
result.props.children.should.equal('v');
14+
});
15+
16+
it('getValue() pulls from either value or children', function () {
17+
let instance = ReactTestUtils.renderIntoDocument(
18+
<FormControls.Static value='v' />
19+
);
20+
21+
instance.getValue().should.equal('v');
22+
23+
instance = ReactTestUtils.renderIntoDocument(
24+
<FormControls.Static>5</FormControls.Static>
25+
);
26+
27+
instance.getValue().should.equal('5');
28+
});
29+
30+
it('throws an error if both value and children are provided', function () {
31+
const testData = { value: 'blah', children: 'meh' };
32+
const result = FormControls.Static.propTypes.children(testData, 'children', 'Static');
33+
34+
result.should.be.instanceOf(Error);
35+
});
36+
});
37+
});

test/InputSpec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ describe('Input', function () {
6565
shouldWarn('deprecated');
6666
});
6767

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

73-
assert.ok(ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'p'));
74-
assert.equal(instance.getValue(), 'v');
73+
shouldWarn('deprecated');
7574
});
7675

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

0 commit comments

Comments
 (0)