Skip to content

Commit 09e5af9

Browse files
committed
Merge pull request react-bootstrap#674 from aabenoja/static-split
Static FormControl Component
2 parents d278201 + 93c95b6 commit 09e5af9

17 files changed

Lines changed: 167 additions & 24 deletions

docs/examples/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"CarouselItem",
2121
"Col",
2222
"DropdownButton",
23+
"FormControls",
2324
"Glyphicon",
2425
"Grid",
2526
"Input",

docs/examples/InputTypes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const inputTypeInstance = (
1515
<option value='other'>...</option>
1616
</Input>
1717
<Input type='textarea' label='Text Area' placeholder='textarea' />
18-
<Input type='static' value='Static Text' />
1918
<ButtonInput value='Button Input' />
2019
<ButtonInput type='reset' value='Reset Button' />
2120
<ButtonInput type='submit' value='Submit Button' />

docs/examples/StaticText.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const staticTextExample = (
2+
<form className="form-horizontal">
3+
<FormControls.Static className="col-xs-10 col-xs-offset-2" value="I'm in a form" />
4+
<FormControls.Static label="First Name" labelClassName="col-xs-2" wrapperClassName="col-xs-10" value="Billy" />
5+
<FormControls.Static label="Last Name" labelClassName="col-xs-2" wrapperClassName="col-xs-10">Bob</FormControls.Static>
6+
</form>
7+
);
8+
9+
React.render(staticTextExample, mountNode);

docs/src/ComponentsPage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,10 @@ const ComponentsPage = React.createClass({
577577
The helper method <code>getInputDOMNode()</code> returns the internal input element. If you don't want the <code>form-group</code> class applied apply the prop named <code>standalone</code>.</p>
578578
<ReactPlayground codeText={Samples.Input} />
579579
<h2 id='input-types'>Types</h2>
580-
<p>Supports <code>select</code>, <code>textarea</code>, <code>static</code> as well as standard HTML input types. <code>getValue()</code> returns an array for multiple select.</p>
580+
<p>Supports <code>select</code>, <code>textarea</code>, as well as standard HTML input types. <code>getValue()</code> returns an array for multiple select.</p>
581581
<ReactPlayground codeText={Samples.InputTypes} />
582+
<p>Static text can be added to your form controls through the use of the <code>FormControls.Static</code> component.</p>
583+
<ReactPlayground codeText={Samples.StaticText} />
582584
<h2 id='button-input-types'>Button Input Types</h2>
583585
<p>Form buttons are encapsulated by <code>ButtonInput</code>. Pass in <code>type="reset"</code> or <code>type="submit"</code> to suit your needs. Styling is the same as <code>Button</code>.</p>
584586
<ReactPlayground codeText={Samples.ButtonInput} />

docs/src/ReactPlayground.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as modCarousel from '../../src/Carousel';
1313
import * as modCarouselItem from '../../src/CarouselItem';
1414
import * as modCol from '../../src/Col';
1515
import * as modDropdownButton from '../../src/DropdownButton';
16+
import * as modFormControls from '../../src/FormControls';
1617
import * as modGlyphicon from '../../src/Glyphicon';
1718
import * as modGrid from '../../src/Grid';
1819
import * as modInput from '../../src/Input';
@@ -64,6 +65,7 @@ const Carousel = modCarousel.default;
6465
const CarouselItem = modCarouselItem.default;
6566
const Col = modCol.default;
6667
const DropdownButton = modDropdownButton.default;
68+
const FormControls = modFormControls.default;
6769
const Glyphicon = modGlyphicon.default;
6870
const Grid = modGrid.default;
6971
const Input = modInput.default;

docs/src/Samples.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default {
8989
TableResponsive: require('fs').readFileSync(__dirname + '/../examples/TableResponsive.js', 'utf8'),
9090
Input: require('fs').readFileSync(__dirname + '/../examples/Input.js', 'utf8'),
9191
InputTypes: require('fs').readFileSync(__dirname + '/../examples/InputTypes.js', 'utf8'),
92+
StaticText: require('fs').readFileSync(__dirname + '/../examples/StaticText.js', 'utf8'),
9293
ButtonInput: require('fs').readFileSync(__dirname + '/../examples/ButtonInput.js', 'utf8'),
9394
InputAddons: require('fs').readFileSync(__dirname + '/../examples/InputAddons.js', 'utf8'),
9495
InputSizes: require('fs').readFileSync(__dirname + '/../examples/InputSizes.js', 'utf8'),

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/FormControls/Static.js

Lines changed: 26 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +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

6-
const buttonTypes = ['button', 'reset', 'submit'];
7-
87
class Input extends InputBase {
98
render() {
10-
if (buttonTypes.indexOf(this.props.type) > -1) {
9+
if (ButtonInput.types.indexOf(this.props.type) > -1) {
1110
deprecationWarning(`Input type=${this.props.type}`, 'ButtonInput');
1211
return <ButtonInput {...this.props} />;
12+
} else if (this.props.type === 'static') {
13+
deprecationWarning('Input type=static', 'StaticText');
14+
return <FormControls.Static {...this.props} />;
1315
}
1416

1517
return super.render();

0 commit comments

Comments
 (0)